View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.jpetstore.domain;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  
20  import java.math.BigDecimal;
21  import java.util.Iterator;
22  
23  import org.junit.jupiter.api.Test;
24  
25  class CartTest {
26  
27    @Test
28    void addItemWhenIsInStockIsTrue() {
29      // given
30      Cart cart = new Cart();
31      Item item = new Item();
32      item.setItemId("I01");
33      item.setListPrice(new BigDecimal("2.05"));
34  
35      // when
36      cart.addItem(item, true);
37      cart.addItem(item, true);
38  
39      // then
40      assertThat(cart.getCartItemList().get(0).getItem()).isSameAs(item);
41      assertThat(cart.getCartItemList().get(0).isInStock()).isTrue();
42      assertThat(cart.getCartItemList().get(0).getQuantity()).isEqualTo(2);
43      assertThat(cart.getCartItemList().get(0).getTotal()).isEqualTo(new BigDecimal("4.10"));
44      assertThat(cart.containsItemId("I01")).isTrue();
45      assertThat(cart.getNumberOfItems()).isEqualTo(1);
46      {
47        Iterator<CartItem> cartItems = cart.getCartItems();
48        assertThat(cartItems.next()).isNotNull();
49        assertThat(cartItems.hasNext()).isFalse();
50      }
51      {
52        Iterator<CartItem> cartItems = cart.getAllCartItems();
53        assertThat(cartItems.next()).isNotNull();
54        assertThat(cartItems.hasNext()).isFalse();
55      }
56    }
57  
58    @Test
59    void addItemWhenIsInStockIsFalse() {
60      // given
61      Cart cart = new Cart();
62      Item item = new Item();
63      item.setItemId("I01");
64      item.setListPrice(new BigDecimal("2.05"));
65  
66      // when
67      cart.addItem(item, false);
68  
69      // then
70      assertThat(cart.getCartItemList().get(0).getItem()).isSameAs(item);
71      assertThat(cart.getCartItemList().get(0).isInStock()).isFalse();
72      assertThat(cart.getCartItemList().get(0).getQuantity()).isEqualTo(1);
73      assertThat(cart.getCartItemList().get(0).getTotal()).isEqualTo(new BigDecimal("2.05"));
74    }
75  
76    @Test
77    void removeItemByIdWhenItemNotFound() {
78      // given
79      Cart cart = new Cart();
80  
81      // when
82      Item item = cart.removeItemById("I01");
83  
84      // then
85      assertThat(item).isNull();
86      assertThat(cart.containsItemId("I01")).isFalse();
87      assertThat(cart.getNumberOfItems()).isEqualTo(0);
88      assertThat(cart.getCartItems().hasNext()).isFalse();
89      assertThat(cart.getAllCartItems().hasNext()).isFalse();
90    }
91  
92    @Test
93    void removeItemByIdWhenItemFound() {
94      // given
95      Cart cart = new Cart();
96      Item item = new Item();
97      item.setItemId("I01");
98      item.setListPrice(new BigDecimal("2.05"));
99      cart.addItem(item, true);
100 
101     // when
102     Item removedItem = cart.removeItemById("I01");
103 
104     // then
105     assertThat(removedItem).isSameAs(item);
106     assertThat(cart.getCartItemList()).isEmpty();
107   }
108 
109   @Test
110   void incrementQuantityByItemId() {
111     // given
112     Cart cart = new Cart();
113     Item item = new Item();
114     item.setItemId("I01");
115     item.setListPrice(new BigDecimal("2.05"));
116     cart.addItem(item, true);
117 
118     // when
119     cart.incrementQuantityByItemId("I01");
120     cart.incrementQuantityByItemId("I01");
121 
122     // then
123     assertThat(cart.getCartItemList().get(0).getItem()).isSameAs(item);
124     assertThat(cart.getCartItemList().get(0).isInStock()).isTrue();
125     assertThat(cart.getCartItemList().get(0).getQuantity()).isEqualTo(3);
126     assertThat(cart.getCartItemList().get(0).getTotal()).isEqualTo(new BigDecimal("6.15"));
127   }
128 
129   @Test
130   void setQuantityByItemId() {
131     // given
132     Cart cart = new Cart();
133     Item item = new Item();
134     item.setItemId("I01");
135     item.setListPrice(new BigDecimal("2.05"));
136     cart.addItem(item, true);
137 
138     // when
139     cart.setQuantityByItemId("I01", 10);
140 
141     // then
142     assertThat(cart.getCartItemList().get(0).getItem()).isSameAs(item);
143     assertThat(cart.getCartItemList().get(0).isInStock()).isTrue();
144     assertThat(cart.getCartItemList().get(0).getQuantity()).isEqualTo(10);
145     assertThat(cart.getCartItemList().get(0).getTotal()).isEqualTo(new BigDecimal("20.50"));
146   }
147 
148   @Test
149   void getSubTotalWhenItemIsEmpty() {
150     // given
151     Cart cart = new Cart();
152 
153     // when
154     BigDecimal subTotal = cart.getSubTotal();
155 
156     // then
157     assertThat(subTotal).isEqualTo(BigDecimal.ZERO);
158 
159   }
160 
161   @Test
162   void getSubTotalWhenItemIsExist() {
163     // given
164     Cart cart = new Cart();
165     {
166       Item item = new Item();
167       item.setItemId("I01");
168       item.setListPrice(new BigDecimal("2.05"));
169       cart.addItem(item, true);
170       cart.setQuantityByItemId("I01", 5);
171     }
172     {
173       Item item = new Item();
174       item.setItemId("I02");
175       item.setListPrice(new BigDecimal("3.06"));
176       cart.addItem(item, true);
177       cart.setQuantityByItemId("I02", 6);
178     }
179 
180     // when
181     BigDecimal subTotal = cart.getSubTotal();
182 
183     // then
184     assertThat(subTotal).isEqualTo(new BigDecimal("28.61"));
185   }
186 
187 }