-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathOrderServiceTest.java
396 lines (358 loc) · 18.3 KB
/
OrderServiceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package kitchenpos.application;
import kitchenpos.legacy.application.OrderService;
import kitchenpos.menu.domain.MenuRepository;
import kitchenpos.legacy.domain.Order;
import kitchenpos.legacy.domain.OrderLineItem;
import kitchenpos.legacy.domain.OrderRepository;
import kitchenpos.legacy.domain.OrderStatus;
import kitchenpos.order.delivery_order.domain.OrderTable;
import kitchenpos.order.delivery_order.domain.OrderTableRepository;
import kitchenpos.legacy.domain.OrderType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import static kitchenpos.Fixtures.INVALID_ID;
import static kitchenpos.Fixtures.menu;
import static kitchenpos.Fixtures.menuProduct;
import static kitchenpos.Fixtures.order;
import static kitchenpos.Fixtures.orderTable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class OrderServiceTest {
private OrderRepository orderRepository;
private MenuRepository menuRepository;
private OrderTableRepository orderTableRepository;
private FakeKitchenridersClient kitchenridersClient;
private OrderService orderService;
@BeforeEach
void setUp() {
orderRepository = new InMemoryOrderRepository();
menuRepository = new InMemoryMenuRepository();
orderTableRepository = new InMemoryOrderTableRepository();
kitchenridersClient = new FakeKitchenridersClient();
orderService = new OrderService(orderRepository, menuRepository, orderTableRepository, kitchenridersClient);
}
@DisplayName("1개 이상의 등록된 메뉴로 배달 주문을 등록할 수 있다.")
@Test
void createDeliveryOrder() {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final Order expected = createOrderRequest(
OrderType.DELIVERY, "서울시 송파구 위례성대로 2", createOrderLineItemRequest(menuId, 19_000L, 3L)
);
final Order actual = orderService.create(expected);
assertThat(actual).isNotNull();
assertAll(
() -> assertThat(actual.getId()).isNotNull(),
() -> assertThat(actual.getType()).isEqualTo(expected.getType()),
() -> assertThat(actual.getStatus()).isEqualTo(OrderStatus.WAITING),
() -> assertThat(actual.getOrderDateTime()).isNotNull(),
() -> assertThat(actual.getOrderLineItems()).hasSize(1),
() -> assertThat(actual.getDeliveryAddress()).isEqualTo(expected.getDeliveryAddress())
);
}
@DisplayName("1개 이상의 등록된 메뉴로 포장 주문을 등록할 수 있다.")
@Test
void createTakeoutOrder() {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final Order expected = createOrderRequest(OrderType.TAKEOUT, createOrderLineItemRequest(menuId, 19_000L, 3L));
final Order actual = orderService.create(expected);
assertThat(actual).isNotNull();
assertAll(
() -> assertThat(actual.getId()).isNotNull(),
() -> assertThat(actual.getType()).isEqualTo(expected.getType()),
() -> assertThat(actual.getStatus()).isEqualTo(OrderStatus.WAITING),
() -> assertThat(actual.getOrderDateTime()).isNotNull(),
() -> assertThat(actual.getOrderLineItems()).hasSize(1)
);
}
@DisplayName("1개 이상의 등록된 메뉴로 매장 주문을 등록할 수 있다.")
@Test
void createEatInOrder() {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final UUID orderTableId = orderTableRepository.save(orderTable(true, 4)).getId();
final Order expected = createOrderRequest(OrderType.EAT_IN, orderTableId, createOrderLineItemRequest(menuId, 19_000L, 3L));
final Order actual = orderService.create(expected);
assertThat(actual).isNotNull();
assertAll(
() -> assertThat(actual.getId()).isNotNull(),
() -> assertThat(actual.getType()).isEqualTo(expected.getType()),
() -> assertThat(actual.getStatus()).isEqualTo(OrderStatus.WAITING),
() -> assertThat(actual.getOrderDateTime()).isNotNull(),
() -> assertThat(actual.getOrderLineItems()).hasSize(1),
() -> assertThat(actual.getOrderTable().getId()).isEqualTo(expected.getOrderTableId())
);
}
@DisplayName("주문 유형이 올바르지 않으면 등록할 수 없다.")
@NullSource
@ParameterizedTest
void create(final OrderType type) {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final Order expected = createOrderRequest(type, createOrderLineItemRequest(menuId, 19_000L, 3L));
assertThatThrownBy(() -> orderService.create(expected))
.isInstanceOf(IllegalArgumentException.class);
}
@DisplayName("메뉴가 없으면 등록할 수 없다.")
@MethodSource("orderLineItems")
@ParameterizedTest
void create(final List<OrderLineItem> orderLineItems) {
final Order expected = createOrderRequest(OrderType.TAKEOUT, orderLineItems);
assertThatThrownBy(() -> orderService.create(expected))
.isInstanceOf(IllegalArgumentException.class);
}
private static List<Arguments> orderLineItems() {
return Arrays.asList(
null,
Arguments.of(Collections.emptyList()),
Arguments.of(Arrays.asList(createOrderLineItemRequest(INVALID_ID, 19_000L, 3L)))
);
}
@DisplayName("매장 주문은 주문 항목의 수량이 0 미만일 수 있다.")
@ValueSource(longs = -1L)
@ParameterizedTest
void createEatInOrder(final long quantity) {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final UUID orderTableId = orderTableRepository.save(orderTable(true, 4)).getId();
final Order expected = createOrderRequest(
OrderType.EAT_IN, orderTableId, createOrderLineItemRequest(menuId, 19_000L, quantity)
);
assertDoesNotThrow(() -> orderService.create(expected));
}
@DisplayName("매장 주문을 제외한 주문의 경우 주문 항목의 수량은 0 이상이어야 한다.")
@ValueSource(longs = -1L)
@ParameterizedTest
void createWithoutEatInOrder(final long quantity) {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final Order expected = createOrderRequest(
OrderType.TAKEOUT, createOrderLineItemRequest(menuId, 19_000L, quantity)
);
assertThatThrownBy(() -> orderService.create(expected))
.isInstanceOf(IllegalArgumentException.class);
}
@DisplayName("배달 주소가 올바르지 않으면 배달 주문을 등록할 수 없다.")
@NullAndEmptySource
@ParameterizedTest
void create(final String deliveryAddress) {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final Order expected = createOrderRequest(
OrderType.DELIVERY, deliveryAddress, createOrderLineItemRequest(menuId, 19_000L, 3L)
);
assertThatThrownBy(() -> orderService.create(expected))
.isInstanceOf(IllegalArgumentException.class);
}
@DisplayName("빈 테이블에는 매장 주문을 등록할 수 없다.")
@Test
void createEmptyTableEatInOrder() {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final UUID orderTableId = orderTableRepository.save(orderTable(false, 0)).getId();
final Order expected = createOrderRequest(
OrderType.EAT_IN, orderTableId, createOrderLineItemRequest(menuId, 19_000L, 3L)
);
assertThatThrownBy(() -> orderService.create(expected))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("숨겨진 메뉴는 주문할 수 없다.")
@Test
void createNotDisplayedMenuOrder() {
final UUID menuId = menuRepository.save(menu(19_000L, false, menuProduct())).getId();
final Order expected = createOrderRequest(OrderType.TAKEOUT, createOrderLineItemRequest(menuId, 19_000L, 3L));
assertThatThrownBy(() -> orderService.create(expected))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("주문한 메뉴의 가격은 실제 메뉴 가격과 일치해야 한다.")
@Test
void createNotMatchedMenuPriceOrder() {
final UUID menuId = menuRepository.save(menu(19_000L, true, menuProduct())).getId();
final Order expected = createOrderRequest(OrderType.TAKEOUT, createOrderLineItemRequest(menuId, 16_000L, 3L));
assertThatThrownBy(() -> orderService.create(expected))
.isInstanceOf(IllegalArgumentException.class);
}
@DisplayName("주문을 접수한다.")
@Test
void accept() {
final UUID orderId = orderRepository.save(order(OrderStatus.WAITING, orderTable(true, 4))).getId();
final Order actual = orderService.accept(orderId);
assertThat(actual.getStatus()).isEqualTo(OrderStatus.ACCEPTED);
}
@DisplayName("접수 대기 중인 주문만 접수할 수 있다.")
@EnumSource(value = OrderStatus.class, names = "WAITING", mode = EnumSource.Mode.EXCLUDE)
@ParameterizedTest
void accept(final OrderStatus status) {
final UUID orderId = orderRepository.save(order(status, orderTable(true, 4))).getId();
assertThatThrownBy(() -> orderService.accept(orderId))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("배달 주문을 접수되면 배달 대행사를 호출한다.")
@Test
void acceptDeliveryOrder() {
final UUID orderId = orderRepository.save(order(OrderStatus.WAITING, "서울시 송파구 위례성대로 2")).getId();
final Order actual = orderService.accept(orderId);
assertAll(
() -> assertThat(actual.getStatus()).isEqualTo(OrderStatus.ACCEPTED),
() -> assertThat(kitchenridersClient.getOrderId()).isEqualTo(orderId),
() -> assertThat(kitchenridersClient.getDeliveryAddress()).isEqualTo("서울시 송파구 위례성대로 2")
);
}
@DisplayName("주문을 서빙한다.")
@Test
void serve() {
final UUID orderId = orderRepository.save(order(OrderStatus.ACCEPTED)).getId();
final Order actual = orderService.serve(orderId);
assertThat(actual.getStatus()).isEqualTo(OrderStatus.SERVED);
}
@DisplayName("접수된 주문만 서빙할 수 있다.")
@EnumSource(value = OrderStatus.class, names = "ACCEPTED", mode = EnumSource.Mode.EXCLUDE)
@ParameterizedTest
void serve(final OrderStatus status) {
final UUID orderId = orderRepository.save(order(status)).getId();
assertThatThrownBy(() -> orderService.serve(orderId))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("주문을 배달한다.")
@Test
void startDelivery() {
final UUID orderId = orderRepository.save(order(OrderStatus.SERVED, "서울시 송파구 위례성대로 2")).getId();
final Order actual = orderService.startDelivery(orderId);
assertThat(actual.getStatus()).isEqualTo(OrderStatus.DELIVERING);
}
@DisplayName("배달 주문만 배달할 수 있다.")
@Test
void startDeliveryWithoutDeliveryOrder() {
final UUID orderId = orderRepository.save(order(OrderStatus.SERVED)).getId();
assertThatThrownBy(() -> orderService.startDelivery(orderId))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("서빙된 주문만 배달할 수 있다.")
@EnumSource(value = OrderStatus.class, names = "SERVED", mode = EnumSource.Mode.EXCLUDE)
@ParameterizedTest
void startDelivery(final OrderStatus status) {
final UUID orderId = orderRepository.save(order(status, "서울시 송파구 위례성대로 2")).getId();
assertThatThrownBy(() -> orderService.startDelivery(orderId))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("주문을 배달 완료한다.")
@Test
void completeDelivery() {
final UUID orderId = orderRepository.save(order(OrderStatus.DELIVERING, "서울시 송파구 위례성대로 2")).getId();
final Order actual = orderService.completeDelivery(orderId);
assertThat(actual.getStatus()).isEqualTo(OrderStatus.DELIVERED);
}
@DisplayName("배달 중인 주문만 배달 완료할 수 있다.")
@EnumSource(value = OrderStatus.class, names = "DELIVERING", mode = EnumSource.Mode.EXCLUDE)
@ParameterizedTest
void completeDelivery(final OrderStatus status) {
final UUID orderId = orderRepository.save(order(status, "서울시 송파구 위례성대로 2")).getId();
assertThatThrownBy(() -> orderService.completeDelivery(orderId))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("주문을 완료한다.")
@Test
void complete() {
final Order expected = orderRepository.save(order(OrderStatus.DELIVERED, "서울시 송파구 위례성대로 2"));
final Order actual = orderService.complete(expected.getId());
assertThat(actual.getStatus()).isEqualTo(OrderStatus.COMPLETED);
}
@DisplayName("배달 주문의 경우 배달 완료된 주문만 완료할 수 있다.")
@EnumSource(value = OrderStatus.class, names = "DELIVERED", mode = EnumSource.Mode.EXCLUDE)
@ParameterizedTest
void completeDeliveryOrder(final OrderStatus status) {
final UUID orderId = orderRepository.save(order(status, "서울시 송파구 위례성대로 2")).getId();
assertThatThrownBy(() -> orderService.complete(orderId))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("포장 및 매장 주문의 경우 서빙된 주문만 완료할 수 있다.")
@EnumSource(value = OrderStatus.class, names = "SERVED", mode = EnumSource.Mode.EXCLUDE)
@ParameterizedTest
void completeTakeoutAndEatInOrder(final OrderStatus status) {
final UUID orderId = orderRepository.save(order(status)).getId();
assertThatThrownBy(() -> orderService.complete(orderId))
.isInstanceOf(IllegalStateException.class);
}
@DisplayName("주문 테이블의 모든 매장 주문이 완료되면 빈 테이블로 설정한다.")
@Test
void completeEatInOrder() {
final OrderTable orderTable = orderTableRepository.save(orderTable(true, 4));
final Order expected = orderRepository.save(order(OrderStatus.SERVED, orderTable));
final Order actual = orderService.complete(expected.getId());
assertAll(
() -> assertThat(actual.getStatus()).isEqualTo(OrderStatus.COMPLETED),
() -> assertThat(orderTableRepository.findById(orderTable.getId()).get().isOccupied()).isFalse(),
() -> assertThat(orderTableRepository.findById(orderTable.getId()).get().getNumberOfGuests()).isEqualTo(0)
);
}
@DisplayName("완료되지 않은 매장 주문이 있는 주문 테이블은 빈 테이블로 설정하지 않는다.")
@Test
void completeNotTable() {
final OrderTable orderTable = orderTableRepository.save(orderTable(true, 4));
orderRepository.save(order(OrderStatus.ACCEPTED, orderTable));
final Order expected = orderRepository.save(order(OrderStatus.SERVED, orderTable));
final Order actual = orderService.complete(expected.getId());
assertAll(
() -> assertThat(actual.getStatus()).isEqualTo(OrderStatus.COMPLETED),
() -> assertThat(orderTableRepository.findById(orderTable.getId()).get().isOccupied()).isTrue(),
() -> assertThat(orderTableRepository.findById(orderTable.getId()).get().getNumberOfGuests()).isEqualTo(4)
);
}
@DisplayName("주문의 목록을 조회할 수 있다.")
@Test
void findAll() {
final OrderTable orderTable = orderTableRepository.save(orderTable(true, 4));
orderRepository.save(order(OrderStatus.SERVED, orderTable));
orderRepository.save(order(OrderStatus.DELIVERED, "서울시 송파구 위례성대로 2"));
final List<Order> actual = orderService.findAll();
assertThat(actual).hasSize(2);
}
private Order createOrderRequest(
final OrderType type,
final String deliveryAddress,
final OrderLineItem... orderLineItems
) {
final Order order = new Order();
order.setType(type);
order.setDeliveryAddress(deliveryAddress);
order.setOrderLineItems(Arrays.asList(orderLineItems));
return order;
}
private Order createOrderRequest(final OrderType orderType, final OrderLineItem... orderLineItems) {
return createOrderRequest(orderType, Arrays.asList(orderLineItems));
}
private Order createOrderRequest(final OrderType orderType, final List<OrderLineItem> orderLineItems) {
final Order order = new Order();
order.setType(orderType);
order.setOrderLineItems(orderLineItems);
return order;
}
private Order createOrderRequest(
final OrderType type,
final UUID orderTableId,
final OrderLineItem... orderLineItems
) {
final Order order = new Order();
order.setType(type);
order.setOrderTableId(orderTableId);
order.setOrderLineItems(Arrays.asList(orderLineItems));
return order;
}
private static OrderLineItem createOrderLineItemRequest(final UUID menuId, final long price, final long quantity) {
final OrderLineItem orderLineItem = new OrderLineItem();
orderLineItem.setSeq(new Random().nextLong());
orderLineItem.setMenuId(menuId);
orderLineItem.setPrice(BigDecimal.valueOf(price));
orderLineItem.setQuantity(quantity);
return orderLineItem;
}
}