1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.jpetstore.service;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.mockito.Mockito.when;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.mybatis.jpetstore.domain.Category;
30 import org.mybatis.jpetstore.domain.Item;
31 import org.mybatis.jpetstore.domain.Product;
32 import org.mybatis.jpetstore.mapper.CategoryMapper;
33 import org.mybatis.jpetstore.mapper.ItemMapper;
34 import org.mybatis.jpetstore.mapper.ProductMapper;
35
36
37
38
39 @ExtendWith(MockitoExtension.class)
40 class CatalogServiceTest {
41
42 @Mock(lenient = true)
43 private ProductMapper productMapper;
44 @Mock
45 private CategoryMapper categoryMapper;
46 @Mock
47 private ItemMapper itemMapper;
48
49 @InjectMocks
50 private CatalogService catalogService;
51
52 @Test
53 void shouldCallTheSearchMapperTwice() {
54
55 String keywords = "a b";
56 List<Product> l1 = new ArrayList<>();
57 l1.add(new Product());
58 List<Product> l2 = new ArrayList<>();
59 l2.add(new Product());
60
61
62 when(productMapper.searchProductList("%a%")).thenReturn(l1);
63 when(productMapper.searchProductList("%b%")).thenReturn(l2);
64 List<Product> r = catalogService.searchProductList(keywords);
65
66
67 assertThat(r).hasSize(2);
68 assertThat(r.get(0)).isSameAs(l1.get(0));
69 assertThat(r.get(1)).isSameAs(l2.get(0));
70 }
71
72 @Test
73 void shouldReturnCategoryList() {
74
75 List<Category> expectedCategories = new ArrayList<>();
76
77
78 when(categoryMapper.getCategoryList()).thenReturn(expectedCategories);
79 List<Category> categories = catalogService.getCategoryList();
80
81
82 assertThat(categories).isSameAs(expectedCategories);
83 }
84
85 @Test
86 void shouldReturnCategory() {
87
88
89 String categoryId = "C01";
90 Category expectedCategory = new Category();
91
92
93 when(categoryMapper.getCategory(categoryId)).thenReturn(expectedCategory);
94 Category category = catalogService.getCategory(categoryId);
95
96
97 assertThat(category).isSameAs(expectedCategory);
98
99 }
100
101 @Test
102 void shouldReturnProduct() {
103
104
105 String productId = "P01";
106 Product expectedProduct = new Product();
107
108
109 when(productMapper.getProduct(productId)).thenReturn(expectedProduct);
110 Product product = catalogService.getProduct(productId);
111
112
113 assertThat(product).isSameAs(expectedProduct);
114
115 }
116
117 @Test
118 void shouldReturnProductList() {
119
120 String categoryId = "C01";
121 List<Product> expectedProducts = new ArrayList<>();
122
123
124 when(productMapper.getProductListByCategory(categoryId)).thenReturn(expectedProducts);
125 List<Product> products = catalogService.getProductListByCategory(categoryId);
126
127
128 assertThat(products).isSameAs(expectedProducts);
129
130 }
131
132 @Test
133 void shouldReturnItemList() {
134
135 String productId = "P01";
136 List<Item> expectedItems = new ArrayList<>();
137
138
139 when(itemMapper.getItemListByProduct(productId)).thenReturn(expectedItems);
140 List<Item> items = catalogService.getItemListByProduct(productId);
141
142
143 assertThat(items).isSameAs(expectedItems);
144
145 }
146
147 @Test
148 void shouldReturnItem() {
149
150
151 String itemCode = "I01";
152 Item expectedItem = new Item();
153
154
155 when(itemMapper.getItem(itemCode)).thenReturn(expectedItem);
156 Item item = catalogService.getItem(itemCode);
157
158
159 assertThat(item).isSameAs(expectedItem);
160
161 }
162
163 @Test
164 void shouldReturnTrueWhenExistStock() {
165
166
167 String itemCode = "I01";
168
169
170 when(itemMapper.getInventoryQuantity(itemCode)).thenReturn(1);
171 boolean result = catalogService.isItemInStock(itemCode);
172
173
174 assertThat(result).isTrue();
175
176 }
177
178 @Test
179 void shouldReturnFalseWhenNotExistStock() {
180
181
182 String itemCode = "I01";
183
184
185 when(itemMapper.getInventoryQuantity(itemCode)).thenReturn(0);
186 boolean result = catalogService.isItemInStock(itemCode);
187
188
189 assertThat(result).isFalse();
190
191 }
192
193 }