|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+package com.ruoyi.web.modules.home.service;
|
|
|
2
|
+
|
|
|
3
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
4
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
5
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
6
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
7
|
+import static org.mockito.Mockito.verify;
|
|
|
8
|
+import static org.mockito.Mockito.when;
|
|
|
9
|
+import java.math.BigDecimal;
|
|
|
10
|
+import java.util.Arrays;
|
|
|
11
|
+import java.util.Collections;
|
|
|
12
|
+import java.util.List;
|
|
|
13
|
+import org.junit.jupiter.api.Test;
|
|
|
14
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
15
|
+import org.mockito.InjectMocks;
|
|
|
16
|
+import org.mockito.Mock;
|
|
|
17
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
18
|
+import com.ruoyi.common.exception.ServiceException;
|
|
|
19
|
+import com.ruoyi.web.modules.category.constant.CategoryConstants;
|
|
|
20
|
+import com.ruoyi.web.modules.category.domain.BizGoodsCategory;
|
|
|
21
|
+import com.ruoyi.web.modules.category.facade.ICategoryFacade;
|
|
|
22
|
+import com.ruoyi.web.modules.category.mapper.BizGoodsCategoryMapper;
|
|
|
23
|
+import com.ruoyi.web.modules.category.vo.CategoryVisibleChildVO;
|
|
|
24
|
+import com.ruoyi.web.modules.category.vo.CategoryVisibleVO;
|
|
|
25
|
+import com.ruoyi.web.modules.home.constant.CategoryAppConstants;
|
|
|
26
|
+import com.ruoyi.web.modules.home.dto.CategoryGoodsQuery;
|
|
|
27
|
+import com.ruoyi.web.modules.home.mapper.CategoryAppMapper;
|
|
|
28
|
+import com.ruoyi.web.modules.home.service.impl.CategoryAppServiceImpl;
|
|
|
29
|
+import com.ruoyi.web.modules.home.vo.HomeHotGoodsVO;
|
|
|
30
|
+
|
|
|
31
|
+/**
|
|
|
32
|
+ * C 端商品分类 Service 单元测试(CGC-UT-001~012)
|
|
|
33
|
+ */
|
|
|
34
|
+@ExtendWith(MockitoExtension.class)
|
|
|
35
|
+class CategoryAppServiceImplTest
|
|
|
36
|
+{
|
|
|
37
|
+ @Mock
|
|
|
38
|
+ private ICategoryFacade categoryFacade;
|
|
|
39
|
+
|
|
|
40
|
+ @Mock
|
|
|
41
|
+ private BizGoodsCategoryMapper categoryMapper;
|
|
|
42
|
+
|
|
|
43
|
+ @Mock
|
|
|
44
|
+ private CategoryAppMapper categoryAppMapper;
|
|
|
45
|
+
|
|
|
46
|
+ @InjectMocks
|
|
|
47
|
+ private CategoryAppServiceImpl categoryAppService;
|
|
|
48
|
+
|
|
|
49
|
+ @Test
|
|
|
50
|
+ void listCategoryTree_delegatesPlatformFacade()
|
|
|
51
|
+ {
|
|
|
52
|
+ CategoryVisibleVO l1 = visibleLevel1(1L, "兽药");
|
|
|
53
|
+ when(categoryFacade.listVisibleByShopId(null)).thenReturn(Collections.singletonList(l1));
|
|
|
54
|
+
|
|
|
55
|
+ List<CategoryVisibleVO> result = categoryAppService.listCategoryTree();
|
|
|
56
|
+
|
|
|
57
|
+ verify(categoryFacade).listVisibleByShopId(null);
|
|
|
58
|
+ assertEquals(1, result.size());
|
|
|
59
|
+ assertEquals("兽药", result.get(0).getCategoryName());
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ @Test
|
|
|
63
|
+ void listCategoryTree_emptyListWhenNoData()
|
|
|
64
|
+ {
|
|
|
65
|
+ when(categoryFacade.listVisibleByShopId(null)).thenReturn(Collections.emptyList());
|
|
|
66
|
+
|
|
|
67
|
+ List<CategoryVisibleVO> result = categoryAppService.listCategoryTree();
|
|
|
68
|
+
|
|
|
69
|
+ assertNotNull(result);
|
|
|
70
|
+ assertTrue(result.isEmpty());
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ @Test
|
|
|
74
|
+ void listCategoryTree_nullFacadeResultReturnsEmpty()
|
|
|
75
|
+ {
|
|
|
76
|
+ when(categoryFacade.listVisibleByShopId(null)).thenReturn(null);
|
|
|
77
|
+
|
|
|
78
|
+ List<CategoryVisibleVO> result = categoryAppService.listCategoryTree();
|
|
|
79
|
+
|
|
|
80
|
+ assertNotNull(result);
|
|
|
81
|
+ assertTrue(result.isEmpty());
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ @Test
|
|
|
85
|
+ void listLevel2Tabs_returnsVisibleChildren()
|
|
|
86
|
+ {
|
|
|
87
|
+ when(categoryMapper.selectByIdIgnoreShop(1L)).thenReturn(platformLevel1(1L));
|
|
|
88
|
+ CategoryVisibleChildVO tab1 = level2Tab(11L, "抗生素", 0);
|
|
|
89
|
+ CategoryVisibleChildVO tab2 = level2Tab(12L, "驱虫药", 1);
|
|
|
90
|
+ when(categoryAppMapper.selectPlatformLevel2Tabs(1L)).thenReturn(Arrays.asList(tab1, tab2));
|
|
|
91
|
+
|
|
|
92
|
+ List<CategoryVisibleChildVO> result = categoryAppService.listLevel2Tabs(1L);
|
|
|
93
|
+
|
|
|
94
|
+ assertEquals(2, result.size());
|
|
|
95
|
+ assertEquals("抗生素", result.get(0).getCategoryName());
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ @Test
|
|
|
99
|
+ void listLevel2Tabs_hiddenLevel1Rejected()
|
|
|
100
|
+ {
|
|
|
101
|
+ BizGoodsCategory hidden = platformLevel1(99L);
|
|
|
102
|
+ hidden.setShowFlag(CategoryConstants.SHOW_NO);
|
|
|
103
|
+ when(categoryMapper.selectByIdIgnoreShop(99L)).thenReturn(hidden);
|
|
|
104
|
+
|
|
|
105
|
+ ServiceException ex = assertThrows(ServiceException.class,
|
|
|
106
|
+ () -> categoryAppService.listLevel2Tabs(99L));
|
|
|
107
|
+
|
|
|
108
|
+ assertEquals(CategoryAppConstants.MSG_CATEGORY_INVISIBLE, ex.getMessage());
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ @Test
|
|
|
112
|
+ void listCategoryGoods_missingCategoryIdRejected()
|
|
|
113
|
+ {
|
|
|
114
|
+ CategoryGoodsQuery query = new CategoryGoodsQuery();
|
|
|
115
|
+
|
|
|
116
|
+ ServiceException ex = assertThrows(ServiceException.class,
|
|
|
117
|
+ () -> categoryAppService.listCategoryGoods(query));
|
|
|
118
|
+
|
|
|
119
|
+ assertEquals(CategoryAppConstants.MSG_CATEGORY_REQUIRED, ex.getMessage());
|
|
|
120
|
+ }
|
|
|
121
|
+
|
|
|
122
|
+ @Test
|
|
|
123
|
+ void listCategoryGoods_invisibleCategoryRejected()
|
|
|
124
|
+ {
|
|
|
125
|
+ CategoryGoodsQuery query = new CategoryGoodsQuery();
|
|
|
126
|
+ query.setCategoryId(20L);
|
|
|
127
|
+ when(categoryFacade.isCategoryVisible(20L)).thenReturn(false);
|
|
|
128
|
+
|
|
|
129
|
+ ServiceException ex = assertThrows(ServiceException.class,
|
|
|
130
|
+ () -> categoryAppService.listCategoryGoods(query));
|
|
|
131
|
+
|
|
|
132
|
+ assertEquals(CategoryAppConstants.MSG_CATEGORY_INVISIBLE, ex.getMessage());
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ @Test
|
|
|
136
|
+ void listCategoryGoods_defaultSalesDescSort()
|
|
|
137
|
+ {
|
|
|
138
|
+ CategoryGoodsQuery query = new CategoryGoodsQuery();
|
|
|
139
|
+ query.setCategoryId(11L);
|
|
|
140
|
+ when(categoryFacade.isCategoryVisible(11L)).thenReturn(true);
|
|
|
141
|
+ when(categoryAppMapper.selectGoodsByCategory(11L, CategoryAppConstants.DEFAULT_SORT))
|
|
|
142
|
+ .thenReturn(Collections.singletonList(goods(1L, "G001")));
|
|
|
143
|
+
|
|
|
144
|
+ categoryAppService.listCategoryGoods(query);
|
|
|
145
|
+
|
|
|
146
|
+ verify(categoryAppMapper).selectGoodsByCategory(11L, CategoryAppConstants.SORT_SALES_DESC);
|
|
|
147
|
+ }
|
|
|
148
|
+
|
|
|
149
|
+ @Test
|
|
|
150
|
+ void listCategoryGoods_priceAscSort()
|
|
|
151
|
+ {
|
|
|
152
|
+ CategoryGoodsQuery query = new CategoryGoodsQuery();
|
|
|
153
|
+ query.setCategoryId(11L);
|
|
|
154
|
+ query.setSortBy(CategoryAppConstants.SORT_PRICE_ASC);
|
|
|
155
|
+ when(categoryFacade.isCategoryVisible(11L)).thenReturn(true);
|
|
|
156
|
+ when(categoryAppMapper.selectGoodsByCategory(11L, CategoryAppConstants.SORT_PRICE_ASC))
|
|
|
157
|
+ .thenReturn(Collections.singletonList(goods(1L, "G001")));
|
|
|
158
|
+
|
|
|
159
|
+ categoryAppService.listCategoryGoods(query);
|
|
|
160
|
+
|
|
|
161
|
+ verify(categoryAppMapper).selectGoodsByCategory(11L, CategoryAppConstants.SORT_PRICE_ASC);
|
|
|
162
|
+ }
|
|
|
163
|
+
|
|
|
164
|
+ @Test
|
|
|
165
|
+ void listCategoryGoods_invalidSortRejected()
|
|
|
166
|
+ {
|
|
|
167
|
+ CategoryGoodsQuery query = new CategoryGoodsQuery();
|
|
|
168
|
+ query.setCategoryId(11L);
|
|
|
169
|
+ query.setSortBy("invalid");
|
|
|
170
|
+ when(categoryFacade.isCategoryVisible(11L)).thenReturn(true);
|
|
|
171
|
+
|
|
|
172
|
+ ServiceException ex = assertThrows(ServiceException.class,
|
|
|
173
|
+ () -> categoryAppService.listCategoryGoods(query));
|
|
|
174
|
+
|
|
|
175
|
+ assertEquals(CategoryAppConstants.MSG_SORT_INVALID, ex.getMessage());
|
|
|
176
|
+ }
|
|
|
177
|
+
|
|
|
178
|
+ @Test
|
|
|
179
|
+ void listCategoryGoods_preservesMapperOrder()
|
|
|
180
|
+ {
|
|
|
181
|
+ CategoryGoodsQuery query = new CategoryGoodsQuery();
|
|
|
182
|
+ query.setCategoryId(11L);
|
|
|
183
|
+ when(categoryFacade.isCategoryVisible(11L)).thenReturn(true);
|
|
|
184
|
+ HomeHotGoodsVO g1 = goods(1L, "G001");
|
|
|
185
|
+ HomeHotGoodsVO g3 = goods(3L, "G003");
|
|
|
186
|
+ when(categoryAppMapper.selectGoodsByCategory(11L, CategoryAppConstants.DEFAULT_SORT))
|
|
|
187
|
+ .thenReturn(Arrays.asList(g1, g3));
|
|
|
188
|
+
|
|
|
189
|
+ List<HomeHotGoodsVO> result = categoryAppService.listCategoryGoods(query);
|
|
|
190
|
+
|
|
|
191
|
+ assertEquals(1L, result.get(0).getGoodsId());
|
|
|
192
|
+ assertEquals(3L, result.get(1).getGoodsId());
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ @Test
|
|
|
196
|
+ void listCategoryGoods_emptyListWhenNoGoods()
|
|
|
197
|
+ {
|
|
|
198
|
+ CategoryGoodsQuery query = new CategoryGoodsQuery();
|
|
|
199
|
+ query.setCategoryId(11L);
|
|
|
200
|
+ when(categoryFacade.isCategoryVisible(11L)).thenReturn(true);
|
|
|
201
|
+ when(categoryAppMapper.selectGoodsByCategory(11L, CategoryAppConstants.DEFAULT_SORT))
|
|
|
202
|
+ .thenReturn(Collections.emptyList());
|
|
|
203
|
+
|
|
|
204
|
+ List<HomeHotGoodsVO> result = categoryAppService.listCategoryGoods(query);
|
|
|
205
|
+
|
|
|
206
|
+ assertNotNull(result);
|
|
|
207
|
+ assertTrue(result.isEmpty());
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+ private static CategoryVisibleVO visibleLevel1(Long id, String name)
|
|
|
211
|
+ {
|
|
|
212
|
+ CategoryVisibleVO vo = new CategoryVisibleVO();
|
|
|
213
|
+ vo.setCategoryId(id);
|
|
|
214
|
+ vo.setCategoryName(name);
|
|
|
215
|
+ vo.setSortNo(0);
|
|
|
216
|
+ return vo;
|
|
|
217
|
+ }
|
|
|
218
|
+
|
|
|
219
|
+ private static BizGoodsCategory platformLevel1(Long id)
|
|
|
220
|
+ {
|
|
|
221
|
+ BizGoodsCategory c = new BizGoodsCategory();
|
|
|
222
|
+ c.setCategoryId(id);
|
|
|
223
|
+ c.setShopId(null);
|
|
|
224
|
+ c.setCategoryLevel(CategoryConstants.LEVEL_ONE);
|
|
|
225
|
+ c.setShowFlag(CategoryConstants.SHOW_YES);
|
|
|
226
|
+ c.setDelFlag(CategoryConstants.DEL_FLAG_NORMAL);
|
|
|
227
|
+ return c;
|
|
|
228
|
+ }
|
|
|
229
|
+
|
|
|
230
|
+ private static CategoryVisibleChildVO level2Tab(Long id, String name, int sortNo)
|
|
|
231
|
+ {
|
|
|
232
|
+ CategoryVisibleChildVO vo = new CategoryVisibleChildVO();
|
|
|
233
|
+ vo.setCategoryId(id);
|
|
|
234
|
+ vo.setCategoryName(name);
|
|
|
235
|
+ vo.setSortNo(sortNo);
|
|
|
236
|
+ return vo;
|
|
|
237
|
+ }
|
|
|
238
|
+
|
|
|
239
|
+ private static HomeHotGoodsVO goods(Long goodsId, String goodsSn)
|
|
|
240
|
+ {
|
|
|
241
|
+ HomeHotGoodsVO vo = new HomeHotGoodsVO();
|
|
|
242
|
+ vo.setGoodsId(goodsId);
|
|
|
243
|
+ vo.setGoodsSn(goodsSn);
|
|
|
244
|
+ vo.setGoodsName("商品" + goodsSn);
|
|
|
245
|
+ vo.setMainPic("/pic/" + goodsSn + ".jpg");
|
|
|
246
|
+ vo.setSalePrice(new BigDecimal("88.50"));
|
|
|
247
|
+ vo.setShopId(101L);
|
|
|
248
|
+ vo.setShopName("测试店");
|
|
|
249
|
+ return vo;
|
|
|
250
|
+ }
|
|
|
251
|
+}
|