Skip to content

Commit

Permalink
[test] OrderCreationservice 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-KoKo committed Aug 16, 2024
1 parent 82b8c3d commit 6774540
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/camp/woowak/lab/order/domain/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ public Order(Customer requester, Store store, List<CartItem> cartItems,
public Long getId() {
return id;
}

public List<OrderItem> getOrderItems() {
return orderItems;
}
}
82 changes: 82 additions & 0 deletions src/test/java/camp/woowak/lab/order/domain/OrderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package camp.woowak.lab.order.domain;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import camp.woowak.lab.cart.domain.vo.CartItem;
import camp.woowak.lab.customer.domain.Customer;
import camp.woowak.lab.order.domain.vo.OrderItem;
import camp.woowak.lab.order.exception.MultiStoreOrderException;
import camp.woowak.lab.store.domain.Store;

class OrderTest {

private SingleStoreOrderValidator singleStoreOrderValidator;
private StockRequester stockRequester;
private PriceChecker priceChecker;
private WithdrawPointService withdrawPointService;

@BeforeEach
void setUp() {
singleStoreOrderValidator = mock(SingleStoreOrderValidator.class);
stockRequester = mock(StockRequester.class);
priceChecker = mock(PriceChecker.class);
withdrawPointService = mock(WithdrawPointService.class);
}

@Test
void createOrder_ValidInputs_Success() {
// Given
Customer customer = mock(Customer.class);
Store store = mock(Store.class);
CartItem cartItem = mock(CartItem.class);
List<CartItem> cartItems = List.of(cartItem);

OrderItem orderItem = mock(OrderItem.class);
List<OrderItem> orderItems = List.of(orderItem);

// Mocking behavior
doNothing().when(singleStoreOrderValidator).check(store, cartItems);
doNothing().when(stockRequester).request(cartItems);
when(priceChecker.check(cartItems)).thenReturn(orderItems);
when(withdrawPointService.withdraw(customer, orderItems)).thenReturn(orderItems);

// When
Order order = new Order(customer, store, cartItems, singleStoreOrderValidator, stockRequester, priceChecker,
withdrawPointService);

// Then
assertEquals(orderItems, order.getOrderItems());

verify(singleStoreOrderValidator, times(1)).check(store, cartItems);
verify(stockRequester, times(1)).request(cartItems);
verify(priceChecker, times(1)).check(cartItems);
verify(withdrawPointService, times(1)).withdraw(customer, orderItems);
}

@Test
void createOrder_InvalidStore_ThrowsException() {
// Given
Customer customer = mock(Customer.class);
Store store = mock(Store.class);
CartItem cartItem = mock(CartItem.class);
List<CartItem> cartItems = List.of(cartItem);

// Mock behavior to throw exception
doThrow(new MultiStoreOrderException("다른 가게의 메뉴를 같이 주문할 수 없습니다."))
.when(singleStoreOrderValidator).check(store, cartItems);

// When & Then
MultiStoreOrderException exception = assertThrows(MultiStoreOrderException.class, () -> {
new Order(customer, store, cartItems, singleStoreOrderValidator, stockRequester, priceChecker,
withdrawPointService);
});

assertEquals("다른 가게의 메뉴를 같이 주문할 수 없습니다.", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package camp.woowak.lab.order.domain;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import camp.woowak.lab.cart.domain.vo.CartItem;
import camp.woowak.lab.order.exception.EmptyCartException;
import camp.woowak.lab.order.exception.MultiStoreOrderException;
import camp.woowak.lab.store.domain.Store;

class SingleStoreOrderValidatorTest {

private SingleStoreOrderValidator validator;

@BeforeEach
void setUp() {
validator = new SingleStoreOrderValidator();
}

@Test
void check_NullCartItems_ThrowsEmptyCartException() {
// Given
Store store = mock(Store.class);

// When & Then
assertThrows(EmptyCartException.class, () -> validator.check(store, null));
}

@Test
void check_EmptyCartItems_ThrowsEmptyCartException() {
// Given
Store store = mock(Store.class);

// When & Then
assertThrows(EmptyCartException.class, () -> validator.check(store, List.of()));
}

@Test
void check_AllItemsBelongToSameStore_NoExceptionThrown() {
// Given
Store store = mock(Store.class);
Long storeId = 1L;
when(store.getId()).thenReturn(storeId);

CartItem item1 = mock(CartItem.class);
CartItem item2 = mock(CartItem.class);
when(item1.getStoreId()).thenReturn(storeId);
when(item2.getStoreId()).thenReturn(storeId);

List<CartItem> cartItems = List.of(item1, item2);

// When & Then
assertDoesNotThrow(() -> validator.check(store, cartItems));
}

@Test
void check_ItemsFromDifferentStores_ThrowsMultiStoreOrderException() {
// Given
Store store = mock(Store.class);
Long storeId1 = 1L;
Long storeId2 = 2L;
when(store.getId()).thenReturn(storeId1);

CartItem item1 = mock(CartItem.class);
CartItem item2 = mock(CartItem.class);
when(item1.getStoreId()).thenReturn(storeId1);
when(item2.getStoreId()).thenReturn(storeId2);

List<CartItem> cartItems = List.of(item1, item2);

// When & Then
assertThrows(MultiStoreOrderException.class, () -> validator.check(store, cartItems));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package camp.woowak.lab.order.domain;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import camp.woowak.lab.customer.domain.Customer;
import camp.woowak.lab.order.domain.vo.OrderItem;
import camp.woowak.lab.payaccount.domain.PayAccount;
import camp.woowak.lab.payaccount.exception.NotFoundAccountException;
import camp.woowak.lab.payaccount.repository.PayAccountRepository;

class WithdrawPointServiceTest {

private WithdrawPointService withdrawPointService;
private PayAccountRepository payAccountRepository;

@BeforeEach
void setUp() {
payAccountRepository = mock(PayAccountRepository.class);
withdrawPointService = new WithdrawPointService(payAccountRepository);
}

@Test
void withdraw_NoPayAccount_ThrowsNotFoundAccountException() {
// Given
Customer customer = mock(Customer.class);
UUID customerId = UUID.randomUUID();
when(customer.getId()).thenReturn(customerId);

when(payAccountRepository.findByCustomerIdForUpdate(customerId))
.thenReturn(Optional.empty());

// When & Then
assertThrows(NotFoundAccountException.class, () -> withdrawPointService.withdraw(customer, List.of()));
}

@Test
void withdraw_ValidPayAccount_WithdrawsPointsSuccessfully() {
// Given
Customer customer = mock(Customer.class);
UUID customerId = UUID.randomUUID();
when(customer.getId()).thenReturn(customerId);

PayAccount payAccount = mock(PayAccount.class);
when(payAccountRepository.findByCustomerIdForUpdate(customerId))
.thenReturn(Optional.of(payAccount));

OrderItem orderItem1 = mock(OrderItem.class);
OrderItem orderItem2 = mock(OrderItem.class);
when(orderItem1.getTotalPrice()).thenReturn(500);
when(orderItem2.getTotalPrice()).thenReturn(1500);

List<OrderItem> orderItems = List.of(orderItem1, orderItem2);

// When
List<OrderItem> result = withdrawPointService.withdraw(customer, orderItems);

// Then
verify(payAccount).withdraw(2000); // 500 + 1500
assertEquals(orderItems, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package camp.woowak.lab.order.service;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;

import camp.woowak.lab.cart.domain.vo.CartItem;
import camp.woowak.lab.cart.repository.CartRepository;
import camp.woowak.lab.customer.domain.Customer;
import camp.woowak.lab.customer.repository.CustomerRepository;
import camp.woowak.lab.order.domain.Order;
import camp.woowak.lab.order.domain.PriceChecker;
import camp.woowak.lab.order.domain.SingleStoreOrderValidator;
import camp.woowak.lab.order.domain.StockRequester;
import camp.woowak.lab.order.domain.WithdrawPointService;
import camp.woowak.lab.order.domain.vo.OrderItem;
import camp.woowak.lab.order.exception.EmptyCartException;
import camp.woowak.lab.order.repository.OrderRepository;
import camp.woowak.lab.order.service.command.OrderCreationCommand;
import camp.woowak.lab.store.domain.Store;
import camp.woowak.lab.store.repository.StoreRepository;

@SpringBootTest
class OrderCreationServiceTest {

@Mock
private OrderRepository orderRepository;
@Mock
private CartRepository cartRepository;
@Mock
private StoreRepository storeRepository;
@Mock
private CustomerRepository customerRepository;
@Mock
private SingleStoreOrderValidator singleStoreOrderValidator;
@Mock
private StockRequester stockRequester;
@Mock
private WithdrawPointService withdrawPointService;
@Mock
private PriceChecker priceChecker;

@InjectMocks
private OrderCreationService orderCreationService;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void createOrder_Success() {
// Given
Customer customer = mock(Customer.class);
Store store = mock(Store.class);
CartItem cartItem = mock(CartItem.class);
List<CartItem> cartItems = List.of(cartItem);

// Prepare OrderItems to return from priceChecker
OrderItem orderItem = mock(OrderItem.class);
List<OrderItem> orderItems = List.of(orderItem);

// Mocking behavior
when(store.getId()).thenReturn(1L); // Ensure this is mocked
when(cartItem.getStoreId()).thenReturn(1L); // Mock the cartItem's storeId
doNothing().when(singleStoreOrderValidator).check(any(Store.class), anyList());
doNothing().when(stockRequester).request(anyList());
when(priceChecker.check(anyList())).thenReturn(orderItems);
when(withdrawPointService.withdraw(any(Customer.class), anyList())).thenReturn(orderItems);

// When
Order order = new Order(customer, store, cartItems, singleStoreOrderValidator, stockRequester, priceChecker,
withdrawPointService);

// Then
assertEquals(orderItems, order.getOrderItems());

verify(singleStoreOrderValidator, times(1)).check(store, cartItems);
verify(stockRequester, times(1)).request(cartItems);
verify(priceChecker, times(1)).check(cartItems);
verify(withdrawPointService, times(1)).withdraw(customer, orderItems);
}

@Test
void createOrder_EmptyCart_ThrowsException() {
// Given
UUID customerId = UUID.randomUUID();
OrderCreationCommand command = new OrderCreationCommand(customerId);
Customer customer = mock(Customer.class);

when(customerRepository.findByIdOrThrow(customerId)).thenReturn(customer);
when(cartRepository.findByCustomerId(customerId.toString())).thenReturn(Optional.empty());

// When & Then
EmptyCartException exception = assertThrows(EmptyCartException.class, () -> {
orderCreationService.create(command);
});

assertEquals("구매자 " + customerId + "가 비어있는 카트로 주문을 시도했습니다.", exception.getMessage());
}
}

0 comments on commit 6774540

Please sign in to comment.