diff --git a/src/main/java/camp/woowak/lab/cart/domain/Cart.java b/src/main/java/camp/woowak/lab/cart/domain/Cart.java index 7211ee98..9d434931 100644 --- a/src/main/java/camp/woowak/lab/cart/domain/Cart.java +++ b/src/main/java/camp/woowak/lab/cart/domain/Cart.java @@ -15,8 +15,8 @@ @Getter public class Cart { - private String customerId; - private List menuList; + private final String customerId; + private final List menuList; /** * 생성될 때 무조건 cart가 비어있도록 구현 @@ -24,8 +24,18 @@ public class Cart { * @param customerId 장바구니 소유주의 ID값입니다. */ public Cart(String customerId) { + this(customerId, new LinkedList<>()); + } + + /** + * 해당 Domain을 사용하는 같은 패키지내의 클래스, 혹은 자식 클래스는 List를 커스텀할 수 있습니다. + * + * @param customerId 장바구니 소유주의 ID값입니다. + * @param menuList 장바구니에 사용될 List입니다. + */ + protected Cart(String customerId, List menuList) { this.customerId = customerId; - this.menuList = new LinkedList<>(); + this.menuList = menuList; } public void addMenu(Menu menu) { @@ -36,6 +46,14 @@ public void addMenu(Menu menu) { this.menuList.add(menu); } + public long getTotalPrice() { + return this.menuList.stream() + .map(Menu::getPrice) + .mapToLong(Long::valueOf) + .boxed() + .reduce(0L, Long::sum); + } + private void validateStoreOpenTime(Store store) { if (!store.isOpen()) { StoreTime storeTime = store.getStoreTime(); diff --git a/src/main/java/camp/woowak/lab/cart/service/CartService.java b/src/main/java/camp/woowak/lab/cart/service/CartService.java index 5df2ebe9..4841ac81 100644 --- a/src/main/java/camp/woowak/lab/cart/service/CartService.java +++ b/src/main/java/camp/woowak/lab/cart/service/CartService.java @@ -6,6 +6,7 @@ import camp.woowak.lab.cart.exception.MenuNotFoundException; import camp.woowak.lab.cart.repository.CartRepository; import camp.woowak.lab.cart.service.command.AddCartCommand; +import camp.woowak.lab.cart.service.command.CartTotalPriceCommand; import camp.woowak.lab.menu.domain.Menu; import camp.woowak.lab.menu.repository.MenuRepository; @@ -34,6 +35,11 @@ public void addMenu(AddCartCommand command) { cartRepository.save(customerCart); } + public long getTotalPrice(CartTotalPriceCommand command) { + return getCart(command.customerId()) + .getTotalPrice(); + } + private Cart getCart(String customerId) { return cartRepository.findByCustomerId(customerId) .orElse(cartRepository.save(new Cart(customerId))); diff --git a/src/main/java/camp/woowak/lab/cart/service/command/CartTotalPriceCommand.java b/src/main/java/camp/woowak/lab/cart/service/command/CartTotalPriceCommand.java new file mode 100644 index 00000000..2b0efef6 --- /dev/null +++ b/src/main/java/camp/woowak/lab/cart/service/command/CartTotalPriceCommand.java @@ -0,0 +1,6 @@ +package camp.woowak.lab.cart.service.command; + +public record CartTotalPriceCommand( + String customerId +) { +} diff --git a/src/main/java/camp/woowak/lab/web/api/cart/CartApiController.java b/src/main/java/camp/woowak/lab/web/api/cart/CartApiController.java index b8906038..3a550e56 100644 --- a/src/main/java/camp/woowak/lab/web/api/cart/CartApiController.java +++ b/src/main/java/camp/woowak/lab/web/api/cart/CartApiController.java @@ -1,5 +1,6 @@ package camp.woowak.lab.web.api.cart; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -7,10 +8,12 @@ import camp.woowak.lab.cart.service.CartService; import camp.woowak.lab.cart.service.command.AddCartCommand; +import camp.woowak.lab.cart.service.command.CartTotalPriceCommand; import camp.woowak.lab.web.authentication.LoginCustomer; import camp.woowak.lab.web.authentication.annotation.AuthenticationPrincipal; import camp.woowak.lab.web.dto.request.cart.AddCartRequest; import camp.woowak.lab.web.dto.response.cart.AddCartResponse; +import camp.woowak.lab.web.dto.response.cart.CartTotalPriceResponse; import lombok.extern.slf4j.Slf4j; @RestController @@ -33,4 +36,15 @@ public AddCartResponse addCart( log.info("Customer({}) add Menu({}) in Cart", loginCustomer.getId(), addCartRequest.menuId()); return new AddCartResponse(true); } + + @GetMapping("/price") + public CartTotalPriceResponse getCartTotalPrice( + @AuthenticationPrincipal LoginCustomer loginCustomer + ) { + CartTotalPriceCommand command = new CartTotalPriceCommand(loginCustomer.getId().toString()); + long totalPrice = cartService.getTotalPrice(command); + + log.info("Customer({})'s total price in cart is {}", loginCustomer.getId(), totalPrice); + return new CartTotalPriceResponse(totalPrice); + } } diff --git a/src/main/java/camp/woowak/lab/web/dto/response/cart/CartTotalPriceResponse.java b/src/main/java/camp/woowak/lab/web/dto/response/cart/CartTotalPriceResponse.java new file mode 100644 index 00000000..bbb8411e --- /dev/null +++ b/src/main/java/camp/woowak/lab/web/dto/response/cart/CartTotalPriceResponse.java @@ -0,0 +1,6 @@ +package camp.woowak.lab.web.dto.response.cart; + +public record CartTotalPriceResponse( + long totalPrice +) { +} diff --git a/src/test/java/camp/woowak/lab/cart/domain/CartTest.java b/src/test/java/camp/woowak/lab/cart/domain/CartTest.java new file mode 100644 index 00000000..a69a4586 --- /dev/null +++ b/src/test/java/camp/woowak/lab/cart/domain/CartTest.java @@ -0,0 +1,136 @@ +package camp.woowak.lab.cart.domain; + +import static org.assertj.core.api.Assertions.*; + +import java.time.LocalDateTime; +import java.util.LinkedList; +import java.util.List; +import java.util.UUID; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import camp.woowak.lab.cart.exception.OtherStoreMenuException; +import camp.woowak.lab.cart.exception.StoreNotOpenException; +import camp.woowak.lab.fixture.CartFixture; +import camp.woowak.lab.menu.domain.Menu; +import camp.woowak.lab.menu.domain.MenuCategory; +import camp.woowak.lab.payaccount.domain.PayAccount; +import camp.woowak.lab.store.domain.Store; +import camp.woowak.lab.vendor.domain.Vendor; +import camp.woowak.lab.web.authentication.NoOpPasswordEncoder; + +@DisplayName("Cart 클래스는") +class CartTest implements CartFixture { + private final String customerId = UUID.randomUUID().toString(); + + private List cartList; + private Cart cart; + private Menu menu; + private int minPrice = 8000; + private Store store; + private Vendor vendor; + + @BeforeEach + void setUp() { + cartList = new LinkedList<>(); + cart = new Cart(customerId, cartList); + + vendor = createSavedVendor(UUID.randomUUID(), new PayAccount(), new NoOpPasswordEncoder()); + store = createSavedStore(1L, vendor, "중화반점", minPrice, + LocalDateTime.now().minusMinutes(10).withSecond(0).withNano(0), + LocalDateTime.now().plusMinutes(10).withSecond(0).withNano(0)); + menu = createSavedMenu(1L, store, new MenuCategory(store, "중식"), "짜장면", 9000); + } + + @Nested + @DisplayName("addMenu 메서드") + class AddMenuTest { + @Test + @DisplayName("Menu를 받으면 cart에 저장된다.") + void addMenuTest() { + //given + + //when + cart.addMenu(menu); + + //then + assertThat(cartList.get(0)).isEqualTo(menu); + } + + @Test + @DisplayName("열지 않은 가게의 메뉴를 담으면 StoreNotOpenException을 던진다.") + void storeNotOpenExceptionTest() { + //given + Store closedStore = createSavedStore(2L, vendor, "closed", minPrice, + LocalDateTime.now().minusMinutes(30).withSecond(0).withNano(0), + LocalDateTime.now().minusMinutes(10).withSecond(0).withNano(0)); + Menu closedMenu = createSavedMenu(2L, closedStore, new MenuCategory(closedStore, "중식"), "짬뽕", 9000); + + //when & then + assertThatThrownBy(() -> cart.addMenu(closedMenu)) + .isExactlyInstanceOf(StoreNotOpenException.class); + assertThat(cartList).isEmpty(); + } + + @Test + @DisplayName("다른 가게의 메뉴를 함께 담으면 OtherStoreMenuException을 던진다.") + void otherStoreMenuExceptionTest() { + //given + cart.addMenu(menu); + + Store otherStore = createSavedStore(2L, vendor, "closed", minPrice, + LocalDateTime.now().minusMinutes(30).withSecond(0).withNano(0), + LocalDateTime.now().plusMinutes(30).withSecond(0).withNano(0)); + Menu otherStoreMenu = createSavedMenu(2L, otherStore, new MenuCategory(otherStore, "중식"), "짬뽕", 9000); + + //when & then + assertThatThrownBy(() -> cart.addMenu(otherStoreMenu)) + .isExactlyInstanceOf(OtherStoreMenuException.class); + assertThat(cartList).doesNotContain(otherStoreMenu); + assertThat(cartList).contains(menu); + } + } + + @Nested + @DisplayName("totalPrice 메서드는") + class GetTotalPriceTest { + @Test + @DisplayName("장바구니가 비어있으면 0원을 return한다.") + void getTotalPriceWithEmptyList() { + //given + + //when + long totalPrice = cart.getTotalPrice(); + + //then + assertThat(totalPrice).isEqualTo(0); + } + + @Test + @DisplayName("현재 장바구니에 담긴 모든 메뉴의 총 금액을 return 받는다.") + void getTotalPriceTest() { + //given + MenuCategory menuCategory = new MenuCategory(store, "중식"); + int price1 = 1000; + Menu menu1 = createSavedMenu(2L, store, menuCategory, "짜장면1", price1); + cart.addMenu(menu1); + + int price2 = 2000; + Menu menu2 = createSavedMenu(3L, store, menuCategory, "짬뽕1", price2); + cart.addMenu(menu2); + + int price3 = Integer.MAX_VALUE; + Menu menu3 = createSavedMenu(4L, store, menuCategory, "황제정식", price3); + cart.addMenu(menu3); + + //when + long totalPrice = cart.getTotalPrice(); + + //then + assertThat(totalPrice).isEqualTo((long)price1 + (long)price2 + (long)price3); + } + } +} \ No newline at end of file diff --git a/src/test/java/camp/woowak/lab/cart/service/CartServiceTest.java b/src/test/java/camp/woowak/lab/cart/service/CartServiceTest.java index e4cfbd3a..dbf3db64 100644 --- a/src/test/java/camp/woowak/lab/cart/service/CartServiceTest.java +++ b/src/test/java/camp/woowak/lab/cart/service/CartServiceTest.java @@ -18,6 +18,7 @@ import camp.woowak.lab.cart.exception.StoreNotOpenException; import camp.woowak.lab.cart.repository.CartRepository; import camp.woowak.lab.cart.service.command.AddCartCommand; +import camp.woowak.lab.cart.service.command.CartTotalPriceCommand; import camp.woowak.lab.customer.domain.Customer; import camp.woowak.lab.customer.repository.CustomerRepository; import camp.woowak.lab.menu.domain.Menu; @@ -66,6 +67,7 @@ class CartServiceTest { private Customer customer; private Menu menu; + private MenuCategory menuCategory; private Store store; private Vendor vendor; @@ -77,7 +79,9 @@ void setUp() { store = createStore(vendor, "중화반점", minOrderPrice, startTime, endTime); - menu = createMenu(store, "짜장면", 90000); + menuCategory = createMenuCategory(store, "중식"); + + menu = createMenu(store, menuCategory, "짜장면", 90000); } @Nested @@ -118,7 +122,8 @@ void throwExceptionWhenAddMenuInCartWithClosedStoresMenu() { LocalDateTime startTime = LocalDateTime.now().minusMinutes(10).withSecond(0).withNano(0); LocalDateTime endTime = LocalDateTime.now().minusMinutes(1).withSecond(0).withNano(0); Store closedStore = createStore(vendor, "오픈 전의 중화반점", 8000, startTime, endTime); - Menu menu = createMenu(closedStore, "닫힌 가게의 메뉴", 1000); + MenuCategory closedMenuCategory = createMenuCategory(closedStore, "닫힌 카테고리"); + Menu menu = createMenu(closedStore, closedMenuCategory, "닫힌 가게의 메뉴", 1000); AddCartCommand command = new AddCartCommand(customer.getId().toString(), menu.getId()); @@ -132,7 +137,8 @@ void throwExceptionWhenAddMenuInCartWithClosedStoresMenu() { void throwExceptionWhenAddMenuInCartWithOtherStoresMenu() { //given Store otherStore = createStore(vendor, "다른집", 8000, startTime, endTime); - Menu otherStoresMenu = createMenu(otherStore, "다른집 짜장면", 9000); + MenuCategory otherMenuCategory = createMenuCategory(otherStore, "다른집 카테고리"); + Menu otherStoresMenu = createMenu(otherStore, otherMenuCategory, "다른집 짜장면", 9000); AddCartCommand command1 = new AddCartCommand(customer.getId().toString(), menu.getId()); cartService.addMenu(command1); @@ -144,10 +150,59 @@ void throwExceptionWhenAddMenuInCartWithOtherStoresMenu() { } } - private Menu createMenu(Store store, String name, int price) { - MenuCategory menuCategory = new MenuCategory(store, "카테고리1"); - menuCategoryRepository.saveAndFlush(menuCategory); - Menu menu1 = new Menu(store, menuCategory,name, price,"imageUrl"); + @Nested + @DisplayName("getTotalPrice 메서드") + class GetTotalPrice { + @Nested + @DisplayName("totalPrice 메서드는") + class GetTotalPriceTest { + @Test + @DisplayName("장바구니가 비어있으면 0원을 return한다.") + void getTotalPriceWithEmptyList() { + //given + CartTotalPriceCommand command = new CartTotalPriceCommand(customer.getId().toString()); + + //when + long totalPrice = cartService.getTotalPrice(command); + + //then + assertThat(totalPrice).isEqualTo(0); + } + + @Test + @DisplayName("현재 장바구니에 담긴 모든 메뉴의 총 금액을 return 받는다.") + void getTotalPriceTest() { + //given + int price1 = 1000; + Menu menu1 = createMenu(store, menuCategory, "짜장면1", price1); + cartService.addMenu(new AddCartCommand(customer.getId().toString(), menu1.getId())); + + int price2 = 2000; + Menu menu2 = createMenu(store, menuCategory, "짬뽕1", price2); + cartService.addMenu(new AddCartCommand(customer.getId().toString(), menu2.getId())); + + int price3 = Integer.MAX_VALUE; + Menu menu3 = createMenu(store, menuCategory, "황제정식", price3); + cartService.addMenu(new AddCartCommand(customer.getId().toString(), menu3.getId())); + + CartTotalPriceCommand command = new CartTotalPriceCommand(customer.getId().toString()); + + //when + long totalPrice = cartService.getTotalPrice(command); + + //then + assertThat(totalPrice).isEqualTo((long)price1 + (long)price2 + (long)price3); + } + } + } + + private MenuCategory createMenuCategory(Store store, String name) { + MenuCategory menuCategory1 = new MenuCategory(store, name); + return menuCategoryRepository.saveAndFlush(menuCategory1); + } + + private Menu createMenu(Store store, MenuCategory menuCategory, String name, int price) { + Menu menu1 = new Menu(store, menuCategory, name, price, "imageUrl"); menuRepository.saveAndFlush(menu1); return menu1; diff --git a/src/test/java/camp/woowak/lab/fixture/CartFixture.java b/src/test/java/camp/woowak/lab/fixture/CartFixture.java new file mode 100644 index 00000000..431bce91 --- /dev/null +++ b/src/test/java/camp/woowak/lab/fixture/CartFixture.java @@ -0,0 +1,31 @@ +package camp.woowak.lab.fixture; + +import java.time.LocalDateTime; +import java.util.UUID; + +import camp.woowak.lab.menu.TestMenu; +import camp.woowak.lab.menu.domain.Menu; +import camp.woowak.lab.menu.domain.MenuCategory; +import camp.woowak.lab.payaccount.domain.PayAccount; +import camp.woowak.lab.store.TestStore; +import camp.woowak.lab.store.domain.Store; +import camp.woowak.lab.vendor.TestVendor; +import camp.woowak.lab.vendor.domain.Vendor; +import camp.woowak.lab.web.authentication.PasswordEncoder; + +public interface CartFixture { + default Vendor createSavedVendor(UUID id, PayAccount payAccount, PasswordEncoder passwordEncoder) { + return new TestVendor( + id, "vendorName", "vendorEmail@example.com", "vendorPassword", "010-0000-0000", payAccount, + passwordEncoder); + } + + default Store createSavedStore(Long id, Vendor vendor, String name, int minOrderPrice, LocalDateTime startTIme, + LocalDateTime endTime) { + return new TestStore(id, vendor, name, "송파", "010-1234-5678", minOrderPrice, startTIme, endTime); + } + + default Menu createSavedMenu(Long id, Store store, MenuCategory menuCategory, String name, int price) { + return new TestMenu(id, store, menuCategory, name, price); + } +} diff --git a/src/test/java/camp/woowak/lab/menu/TestMenu.java b/src/test/java/camp/woowak/lab/menu/TestMenu.java new file mode 100644 index 00000000..54b2f4f8 --- /dev/null +++ b/src/test/java/camp/woowak/lab/menu/TestMenu.java @@ -0,0 +1,20 @@ +package camp.woowak.lab.menu; + +import camp.woowak.lab.menu.domain.Menu; +import camp.woowak.lab.menu.domain.MenuCategory; +import camp.woowak.lab.store.domain.Store; + +public class TestMenu extends Menu { + private Long id; + + public TestMenu(Long id, Store store, MenuCategory menuCategory, + String name, Integer price) { + super(store, menuCategory, name, price, "imageUrl"); + this.id = id; + } + + @Override + public Long getId() { + return id; + } +} diff --git a/src/test/java/camp/woowak/lab/store/TestStore.java b/src/test/java/camp/woowak/lab/store/TestStore.java index ff3b49fd..15cae339 100644 --- a/src/test/java/camp/woowak/lab/store/TestStore.java +++ b/src/test/java/camp/woowak/lab/store/TestStore.java @@ -16,6 +16,12 @@ public TestStore(Long id, Vendor owner, StoreCategory storeCategory, String name this.id = id; } + public TestStore(Long id, Vendor vendor, String name, String address, String phoneNumber, int minOrderPrice, + LocalDateTime startTime, LocalDateTime endTime) { + super(vendor, new StoreCategory("중식"), name, address, phoneNumber, minOrderPrice, startTime, endTime); + this.id = id; + } + @Override public Long getId() { return id; diff --git a/src/test/java/camp/woowak/lab/web/api/cart/CartApiControllerTest.java b/src/test/java/camp/woowak/lab/web/api/cart/CartApiControllerTest.java index d11a46b7..e06b7ee5 100644 --- a/src/test/java/camp/woowak/lab/web/api/cart/CartApiControllerTest.java +++ b/src/test/java/camp/woowak/lab/web/api/cart/CartApiControllerTest.java @@ -19,7 +19,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; +import camp.woowak.lab.cart.domain.Cart; import camp.woowak.lab.cart.exception.CartErrorCode; +import camp.woowak.lab.cart.repository.CartRepository; +import camp.woowak.lab.cart.service.command.CartTotalPriceCommand; import camp.woowak.lab.common.exception.ErrorCode; import camp.woowak.lab.customer.domain.Customer; import camp.woowak.lab.customer.repository.CustomerRepository; @@ -66,27 +69,38 @@ class CartApiControllerTest { private CustomerRepository customerRepository; @Autowired private ObjectMapper mapper; + @Autowired + private CartRepository cartRepository; + + private MockHttpSession session; + private Customer customer; + private Vendor vendor; + private Store store; + private MenuCategory menuCategory; + private static final LocalDateTime startTime = LocalDateTime.now().minusMinutes(10).withSecond(0).withNano(0); + private static final LocalDateTime endTime = LocalDateTime.now().plusMinutes(10).withSecond(0).withNano(0); + + @BeforeEach + void setUp() throws Exception { + customer = createCustomer(); + vendor = createVendor(); + store = createStore(vendor, "중화반점", 8000, startTime, endTime); + menuCategory = createMenuCategory(store, "카테고리"); + + session = new MockHttpSession(); + session.setAttribute(SessionConst.SESSION_CUSTOMER_KEY, new LoginCustomer(customer.getId())); + } @Nested @DisplayName("addMenu 메서드는") class AddMenu { private final String BASE_URL = "/cart"; - private Customer customer; - private Vendor vendor; private Menu menu; private static final int minOrderPrice = 8000; - private static final LocalDateTime startTime = LocalDateTime.now().minusMinutes(10).withSecond(0).withNano(0); - private static final LocalDateTime endTime = LocalDateTime.now().plusMinutes(10).withSecond(0).withNano(0); - private MockHttpSession session; @BeforeEach void setUp() throws Exception { - customer = createCustomer(); - vendor = createVendor(); - Store store = createStore(vendor, "중화반점", 8000, startTime, endTime); - menu = createMenu(store, "짜장면", 90000); - session = new MockHttpSession(); - session.setAttribute(SessionConst.SESSION_CUSTOMER_KEY, new LoginCustomer(customer.getId())); + menu = createMenu(store, menuCategory, "짜장면", 90000); } @Test @@ -128,7 +142,8 @@ void cantAddMenuWithClosedStoresMenu() throws Exception { LocalDateTime closedStartTime = LocalDateTime.now().minusMinutes(10).withSecond(0).withNano(0); LocalDateTime closedEndTime = LocalDateTime.now().minusMinutes(1).withSecond(0).withNano(0); Store closedStore = createStore(vendor, "닫힌 가게", minOrderPrice, closedStartTime, closedEndTime); - Menu closedStoresMenu = createMenu(closedStore, "닫힌 가게의 메뉴", 1000); + MenuCategory closedStoreMenuCategory = createMenuCategory(closedStore, "닫힌 카테고리"); + Menu closedStoresMenu = createMenu(closedStore, closedStoreMenuCategory, "닫힌 가게의 메뉴", 1000); AddCartRequest request = new AddCartRequest(closedStoresMenu.getId()); String content = mapper.writeValueAsString(request); @@ -148,7 +163,8 @@ void cantAddMenuWithClosedStoresMenu() throws Exception { void cantAddMenuWithOtherStoresMenu() throws Exception { //given Store otherStore = createStore(vendor, "옆집 가게", minOrderPrice, startTime, endTime); - Menu otherStoreMenu = createMenu(otherStore, "옆집 가게 메뉴", 10000); + MenuCategory otherStoreMenuCategory = createMenuCategory(otherStore, "옆집 카테고리"); + Menu otherStoreMenu = createMenu(otherStore, otherStoreMenuCategory, "옆집 가게 메뉴", 10000); AddCartRequest givenRequest = new AddCartRequest(menu.getId()); String givenContent = mapper.writeValueAsString(givenRequest); @@ -173,10 +189,60 @@ void cantAddMenuWithOtherStoresMenu() throws Exception { } } - private Menu createMenu(Store store, String name, int price) { - MenuCategory menuCategory = new MenuCategory(store, "카테고리1"); - menuCategoryRepository.saveAndFlush(menuCategory); - Menu menu1 = new Menu(store, menuCategory,name, price,"imageUrl"); + @Nested + @DisplayName("getCartTotalPrice 메서드는") + class GetCartTotalPrice { + private final String BASE_URL = "/cart/price"; + + @Test + @DisplayName("장바구니가 비어있으면 0원을 return한다.") + void getTotalPriceWithEmptyList() throws Exception { + //given + + //when & then + mvc.perform(get(BASE_URL) + .session(session)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data.totalPrice").value(0)); + } + + @Test + @DisplayName("현재 장바구니에 담긴 모든 메뉴의 총 금액을 return 받는다.") + void getTotalPriceTest() throws Exception { + //given + Cart cart = new Cart(customer.getId().toString()); + + int price1 = 1000; + Menu menu1 = createMenu(store, menuCategory, "짜장면1", price1); + cart.addMenu(menu1); + + int price2 = 2000; + Menu menu2 = createMenu(store, menuCategory, "짬뽕1", price2); + cart.addMenu(menu2); + + int price3 = Integer.MAX_VALUE; + Menu menu3 = createMenu(store, menuCategory, "황제정식", price3); + cart.addMenu(menu3); + cartRepository.save(cart); + + CartTotalPriceCommand command = new CartTotalPriceCommand(customer.getId().toString()); + + //when & then + mvc.perform(get(BASE_URL) + .session(session)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data.totalPrice").value( + (long)menu1.getPrice() + (long)menu2.getPrice() + (long)menu3.getPrice())); + } + } + + private MenuCategory createMenuCategory(Store store, String name) { + MenuCategory menuCategory = new MenuCategory(store, name); + return menuCategoryRepository.saveAndFlush(menuCategory); + } + + private Menu createMenu(Store store, MenuCategory menuCategory, String name, int price) { + Menu menu1 = new Menu(store, menuCategory, name, price, "imageUrl"); menuRepository.saveAndFlush(menu1); return menu1;