From 4ccc64c52be47f1eba773b164a9829f6f176e047 Mon Sep 17 00:00:00 2001 From: kimhyun5u <22kimhyun5u@gmail.com> Date: Thu, 19 Sep 2024 19:28:18 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20Order=20=EB=B3=80=EA=B2=BD=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../woowak/lab/order/domain/OrderTest.java | 10 ++-- .../order/repository/OrderRepositoryTest.java | 23 ++++----- .../service/OrderCreationServiceTest.java | 8 ++- .../lab/payment/domain/OrderPaymentTest.java | 50 ++++++++++++++----- .../web/dao/order/QueryDslOrderDaoTest.java | 11 ++-- .../web/dao/store/StoreDummiesFixture.java | 9 ++-- 6 files changed, 74 insertions(+), 37 deletions(-) diff --git a/src/test/java/camp/woowak/lab/order/domain/OrderTest.java b/src/test/java/camp/woowak/lab/order/domain/OrderTest.java index 16e14db7..96f1280e 100644 --- a/src/test/java/camp/woowak/lab/order/domain/OrderTest.java +++ b/src/test/java/camp/woowak/lab/order/domain/OrderTest.java @@ -23,6 +23,7 @@ class OrderTest { private PriceChecker priceChecker; private WithdrawPointService withdrawPointService; private final DateTimeProvider fixedDateTime = () -> LocalDateTime.of(2024, 8, 18, 1, 30, 30); + private OrderFactory orderFactory; @BeforeEach void setUp() { @@ -30,6 +31,9 @@ void setUp() { stockRequester = mock(StockRequester.class); priceChecker = mock(PriceChecker.class); withdrawPointService = mock(WithdrawPointService.class); + + orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, withdrawPointService, + fixedDateTime); } @Test @@ -50,8 +54,7 @@ void createOrder_ValidInputs_Success() { when(withdrawPointService.withdraw(customer, orderItems)).thenReturn(orderItems); // When - Order order = new Order(customer, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, fixedDateTime.now()); + Order order = orderFactory.createOrder(customer, cartItems); // Then assertEquals(orderItems, order.getOrderItems()); @@ -76,8 +79,7 @@ void createOrder_InvalidStore_ThrowsException() { // When & Then MultiStoreOrderException exception = assertThrows(MultiStoreOrderException.class, () -> { - new Order(customer, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, fixedDateTime.now()); + orderFactory.createOrder(customer, cartItems); }); assertEquals("다른 가게의 메뉴를 같이 주문할 수 없습니다.", exception.getMessage()); diff --git a/src/test/java/camp/woowak/lab/order/repository/OrderRepositoryTest.java b/src/test/java/camp/woowak/lab/order/repository/OrderRepositoryTest.java index a0717637..2adf26e8 100644 --- a/src/test/java/camp/woowak/lab/order/repository/OrderRepositoryTest.java +++ b/src/test/java/camp/woowak/lab/order/repository/OrderRepositoryTest.java @@ -23,6 +23,7 @@ import camp.woowak.lab.menu.repository.MenuCategoryRepository; import camp.woowak.lab.menu.repository.MenuRepository; import camp.woowak.lab.order.domain.Order; +import camp.woowak.lab.order.domain.OrderFactory; import camp.woowak.lab.order.domain.PriceChecker; import camp.woowak.lab.order.domain.SingleStoreOrderValidator; import camp.woowak.lab.order.domain.StockRequester; @@ -67,6 +68,8 @@ class OrderRepositoryTest { @Autowired MenuStockCacheService menuStockCacheService; + private OrderFactory orderFactory; + private Store store1; private Store store2; @@ -83,6 +86,9 @@ class OrderRepositoryTest { @BeforeEach void setUp() { + orderFactory = new OrderFactory(new SingleStoreOrderValidator(storeRepository), + new StockRequester(menuRepository, menuStockCacheService), new PriceChecker(menuRepository), + new WithdrawPointService(payAccountRepository), LocalDateTime::now); StoreCategory storeCategory = storeCategoryRepository.save(new StoreCategory("storeCategory")); vendor = vendorRepository.saveAndFlush( new Vendor("vendor", "vendor@email.com", "password", "010-1234-5678", @@ -118,15 +124,10 @@ void testFindAllByOwner() { // given List store1CartItems = List.of(new CartItem(menu1.getId(), store1.getId(), 1)); orderRepository.saveAndFlush( - new Order(customer, store1CartItems, new SingleStoreOrderValidator(storeRepository), - new StockRequester(menuRepository, menuStockCacheService), new PriceChecker(menuRepository), - new WithdrawPointService(payAccountRepository), LocalDateTime.now())); + orderFactory.createOrder(customer, store1CartItems)); List store2CartItems = List.of(new CartItem(menu1.getId(), store2.getId(), 1)); orderRepository.saveAndFlush( - new Order(customer, store2CartItems, new SingleStoreOrderValidator(storeRepository), - new StockRequester(menuRepository, menuStockCacheService), new PriceChecker(menuRepository), - new WithdrawPointService(payAccountRepository), LocalDateTime.now())); - + orderFactory.createOrder(customer, store2CartItems)); // when List orders = orderRepository.findAllByOwner(vendor.getId()); @@ -153,14 +154,10 @@ void testFindByStore() { // given List store1CartItems = List.of(new CartItem(menu1.getId(), store1.getId(), 1)); Order order = orderRepository.saveAndFlush( - new Order(customer, store1CartItems, new SingleStoreOrderValidator(storeRepository), - new StockRequester(menuRepository, menuStockCacheService), new PriceChecker(menuRepository), - new WithdrawPointService(payAccountRepository), LocalDateTime.now())); + orderFactory.createOrder(customer, store1CartItems)); List store2CartItems = List.of(new CartItem(menu1.getId(), store2.getId(), 1)); orderRepository.saveAndFlush( - new Order(customer, store2CartItems, new SingleStoreOrderValidator(storeRepository), - new StockRequester(menuRepository, menuStockCacheService), new PriceChecker(menuRepository), - new WithdrawPointService(payAccountRepository), LocalDateTime.now())); + orderFactory.createOrder(customer, store2CartItems)); // when List orders = orderRepository.findByStore(store1.getId(), vendor.getId()); diff --git a/src/test/java/camp/woowak/lab/order/service/OrderCreationServiceTest.java b/src/test/java/camp/woowak/lab/order/service/OrderCreationServiceTest.java index f5267b65..9d273a2d 100644 --- a/src/test/java/camp/woowak/lab/order/service/OrderCreationServiceTest.java +++ b/src/test/java/camp/woowak/lab/order/service/OrderCreationServiceTest.java @@ -22,6 +22,7 @@ import camp.woowak.lab.customer.repository.CustomerRepository; import camp.woowak.lab.infra.date.DateTimeProvider; import camp.woowak.lab.order.domain.Order; +import camp.woowak.lab.order.domain.OrderFactory; import camp.woowak.lab.order.domain.PriceChecker; import camp.woowak.lab.order.domain.SingleStoreOrderValidator; import camp.woowak.lab.order.domain.StockRequester; @@ -61,9 +62,13 @@ class OrderCreationServiceTest { private final DateTimeProvider fixedDateTime = () -> LocalDateTime.of(2024, 8, 18, 1, 30, 30); + private OrderFactory orderFactory; + @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); + orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, withdrawPointService, + fixedDateTime); } @Test @@ -87,8 +92,7 @@ void createOrder_Success() { when(withdrawPointService.withdraw(any(Customer.class), anyList())).thenReturn(orderItems); // When - Order order = new Order(customer, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, fixedDateTime.now()); + Order order = orderFactory.createOrder(customer, cartItems); // Then assertEquals(orderItems, order.getOrderItems()); diff --git a/src/test/java/camp/woowak/lab/payment/domain/OrderPaymentTest.java b/src/test/java/camp/woowak/lab/payment/domain/OrderPaymentTest.java index 6b90551a..5c12c6cc 100644 --- a/src/test/java/camp/woowak/lab/payment/domain/OrderPaymentTest.java +++ b/src/test/java/camp/woowak/lab/payment/domain/OrderPaymentTest.java @@ -19,6 +19,7 @@ import camp.woowak.lab.menu.domain.Menu; import camp.woowak.lab.menu.domain.MenuCategory; import camp.woowak.lab.order.domain.Order; +import camp.woowak.lab.order.domain.OrderFactory; import camp.woowak.lab.order.domain.PriceChecker; import camp.woowak.lab.order.domain.SingleStoreOrderValidator; import camp.woowak.lab.order.domain.StockRequester; @@ -58,6 +59,8 @@ class OrderPaymentTest { @Mock WithdrawPointService withdrawPointService; + private OrderFactory orderFactory; + @Nested @DisplayName("총 주문 금액을 계산하는 기능은") class CalculateOrderPriceTest { @@ -103,8 +106,12 @@ void shouldCalculateTotalPriceCorrectly() { given(priceChecker.check(store, cartItems)).willReturn(orderItems); LocalDateTime now = LocalDateTime.now(); - Order order = new Order(customer, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, now); + + orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, + withdrawPointService, + () -> now); + + Order order = orderFactory.createOrder(customer, cartItems); OrderPayment orderPayment = new OrderPayment(order, customer, vendor, OrderPaymentStatus.ORDER_SUCCESS, now); @@ -139,8 +146,12 @@ void shouldReturnZeroForEmptyOrder() { given(priceChecker.check(store, cartItems)).willReturn(orderItems); LocalDateTime now = LocalDateTime.now(); - Order order = new Order(customer, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, now); + + orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, + withdrawPointService, + () -> now); + + Order order = orderFactory.createOrder(customer, cartItems); OrderPayment orderPayment = new OrderPayment(order, customer, vendor, OrderPaymentStatus.ORDER_SUCCESS, now); @@ -160,7 +171,7 @@ class ValidateAbleToSettleTest { @Test @DisplayName("[Success] 정산 대상 Vendor가 수령자이고 상태가 ORDER_SUCCESS이면 예외가 발생하지 않는다.") - void test1() { + void ƒtest1() { // given customer = createCustomer(createPayAccount()); Vendor recipientVendor = createVendor(createPayAccount()); @@ -169,8 +180,13 @@ void test1() { given(priceChecker.check(store, cartItems)).willReturn(orderItems); LocalDateTime now = LocalDateTime.now(); - Order order = new Order(customer, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, now); + + orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, + withdrawPointService, + () -> now); + + Order order = orderFactory.createOrder(customer, cartItems); + OrderPayment orderPayment = new OrderPayment(order, customer, recipientVendor, OrderPaymentStatus.ORDER_SUCCESS, now); @@ -190,8 +206,13 @@ void test2() { given(priceChecker.check(store, cartItems)).willReturn(orderItems); LocalDateTime now = LocalDateTime.now(); - Order order = new Order(customer, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, now); + + orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, + withdrawPointService, + () -> now); + + Order order = orderFactory.createOrder(customer, cartItems); + OrderPayment orderPayment = new OrderPayment(order, customer, recipientVendor, OrderPaymentStatus.ORDER_SUCCESS, now); @@ -210,8 +231,13 @@ void test3() { given(priceChecker.check(store, cartItems)).willReturn(orderItems); LocalDateTime now = LocalDateTime.now(); - Order order = new Order(customer, cartItems, singleStoreOrderValidator, stockRequester, priceChecker, - withdrawPointService, now); + + orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, + withdrawPointService, + () -> now); + + Order order = orderFactory.createOrder(customer, cartItems); + OrderPayment orderPayment = new OrderPayment(order, customer, recipientVendor, OrderPaymentStatus.SETTLEMENT_SUCCESS, now); @@ -262,4 +288,4 @@ private StoreCategory createStoreCategory() { return new StoreCategory("양식"); } -} \ No newline at end of file +} diff --git a/src/test/java/camp/woowak/lab/web/dao/order/QueryDslOrderDaoTest.java b/src/test/java/camp/woowak/lab/web/dao/order/QueryDslOrderDaoTest.java index c6c443ae..0d3bf4f7 100644 --- a/src/test/java/camp/woowak/lab/web/dao/order/QueryDslOrderDaoTest.java +++ b/src/test/java/camp/woowak/lab/web/dao/order/QueryDslOrderDaoTest.java @@ -28,6 +28,7 @@ import camp.woowak.lab.menu.repository.MenuCategoryRepository; import camp.woowak.lab.menu.repository.MenuRepository; import camp.woowak.lab.order.domain.Order; +import camp.woowak.lab.order.domain.OrderFactory; import camp.woowak.lab.order.domain.PriceChecker; import camp.woowak.lab.order.domain.SingleStoreOrderValidator; import camp.woowak.lab.order.domain.StockRequester; @@ -83,6 +84,8 @@ class QueryDslOrderDaoTest { private Vendor vendor1; private Vendor vendor2; + private OrderFactory orderFactory; + @BeforeEach void setUp() { // 테스트에 사용할 엔티티 생성 및 저장 @@ -161,8 +164,10 @@ private Order createOrder(Customer customer, Store store, List orderI when(priceChecker.check(any(Store.class), anyList())).thenReturn(orderItems); DateTimeProvider fixedTime = () -> LocalDateTime.of(2024, 8, 24, 1, 0, 0); - Order order = new Order(customer, Collections.EMPTY_LIST, singleStoreOrderValidator, stockRequester, - priceChecker, withdrawPointService, fixedTime.now()); + orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, withdrawPointService, + fixedTime); + Order order = orderFactory.createOrder(customer, Collections.EMPTY_LIST); + orderRepository.save(order); return order; } @@ -211,4 +216,4 @@ private Vendor createVendor(String email) { vendorRepository.saveAndFlush(vendor); return vendor; } -} \ No newline at end of file +} diff --git a/src/test/java/camp/woowak/lab/web/dao/store/StoreDummiesFixture.java b/src/test/java/camp/woowak/lab/web/dao/store/StoreDummiesFixture.java index 798223ad..c00088a8 100644 --- a/src/test/java/camp/woowak/lab/web/dao/store/StoreDummiesFixture.java +++ b/src/test/java/camp/woowak/lab/web/dao/store/StoreDummiesFixture.java @@ -15,6 +15,7 @@ import camp.woowak.lab.menu.repository.MenuCategoryRepository; import camp.woowak.lab.menu.repository.MenuRepository; import camp.woowak.lab.order.domain.Order; +import camp.woowak.lab.order.domain.OrderFactory; import camp.woowak.lab.order.domain.PriceChecker; import camp.woowak.lab.order.domain.SingleStoreOrderValidator; import camp.woowak.lab.order.domain.StockRequester; @@ -103,12 +104,14 @@ protected List createOrdersWithRandomCount(List store) { for (int i = 0; i < store.size(); i++) { Store s = store.get(i); SingleStoreOrderValidator singleStoreOrderValidator = new TestSingleStoreOrderValidator(s, storeRepository); + + OrderFactory orderFactory = new OrderFactory(singleStoreOrderValidator, + new StockRequester(menuRepository, menuStockCacheService), new TestPriceChecker(menuRepository), + new TestWithdrawPointService(payAccountRepository), LocalDateTime::now); for (int j = 0; j < orderCount[i]; j++) { Customer customer = dummyCustomers.get( new Random(System.currentTimeMillis()).nextInt(dummyCustomers.size())); - Order order = new Order(customer, new ArrayList<>(), singleStoreOrderValidator, - new StockRequester(menuRepository, menuStockCacheService), new TestPriceChecker(menuRepository), - new TestWithdrawPointService(payAccountRepository), LocalDateTime.now()); + Order order = orderFactory.createOrder(customer, new ArrayList<>()); orders.add(order); } }