Skip to content

Commit

Permalink
[fix] Order 변경에 따른 테스트 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kimhyun5u committed Sep 19, 2024
1 parent 3bf99bc commit 4ccc64c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 37 deletions.
10 changes: 6 additions & 4 deletions src/test/java/camp/woowak/lab/order/domain/OrderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ 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() {
singleStoreOrderValidator = mock(SingleStoreOrderValidator.class);
stockRequester = mock(StockRequester.class);
priceChecker = mock(PriceChecker.class);
withdrawPointService = mock(WithdrawPointService.class);

orderFactory = new OrderFactory(singleStoreOrderValidator, stockRequester, priceChecker, withdrawPointService,
fixedDateTime);
}

@Test
Expand All @@ -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());
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,6 +68,8 @@ class OrderRepositoryTest {
@Autowired
MenuStockCacheService menuStockCacheService;

private OrderFactory orderFactory;

private Store store1;

private Store store2;
Expand All @@ -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", "[email protected]", "password", "010-1234-5678",
Expand Down Expand Up @@ -118,15 +124,10 @@ void testFindAllByOwner() {
// given
List<CartItem> 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<CartItem> 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<Order> orders = orderRepository.findAllByOwner(vendor.getId());

Expand All @@ -153,14 +154,10 @@ void testFindByStore() {
// given
List<CartItem> 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<CartItem> 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<Order> orders = orderRepository.findByStore(store1.getId(), vendor.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand Down
50 changes: 38 additions & 12 deletions src/test/java/camp/woowak/lab/payment/domain/OrderPaymentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,8 @@ class OrderPaymentTest {
@Mock
WithdrawPointService withdrawPointService;

private OrderFactory orderFactory;

@Nested
@DisplayName("총 주문 금액을 계산하는 기능은")
class CalculateOrderPriceTest {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -160,7 +171,7 @@ class ValidateAbleToSettleTest {

@Test
@DisplayName("[Success] 정산 대상 Vendor가 수령자이고 상태가 ORDER_SUCCESS이면 예외가 발생하지 않는다.")
void test1() {
void ƒtest1() {
// given
customer = createCustomer(createPayAccount());
Vendor recipientVendor = createVendor(createPayAccount());
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -262,4 +288,4 @@ private StoreCategory createStoreCategory() {
return new StoreCategory("양식");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,6 +84,8 @@ class QueryDslOrderDaoTest {
private Vendor vendor1;
private Vendor vendor2;

private OrderFactory orderFactory;

@BeforeEach
void setUp() {
// 테스트에 사용할 엔티티 생성 및 저장
Expand Down Expand Up @@ -161,8 +164,10 @@ private Order createOrder(Customer customer, Store store, List<OrderItem> 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;
}
Expand Down Expand Up @@ -211,4 +216,4 @@ private Vendor createVendor(String email) {
vendorRepository.saveAndFlush(vendor);
return vendor;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,12 +104,14 @@ protected List<Order> createOrdersWithRandomCount(List<Store> 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);
}
}
Expand Down

0 comments on commit 4ccc64c

Please sign in to comment.