提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.dept; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
4 |
import com.iailab.framework.common.util.object.ObjectUtils; |
|
5 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
6 |
import com.iailab.module.system.controller.admin.dept.vo.dept.DeptListReqVO; |
|
7 |
import com.iailab.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO; |
|
8 |
import com.iailab.module.system.dal.dataobject.dept.DeptDO; |
|
9 |
import com.iailab.module.system.dal.mysql.dept.DeptMapper; |
|
10 |
import org.junit.jupiter.api.Test; |
|
11 |
import org.springframework.context.annotation.Import; |
|
12 |
|
|
13 |
import javax.annotation.Resource; |
|
14 |
import java.util.Arrays; |
|
15 |
import java.util.List; |
|
16 |
import java.util.Set; |
|
17 |
|
|
18 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
19 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
20 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
21 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
22 |
import static java.util.Collections.singletonList; |
|
23 |
import static org.junit.jupiter.api.Assertions.*; |
|
24 |
|
|
25 |
/** |
|
26 |
* {@link DeptServiceImpl} 的单元测试类 |
|
27 |
* |
|
28 |
* @author niudehua |
|
29 |
*/ |
|
30 |
@Import(DeptServiceImpl.class) |
|
31 |
public class DeptServiceImplTest extends BaseDbUnitTest { |
|
32 |
|
|
33 |
@Resource |
|
34 |
private DeptServiceImpl deptService; |
|
35 |
@Resource |
|
36 |
private DeptMapper deptMapper; |
|
37 |
|
|
38 |
@Test |
|
39 |
public void testCreateDept() { |
|
40 |
// 准备参数 |
|
41 |
DeptSaveReqVO reqVO = randomPojo(DeptSaveReqVO.class, o -> { |
|
42 |
o.setId(null); // 防止 id 被设置 |
|
43 |
o.setParentId(DeptDO.PARENT_ID_ROOT); |
|
44 |
o.setStatus(randomCommonStatus()); |
|
45 |
}); |
|
46 |
|
|
47 |
// 调用 |
|
48 |
Long deptId = deptService.createDept(reqVO); |
|
49 |
// 断言 |
|
50 |
assertNotNull(deptId); |
|
51 |
// 校验记录的属性是否正确 |
|
52 |
DeptDO deptDO = deptMapper.selectById(deptId); |
|
53 |
assertPojoEquals(reqVO, deptDO, "id"); |
|
54 |
} |
|
55 |
|
|
56 |
@Test |
|
57 |
public void testUpdateDept() { |
|
58 |
// mock 数据 |
|
59 |
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); |
|
60 |
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 |
|
61 |
// 准备参数 |
|
62 |
DeptSaveReqVO reqVO = randomPojo(DeptSaveReqVO.class, o -> { |
|
63 |
// 设置更新的 ID |
|
64 |
o.setParentId(DeptDO.PARENT_ID_ROOT); |
|
65 |
o.setId(dbDeptDO.getId()); |
|
66 |
o.setStatus(randomCommonStatus()); |
|
67 |
}); |
|
68 |
|
|
69 |
// 调用 |
|
70 |
deptService.updateDept(reqVO); |
|
71 |
// 校验是否更新正确 |
|
72 |
DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的 |
|
73 |
assertPojoEquals(reqVO, deptDO); |
|
74 |
} |
|
75 |
|
|
76 |
@Test |
|
77 |
public void testDeleteDept_success() { |
|
78 |
// mock 数据 |
|
79 |
DeptDO dbDeptDO = randomPojo(DeptDO.class); |
|
80 |
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 |
|
81 |
// 准备参数 |
|
82 |
Long id = dbDeptDO.getId(); |
|
83 |
|
|
84 |
// 调用 |
|
85 |
deptService.deleteDept(id); |
|
86 |
// 校验数据不存在了 |
|
87 |
assertNull(deptMapper.selectById(id)); |
|
88 |
} |
|
89 |
|
|
90 |
@Test |
|
91 |
public void testDeleteDept_exitsChildren() { |
|
92 |
// mock 数据 |
|
93 |
DeptDO parentDept = randomPojo(DeptDO.class); |
|
94 |
deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据 |
|
95 |
// 准备参数 |
|
96 |
DeptDO childrenDeptDO = randomPojo(DeptDO.class, o -> { |
|
97 |
o.setParentId(parentDept.getId()); |
|
98 |
o.setStatus(randomCommonStatus()); |
|
99 |
}); |
|
100 |
// 插入子部门 |
|
101 |
deptMapper.insert(childrenDeptDO); |
|
102 |
|
|
103 |
// 调用, 并断言异常 |
|
104 |
assertServiceException(() -> deptService.deleteDept(parentDept.getId()), DEPT_EXITS_CHILDREN); |
|
105 |
} |
|
106 |
|
|
107 |
@Test |
|
108 |
public void testValidateDeptExists_notFound() { |
|
109 |
// 准备参数 |
|
110 |
Long id = randomLongId(); |
|
111 |
|
|
112 |
// 调用, 并断言异常 |
|
113 |
assertServiceException(() -> deptService.validateDeptExists(id), DEPT_NOT_FOUND); |
|
114 |
} |
|
115 |
|
|
116 |
@Test |
|
117 |
public void testValidateParentDept_parentError() { |
|
118 |
// 准备参数 |
|
119 |
Long id = randomLongId(); |
|
120 |
|
|
121 |
// 调用, 并断言异常 |
|
122 |
assertServiceException(() -> deptService.validateParentDept(id, id), |
|
123 |
DEPT_PARENT_ERROR); |
|
124 |
} |
|
125 |
|
|
126 |
@Test |
|
127 |
public void testValidateParentDept_parentIsChild() { |
|
128 |
// mock 数据(父节点) |
|
129 |
DeptDO parentDept = randomPojo(DeptDO.class); |
|
130 |
deptMapper.insert(parentDept); |
|
131 |
// mock 数据(子节点) |
|
132 |
DeptDO childDept = randomPojo(DeptDO.class, o -> { |
|
133 |
o.setParentId(parentDept.getId()); |
|
134 |
}); |
|
135 |
deptMapper.insert(childDept); |
|
136 |
|
|
137 |
// 准备参数 |
|
138 |
Long id = parentDept.getId(); |
|
139 |
Long parentId = childDept.getId(); |
|
140 |
|
|
141 |
// 调用, 并断言异常 |
|
142 |
assertServiceException(() -> deptService.validateParentDept(id, parentId), DEPT_PARENT_IS_CHILD); |
|
143 |
} |
|
144 |
|
|
145 |
@Test |
|
146 |
public void testValidateNameUnique_duplicate() { |
|
147 |
// mock 数据 |
|
148 |
DeptDO deptDO = randomPojo(DeptDO.class); |
|
149 |
deptMapper.insert(deptDO); |
|
150 |
|
|
151 |
// 准备参数 |
|
152 |
Long id = randomLongId(); |
|
153 |
Long parentId = deptDO.getParentId(); |
|
154 |
String name = deptDO.getName(); |
|
155 |
|
|
156 |
// 调用, 并断言异常 |
|
157 |
assertServiceException(() -> deptService.validateDeptNameUnique(id, parentId, name), |
|
158 |
DEPT_NAME_DUPLICATE); |
|
159 |
} |
|
160 |
|
|
161 |
@Test |
|
162 |
public void testGetDept() { |
|
163 |
// mock 数据 |
|
164 |
DeptDO deptDO = randomPojo(DeptDO.class); |
|
165 |
deptMapper.insert(deptDO); |
|
166 |
// 准备参数 |
|
167 |
Long id = deptDO.getId(); |
|
168 |
|
|
169 |
// 调用 |
|
170 |
DeptDO dbDept = deptService.getDept(id); |
|
171 |
// 断言 |
|
172 |
assertEquals(deptDO, dbDept); |
|
173 |
} |
|
174 |
|
|
175 |
@Test |
|
176 |
public void testGetDeptList_ids() { |
|
177 |
// mock 数据 |
|
178 |
DeptDO deptDO01 = randomPojo(DeptDO.class); |
|
179 |
deptMapper.insert(deptDO01); |
|
180 |
DeptDO deptDO02 = randomPojo(DeptDO.class); |
|
181 |
deptMapper.insert(deptDO02); |
|
182 |
// 准备参数 |
|
183 |
List<Long> ids = Arrays.asList(deptDO01.getId(), deptDO02.getId()); |
|
184 |
|
|
185 |
// 调用 |
|
186 |
List<DeptDO> deptDOList = deptService.getDeptList(ids); |
|
187 |
// 断言 |
|
188 |
assertEquals(2, deptDOList.size()); |
|
189 |
assertEquals(deptDO01, deptDOList.get(0)); |
|
190 |
assertEquals(deptDO02, deptDOList.get(1)); |
|
191 |
} |
|
192 |
|
|
193 |
@Test |
|
194 |
public void testGetDeptList_reqVO() { |
|
195 |
// mock 数据 |
|
196 |
DeptDO dept = randomPojo(DeptDO.class, o -> { // 等会查询到 |
|
197 |
o.setName("开发部"); |
|
198 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
199 |
}); |
|
200 |
deptMapper.insert(dept); |
|
201 |
// 测试 name 不匹配 |
|
202 |
deptMapper.insert(ObjectUtils.cloneIgnoreId(dept, o -> o.setName("发"))); |
|
203 |
// 测试 status 不匹配 |
|
204 |
deptMapper.insert(ObjectUtils.cloneIgnoreId(dept, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
205 |
// 准备参数 |
|
206 |
DeptListReqVO reqVO = new DeptListReqVO(); |
|
207 |
reqVO.setName("开"); |
|
208 |
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
209 |
|
|
210 |
// 调用 |
|
211 |
List<DeptDO> sysDeptDOS = deptService.getDeptList(reqVO); |
|
212 |
// 断言 |
|
213 |
assertEquals(1, sysDeptDOS.size()); |
|
214 |
assertPojoEquals(dept, sysDeptDOS.get(0)); |
|
215 |
} |
|
216 |
|
|
217 |
@Test |
|
218 |
public void testGetChildDeptList() { |
|
219 |
// mock 数据(1 级别子节点) |
|
220 |
DeptDO dept1 = randomPojo(DeptDO.class, o -> o.setName("1")); |
|
221 |
deptMapper.insert(dept1); |
|
222 |
DeptDO dept2 = randomPojo(DeptDO.class, o -> o.setName("2")); |
|
223 |
deptMapper.insert(dept2); |
|
224 |
// mock 数据(2 级子节点) |
|
225 |
DeptDO dept1a = randomPojo(DeptDO.class, o -> o.setName("1-a").setParentId(dept1.getId())); |
|
226 |
deptMapper.insert(dept1a); |
|
227 |
DeptDO dept2a = randomPojo(DeptDO.class, o -> o.setName("2-a").setParentId(dept2.getId())); |
|
228 |
deptMapper.insert(dept2a); |
|
229 |
// 准备参数 |
|
230 |
Long id = dept1.getParentId(); |
|
231 |
|
|
232 |
// 调用 |
|
233 |
List<DeptDO> result = deptService.getChildDeptList(id); |
|
234 |
// 断言 |
|
235 |
assertEquals(result.size(), 2); |
|
236 |
assertPojoEquals(dept1, result.get(0)); |
|
237 |
assertPojoEquals(dept1a, result.get(1)); |
|
238 |
} |
|
239 |
|
|
240 |
@Test |
|
241 |
public void testGetChildDeptListFromCache() { |
|
242 |
// mock 数据(1 级别子节点) |
|
243 |
DeptDO dept1 = randomPojo(DeptDO.class, o -> o.setName("1")); |
|
244 |
deptMapper.insert(dept1); |
|
245 |
DeptDO dept2 = randomPojo(DeptDO.class, o -> o.setName("2")); |
|
246 |
deptMapper.insert(dept2); |
|
247 |
// mock 数据(2 级子节点) |
|
248 |
DeptDO dept1a = randomPojo(DeptDO.class, o -> o.setName("1-a").setParentId(dept1.getId())); |
|
249 |
deptMapper.insert(dept1a); |
|
250 |
DeptDO dept2a = randomPojo(DeptDO.class, o -> o.setName("2-a").setParentId(dept2.getId())); |
|
251 |
deptMapper.insert(dept2a); |
|
252 |
// 准备参数 |
|
253 |
Long id = dept1.getParentId(); |
|
254 |
|
|
255 |
// 调用 |
|
256 |
Set<Long> result = deptService.getChildDeptIdListFromCache(id); |
|
257 |
// 断言 |
|
258 |
assertEquals(result.size(), 2); |
|
259 |
assertTrue(result.contains(dept1.getId())); |
|
260 |
assertTrue(result.contains(dept1a.getId())); |
|
261 |
} |
|
262 |
|
|
263 |
@Test |
|
264 |
public void testValidateDeptList_success() { |
|
265 |
// mock 数据 |
|
266 |
DeptDO deptDO = randomPojo(DeptDO.class).setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
267 |
deptMapper.insert(deptDO); |
|
268 |
// 准备参数 |
|
269 |
List<Long> ids = singletonList(deptDO.getId()); |
|
270 |
|
|
271 |
// 调用,无需断言 |
|
272 |
deptService.validateDeptList(ids); |
|
273 |
} |
|
274 |
|
|
275 |
@Test |
|
276 |
public void testValidateDeptList_notFound() { |
|
277 |
// 准备参数 |
|
278 |
List<Long> ids = singletonList(randomLongId()); |
|
279 |
|
|
280 |
// 调用, 并断言异常 |
|
281 |
assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_FOUND); |
|
282 |
} |
|
283 |
|
|
284 |
@Test |
|
285 |
public void testValidateDeptList_notEnable() { |
|
286 |
// mock 数据 |
|
287 |
DeptDO deptDO = randomPojo(DeptDO.class).setStatus(CommonStatusEnum.DISABLE.getStatus()); |
|
288 |
deptMapper.insert(deptDO); |
|
289 |
// 准备参数 |
|
290 |
List<Long> ids = singletonList(deptDO.getId()); |
|
291 |
|
|
292 |
// 调用, 并断言异常 |
|
293 |
assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_ENABLE, deptDO.getName()); |
|
294 |
} |
|
295 |
|
|
296 |
} |