提交 | 用户 | 时间
|
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.type.DictTypePageReqVO; |
|
8 |
import com.iailab.module.system.controller.admin.dict.vo.type.DictTypeSaveReqVO; |
|
9 |
import com.iailab.module.system.dal.dataobject.dict.DictTypeDO; |
|
10 |
import com.iailab.module.system.dal.mysql.dict.DictTypeMapper; |
|
11 |
import org.junit.jupiter.api.Test; |
|
12 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
13 |
import org.springframework.context.annotation.Import; |
|
14 |
|
|
15 |
import javax.annotation.Resource; |
|
16 |
import java.util.List; |
|
17 |
import java.util.function.Consumer; |
|
18 |
|
|
19 |
import static cn.hutool.core.util.RandomUtil.randomEle; |
|
20 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
|
21 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
22 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
23 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
24 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
25 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
26 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
27 |
import static org.junit.jupiter.api.Assertions.*; |
|
28 |
import static org.mockito.ArgumentMatchers.eq; |
|
29 |
import static org.mockito.Mockito.when; |
|
30 |
|
|
31 |
@Import(DictTypeServiceImpl.class) |
|
32 |
public class DictTypeServiceImplTest extends BaseDbUnitTest { |
|
33 |
|
|
34 |
@Resource |
|
35 |
private DictTypeServiceImpl dictTypeService; |
|
36 |
|
|
37 |
@Resource |
|
38 |
private DictTypeMapper dictTypeMapper; |
|
39 |
@MockBean |
|
40 |
private DictDataService dictDataService; |
|
41 |
|
|
42 |
@Test |
|
43 |
public void testGetDictTypePage() { |
|
44 |
// mock 数据 |
|
45 |
DictTypeDO dbDictType = randomPojo(DictTypeDO.class, o -> { // 等会查询到 |
|
46 |
o.setName("yunai"); |
|
47 |
o.setType("iailab"); |
|
48 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
49 |
o.setCreateTime(buildTime(2021, 1, 15)); |
|
50 |
}); |
|
51 |
dictTypeMapper.insert(dbDictType); |
|
52 |
// 测试 name 不匹配 |
|
53 |
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); |
|
54 |
// 测试 type 不匹配 |
|
55 |
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); |
|
56 |
// 测试 status 不匹配 |
|
57 |
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
58 |
// 测试 createTime 不匹配 |
|
59 |
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); |
|
60 |
// 准备参数 |
|
61 |
DictTypePageReqVO reqVO = new DictTypePageReqVO(); |
|
62 |
reqVO.setName("nai"); |
|
63 |
reqVO.setType("艿"); |
|
64 |
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
65 |
reqVO.setCreateTime(buildBetweenTime(2021, 1, 10, 2021, 1, 20)); |
|
66 |
|
|
67 |
// 调用 |
|
68 |
PageResult<DictTypeDO> pageResult = dictTypeService.getDictTypePage(reqVO); |
|
69 |
// 断言 |
|
70 |
assertEquals(1, pageResult.getTotal()); |
|
71 |
assertEquals(1, pageResult.getList().size()); |
|
72 |
assertPojoEquals(dbDictType, pageResult.getList().get(0)); |
|
73 |
} |
|
74 |
|
|
75 |
@Test |
|
76 |
public void testGetDictType_id() { |
|
77 |
// mock 数据 |
|
78 |
DictTypeDO dbDictType = randomDictTypeDO(); |
|
79 |
dictTypeMapper.insert(dbDictType); |
|
80 |
// 准备参数 |
|
81 |
Long id = dbDictType.getId(); |
|
82 |
|
|
83 |
// 调用 |
|
84 |
DictTypeDO dictType = dictTypeService.getDictType(id); |
|
85 |
// 断言 |
|
86 |
assertNotNull(dictType); |
|
87 |
assertPojoEquals(dbDictType, dictType); |
|
88 |
} |
|
89 |
|
|
90 |
@Test |
|
91 |
public void testGetDictType_type() { |
|
92 |
// mock 数据 |
|
93 |
DictTypeDO dbDictType = randomDictTypeDO(); |
|
94 |
dictTypeMapper.insert(dbDictType); |
|
95 |
// 准备参数 |
|
96 |
String type = dbDictType.getType(); |
|
97 |
|
|
98 |
// 调用 |
|
99 |
DictTypeDO dictType = dictTypeService.getDictType(type); |
|
100 |
// 断言 |
|
101 |
assertNotNull(dictType); |
|
102 |
assertPojoEquals(dbDictType, dictType); |
|
103 |
} |
|
104 |
|
|
105 |
@Test |
|
106 |
public void testCreateDictType_success() { |
|
107 |
// 准备参数 |
|
108 |
DictTypeSaveReqVO reqVO = randomPojo(DictTypeSaveReqVO.class, |
|
109 |
o -> o.setStatus(randomEle(CommonStatusEnum.values()).getStatus())) |
|
110 |
.setId(null); // 避免 id 被赋值 |
|
111 |
|
|
112 |
// 调用 |
|
113 |
Long dictTypeId = dictTypeService.createDictType(reqVO); |
|
114 |
// 断言 |
|
115 |
assertNotNull(dictTypeId); |
|
116 |
// 校验记录的属性是否正确 |
|
117 |
DictTypeDO dictType = dictTypeMapper.selectById(dictTypeId); |
|
118 |
assertPojoEquals(reqVO, dictType, "id"); |
|
119 |
} |
|
120 |
|
|
121 |
@Test |
|
122 |
public void testUpdateDictType_success() { |
|
123 |
// mock 数据 |
|
124 |
DictTypeDO dbDictType = randomDictTypeDO(); |
|
125 |
dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 |
|
126 |
// 准备参数 |
|
127 |
DictTypeSaveReqVO reqVO = randomPojo(DictTypeSaveReqVO.class, o -> { |
|
128 |
o.setId(dbDictType.getId()); // 设置更新的 ID |
|
129 |
o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); |
|
130 |
}); |
|
131 |
|
|
132 |
// 调用 |
|
133 |
dictTypeService.updateDictType(reqVO); |
|
134 |
// 校验是否更新正确 |
|
135 |
DictTypeDO dictType = dictTypeMapper.selectById(reqVO.getId()); // 获取最新的 |
|
136 |
assertPojoEquals(reqVO, dictType); |
|
137 |
} |
|
138 |
|
|
139 |
@Test |
|
140 |
public void testDeleteDictType_success() { |
|
141 |
// mock 数据 |
|
142 |
DictTypeDO dbDictType = randomDictTypeDO(); |
|
143 |
dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 |
|
144 |
// 准备参数 |
|
145 |
Long id = dbDictType.getId(); |
|
146 |
|
|
147 |
// 调用 |
|
148 |
dictTypeService.deleteDictType(id); |
|
149 |
// 校验数据不存在了 |
|
150 |
assertNull(dictTypeMapper.selectById(id)); |
|
151 |
} |
|
152 |
|
|
153 |
@Test |
|
154 |
public void testDeleteDictType_hasChildren() { |
|
155 |
// mock 数据 |
|
156 |
DictTypeDO dbDictType = randomDictTypeDO(); |
|
157 |
dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 |
|
158 |
// 准备参数 |
|
159 |
Long id = dbDictType.getId(); |
|
160 |
// mock 方法 |
|
161 |
when(dictDataService.getDictDataCountByDictType(eq(dbDictType.getType()))).thenReturn(1L); |
|
162 |
|
|
163 |
// 调用, 并断言异常 |
|
164 |
assertServiceException(() -> dictTypeService.deleteDictType(id), DICT_TYPE_HAS_CHILDREN); |
|
165 |
} |
|
166 |
|
|
167 |
@Test |
|
168 |
public void testGetDictTypeList() { |
|
169 |
// 准备参数 |
|
170 |
DictTypeDO dictTypeDO01 = randomDictTypeDO(); |
|
171 |
dictTypeMapper.insert(dictTypeDO01); |
|
172 |
DictTypeDO dictTypeDO02 = randomDictTypeDO(); |
|
173 |
dictTypeMapper.insert(dictTypeDO02); |
|
174 |
// mock 方法 |
|
175 |
|
|
176 |
// 调用 |
|
177 |
List<DictTypeDO> dictTypeDOList = dictTypeService.getDictTypeList(); |
|
178 |
// 断言 |
|
179 |
assertEquals(2, dictTypeDOList.size()); |
|
180 |
assertPojoEquals(dictTypeDO01, dictTypeDOList.get(0)); |
|
181 |
assertPojoEquals(dictTypeDO02, dictTypeDOList.get(1)); |
|
182 |
} |
|
183 |
|
|
184 |
@Test |
|
185 |
public void testValidateDictDataExists_success() { |
|
186 |
// mock 数据 |
|
187 |
DictTypeDO dbDictType = randomDictTypeDO(); |
|
188 |
dictTypeMapper.insert(dbDictType);// @Sql: 先插入出一条存在的数据 |
|
189 |
|
|
190 |
// 调用成功 |
|
191 |
dictTypeService.validateDictTypeExists(dbDictType.getId()); |
|
192 |
} |
|
193 |
|
|
194 |
@Test |
|
195 |
public void testValidateDictDataExists_notExists() { |
|
196 |
assertServiceException(() -> dictTypeService.validateDictTypeExists(randomLongId()), DICT_TYPE_NOT_EXISTS); |
|
197 |
} |
|
198 |
|
|
199 |
@Test |
|
200 |
public void testValidateDictTypeUnique_success() { |
|
201 |
// 调用,成功 |
|
202 |
dictTypeService.validateDictTypeUnique(randomLongId(), randomString()); |
|
203 |
} |
|
204 |
|
|
205 |
@Test |
|
206 |
public void testValidateDictTypeUnique_valueDuplicateForCreate() { |
|
207 |
// 准备参数 |
|
208 |
String type = randomString(); |
|
209 |
// mock 数据 |
|
210 |
dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); |
|
211 |
|
|
212 |
// 调用,校验异常 |
|
213 |
assertServiceException(() -> dictTypeService.validateDictTypeUnique(null, type), |
|
214 |
DICT_TYPE_TYPE_DUPLICATE); |
|
215 |
} |
|
216 |
|
|
217 |
@Test |
|
218 |
public void testValidateDictTypeUnique_valueDuplicateForUpdate() { |
|
219 |
// 准备参数 |
|
220 |
Long id = randomLongId(); |
|
221 |
String type = randomString(); |
|
222 |
// mock 数据 |
|
223 |
dictTypeMapper.insert(randomDictTypeDO(o -> o.setType(type))); |
|
224 |
|
|
225 |
// 调用,校验异常 |
|
226 |
assertServiceException(() -> dictTypeService.validateDictTypeUnique(id, type), |
|
227 |
DICT_TYPE_TYPE_DUPLICATE); |
|
228 |
} |
|
229 |
|
|
230 |
@Test |
|
231 |
public void testValidateDictTypNameUnique_success() { |
|
232 |
// 调用,成功 |
|
233 |
dictTypeService.validateDictTypeNameUnique(randomLongId(), randomString()); |
|
234 |
} |
|
235 |
|
|
236 |
@Test |
|
237 |
public void testValidateDictTypeNameUnique_nameDuplicateForCreate() { |
|
238 |
// 准备参数 |
|
239 |
String name = randomString(); |
|
240 |
// mock 数据 |
|
241 |
dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); |
|
242 |
|
|
243 |
// 调用,校验异常 |
|
244 |
assertServiceException(() -> dictTypeService.validateDictTypeNameUnique(null, name), |
|
245 |
DICT_TYPE_NAME_DUPLICATE); |
|
246 |
} |
|
247 |
|
|
248 |
@Test |
|
249 |
public void testValidateDictTypeNameUnique_nameDuplicateForUpdate() { |
|
250 |
// 准备参数 |
|
251 |
Long id = randomLongId(); |
|
252 |
String name = randomString(); |
|
253 |
// mock 数据 |
|
254 |
dictTypeMapper.insert(randomDictTypeDO(o -> o.setName(name))); |
|
255 |
|
|
256 |
// 调用,校验异常 |
|
257 |
assertServiceException(() -> dictTypeService.validateDictTypeNameUnique(id, name), |
|
258 |
DICT_TYPE_NAME_DUPLICATE); |
|
259 |
} |
|
260 |
|
|
261 |
// ========== 随机对象 ========== |
|
262 |
|
|
263 |
@SafeVarargs |
|
264 |
private static DictTypeDO randomDictTypeDO(Consumer<DictTypeDO>... consumers) { |
|
265 |
Consumer<DictTypeDO> consumer = (o) -> { |
|
266 |
o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 |
|
267 |
}; |
|
268 |
return randomPojo(DictTypeDO.class, ArrayUtils.append(consumer, consumers)); |
|
269 |
} |
|
270 |
|
|
271 |
} |