提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.dict; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
4 |
import com.iailab.framework.common.pojo.PageResult; |
|
5 |
import com.iailab.framework.common.util.collection.ArrayUtils; |
|
6 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
7 |
import com.iailab.module.system.controller.admin.dict.vo.data.DictDataPageReqVO; |
|
8 |
import com.iailab.module.system.controller.admin.dict.vo.data.DictDataSaveReqVO; |
|
9 |
import com.iailab.module.system.dal.dataobject.dict.DictDataDO; |
|
10 |
import com.iailab.module.system.dal.dataobject.dict.DictTypeDO; |
|
11 |
import com.iailab.module.system.dal.mysql.dict.DictDataMapper; |
|
12 |
import org.junit.jupiter.api.Test; |
|
13 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
14 |
import org.springframework.context.annotation.Import; |
|
15 |
|
|
16 |
import javax.annotation.Resource; |
|
17 |
import java.util.List; |
|
18 |
import java.util.function.Consumer; |
|
19 |
|
|
20 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
21 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
22 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
23 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
24 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
25 |
import static java.util.Collections.singletonList; |
|
26 |
import static org.junit.jupiter.api.Assertions.*; |
|
27 |
import static org.mockito.ArgumentMatchers.eq; |
|
28 |
import static org.mockito.Mockito.when; |
|
29 |
|
|
30 |
@Import(DictDataServiceImpl.class) |
|
31 |
public class DictDataServiceImplTest extends BaseDbUnitTest { |
|
32 |
|
|
33 |
@Resource |
|
34 |
private DictDataServiceImpl dictDataService; |
|
35 |
|
|
36 |
@Resource |
|
37 |
private DictDataMapper dictDataMapper; |
|
38 |
@MockBean |
|
39 |
private DictTypeService dictTypeService; |
|
40 |
|
|
41 |
@Test |
|
42 |
public void testGetDictDataList() { |
|
43 |
// mock 数据 |
|
44 |
DictDataDO dictDataDO01 = randomDictDataDO().setDictType("yunai").setSort(2) |
|
45 |
.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
46 |
dictDataMapper.insert(dictDataDO01); |
|
47 |
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setSort(1) |
|
48 |
.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
49 |
dictDataMapper.insert(dictDataDO02); |
|
50 |
DictDataDO dictDataDO03 = randomDictDataDO().setDictType("yunai").setSort(3) |
|
51 |
.setStatus(CommonStatusEnum.DISABLE.getStatus()); |
|
52 |
dictDataMapper.insert(dictDataDO03); |
|
53 |
DictDataDO dictDataDO04 = randomDictDataDO().setDictType("yunai2").setSort(3) |
|
54 |
.setStatus(CommonStatusEnum.DISABLE.getStatus()); |
|
55 |
dictDataMapper.insert(dictDataDO04); |
|
56 |
// 准备参数 |
|
57 |
Integer status = CommonStatusEnum.ENABLE.getStatus(); |
|
58 |
String dictType = "yunai"; |
|
59 |
|
|
60 |
// 调用 |
|
61 |
List<DictDataDO> dictDataDOList = dictDataService.getDictDataList(status, dictType); |
|
62 |
// 断言 |
|
63 |
assertEquals(2, dictDataDOList.size()); |
|
64 |
assertPojoEquals(dictDataDO02, dictDataDOList.get(0)); |
|
65 |
assertPojoEquals(dictDataDO01, dictDataDOList.get(1)); |
|
66 |
} |
|
67 |
|
|
68 |
@Test |
|
69 |
public void testGetDictDataPage() { |
|
70 |
// mock 数据 |
|
71 |
DictDataDO dbDictData = randomPojo(DictDataDO.class, o -> { // 等会查询到 |
|
72 |
o.setLabel("iailab"); |
|
73 |
o.setDictType("yunai"); |
|
74 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
75 |
}); |
|
76 |
dictDataMapper.insert(dbDictData); |
|
77 |
// 测试 label 不匹配 |
|
78 |
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setLabel("艿"))); |
|
79 |
// 测试 dictType 不匹配 |
|
80 |
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setDictType("nai"))); |
|
81 |
// 测试 status 不匹配 |
|
82 |
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
83 |
// 准备参数 |
|
84 |
DictDataPageReqVO reqVO = new DictDataPageReqVO(); |
|
85 |
reqVO.setLabel("芋"); |
|
86 |
reqVO.setDictType("yunai"); |
|
87 |
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
88 |
|
|
89 |
// 调用 |
|
90 |
PageResult<DictDataDO> pageResult = dictDataService.getDictDataPage(reqVO); |
|
91 |
// 断言 |
|
92 |
assertEquals(1, pageResult.getTotal()); |
|
93 |
assertEquals(1, pageResult.getList().size()); |
|
94 |
assertPojoEquals(dbDictData, pageResult.getList().get(0)); |
|
95 |
} |
|
96 |
|
|
97 |
@Test |
|
98 |
public void testGetDictData() { |
|
99 |
// mock 数据 |
|
100 |
DictDataDO dbDictData = randomDictDataDO(); |
|
101 |
dictDataMapper.insert(dbDictData); |
|
102 |
// 准备参数 |
|
103 |
Long id = dbDictData.getId(); |
|
104 |
|
|
105 |
// 调用 |
|
106 |
DictDataDO dictData = dictDataService.getDictData(id); |
|
107 |
// 断言 |
|
108 |
assertPojoEquals(dbDictData, dictData); |
|
109 |
} |
|
110 |
|
|
111 |
@Test |
|
112 |
public void testCreateDictData_success() { |
|
113 |
// 准备参数 |
|
114 |
DictDataSaveReqVO reqVO = randomPojo(DictDataSaveReqVO.class, |
|
115 |
o -> o.setStatus(randomCommonStatus())) |
|
116 |
.setId(null); // 防止 id 被赋值 |
|
117 |
// mock 方法 |
|
118 |
when(dictTypeService.getDictType(eq(reqVO.getDictType()))).thenReturn(randomDictTypeDO(reqVO.getDictType())); |
|
119 |
|
|
120 |
// 调用 |
|
121 |
Long dictDataId = dictDataService.createDictData(reqVO); |
|
122 |
// 断言 |
|
123 |
assertNotNull(dictDataId); |
|
124 |
// 校验记录的属性是否正确 |
|
125 |
DictDataDO dictData = dictDataMapper.selectById(dictDataId); |
|
126 |
assertPojoEquals(reqVO, dictData, "id"); |
|
127 |
} |
|
128 |
|
|
129 |
@Test |
|
130 |
public void testUpdateDictData_success() { |
|
131 |
// mock 数据 |
|
132 |
DictDataDO dbDictData = randomDictDataDO(); |
|
133 |
dictDataMapper.insert(dbDictData);// @Sql: 先插入出一条存在的数据 |
|
134 |
// 准备参数 |
|
135 |
DictDataSaveReqVO reqVO = randomPojo(DictDataSaveReqVO.class, o -> { |
|
136 |
o.setId(dbDictData.getId()); // 设置更新的 ID |
|
137 |
o.setStatus(randomCommonStatus()); |
|
138 |
}); |
|
139 |
// mock 方法,字典类型 |
|
140 |
when(dictTypeService.getDictType(eq(reqVO.getDictType()))).thenReturn(randomDictTypeDO(reqVO.getDictType())); |
|
141 |
|
|
142 |
// 调用 |
|
143 |
dictDataService.updateDictData(reqVO); |
|
144 |
// 校验是否更新正确 |
|
145 |
DictDataDO dictData = dictDataMapper.selectById(reqVO.getId()); // 获取最新的 |
|
146 |
assertPojoEquals(reqVO, dictData); |
|
147 |
} |
|
148 |
|
|
149 |
@Test |
|
150 |
public void testDeleteDictData_success() { |
|
151 |
// mock 数据 |
|
152 |
DictDataDO dbDictData = randomDictDataDO(); |
|
153 |
dictDataMapper.insert(dbDictData);// @Sql: 先插入出一条存在的数据 |
|
154 |
// 准备参数 |
|
155 |
Long id = dbDictData.getId(); |
|
156 |
|
|
157 |
// 调用 |
|
158 |
dictDataService.deleteDictData(id); |
|
159 |
// 校验数据不存在了 |
|
160 |
assertNull(dictDataMapper.selectById(id)); |
|
161 |
} |
|
162 |
|
|
163 |
@Test |
|
164 |
public void testValidateDictDataExists_success() { |
|
165 |
// mock 数据 |
|
166 |
DictDataDO dbDictData = randomDictDataDO(); |
|
167 |
dictDataMapper.insert(dbDictData);// @Sql: 先插入出一条存在的数据 |
|
168 |
|
|
169 |
// 调用成功 |
|
170 |
dictDataService.validateDictDataExists(dbDictData.getId()); |
|
171 |
} |
|
172 |
|
|
173 |
@Test |
|
174 |
public void testValidateDictDataExists_notExists() { |
|
175 |
assertServiceException(() -> dictDataService.validateDictDataExists(randomLongId()), DICT_DATA_NOT_EXISTS); |
|
176 |
} |
|
177 |
|
|
178 |
@Test |
|
179 |
public void testValidateDictTypeExists_success() { |
|
180 |
// mock 方法,数据类型被禁用 |
|
181 |
String type = randomString(); |
|
182 |
when(dictTypeService.getDictType(eq(type))).thenReturn(randomDictTypeDO(type)); |
|
183 |
|
|
184 |
// 调用, 成功 |
|
185 |
dictDataService.validateDictTypeExists(type); |
|
186 |
} |
|
187 |
|
|
188 |
@Test |
|
189 |
public void testValidateDictTypeExists_notExists() { |
|
190 |
assertServiceException(() -> dictDataService.validateDictTypeExists(randomString()), DICT_TYPE_NOT_EXISTS); |
|
191 |
} |
|
192 |
|
|
193 |
@Test |
|
194 |
public void testValidateDictTypeExists_notEnable() { |
|
195 |
// mock 方法,数据类型被禁用 |
|
196 |
String dictType = randomString(); |
|
197 |
when(dictTypeService.getDictType(eq(dictType))).thenReturn( |
|
198 |
randomPojo(DictTypeDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
199 |
|
|
200 |
// 调用, 并断言异常 |
|
201 |
assertServiceException(() -> dictDataService.validateDictTypeExists(dictType), DICT_TYPE_NOT_ENABLE); |
|
202 |
} |
|
203 |
|
|
204 |
@Test |
|
205 |
public void testValidateDictDataValueUnique_success() { |
|
206 |
// 调用,成功 |
|
207 |
dictDataService.validateDictDataValueUnique(randomLongId(), randomString(), randomString()); |
|
208 |
} |
|
209 |
|
|
210 |
@Test |
|
211 |
public void testValidateDictDataValueUnique_valueDuplicateForCreate() { |
|
212 |
// 准备参数 |
|
213 |
String dictType = randomString(); |
|
214 |
String value = randomString(); |
|
215 |
// mock 数据 |
|
216 |
dictDataMapper.insert(randomDictDataDO(o -> { |
|
217 |
o.setDictType(dictType); |
|
218 |
o.setValue(value); |
|
219 |
})); |
|
220 |
|
|
221 |
// 调用,校验异常 |
|
222 |
assertServiceException(() -> dictDataService.validateDictDataValueUnique(null, dictType, value), |
|
223 |
DICT_DATA_VALUE_DUPLICATE); |
|
224 |
} |
|
225 |
|
|
226 |
@Test |
|
227 |
public void testValidateDictDataValueUnique_valueDuplicateForUpdate() { |
|
228 |
// 准备参数 |
|
229 |
Long id = randomLongId(); |
|
230 |
String dictType = randomString(); |
|
231 |
String value = randomString(); |
|
232 |
// mock 数据 |
|
233 |
dictDataMapper.insert(randomDictDataDO(o -> { |
|
234 |
o.setDictType(dictType); |
|
235 |
o.setValue(value); |
|
236 |
})); |
|
237 |
|
|
238 |
// 调用,校验异常 |
|
239 |
assertServiceException(() -> dictDataService.validateDictDataValueUnique(id, dictType, value), |
|
240 |
DICT_DATA_VALUE_DUPLICATE); |
|
241 |
} |
|
242 |
|
|
243 |
@Test |
|
244 |
public void testGetDictDataCountByDictType() { |
|
245 |
// mock 数据 |
|
246 |
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai"))); |
|
247 |
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("tudou"))); |
|
248 |
dictDataMapper.insert(randomDictDataDO(o -> o.setDictType("yunai"))); |
|
249 |
// 准备参数 |
|
250 |
String dictType = "yunai"; |
|
251 |
|
|
252 |
// 调用 |
|
253 |
long count = dictDataService.getDictDataCountByDictType(dictType); |
|
254 |
// 校验 |
|
255 |
assertEquals(2L, count); |
|
256 |
} |
|
257 |
|
|
258 |
@Test |
|
259 |
public void testValidateDictDataList_success() { |
|
260 |
// mock 数据 |
|
261 |
DictDataDO dictDataDO = randomDictDataDO().setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
262 |
dictDataMapper.insert(dictDataDO); |
|
263 |
// 准备参数 |
|
264 |
String dictType = dictDataDO.getDictType(); |
|
265 |
List<String> values = singletonList(dictDataDO.getValue()); |
|
266 |
|
|
267 |
// 调用,无需断言 |
|
268 |
dictDataService.validateDictDataList(dictType, values); |
|
269 |
} |
|
270 |
|
|
271 |
@Test |
|
272 |
public void testValidateDictDataList_notFound() { |
|
273 |
// 准备参数 |
|
274 |
String dictType = randomString(); |
|
275 |
List<String> values = singletonList(randomString()); |
|
276 |
|
|
277 |
// 调用, 并断言异常 |
|
278 |
assertServiceException(() -> dictDataService.validateDictDataList(dictType, values), DICT_DATA_NOT_EXISTS); |
|
279 |
} |
|
280 |
|
|
281 |
@Test |
|
282 |
public void testValidateDictDataList_notEnable() { |
|
283 |
// mock 数据 |
|
284 |
DictDataDO dictDataDO = randomDictDataDO().setStatus(CommonStatusEnum.DISABLE.getStatus()); |
|
285 |
dictDataMapper.insert(dictDataDO); |
|
286 |
// 准备参数 |
|
287 |
String dictType = dictDataDO.getDictType(); |
|
288 |
List<String> values = singletonList(dictDataDO.getValue()); |
|
289 |
|
|
290 |
// 调用, 并断言异常 |
|
291 |
assertServiceException(() -> dictDataService.validateDictDataList(dictType, values), |
|
292 |
DICT_DATA_NOT_ENABLE, dictDataDO.getLabel()); |
|
293 |
} |
|
294 |
|
|
295 |
@Test |
|
296 |
public void testGetDictData_dictType() { |
|
297 |
// mock 数据 |
|
298 |
DictDataDO dictDataDO = randomDictDataDO().setDictType("yunai").setValue("1"); |
|
299 |
dictDataMapper.insert(dictDataDO); |
|
300 |
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setValue("2"); |
|
301 |
dictDataMapper.insert(dictDataDO02); |
|
302 |
// 准备参数 |
|
303 |
String dictType = "yunai"; |
|
304 |
String value = "1"; |
|
305 |
|
|
306 |
// 调用 |
|
307 |
DictDataDO dbDictData = dictDataService.getDictData(dictType, value); |
|
308 |
// 断言 |
|
309 |
assertEquals(dictDataDO, dbDictData); |
|
310 |
} |
|
311 |
|
|
312 |
@Test |
|
313 |
public void testParseDictData() { |
|
314 |
// mock 数据 |
|
315 |
DictDataDO dictDataDO = randomDictDataDO().setDictType("yunai").setLabel("1"); |
|
316 |
dictDataMapper.insert(dictDataDO); |
|
317 |
DictDataDO dictDataDO02 = randomDictDataDO().setDictType("yunai").setLabel("2"); |
|
318 |
dictDataMapper.insert(dictDataDO02); |
|
319 |
// 准备参数 |
|
320 |
String dictType = "yunai"; |
|
321 |
String label = "1"; |
|
322 |
|
|
323 |
// 调用 |
|
324 |
DictDataDO dbDictData = dictDataService.parseDictData(dictType, label); |
|
325 |
// 断言 |
|
326 |
assertEquals(dictDataDO, dbDictData); |
|
327 |
} |
|
328 |
|
|
329 |
// ========== 随机对象 ========== |
|
330 |
|
|
331 |
@SafeVarargs |
|
332 |
private static DictDataDO randomDictDataDO(Consumer<DictDataDO>... consumers) { |
|
333 |
Consumer<DictDataDO> consumer = (o) -> { |
|
334 |
o.setStatus(randomCommonStatus()); // 保证 status 的范围 |
|
335 |
}; |
|
336 |
return randomPojo(DictDataDO.class, ArrayUtils.append(consumer, consumers)); |
|
337 |
} |
|
338 |
|
|
339 |
/** |
|
340 |
* 生成一个有效的字典类型 |
|
341 |
* |
|
342 |
* @param type 字典类型 |
|
343 |
* @return DictTypeDO 对象 |
|
344 |
*/ |
|
345 |
private static DictTypeDO randomDictTypeDO(String type) { |
|
346 |
return randomPojo(DictTypeDO.class, o -> { |
|
347 |
o.setType(type); |
|
348 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 保证 status 是开启 |
|
349 |
}); |
|
350 |
} |
|
351 |
|
|
352 |
} |