-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] 장바구니에 현재까지 담은 음식 상품의 금액을 볼 수 있는 기능 추가 (#70)
* [feat] Cart의 customerId와 menuList에 final 추가 - customerId와 menuList를 불변으로 선언해서 내부 안정성 향상 * [feat] Cart생성자 추가 - Cart Domain을 사용하는 같은 패키지내의 클래스, 혹은 자식 클래스는 List를 커스텀할 수 있도록 생성자 추가. * [test] CartFixture 및 필요한 TestMenu, TestStore 구현 * [test] Cart 도메인의 addMenu 테스트 추가 - 같은 가게의 메뉴만 담을 수 있다. - 오픈한 가게의 메뉴만 담을 수 있다. * [test] Cart 도메인의 totalPrice 테스트 추가 - 빈 장바구니면 0원이 return된다. - 담긴 메뉴의 총 합이 return된다. * [chore] totalPrice -> getTotalPrice로 메서드 이름 수정 - 더 명확하게 가져온다는 의도를 메서드명에 명시 * [feat] CartService에 getTotalPrice 메서드 추가 - 현재 담긴 금액을 알 수 있는 메서드 추가 * [test] CartService의 getTotalPrice 테스트 추가 - 빈 장바구니면 0원이 return된다. - 담긴 메뉴의 총 합이 return된다. * [feat] CartApiController에 현재 Cart에 담긴 총 금액을 조회하는 API 생성 * [test] CartApiController의 getCartTotalPrice 테스트 추가 - 빈 장바구니면 0원이 return된다. - 담긴 메뉴의 총 합이 return된다.
- Loading branch information
Showing
11 changed files
with
391 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/camp/woowak/lab/cart/service/command/CartTotalPriceCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package camp.woowak.lab.cart.service.command; | ||
|
||
public record CartTotalPriceCommand( | ||
String customerId | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/camp/woowak/lab/web/dto/response/cart/CartTotalPriceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package camp.woowak.lab.web.dto.response.cart; | ||
|
||
public record CartTotalPriceResponse( | ||
long totalPrice | ||
) { | ||
} |
136 changes: 136 additions & 0 deletions
136
src/test/java/camp/woowak/lab/cart/domain/CartTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Menu> 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.