Commit 1011de327868cf81e8c40bbee7b591dd559cb2f0

Authored by YunaiV
1 parent ba78d996

完善 DeptServiceImpl 单元测试

Showing 11 changed files with 186 additions and 126 deletions
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImpl.java
... ... @@ -216,7 +216,7 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService {
216 216 roleApi.validRoles(options);
217 217 } else if (ObjectUtils.equalsAny(type, BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(),
218 218 BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) {
219   - deptApi.validDepts(options);
  219 + deptApi.validateDeptList(options);
220 220 } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.POST.getType())) {
221 221 postApi.validPosts(options);
222 222 } else if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.USER.getType())) {
... ... @@ -293,7 +293,7 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService {
293 293 }
294 294  
295 295 private Set<Long> calculateTaskCandidateUsersByDeptLeader(BpmTaskAssignRuleDO rule) {
296   - List<DeptRespDTO> depts = deptApi.getDepts(rule.getOptions());
  296 + List<DeptRespDTO> depts = deptApi.getDeptList(rule.getOptions());
297 297 return convertSet(depts, DeptRespDTO::getLeaderUserId);
298 298 }
299 299  
... ...
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/dept/DeptApi.java
... ... @@ -29,7 +29,7 @@ public interface DeptApi {
29 29 * @param ids 部门编号数组
30 30 * @return 部门信息数组
31 31 */
32   - List<DeptRespDTO> getDepts(Collection<Long> ids);
  32 + List<DeptRespDTO> getDeptList(Collection<Long> ids);
33 33  
34 34 /**
35 35 * 校验部门们是否有效。如下情况,视为无效:
... ... @@ -38,7 +38,7 @@ public interface DeptApi {
38 38 *
39 39 * @param ids 角色编号数组
40 40 */
41   - void validDepts(Collection<Long> ids);
  41 + void validateDeptList(Collection<Long> ids);
42 42  
43 43 /**
44 44 * 获得指定编号的部门 Map
... ... @@ -47,7 +47,7 @@ public interface DeptApi {
47 47 * @return 部门 Map
48 48 */
49 49 default Map<Long, DeptRespDTO> getDeptMap(Set<Long> ids) {
50   - List<DeptRespDTO> list = getDepts(ids);
  50 + List<DeptRespDTO> list = getDeptList(ids);
51 51 return CollectionUtils.convertMap(list, DeptRespDTO::getId);
52 52 }
53 53  
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/dept/DeptApiImpl.java
... ... @@ -28,14 +28,14 @@ public class DeptApiImpl implements DeptApi {
28 28 }
29 29  
30 30 @Override
31   - public List<DeptRespDTO> getDepts(Collection<Long> ids) {
32   - List<DeptDO> depts = deptService.getDepts(ids);
  31 + public List<DeptRespDTO> getDeptList(Collection<Long> ids) {
  32 + List<DeptDO> depts = deptService.getDeptList(ids);
33 33 return DeptConvert.INSTANCE.convertList03(depts);
34 34 }
35 35  
36 36 @Override
37   - public void validDepts(Collection<Long> ids) {
38   - deptService.validDepts(ids);
  37 + public void validateDeptList(Collection<Long> ids) {
  38 + deptService.validateDeptList(ids);
39 39 }
40 40  
41 41 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/dept/DeptController.java
... ... @@ -58,7 +58,7 @@ public class DeptController {
58 58 @ApiOperation("获取部门列表")
59 59 @PreAuthorize("@ss.hasPermission('system:dept:query')")
60 60 public CommonResult<List<DeptRespVO>> listDepts(DeptListReqVO reqVO) {
61   - List<DeptDO> list = deptService.getSimpleDepts(reqVO);
  61 + List<DeptDO> list = deptService.getDeptList(reqVO);
62 62 list.sort(Comparator.comparing(DeptDO::getSort));
63 63 return success(DeptConvert.INSTANCE.convertList(list));
64 64 }
... ... @@ -69,7 +69,7 @@ public class DeptController {
69 69 // 获得部门列表,只要开启状态的
70 70 DeptListReqVO reqVO = new DeptListReqVO();
71 71 reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
72   - List<DeptDO> list = deptService.getSimpleDepts(reqVO);
  72 + List<DeptDO> list = deptService.getDeptList(reqVO);
73 73 // 排序后,返回给前端
74 74 list.sort(Comparator.comparing(DeptDO::getSort));
75 75 return success(DeptConvert.INSTANCE.convertList02(list));
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptService.java
... ... @@ -52,7 +52,7 @@ public interface DeptService {
52 52 * @param reqVO 筛选条件请求 VO
53 53 * @return 部门列表
54 54 */
55   - List<DeptDO> getSimpleDepts(DeptListReqVO reqVO);
  55 + List<DeptDO> getDeptList(DeptListReqVO reqVO);
56 56  
57 57 /**
58 58 * 获得所有子部门,从缓存中
... ... @@ -61,7 +61,7 @@ public interface DeptService {
61 61 * @param recursive 是否递归获取所有
62 62 * @return 子部门列表
63 63 */
64   - List<DeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive);
  64 + List<DeptDO> getDeptListByParentIdFromCache(Long parentId, boolean recursive);
65 65  
66 66 /**
67 67 * 获得部门信息数组
... ... @@ -69,7 +69,21 @@ public interface DeptService {
69 69 * @param ids 部门编号数组
70 70 * @return 部门信息数组
71 71 */
72   - List<DeptDO> getDepts(Collection<Long> ids);
  72 + List<DeptDO> getDeptList(Collection<Long> ids);
  73 +
  74 + /**
  75 + * 获得指定编号的部门 Map
  76 + *
  77 + * @param ids 部门编号数组
  78 + * @return 部门 Map
  79 + */
  80 + default Map<Long, DeptDO> getDeptMap(Collection<Long> ids) {
  81 + if (CollUtil.isEmpty(ids)) {
  82 + return Collections.emptyMap();
  83 + }
  84 + List<DeptDO> list = getDeptList(ids);
  85 + return CollectionUtils.convertMap(list, DeptDO::getId);
  86 + }
73 87  
74 88 /**
75 89 * 获得部门信息
... ... @@ -86,27 +100,6 @@ public interface DeptService {
86 100 *
87 101 * @param ids 角色编号数组
88 102 */
89   - void validDepts(Collection<Long> ids);
  103 + void validateDeptList(Collection<Long> ids);
90 104  
91   - /**
92   - * 获得指定编号的部门列表
93   - *
94   - * @param ids 部门编号数组
95   - * @return 部门列表
96   - */
97   - List<DeptDO> getSimpleDepts(Collection<Long> ids);
98   -
99   - /**
100   - * 获得指定编号的部门 Map
101   - *
102   - * @param ids 部门编号数组
103   - * @return 部门 Map
104   - */
105   - default Map<Long, DeptDO> getDeptMap(Collection<Long> ids) {
106   - if (CollUtil.isEmpty(ids)) {
107   - return Collections.emptyMap();
108   - }
109   - List<DeptDO> list = getSimpleDepts(ids);
110   - return CollectionUtils.convertMap(list, DeptDO::getId);
111   - }
112 105 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImpl.java
... ... @@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.system.service.dept;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
5   -import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
6   -import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
7 5 import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
8 6 import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
9 7 import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptCreateReqVO;
... ... @@ -19,13 +17,11 @@ import com.google.common.collect.ImmutableMultimap;
19 17 import com.google.common.collect.Multimap;
20 18 import lombok.Getter;
21 19 import lombok.extern.slf4j.Slf4j;
22   -import org.springframework.scheduling.annotation.Scheduled;
23 20 import org.springframework.stereotype.Service;
24 21 import org.springframework.validation.annotation.Validated;
25 22  
26 23 import javax.annotation.PostConstruct;
27 24 import javax.annotation.Resource;
28   -import java.time.LocalDateTime;
29 25 import java.util.*;
30 26  
31 27 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
... ... @@ -95,7 +91,7 @@ public class DeptServiceImpl implements DeptService {
95 91 if (reqVO.getParentId() == null) {
96 92 reqVO.setParentId(DeptIdEnum.ROOT.getId());
97 93 }
98   - checkCreateOrUpdate(null, reqVO.getParentId(), reqVO.getName());
  94 + validateForCreateOrUpdate(null, reqVO.getParentId(), reqVO.getName());
99 95 // 插入部门
100 96 DeptDO dept = DeptConvert.INSTANCE.convert(reqVO);
101 97 deptMapper.insert(dept);
... ... @@ -110,7 +106,7 @@ public class DeptServiceImpl implements DeptService {
110 106 if (reqVO.getParentId() == null) {
111 107 reqVO.setParentId(DeptIdEnum.ROOT.getId());
112 108 }
113   - checkCreateOrUpdate(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
  109 + validateForCreateOrUpdate(reqVO.getId(), reqVO.getParentId(), reqVO.getName());
114 110 // 更新部门
115 111 DeptDO updateObj = DeptConvert.INSTANCE.convert(reqVO);
116 112 deptMapper.updateById(updateObj);
... ... @@ -121,10 +117,10 @@ public class DeptServiceImpl implements DeptService {
121 117 @Override
122 118 public void deleteDept(Long id) {
123 119 // 校验是否存在
124   - checkDeptExists(id);
  120 + validateDeptExists(id);
125 121 // 校验是否有子部门
126 122 if (deptMapper.selectCountByParentId(id) > 0) {
127   - throw ServiceExceptionUtil.exception(DEPT_EXITS_CHILDREN);
  123 + throw exception(DEPT_EXITS_CHILDREN);
128 124 }
129 125 // 删除部门
130 126 deptMapper.deleteById(id);
... ... @@ -133,16 +129,16 @@ public class DeptServiceImpl implements DeptService {
133 129 }
134 130  
135 131 @Override
136   - public List<DeptDO> getSimpleDepts(DeptListReqVO reqVO) {
  132 + public List<DeptDO> getDeptList(DeptListReqVO reqVO) {
137 133 return deptMapper.selectList(reqVO);
138 134 }
139 135  
140 136 @Override
141   - public List<DeptDO> getDeptsByParentIdFromCache(Long parentId, boolean recursive) {
  137 + public List<DeptDO> getDeptListByParentIdFromCache(Long parentId, boolean recursive) {
142 138 if (parentId == null) {
143 139 return Collections.emptyList();
144 140 }
145   - List<DeptDO> result = new ArrayList<>(); // TODO 芋艿:待优化,新增缓存,避免每次遍历的计算
  141 + List<DeptDO> result = new ArrayList<>();
146 142 // 递归,简单粗暴
147 143 this.getDeptsByParentIdFromCache(result, parentId,
148 144 recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次
... ... @@ -182,65 +178,65 @@ public class DeptServiceImpl implements DeptService {
182 178 recursiveCount - 1, parentDeptMap));
183 179 }
184 180  
185   - private void checkCreateOrUpdate(Long id, Long parentId, String name) {
  181 + private void validateForCreateOrUpdate(Long id, Long parentId, String name) {
186 182 // 校验自己存在
187   - checkDeptExists(id);
  183 + validateDeptExists(id);
188 184 // 校验父部门的有效性
189   - checkParentDeptEnable(id, parentId);
  185 + validateParentDeptEnable(id, parentId);
190 186 // 校验部门名的唯一性
191   - checkDeptNameUnique(id, parentId, name);
  187 + validateDeptNameUnique(id, parentId, name);
192 188 }
193 189  
194   - private void checkParentDeptEnable(Long id, Long parentId) {
  190 + private void validateParentDeptEnable(Long id, Long parentId) {
195 191 if (parentId == null || DeptIdEnum.ROOT.getId().equals(parentId)) {
196 192 return;
197 193 }
198 194 // 不能设置自己为父部门
199 195 if (parentId.equals(id)) {
200   - throw ServiceExceptionUtil.exception(DEPT_PARENT_ERROR);
  196 + throw exception(DEPT_PARENT_ERROR);
201 197 }
202 198 // 父岗位不存在
203 199 DeptDO dept = deptMapper.selectById(parentId);
204 200 if (dept == null) {
205   - throw ServiceExceptionUtil.exception(DEPT_PARENT_NOT_EXITS);
  201 + throw exception(DEPT_PARENT_NOT_EXITS);
206 202 }
207 203 // 父部门被禁用
208 204 if (!CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus())) {
209   - throw ServiceExceptionUtil.exception(DEPT_NOT_ENABLE);
  205 + throw exception(DEPT_NOT_ENABLE);
210 206 }
211 207 // 父部门不能是原来的子部门
212   - List<DeptDO> children = this.getDeptsByParentIdFromCache(id, true);
  208 + List<DeptDO> children = this.getDeptListByParentIdFromCache(id, true);
213 209 if (children.stream().anyMatch(dept1 -> dept1.getId().equals(parentId))) {
214   - throw ServiceExceptionUtil.exception(DEPT_PARENT_IS_CHILD);
  210 + throw exception(DEPT_PARENT_IS_CHILD);
215 211 }
216 212 }
217 213  
218   - private void checkDeptExists(Long id) {
  214 + private void validateDeptExists(Long id) {
219 215 if (id == null) {
220 216 return;
221 217 }
222 218 DeptDO dept = deptMapper.selectById(id);
223 219 if (dept == null) {
224   - throw ServiceExceptionUtil.exception(DEPT_NOT_FOUND);
  220 + throw exception(DEPT_NOT_FOUND);
225 221 }
226 222 }
227 223  
228   - private void checkDeptNameUnique(Long id, Long parentId, String name) {
  224 + private void validateDeptNameUnique(Long id, Long parentId, String name) {
229 225 DeptDO menu = deptMapper.selectByParentIdAndName(parentId, name);
230 226 if (menu == null) {
231 227 return;
232 228 }
233 229 // 如果 id 为空,说明不用比较是否为相同 id 的岗位
234 230 if (id == null) {
235   - throw ServiceExceptionUtil.exception(DEPT_NAME_DUPLICATE);
  231 + throw exception(DEPT_NAME_DUPLICATE);
236 232 }
237 233 if (!menu.getId().equals(id)) {
238   - throw ServiceExceptionUtil.exception(DEPT_NAME_DUPLICATE);
  234 + throw exception(DEPT_NAME_DUPLICATE);
239 235 }
240 236 }
241 237  
242 238 @Override
243   - public List<DeptDO> getDepts(Collection<Long> ids) {
  239 + public List<DeptDO> getDeptList(Collection<Long> ids) {
244 240 return deptMapper.selectBatchIds(ids);
245 241 }
246 242  
... ... @@ -250,13 +246,12 @@ public class DeptServiceImpl implements DeptService {
250 246 }
251 247  
252 248 @Override
253   - public void validDepts(Collection<Long> ids) {
  249 + public void validateDeptList(Collection<Long> ids) {
254 250 if (CollUtil.isEmpty(ids)) {
255 251 return;
256 252 }
257 253 // 获得科室信息
258   - List<DeptDO> depts = deptMapper.selectBatchIds(ids);
259   - Map<Long, DeptDO> deptMap = CollectionUtils.convertMap(depts, DeptDO::getId);
  254 + Map<Long, DeptDO> deptMap = getDeptMap(ids);
260 255 // 校验
261 256 ids.forEach(id -> {
262 257 DeptDO dept = deptMap.get(id);
... ... @@ -269,9 +264,4 @@ public class DeptServiceImpl implements DeptService {
269 264 });
270 265 }
271 266  
272   - @Override
273   - public List<DeptDO> getSimpleDepts(Collection<Long> ids) {
274   - return deptMapper.selectBatchIds(ids);
275   - }
276   -
277 267 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java
... ... @@ -418,7 +418,7 @@ public class PermissionServiceImpl implements PermissionService {
418 418 }
419 419 // 情况四,DEPT_DEPT_AND_CHILD
420 420 if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_AND_CHILD.getScope())) {
421   - List<DeptDO> depts = deptService.getDeptsByParentIdFromCache(userDeptIdCache.get(), true);
  421 + List<DeptDO> depts = deptService.getDeptListByParentIdFromCache(userDeptIdCache.get(), true);
422 422 CollUtil.addAll(result.getDeptIds(), CollectionUtils.convertList(depts, DeptDO::getId));
423 423 // 添加本身部门编号
424 424 CollUtil.addAll(result.getDeptIds(), userDeptIdCache.get());
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
... ... @@ -299,7 +299,7 @@ public class AdminUserServiceImpl implements AdminUserService {
299 299 if (deptId == null) {
300 300 return Collections.emptySet();
301 301 }
302   - Set<Long> deptIds = convertSet(deptService.getDeptsByParentIdFromCache(
  302 + Set<Long> deptIds = convertSet(deptService.getDeptListByParentIdFromCache(
303 303 deptId, true), DeptDO::getId);
304 304 deptIds.add(deptId); // 包括自身
305 305 return deptIds;
... ... @@ -317,7 +317,7 @@ public class AdminUserServiceImpl implements AdminUserService {
317 317 // 校验邮箱唯一
318 318 checkEmailUnique(id, email);
319 319 // 校验部门处于开启状态
320   - deptService.validDepts(CollectionUtils.singleton(deptId));
  320 + deptService.validateDeptList(CollectionUtils.singleton(deptId));
321 321 // 校验岗位处于开启状态
322 322 postService.validPosts(postIds);
323 323 }
... ...
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/dept/DeptServiceImplTest.java
... ... @@ -19,6 +19,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
19 19 import org.springframework.context.annotation.Import;
20 20  
21 21 import javax.annotation.Resource;
  22 +import java.util.Arrays;
22 23 import java.util.List;
23 24 import java.util.Map;
24 25 import java.util.function.Consumer;
... ... @@ -28,8 +29,8 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEq
28 29 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
29 30 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
30 31 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
  32 +import static java.util.Collections.singletonList;
31 33 import static org.junit.jupiter.api.Assertions.*;
32   -import static org.mockito.Mockito.times;
33 34 import static org.mockito.Mockito.verify;
34 35  
35 36 /**
... ... @@ -38,7 +39,7 @@ import static org.mockito.Mockito.verify;
38 39 * @author niudehua
39 40 */
40 41 @Import(DeptServiceImpl.class)
41   -public class DeptServiceTest extends BaseDbUnitTest {
  42 +public class DeptServiceImplTest extends BaseDbUnitTest {
42 43  
43 44 @Resource
44 45 private DeptServiceImpl deptService;
... ... @@ -76,7 +77,7 @@ public class DeptServiceTest extends BaseDbUnitTest {
76 77 }
77 78  
78 79 @Test
79   - void testListDepts() {
  80 + public void testListDepts() {
80 81 // mock 数据
81 82 DeptDO dept = randomPojo(DeptDO.class, o -> { // 等会查询到
82 83 o.setName("开发部");
... ... @@ -91,21 +92,22 @@ public class DeptServiceTest extends BaseDbUnitTest {
91 92 DeptListReqVO reqVO = new DeptListReqVO();
92 93 reqVO.setName("开");
93 94 reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
  95 +
94 96 // 调用
95   - List<DeptDO> sysDeptDOS = deptService.getSimpleDepts(reqVO);
  97 + List<DeptDO> sysDeptDOS = deptService.getDeptList(reqVO);
96 98 // 断言
97 99 assertEquals(1, sysDeptDOS.size());
98 100 assertPojoEquals(dept, sysDeptDOS.get(0));
99 101 }
100 102  
101 103 @Test
102   - void testCreateDept_success() {
  104 + public void testCreateDept_success() {
103 105 // 准备参数
104   - DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
105   - o -> {
106   - o.setParentId(DeptIdEnum.ROOT.getId());
107   - o.setStatus(randomCommonStatus());
108   - });
  106 + DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> {
  107 + o.setParentId(DeptIdEnum.ROOT.getId());
  108 + o.setStatus(randomCommonStatus());
  109 + });
  110 +
109 111 // 调用
110 112 Long deptId = deptService.createDept(reqVO);
111 113 // 断言
... ... @@ -114,11 +116,11 @@ public class DeptServiceTest extends BaseDbUnitTest {
114 116 DeptDO deptDO = deptMapper.selectById(deptId);
115 117 assertPojoEquals(reqVO, deptDO);
116 118 // 校验调用
117   - verify(deptProducer, times(1)).sendDeptRefreshMessage();
  119 + verify(deptProducer).sendDeptRefreshMessage();
118 120 }
119 121  
120 122 @Test
121   - void testUpdateDept_success() {
  123 + public void testUpdateDept_success() {
122 124 // mock 数据
123 125 DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
124 126 deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
... ... @@ -129,28 +131,34 @@ public class DeptServiceTest extends BaseDbUnitTest {
129 131 o.setId(dbDeptDO.getId());
130 132 o.setStatus(randomCommonStatus());
131 133 });
  134 +
132 135 // 调用
133 136 deptService.updateDept(reqVO);
134 137 // 校验是否更新正确
135 138 DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的
136 139 assertPojoEquals(reqVO, deptDO);
  140 + // 校验调用
  141 + verify(deptProducer).sendDeptRefreshMessage();
137 142 }
138 143  
139 144 @Test
140   - void testDeleteDept_success() {
  145 + public void testDeleteDept_success() {
141 146 // mock 数据
142 147 DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
143 148 deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
144 149 // 准备参数
145 150 Long id = dbDeptDO.getId();
  151 +
146 152 // 调用
147 153 deptService.deleteDept(id);
148 154 // 校验数据不存在了
149 155 assertNull(deptMapper.selectById(id));
  156 + // 校验调用
  157 + verify(deptProducer).sendDeptRefreshMessage();
150 158 }
151 159  
152 160 @Test
153   - void testCheckDept_nameDuplicateForUpdate() {
  161 + public void testValidateDept_nameDuplicateForUpdate() {
154 162 // mock 数据
155 163 DeptDO deptDO = randomDeptDO();
156 164 // 设置根节点部门
... ... @@ -162,37 +170,40 @@ public class DeptServiceTest extends BaseDbUnitTest {
162 170 nameDeptDO.setParentId(DeptIdEnum.ROOT.getId());
163 171 deptMapper.insert(nameDeptDO);
164 172 // 准备参数
165   - DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
166   - o -> {
167   - // 设置根节点部门
168   - o.setParentId(DeptIdEnum.ROOT.getId());
169   - // 设置更新的 ID
170   - o.setId(deptDO.getId());
171   - // 模拟 name 重复
172   - o.setName(nameDeptDO.getName());
173   - });
  173 + DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
  174 + // 设置根节点部门
  175 + o.setParentId(DeptIdEnum.ROOT.getId());
  176 + // 设置更新的 ID
  177 + o.setId(deptDO.getId());
  178 + // 模拟 name 重复
  179 + o.setName(nameDeptDO.getName());
  180 + });
  181 +
174 182 // 调用, 并断言异常
175 183 assertServiceException(() -> deptService.updateDept(reqVO), DEPT_NAME_DUPLICATE);
176 184 }
177 185  
178 186 @Test
179   - void testCheckDept_parentNotExitsForCreate() {
  187 + public void testValidateDept_parentNotExitsForCreate() {
  188 + // 准备参数
180 189 DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
181 190 o -> o.setStatus(randomCommonStatus()));
  191 +
182 192 // 调用,并断言异常
183 193 assertServiceException(() -> deptService.createDept(reqVO), DEPT_PARENT_NOT_EXITS);
184 194 }
185 195  
186 196 @Test
187   - void testCheckDept_notFoundForDelete() {
  197 + public void testValidateDept_notFoundForDelete() {
188 198 // 准备参数
189 199 Long id = randomLongId();
  200 +
190 201 // 调用, 并断言异常
191 202 assertServiceException(() -> deptService.deleteDept(id), DEPT_NOT_FOUND);
192 203 }
193 204  
194 205 @Test
195   - void testCheckDept_exitsChildrenForDelete() {
  206 + public void testValidateDept_exitsChildrenForDelete() {
196 207 // mock 数据
197 208 DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
198 209 deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据
... ... @@ -208,39 +219,39 @@ public class DeptServiceTest extends BaseDbUnitTest {
208 219 }
209 220  
210 221 @Test
211   - void testCheckDept_parentErrorForUpdate() {
  222 + public void testValidateDept_parentErrorForUpdate() {
212 223 // mock 数据
213 224 DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
214 225 deptMapper.insert(dbDeptDO);
215 226 // 准备参数
216   - DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
217   - o -> {
218   - // 设置自己为父部门
219   - o.setParentId(dbDeptDO.getId());
220   - // 设置更新的 ID
221   - o.setId(dbDeptDO.getId());
222   - });
  227 + DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
  228 + // 设置自己为父部门
  229 + o.setParentId(dbDeptDO.getId());
  230 + // 设置更新的 ID
  231 + o.setId(dbDeptDO.getId());
  232 + });
  233 +
223 234 // 调用, 并断言异常
224 235 assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_ERROR);
225 236 }
226 237  
227 238 @Test
228   - void testCheckDept_notEnableForCreate() {
  239 + public void testValidateDept_notEnableForCreate() {
229 240 // mock 数据
230 241 DeptDO deptDO = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
231 242 deptMapper.insert(deptDO);
232 243 // 准备参数
233   - DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
234   - o -> {
235   - // 设置未启用的部门为副部门
236   - o.setParentId(deptDO.getId());
237   - });
  244 + DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> {
  245 + // 设置未启用的部门为父部门
  246 + o.setParentId(deptDO.getId());
  247 + });
  248 +
238 249 // 调用, 并断言异常
239 250 assertServiceException(() -> deptService.createDept(reqVO), DEPT_NOT_ENABLE);
240 251 }
241 252  
242 253 @Test
243   - void testCheckDept_parentIsChildForUpdate() {
  254 + public void testCheckDept_parentIsChildForUpdate() {
244 255 // mock 数据
245 256 DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
246 257 deptMapper.insert(parentDept);
... ... @@ -251,18 +262,84 @@ public class DeptServiceTest extends BaseDbUnitTest {
251 262 deptMapper.insert(childDept);
252 263 // 初始化本地缓存
253 264 deptService.initLocalCache();
  265 +
254 266 // 准备参数
255   - DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
256   - o -> {
257   - // 设置自己的子部门为父部门
258   - o.setParentId(childDept.getId());
259   - // 设置更新的 ID
260   - o.setId(parentDept.getId());
261   - });
  267 + DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
  268 + // 设置自己的子部门为父部门
  269 + o.setParentId(childDept.getId());
  270 + // 设置更新的 ID
  271 + o.setId(parentDept.getId());
  272 + });
  273 +
262 274 // 调用, 并断言异常
263 275 assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_IS_CHILD);
264 276 }
265 277  
  278 + @Test
  279 + public void testGetDeptList() {
  280 + // mock 数据
  281 + DeptDO deptDO01 = randomDeptDO();
  282 + deptMapper.insert(deptDO01);
  283 + DeptDO deptDO02 = randomDeptDO();
  284 + deptMapper.insert(deptDO02);
  285 + // 准备参数
  286 + List<Long> ids = Arrays.asList(deptDO01.getId(), deptDO02.getId());
  287 +
  288 + // 调用
  289 + List<DeptDO> deptDOList = deptService.getDeptList(ids);
  290 + // 断言
  291 + assertEquals(2, deptDOList.size());
  292 + assertEquals(deptDO01, deptDOList.get(0));
  293 + assertEquals(deptDO02, deptDOList.get(1));
  294 + }
  295 +
  296 + @Test
  297 + public void testGetDept() {
  298 + // mock 数据
  299 + DeptDO deptDO = randomDeptDO();
  300 + deptMapper.insert(deptDO);
  301 + // 准备参数
  302 + Long id = deptDO.getId();
  303 +
  304 + // 调用
  305 + DeptDO dbDept = deptService.getDept(id);
  306 + // 断言
  307 + assertEquals(deptDO, dbDept);
  308 + }
  309 +
  310 + @Test
  311 + public void testValidateDeptList_success() {
  312 + // mock 数据
  313 + DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.ENABLE.getStatus());
  314 + deptMapper.insert(deptDO);
  315 + // 准备参数
  316 + List<Long> ids = singletonList(deptDO.getId());
  317 +
  318 + // 调用,无需断言
  319 + deptService.validateDeptList(ids);
  320 + }
  321 +
  322 + @Test
  323 + public void testValidateDeptList_notFound() {
  324 + // 准备参数
  325 + List<Long> ids = singletonList(randomLongId());
  326 +
  327 + // 调用, 并断言异常
  328 + assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_FOUND);
  329 + }
  330 +
  331 + @Test
  332 + public void testValidateDeptList_notEnable() {
  333 + // mock 数据
  334 + DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.DISABLE.getStatus());
  335 + deptMapper.insert(deptDO);
  336 + // 准备参数
  337 + List<Long> ids = singletonList(deptDO.getId());
  338 +
  339 + // 调用, 并断言异常
  340 + assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_ENABLE);
  341 + }
  342 +
266 343 @SafeVarargs
267 344 private static DeptDO randomDeptDO(Consumer<DeptDO>... consumers) {
268 345 Consumer<DeptDO> consumer = (o) -> {
... ...
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java
... ... @@ -509,7 +509,7 @@ public class PermissionServiceTest extends BaseDbUnitTest {
509 509 when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用
510 510 // mock 方法(部门)
511 511 DeptDO deptDO = randomPojo(DeptDO.class);
512   - when(deptService.getDeptsByParentIdFromCache(eq(3L), eq(true)))
  512 + when(deptService.getDeptListByParentIdFromCache(eq(3L), eq(true)))
513 513 .thenReturn(singletonList(deptDO));
514 514  
515 515 // 调用
... ...
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java
... ... @@ -299,7 +299,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
299 299 reqVO.setDeptId(1L); // 其中,1L 是 2L 的父部门
300 300 // mock 方法
301 301 List<DeptDO> deptList = newArrayList(randomPojo(DeptDO.class, o -> o.setId(2L)));
302   - when(deptService.getDeptsByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList);
  302 + when(deptService.getDeptListByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList);
303 303  
304 304 // 调用
305 305 PageResult<AdminUserDO> pageResult = userService.getUserPage(reqVO);
... ... @@ -322,7 +322,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
322 322 reqVO.setDeptId(1L); // 其中,1L 是 2L 的父部门
323 323 // mock 方法
324 324 List<DeptDO> deptList = newArrayList(randomPojo(DeptDO.class, o -> o.setId(2L)));
325   - when(deptService.getDeptsByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList);
  325 + when(deptService.getDeptListByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList);
326 326  
327 327 // 调用
328 328 List<AdminUserDO> list = userService.getUsers(reqVO);
... ... @@ -366,7 +366,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
366 366 UserImportExcelVO importUser = randomPojo(UserImportExcelVO.class, o -> {
367 367 });
368 368 // mock 方法,模拟失败
369   - doThrow(new ServiceException(DEPT_NOT_FOUND)).when(deptService).validDepts(any());
  369 + doThrow(new ServiceException(DEPT_NOT_FOUND)).when(deptService).validateDeptList(any());
370 370  
371 371 // 调用
372 372 UserImportRespVO respVO = userService.importUsers(newArrayList(importUser), true);
... ...