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