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.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   * @author Eduardo Macarron
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      // given
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      // when
62      when(productMapper.searchProductList("%a%")).thenReturn(l1);
63      when(productMapper.searchProductList("%b%")).thenReturn(l2);
64      List<Product> r = catalogService.searchProductList(keywords);
65  
66      // then
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      // given
75      List<Category> expectedCategories = new ArrayList<>();
76  
77      // when
78      when(categoryMapper.getCategoryList()).thenReturn(expectedCategories);
79      List<Category> categories = catalogService.getCategoryList();
80  
81      // then
82      assertThat(categories).isSameAs(expectedCategories);
83    }
84  
85    @Test
86    void shouldReturnCategory() {
87  
88      // given
89      String categoryId = "C01";
90      Category expectedCategory = new Category();
91  
92      // when
93      when(categoryMapper.getCategory(categoryId)).thenReturn(expectedCategory);
94      Category category = catalogService.getCategory(categoryId);
95  
96      // then
97      assertThat(category).isSameAs(expectedCategory);
98  
99    }
100 
101   @Test
102   void shouldReturnProduct() {
103 
104     // given
105     String productId = "P01";
106     Product expectedProduct = new Product();
107 
108     // when
109     when(productMapper.getProduct(productId)).thenReturn(expectedProduct);
110     Product product = catalogService.getProduct(productId);
111 
112     // then
113     assertThat(product).isSameAs(expectedProduct);
114 
115   }
116 
117   @Test
118   void shouldReturnProductList() {
119     // given
120     String categoryId = "C01";
121     List<Product> expectedProducts = new ArrayList<>();
122 
123     // when
124     when(productMapper.getProductListByCategory(categoryId)).thenReturn(expectedProducts);
125     List<Product> products = catalogService.getProductListByCategory(categoryId);
126 
127     // then
128     assertThat(products).isSameAs(expectedProducts);
129 
130   }
131 
132   @Test
133   void shouldReturnItemList() {
134     // given
135     String productId = "P01";
136     List<Item> expectedItems = new ArrayList<>();
137 
138     // when
139     when(itemMapper.getItemListByProduct(productId)).thenReturn(expectedItems);
140     List<Item> items = catalogService.getItemListByProduct(productId);
141 
142     // then
143     assertThat(items).isSameAs(expectedItems);
144 
145   }
146 
147   @Test
148   void shouldReturnItem() {
149 
150     // given
151     String itemCode = "I01";
152     Item expectedItem = new Item();
153 
154     // when
155     when(itemMapper.getItem(itemCode)).thenReturn(expectedItem);
156     Item item = catalogService.getItem(itemCode);
157 
158     // then
159     assertThat(item).isSameAs(expectedItem);
160 
161   }
162 
163   @Test
164   void shouldReturnTrueWhenExistStock() {
165 
166     // given
167     String itemCode = "I01";
168 
169     // when
170     when(itemMapper.getInventoryQuantity(itemCode)).thenReturn(1);
171     boolean result = catalogService.isItemInStock(itemCode);
172 
173     // then
174     assertThat(result).isTrue();
175 
176   }
177 
178   @Test
179   void shouldReturnFalseWhenNotExistStock() {
180 
181     // given
182     String itemCode = "I01";
183 
184     // when
185     when(itemMapper.getInventoryQuantity(itemCode)).thenReturn(0);
186     boolean result = catalogService.isItemInStock(itemCode);
187 
188     // then
189     assertThat(result).isFalse();
190 
191   }
192 
193 }