Commit 96e8fa42169b9ae0226ade63a15a612be19dcb34
1 parent
3aeebef0
完善 RoleServiceImpl 单元测试
Showing
13 changed files
with
245 additions
and
147 deletions
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/definition/BpmTaskAssignRuleServiceImpl.java
| ... | ... | @@ -213,7 +213,7 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService { |
| 213 | 213 | |
| 214 | 214 | private void validTaskAssignRuleOptions(Integer type, Set<Long> options) { |
| 215 | 215 | if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.ROLE.getType())) { |
| 216 | - roleApi.validRoles(options); | |
| 216 | + roleApi.validRoleList(options); | |
| 217 | 217 | } else if (ObjectUtils.equalsAny(type, BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(), |
| 218 | 218 | BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) { |
| 219 | 219 | deptApi.validateDeptList(options); | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApi.java
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/permission/RoleApiImpl.java
| ... | ... | @@ -18,7 +18,7 @@ public class RoleApiImpl implements RoleApi { |
| 18 | 18 | private RoleService roleService; |
| 19 | 19 | |
| 20 | 20 | @Override |
| 21 | - public void validRoles(Collection<Long> ids) { | |
| 22 | - roleService.validRoles(ids); | |
| 21 | + public void validRoleList(Collection<Long> ids) { | |
| 22 | + roleService.validateRoleList(ids); | |
| 23 | 23 | } |
| 24 | 24 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
| ... | ... | @@ -97,7 +97,7 @@ public class AuthController { |
| 97 | 97 | } |
| 98 | 98 | // 获得角色列表 |
| 99 | 99 | Set<Long> roleIds = permissionService.getUserRoleIdsFromCache(getLoginUserId(), singleton(CommonStatusEnum.ENABLE.getStatus())); |
| 100 | - List<RoleDO> roleList = roleService.getRolesFromCache(roleIds); | |
| 100 | + List<RoleDO> roleList = roleService.getRoleListFromCache(roleIds); | |
| 101 | 101 | // 获得菜单列表 |
| 102 | 102 | List<MenuDO> menuList = permissionService.getRoleMenuListFromCache(roleIds, |
| 103 | 103 | SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType(), MenuTypeEnum.BUTTON.getType()), | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/RoleController.java
| ... | ... | @@ -20,12 +20,12 @@ import javax.annotation.Resource; |
| 20 | 20 | import javax.servlet.http.HttpServletResponse; |
| 21 | 21 | import javax.validation.Valid; |
| 22 | 22 | import java.io.IOException; |
| 23 | -import java.util.Collections; | |
| 24 | 23 | import java.util.Comparator; |
| 25 | 24 | import java.util.List; |
| 26 | 25 | |
| 27 | 26 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 28 | 27 | import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; |
| 28 | +import static java.util.Collections.singleton; | |
| 29 | 29 | |
| 30 | 30 | @Api(tags = "管理后台 - 角色") |
| 31 | 31 | @RestController |
| ... | ... | @@ -85,9 +85,9 @@ public class RoleController { |
| 85 | 85 | |
| 86 | 86 | @GetMapping("/list-all-simple") |
| 87 | 87 | @ApiOperation(value = "获取角色精简信息列表", notes = "只包含被开启的角色,主要用于前端的下拉选项") |
| 88 | - public CommonResult<List<RoleSimpleRespVO>> getSimpleRoles() { | |
| 88 | + public CommonResult<List<RoleSimpleRespVO>> getSimpleRoleList() { | |
| 89 | 89 | // 获得角色列表,只要开启状态的 |
| 90 | - List<RoleDO> list = roleService.getRoles(Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); | |
| 90 | + List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus())); | |
| 91 | 91 | // 排序后,返回给前端 |
| 92 | 92 | list.sort(Comparator.comparing(RoleDO::getSort)); |
| 93 | 93 | return success(RoleConvert.INSTANCE.convertList02(list)); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/UserProfileController.java
| ... | ... | @@ -63,7 +63,7 @@ public class UserProfileController { |
| 63 | 63 | AdminUserDO user = userService.getUser(getLoginUserId()); |
| 64 | 64 | UserProfileRespVO resp = UserConvert.INSTANCE.convert03(user); |
| 65 | 65 | // 获得用户角色 |
| 66 | - List<RoleDO> userRoles = roleService.getRolesFromCache(permissionService.getUserRoleIdListByUserId(user.getId())); | |
| 66 | + List<RoleDO> userRoles = roleService.getRoleListFromCache(permissionService.getUserRoleIdListByUserId(user.getId())); | |
| 67 | 67 | resp.setRoles(UserConvert.INSTANCE.convertList(userRoles)); |
| 68 | 68 | // 获得部门信息 |
| 69 | 69 | if (user.getDeptId() != null) { | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java
| ... | ... | @@ -158,7 +158,7 @@ public class PermissionServiceImpl implements PermissionService { |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // 判断角色是否包含超级管理员。如果是超级管理员,获取到全部 |
| 161 | - List<RoleDO> roleList = roleService.getRolesFromCache(roleIds); | |
| 161 | + List<RoleDO> roleList = roleService.getRoleListFromCache(roleIds); | |
| 162 | 162 | if (roleService.hasAnySuperAdmin(roleList)) { |
| 163 | 163 | return menuService.getMenuListFromCache(menuTypes, menusStatuses); |
| 164 | 164 | } |
| ... | ... | @@ -371,7 +371,7 @@ public class PermissionServiceImpl implements PermissionService { |
| 371 | 371 | if (roleService.hasAnySuperAdmin(roleIds)) { |
| 372 | 372 | return true; |
| 373 | 373 | } |
| 374 | - Set<String> userRoles = convertSet(roleService.getRolesFromCache(roleIds), | |
| 374 | + Set<String> userRoles = convertSet(roleService.getRoleListFromCache(roleIds), | |
| 375 | 375 | RoleDO::getCode); |
| 376 | 376 | return CollUtil.containsAny(userRoles, Sets.newHashSet(roles)); |
| 377 | 377 | } |
| ... | ... | @@ -388,7 +388,7 @@ public class PermissionServiceImpl implements PermissionService { |
| 388 | 388 | result.setSelf(true); |
| 389 | 389 | return result; |
| 390 | 390 | } |
| 391 | - List<RoleDO> roles = roleService.getRolesFromCache(roleIds); | |
| 391 | + List<RoleDO> roles = roleService.getRoleListFromCache(roleIds); | |
| 392 | 392 | |
| 393 | 393 | // 获得用户的部门编号的缓存,通过 Guava 的 Suppliers 惰性求值,即有且仅有第一次发起 DB 的查询 |
| 394 | 394 | Supplier<Long> userDeptIdCache = Suppliers.memoize(() -> userService.getUser(userId).getDeptId()); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleService.java
| ... | ... | @@ -79,7 +79,7 @@ public interface RoleService { |
| 79 | 79 | * @param statuses 筛选的状态。允许空,空时不筛选 |
| 80 | 80 | * @return 角色列表 |
| 81 | 81 | */ |
| 82 | - List<RoleDO> getRoles(@Nullable Collection<Integer> statuses); | |
| 82 | + List<RoleDO> getRoleListByStatus(@Nullable Collection<Integer> statuses); | |
| 83 | 83 | |
| 84 | 84 | /** |
| 85 | 85 | * 获得角色数组,从缓存中 |
| ... | ... | @@ -87,7 +87,7 @@ public interface RoleService { |
| 87 | 87 | * @param ids 角色编号数组 |
| 88 | 88 | * @return 角色数组 |
| 89 | 89 | */ |
| 90 | - List<RoleDO> getRolesFromCache(Collection<Long> ids); | |
| 90 | + List<RoleDO> getRoleListFromCache(Collection<Long> ids); | |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | 93 | * 判断角色数组中,是否有超级管理员 |
| ... | ... | @@ -104,7 +104,7 @@ public interface RoleService { |
| 104 | 104 | * @return 是否有管理员 |
| 105 | 105 | */ |
| 106 | 106 | default boolean hasAnySuperAdmin(Set<Long> ids) { |
| 107 | - return hasAnySuperAdmin(getRolesFromCache(ids)); | |
| 107 | + return hasAnySuperAdmin(getRoleListFromCache(ids)); | |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
| ... | ... | @@ -138,6 +138,6 @@ public interface RoleService { |
| 138 | 138 | * |
| 139 | 139 | * @param ids 角色编号数组 |
| 140 | 140 | */ |
| 141 | - void validRoles(Collection<Long> ids); | |
| 141 | + void validateRoleList(Collection<Long> ids); | |
| 142 | 142 | |
| 143 | 143 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImpl.java
| ... | ... | @@ -5,7 +5,6 @@ import cn.hutool.core.collection.CollectionUtil; |
| 5 | 5 | import cn.hutool.core.util.ObjectUtil; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 7 | 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | -import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | |
| 9 | 8 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 10 | 9 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO; |
| 11 | 10 | import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleExportReqVO; |
| ... | ... | @@ -34,6 +33,7 @@ import java.util.*; |
| 34 | 33 | import java.util.stream.Collectors; |
| 35 | 34 | |
| 36 | 35 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 36 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | |
| 37 | 37 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 38 | 38 | |
| 39 | 39 | /** |
| ... | ... | @@ -76,7 +76,7 @@ public class RoleServiceImpl implements RoleService { |
| 76 | 76 | log.info("[initLocalCache][缓存角色,数量为:{}]", roleList.size()); |
| 77 | 77 | |
| 78 | 78 | // 第二步:构建缓存 |
| 79 | - roleCache = CollectionUtils.convertMap(roleList, RoleDO::getId); | |
| 79 | + roleCache = convertMap(roleList, RoleDO::getId); | |
| 80 | 80 | }); |
| 81 | 81 | } |
| 82 | 82 | |
| ... | ... | @@ -84,7 +84,7 @@ public class RoleServiceImpl implements RoleService { |
| 84 | 84 | @Transactional |
| 85 | 85 | public Long createRole(RoleCreateReqVO reqVO, Integer type) { |
| 86 | 86 | // 校验角色 |
| 87 | - checkDuplicateRole(reqVO.getName(), reqVO.getCode(), null); | |
| 87 | + validateRoleDuplicate(reqVO.getName(), reqVO.getCode(), null); | |
| 88 | 88 | // 插入到数据库 |
| 89 | 89 | RoleDO role = RoleConvert.INSTANCE.convert(reqVO); |
| 90 | 90 | role.setType(ObjectUtil.defaultIfNull(type, RoleTypeEnum.CUSTOM.getType())); |
| ... | ... | @@ -105,13 +105,13 @@ public class RoleServiceImpl implements RoleService { |
| 105 | 105 | @Override |
| 106 | 106 | public void updateRole(RoleUpdateReqVO reqVO) { |
| 107 | 107 | // 校验是否可以更新 |
| 108 | - checkUpdateRole(reqVO.getId()); | |
| 108 | + validateRoleForUpdate(reqVO.getId()); | |
| 109 | 109 | // 校验角色的唯一字段是否重复 |
| 110 | - checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); | |
| 110 | + validateRoleDuplicate(reqVO.getName(), reqVO.getCode(), reqVO.getId()); | |
| 111 | 111 | |
| 112 | 112 | // 更新到数据库 |
| 113 | - RoleDO updateObject = RoleConvert.INSTANCE.convert(reqVO); | |
| 114 | - roleMapper.updateById(updateObject); | |
| 113 | + RoleDO updateObj = RoleConvert.INSTANCE.convert(reqVO); | |
| 114 | + roleMapper.updateById(updateObj); | |
| 115 | 115 | // 发送刷新消息 |
| 116 | 116 | roleProducer.sendRoleRefreshMessage(); |
| 117 | 117 | } |
| ... | ... | @@ -119,12 +119,11 @@ public class RoleServiceImpl implements RoleService { |
| 119 | 119 | @Override |
| 120 | 120 | public void updateRoleStatus(Long id, Integer status) { |
| 121 | 121 | // 校验是否可以更新 |
| 122 | - checkUpdateRole(id); | |
| 122 | + validateRoleForUpdate(id); | |
| 123 | + | |
| 123 | 124 | // 更新状态 |
| 124 | - RoleDO updateObject = new RoleDO(); | |
| 125 | - updateObject.setId(id); | |
| 126 | - updateObject.setStatus(status); | |
| 127 | - roleMapper.updateById(updateObject); | |
| 125 | + RoleDO updateObj = new RoleDO().setId(id).setStatus(status); | |
| 126 | + roleMapper.updateById(updateObj); | |
| 128 | 127 | // 发送刷新消息 |
| 129 | 128 | roleProducer.sendRoleRefreshMessage(); |
| 130 | 129 | } |
| ... | ... | @@ -132,7 +131,8 @@ public class RoleServiceImpl implements RoleService { |
| 132 | 131 | @Override |
| 133 | 132 | public void updateRoleDataScope(Long id, Integer dataScope, Set<Long> dataScopeDeptIds) { |
| 134 | 133 | // 校验是否可以更新 |
| 135 | - checkUpdateRole(id); | |
| 134 | + validateRoleForUpdate(id); | |
| 135 | + | |
| 136 | 136 | // 更新数据范围 |
| 137 | 137 | RoleDO updateObject = new RoleDO(); |
| 138 | 138 | updateObject.setId(id); |
| ... | ... | @@ -147,7 +147,7 @@ public class RoleServiceImpl implements RoleService { |
| 147 | 147 | @Transactional(rollbackFor = Exception.class) |
| 148 | 148 | public void deleteRole(Long id) { |
| 149 | 149 | // 校验是否可以更新 |
| 150 | - this.checkUpdateRole(id); | |
| 150 | + validateRoleForUpdate(id); | |
| 151 | 151 | // 标记删除 |
| 152 | 152 | roleMapper.deleteById(id); |
| 153 | 153 | // 删除相关数据 |
| ... | ... | @@ -169,7 +169,7 @@ public class RoleServiceImpl implements RoleService { |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | @Override |
| 172 | - public List<RoleDO> getRoles(@Nullable Collection<Integer> statuses) { | |
| 172 | + public List<RoleDO> getRoleListByStatus(@Nullable Collection<Integer> statuses) { | |
| 173 | 173 | if (CollUtil.isEmpty(statuses)) { |
| 174 | 174 | return roleMapper.selectList(); |
| 175 | 175 | } |
| ... | ... | @@ -177,7 +177,7 @@ public class RoleServiceImpl implements RoleService { |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | @Override |
| 180 | - public List<RoleDO> getRolesFromCache(Collection<Long> ids) { | |
| 180 | + public List<RoleDO> getRoleListFromCache(Collection<Long> ids) { | |
| 181 | 181 | if (CollectionUtil.isEmpty(ids)) { |
| 182 | 182 | return Collections.emptyList(); |
| 183 | 183 | } |
| ... | ... | @@ -219,7 +219,7 @@ public class RoleServiceImpl implements RoleService { |
| 219 | 219 | * @param id 角色编号 |
| 220 | 220 | */ |
| 221 | 221 | @VisibleForTesting |
| 222 | - public void checkDuplicateRole(String name, String code, Long id) { | |
| 222 | + void validateRoleDuplicate(String name, String code, Long id) { | |
| 223 | 223 | // 0. 超级管理员,不允许创建 |
| 224 | 224 | if (RoleCodeEnum.isSuperAdmin(code)) { |
| 225 | 225 | throw exception(ROLE_ADMIN_CODE_ERROR, code); |
| ... | ... | @@ -246,7 +246,7 @@ public class RoleServiceImpl implements RoleService { |
| 246 | 246 | * @param id 角色编号 |
| 247 | 247 | */ |
| 248 | 248 | @VisibleForTesting |
| 249 | - public void checkUpdateRole(Long id) { | |
| 249 | + void validateRoleForUpdate(Long id) { | |
| 250 | 250 | RoleDO roleDO = roleMapper.selectById(id); |
| 251 | 251 | if (roleDO == null) { |
| 252 | 252 | throw exception(ROLE_NOT_EXISTS); |
| ... | ... | @@ -258,13 +258,13 @@ public class RoleServiceImpl implements RoleService { |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | @Override |
| 261 | - public void validRoles(Collection<Long> ids) { | |
| 261 | + public void validateRoleList(Collection<Long> ids) { | |
| 262 | 262 | if (CollUtil.isEmpty(ids)) { |
| 263 | 263 | return; |
| 264 | 264 | } |
| 265 | 265 | // 获得角色信息 |
| 266 | 266 | List<RoleDO> roles = roleMapper.selectBatchIds(ids); |
| 267 | - Map<Long, RoleDO> roleMap = CollectionUtils.convertMap(roles, RoleDO::getId); | |
| 267 | + Map<Long, RoleDO> roleMap = convertMap(roles, RoleDO::getId); | |
| 268 | 268 | // 校验 |
| 269 | 269 | ids.forEach(id -> { |
| 270 | 270 | RoleDO role = roleMap.get(id); | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java
| ... | ... | @@ -156,7 +156,7 @@ public class TenantServiceImpl implements TenantService { |
| 156 | 156 | public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) { |
| 157 | 157 | TenantUtils.execute(tenantId, () -> { |
| 158 | 158 | // 获得所有角色 |
| 159 | - List<RoleDO> roles = roleService.getRoles(null); | |
| 159 | + List<RoleDO> roles = roleService.getRoleListByStatus(null); | |
| 160 | 160 | roles.forEach(role -> Assert.isTrue(tenantId.equals(role.getTenantId()), "角色({}/{}) 租户不匹配", |
| 161 | 161 | role.getId(), role.getTenantId(), tenantId)); // 兜底校验 |
| 162 | 162 | // 重新分配每个角色的权限 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceTest.java
| ... | ... | @@ -106,7 +106,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 106 | 106 | Collection<Integer> menusStatuses = asList(0, 1); |
| 107 | 107 | // mock 方法 |
| 108 | 108 | List<RoleDO> roleList = singletonList(randomPojo(RoleDO.class, o -> o.setId(100L))); |
| 109 | - when(roleService.getRolesFromCache(eq(roleIds))).thenReturn(roleList); | |
| 109 | + when(roleService.getRoleListFromCache(eq(roleIds))).thenReturn(roleList); | |
| 110 | 110 | when(roleService.hasAnySuperAdmin(same(roleList))).thenReturn(true); |
| 111 | 111 | List<MenuDO> menuList = randomPojoList(MenuDO.class); |
| 112 | 112 | when(menuService.getMenuListFromCache(eq(menuTypes), eq(menusStatuses))).thenReturn(menuList); |
| ... | ... | @@ -419,7 +419,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 419 | 419 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 420 | 420 | when(roleService.getRoleFromCache(eq(100L))).thenReturn(role); |
| 421 | 421 | // mock 其它方法 |
| 422 | - when(roleService.getRolesFromCache(eq(asSet(100L)))).thenReturn(singletonList(role)); | |
| 422 | + when(roleService.getRoleListFromCache(eq(asSet(100L)))).thenReturn(singletonList(role)); | |
| 423 | 423 | |
| 424 | 424 | // 调用 |
| 425 | 425 | boolean has = permissionService.hasAnyRoles(userId, roles); |
| ... | ... | @@ -436,7 +436,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 436 | 436 | // mock 获得用户的角色 |
| 437 | 437 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.ALL.getScope()) |
| 438 | 438 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 439 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 439 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 440 | 440 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 441 | 441 | |
| 442 | 442 | // 调用 |
| ... | ... | @@ -456,7 +456,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 456 | 456 | // mock 获得用户的角色 |
| 457 | 457 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope()) |
| 458 | 458 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 459 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 459 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 460 | 460 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 461 | 461 | // mock 部门的返回 |
| 462 | 462 | when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 |
| ... | ... | @@ -480,7 +480,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 480 | 480 | // mock 获得用户的角色 |
| 481 | 481 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_ONLY.getScope()) |
| 482 | 482 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 483 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 483 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 484 | 484 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 485 | 485 | // mock 部门的返回 |
| 486 | 486 | when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 |
| ... | ... | @@ -503,7 +503,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 503 | 503 | // mock 获得用户的角色 |
| 504 | 504 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope()) |
| 505 | 505 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 506 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 506 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 507 | 507 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 508 | 508 | // mock 部门的返回 |
| 509 | 509 | when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 |
| ... | ... | @@ -531,7 +531,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { |
| 531 | 531 | // mock 获得用户的角色 |
| 532 | 532 | RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.SELF.getScope()) |
| 533 | 533 | .setStatus(CommonStatusEnum.ENABLE.getStatus())); |
| 534 | - when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 534 | + when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | |
| 535 | 535 | when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); |
| 536 | 536 | |
| 537 | 537 | // 调用 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceTest.java renamed to yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.permission; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.util.RandomUtil; | |
| 4 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | 5 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| ... | ... | @@ -18,21 +17,26 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 18 | 17 | import org.springframework.context.annotation.Import; |
| 19 | 18 | |
| 20 | 19 | import javax.annotation.Resource; |
| 21 | -import java.time.LocalDateTime; | |
| 22 | -import java.util.*; | |
| 20 | +import java.util.Collection; | |
| 21 | +import java.util.List; | |
| 22 | +import java.util.Map; | |
| 23 | +import java.util.Set; | |
| 23 | 24 | |
| 25 | +import static cn.hutool.core.util.RandomUtil.randomEle; | |
| 26 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 24 | 27 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 25 | 28 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
| 26 | 29 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 27 | 30 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 28 | 31 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 29 | 32 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 33 | +import static java.util.Collections.singleton; | |
| 34 | +import static java.util.Collections.singletonList; | |
| 30 | 35 | import static org.junit.jupiter.api.Assertions.*; |
| 31 | 36 | import static org.mockito.Mockito.verify; |
| 32 | 37 | |
| 33 | -// TODO @芋艿:单测的代码质量可以提升下 | |
| 34 | 38 | @Import(RoleServiceImpl.class) |
| 35 | -public class RoleServiceTest extends BaseDbUnitTest { | |
| 39 | +public class RoleServiceImplTest extends BaseDbUnitTest { | |
| 36 | 40 | |
| 37 | 41 | @Resource |
| 38 | 42 | private RoleServiceImpl roleService; |
| ... | ... | @@ -47,9 +51,9 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 47 | 51 | |
| 48 | 52 | @Test |
| 49 | 53 | public void testInitLocalCache() { |
| 50 | - RoleDO roleDO1 = randomRole(); | |
| 54 | + RoleDO roleDO1 = randomPojo(RoleDO.class); | |
| 51 | 55 | roleMapper.insert(roleDO1); |
| 52 | - RoleDO roleDO2 = randomRole(); | |
| 56 | + RoleDO roleDO2 = randomPojo(RoleDO.class); | |
| 53 | 57 | roleMapper.insert(roleDO2); |
| 54 | 58 | |
| 55 | 59 | // 调用 |
| ... | ... | @@ -68,7 +72,6 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 68 | 72 | // 调用 |
| 69 | 73 | Long roleId = roleService.createRole(reqVO, null); |
| 70 | 74 | // 断言 |
| 71 | - assertNotNull(roleId); | |
| 72 | 75 | RoleDO roleDO = roleMapper.selectById(roleId); |
| 73 | 76 | assertPojoEquals(reqVO, roleDO); |
| 74 | 77 | assertEquals(RoleTypeEnum.CUSTOM.getType(), roleDO.getType()); |
| ... | ... | @@ -81,81 +84,158 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 81 | 84 | @Test |
| 82 | 85 | public void testUpdateRole_success() { |
| 83 | 86 | // mock 数据 |
| 84 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 87 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | |
| 85 | 88 | roleMapper.insert(roleDO); |
| 86 | - Long roleId = roleDO.getId(); | |
| 89 | + // 准备参数 | |
| 90 | + Long id = roleDO.getId(); | |
| 91 | + RoleUpdateReqVO reqVO = randomPojo(RoleUpdateReqVO.class, o -> o.setId(id)); | |
| 87 | 92 | |
| 88 | - //调用 | |
| 89 | - RoleUpdateReqVO reqVO = randomPojo(RoleUpdateReqVO.class, o -> { | |
| 90 | - o.setId(roleId); | |
| 91 | - o.setCode("role_code"); | |
| 92 | - o.setName("update_name"); | |
| 93 | - o.setSort(999); | |
| 94 | - }); | |
| 93 | + // 调用 | |
| 95 | 94 | roleService.updateRole(reqVO); |
| 96 | - | |
| 97 | - //断言 | |
| 98 | - RoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 95 | + // 断言 | |
| 96 | + RoleDO newRoleDO = roleMapper.selectById(id); | |
| 99 | 97 | assertPojoEquals(reqVO, newRoleDO); |
| 100 | - | |
| 98 | + // verify 发送刷新消息 | |
| 101 | 99 | verify(roleProducer).sendRoleRefreshMessage(); |
| 102 | 100 | } |
| 103 | 101 | |
| 104 | 102 | @Test |
| 105 | 103 | public void testUpdateRoleStatus_success() { |
| 106 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL, CommonStatusEnum.ENABLE.getStatus()); | |
| 104 | + // mock 数据 | |
| 105 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()) | |
| 106 | + .setType(RoleTypeEnum.CUSTOM.getType())); | |
| 107 | 107 | roleMapper.insert(roleDO); |
| 108 | + | |
| 109 | + // 准备参数 | |
| 108 | 110 | Long roleId = roleDO.getId(); |
| 109 | 111 | |
| 110 | - //调用 | |
| 112 | + // 调用 | |
| 111 | 113 | roleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus()); |
| 112 | - | |
| 113 | - //断言 | |
| 114 | - RoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 115 | - assertEquals(CommonStatusEnum.DISABLE.getStatus(), newRoleDO.getStatus()); | |
| 116 | - | |
| 114 | + // 断言 | |
| 115 | + RoleDO dbRoleDO = roleMapper.selectById(roleId); | |
| 116 | + assertEquals(CommonStatusEnum.DISABLE.getStatus(), dbRoleDO.getStatus()); | |
| 117 | + // verify 发送刷新消息 | |
| 117 | 118 | verify(roleProducer).sendRoleRefreshMessage(); |
| 118 | 119 | } |
| 119 | 120 | |
| 120 | 121 | @Test |
| 121 | 122 | public void testUpdateRoleDataScope_success() { |
| 122 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 123 | + // mock 数据 | |
| 124 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | |
| 123 | 125 | roleMapper.insert(roleDO); |
| 124 | - Long roleId = roleDO.getId(); | |
| 125 | - | |
| 126 | - //调用 | |
| 127 | - Set<Long> deptIdSet = new HashSet<>(Arrays.asList(1L, 2L, 3L, 4L, 5L)); | |
| 128 | - roleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScope(), deptIdSet); | |
| 126 | + // 准备参数 | |
| 127 | + Long id = roleDO.getId(); | |
| 128 | + Integer dataScope = randomEle(DataScopeEnum.values()).getScope(); | |
| 129 | + Set<Long> dataScopeRoleIds = randomSet(Long.class); | |
| 129 | 130 | |
| 130 | - //断言 | |
| 131 | - RoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 132 | - assertEquals(DataScopeEnum.DEPT_CUSTOM.getScope(), newRoleDO.getDataScope()); | |
| 131 | + // 调用 | |
| 132 | + roleService.updateRoleDataScope(id, dataScope, dataScopeRoleIds); | |
| 133 | + // 断言 | |
| 134 | + RoleDO dbRoleDO = roleMapper.selectById(id); | |
| 135 | + assertEquals(dataScope, dbRoleDO.getDataScope()); | |
| 136 | + assertEquals(dataScopeRoleIds, dbRoleDO.getDataScopeDeptIds()); | |
| 137 | + // verify 发送刷新消息 | |
| 138 | + verify(roleProducer).sendRoleRefreshMessage(); | |
| 139 | + } | |
| 133 | 140 | |
| 134 | - Set<Long> newDeptIdSet = newRoleDO.getDataScopeDeptIds(); | |
| 135 | - assertEquals(deptIdSet.size(), newDeptIdSet.size()); | |
| 136 | - deptIdSet.stream().forEach(d -> assertTrue(newDeptIdSet.contains(d))); | |
| 141 | + @Test | |
| 142 | + public void testDeleteRole_success() { | |
| 143 | + // mock 数据 | |
| 144 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | |
| 145 | + roleMapper.insert(roleDO); | |
| 146 | + // 参数准备 | |
| 147 | + Long id = roleDO.getId(); | |
| 137 | 148 | |
| 149 | + // 调用 | |
| 150 | + roleService.deleteRole(id); | |
| 151 | + // 断言 | |
| 152 | + assertNull(roleMapper.selectById(id)); | |
| 153 | + // verify 删除相关数据 | |
| 154 | + verify(permissionService).processRoleDeleted(id); | |
| 155 | + // verify 发送刷新消息 | |
| 138 | 156 | verify(roleProducer).sendRoleRefreshMessage(); |
| 139 | 157 | } |
| 140 | 158 | |
| 141 | 159 | @Test |
| 142 | - public void testDeleteRole_success() { | |
| 143 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 160 | + public void testGetRoleFromCache() { | |
| 161 | + // mock 数据(缓存) | |
| 162 | + RoleDO roleDO = randomPojo(RoleDO.class); | |
| 144 | 163 | roleMapper.insert(roleDO); |
| 145 | - Long roleId = roleDO.getId(); | |
| 164 | + roleService.initLocalCache(); | |
| 165 | + // 参数准备 | |
| 166 | + Long id = roleDO.getId(); | |
| 146 | 167 | |
| 147 | - //调用 | |
| 148 | - roleService.deleteRole(roleId); | |
| 168 | + // 调用 | |
| 169 | + RoleDO dbRoleDO = roleService.getRoleFromCache(id); | |
| 170 | + // 断言 | |
| 171 | + assertPojoEquals(roleDO, dbRoleDO); | |
| 172 | + } | |
| 149 | 173 | |
| 150 | - //断言 | |
| 151 | - RoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 152 | - assertNull(newRoleDO); | |
| 174 | + @Test | |
| 175 | + public void testGetRole() { | |
| 176 | + // mock 数据 | |
| 177 | + RoleDO roleDO = randomPojo(RoleDO.class); | |
| 178 | + roleMapper.insert(roleDO); | |
| 179 | + // 参数准备 | |
| 180 | + Long id = roleDO.getId(); | |
| 153 | 181 | |
| 154 | - verify(roleProducer).sendRoleRefreshMessage(); | |
| 182 | + // 调用 | |
| 183 | + RoleDO dbRoleDO = roleService.getRole(id); | |
| 184 | + // 断言 | |
| 185 | + assertPojoEquals(roleDO, dbRoleDO); | |
| 155 | 186 | } |
| 156 | 187 | |
| 157 | 188 | @Test |
| 158 | - public void testGetRoles() { | |
| 189 | + public void testGetRoleListByStatus_statusNotEmpty() { | |
| 190 | + // mock 数据 | |
| 191 | + RoleDO dbRole = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 192 | + roleMapper.insert(dbRole); | |
| 193 | + // 测试 status 不匹配 | |
| 194 | + roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | |
| 195 | + | |
| 196 | + // 调用 | |
| 197 | + List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus())); | |
| 198 | + // 断言 | |
| 199 | + assertEquals(1, list.size()); | |
| 200 | + assertPojoEquals(dbRole, list.get(0)); | |
| 201 | + } | |
| 202 | + | |
| 203 | + @Test | |
| 204 | + public void testGetRoleListByStatus_statusEmpty() { | |
| 205 | + // mock 数据 | |
| 206 | + RoleDO dbRole01 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 207 | + roleMapper.insert(dbRole01); | |
| 208 | + RoleDO dbRole02 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 209 | + roleMapper.insert(dbRole02); | |
| 210 | + | |
| 211 | + // 调用 | |
| 212 | + List<RoleDO> list = roleService.getRoleListByStatus(null); | |
| 213 | + // 断言 | |
| 214 | + assertEquals(2, list.size()); | |
| 215 | + assertPojoEquals(dbRole01, list.get(0)); | |
| 216 | + assertPojoEquals(dbRole02, list.get(1)); | |
| 217 | + } | |
| 218 | + | |
| 219 | + @Test | |
| 220 | + public void testGetRoleListFromCache() { | |
| 221 | + // mock 数据 | |
| 222 | + RoleDO dbRole = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 223 | + roleMapper.insert(dbRole); | |
| 224 | + // 测试 id 不匹配 | |
| 225 | + roleMapper.insert(cloneIgnoreId(dbRole, o -> {})); | |
| 226 | + roleService.initLocalCache(); | |
| 227 | + // 准备参数 | |
| 228 | + Collection<Long> ids = singleton(dbRole.getId()); | |
| 229 | + | |
| 230 | + // 调用 | |
| 231 | + List<RoleDO> list = roleService.getRoleListFromCache(ids); | |
| 232 | + // 断言 | |
| 233 | + assertEquals(1, list.size()); | |
| 234 | + assertPojoEquals(dbRole, list.get(0)); | |
| 235 | + } | |
| 236 | + | |
| 237 | + @Test | |
| 238 | + public void testGetRoleList() { | |
| 159 | 239 | // mock 数据 |
| 160 | 240 | RoleDO dbRole = randomPojo(RoleDO.class, o -> { // 等会查询到 |
| 161 | 241 | o.setName("土豆"); |
| ... | ... | @@ -175,7 +255,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 175 | 255 | reqVO.setName("土豆"); |
| 176 | 256 | reqVO.setCode("tu"); |
| 177 | 257 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 178 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); | |
| 258 | + reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12)); | |
| 179 | 259 | |
| 180 | 260 | // 调用 |
| 181 | 261 | List<RoleDO> list = roleService.getRoleList(reqVO); |
| ... | ... | @@ -205,7 +285,7 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 205 | 285 | reqVO.setName("土豆"); |
| 206 | 286 | reqVO.setCode("tu"); |
| 207 | 287 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 208 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); | |
| 288 | + reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12)); | |
| 209 | 289 | |
| 210 | 290 | // 调用 |
| 211 | 291 | PageResult<RoleDO> pageResult = roleService.getRolePage(reqVO); |
| ... | ... | @@ -216,86 +296,104 @@ public class RoleServiceTest extends BaseDbUnitTest { |
| 216 | 296 | } |
| 217 | 297 | |
| 218 | 298 | @Test |
| 219 | - public void testCheckDuplicateRole_success() { | |
| 220 | - roleService.checkDuplicateRole(randomString(), randomString(), null); | |
| 299 | + public void testHasAnySuperAdmin() { | |
| 300 | + // 是超级 | |
| 301 | + assertTrue(roleService.hasAnySuperAdmin(singletonList(randomPojo(RoleDO.class, | |
| 302 | + o -> o.setCode("super_admin"))))); | |
| 303 | + // 非超级 | |
| 304 | + assertFalse(roleService.hasAnySuperAdmin(singletonList(randomPojo(RoleDO.class, | |
| 305 | + o -> o.setCode("tenant_admin"))))); | |
| 221 | 306 | } |
| 222 | 307 | |
| 223 | 308 | @Test |
| 224 | - public void testCheckDuplicateRole_nameDuplicate() { | |
| 225 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 226 | - roleMapper.insert(roleDO); | |
| 309 | + public void testValidateRoleDuplicate_success() { | |
| 310 | + // 调用,不会抛异常 | |
| 311 | + roleService.validateRoleDuplicate(randomString(), randomString(), null); | |
| 312 | + } | |
| 227 | 313 | |
| 228 | - String duplicateName = "role_name"; | |
| 314 | + @Test | |
| 315 | + public void testValidateRoleDuplicate_nameDuplicate() { | |
| 316 | + // mock 数据 | |
| 317 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setName("role_name")); | |
| 318 | + roleMapper.insert(roleDO); | |
| 319 | + // 准备参数 | |
| 320 | + String name = "role_name"; | |
| 229 | 321 | |
| 230 | - assertServiceException(() -> roleService.checkDuplicateRole(duplicateName, randomString(), null), ROLE_NAME_DUPLICATE, duplicateName); | |
| 322 | + // 调用,并断言异常 | |
| 323 | + assertServiceException(() -> roleService.validateRoleDuplicate(name, randomString(), null), | |
| 324 | + ROLE_NAME_DUPLICATE, name); | |
| 231 | 325 | } |
| 232 | 326 | |
| 233 | 327 | @Test |
| 234 | - public void testCheckDuplicateRole_codeDuplicate() { | |
| 235 | - RoleDO roleDO = randomPojo(RoleDO.class, o -> { | |
| 236 | - o.setName("role_999"); | |
| 237 | - o.setCode("code"); | |
| 238 | - o.setType(RoleTypeEnum.CUSTOM.getType()); | |
| 239 | - o.setStatus(1); | |
| 240 | - o.setDataScope(DataScopeEnum.ALL.getScope()); | |
| 241 | - }); | |
| 328 | + public void testValidateRoleDuplicate_codeDuplicate() { | |
| 329 | + // mock 数据 | |
| 330 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setCode("code")); | |
| 242 | 331 | roleMapper.insert(roleDO); |
| 332 | + // 准备参数 | |
| 333 | + String code = "code"; | |
| 243 | 334 | |
| 244 | - String randomName = randomString(); | |
| 245 | - String duplicateCode = "code"; | |
| 246 | - | |
| 247 | - assertServiceException(() -> roleService.checkDuplicateRole(randomName, duplicateCode, null), ROLE_CODE_DUPLICATE, duplicateCode); | |
| 335 | + // 调用,并断言异常 | |
| 336 | + assertServiceException(() -> roleService.validateRoleDuplicate(randomString(), code, null), | |
| 337 | + ROLE_CODE_DUPLICATE, code); | |
| 248 | 338 | } |
| 249 | 339 | |
| 250 | 340 | @Test |
| 251 | - public void testCheckUpdateRole_success() { | |
| 252 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 341 | + public void testValidateUpdateRole_success() { | |
| 342 | + RoleDO roleDO = randomPojo(RoleDO.class); | |
| 253 | 343 | roleMapper.insert(roleDO); |
| 254 | - Long roleId = roleDO.getId(); | |
| 344 | + // 准备参数 | |
| 345 | + Long id = roleDO.getId(); | |
| 255 | 346 | |
| 256 | - roleService.checkUpdateRole(roleId); | |
| 347 | + // 调用,无异常 | |
| 348 | + roleService.validateRoleForUpdate(id); | |
| 257 | 349 | } |
| 258 | 350 | |
| 259 | 351 | @Test |
| 260 | - public void testCheckUpdateRole_roleIdNotExist() { | |
| 261 | - assertServiceException(() -> roleService.checkUpdateRole(randomLongId()), ROLE_NOT_EXISTS); | |
| 352 | + public void testValidateUpdateRole_roleIdNotExist() { | |
| 353 | + assertServiceException(() -> roleService.validateRoleForUpdate(randomLongId()), ROLE_NOT_EXISTS); | |
| 262 | 354 | } |
| 263 | 355 | |
| 264 | 356 | @Test |
| 265 | - public void testCheckUpdateRole_systemRoleCanNotBeUpdate() { | |
| 266 | - RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.SYSTEM, DataScopeEnum.ALL); | |
| 357 | + public void testValidateUpdateRole_systemRoleCanNotBeUpdate() { | |
| 358 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.SYSTEM.getType())); | |
| 267 | 359 | roleMapper.insert(roleDO); |
| 268 | - Long roleId = roleDO.getId(); | |
| 360 | + // 准备参数 | |
| 361 | + Long id = roleDO.getId(); | |
| 269 | 362 | |
| 270 | - assertServiceException(() -> roleService.checkUpdateRole(roleId), ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | |
| 363 | + assertServiceException(() -> roleService.validateRoleForUpdate(id), | |
| 364 | + ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | |
| 271 | 365 | } |
| 272 | 366 | |
| 273 | - private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status) { | |
| 274 | - return createRoleDO( name, typeEnum, scopeEnum, status, randomString()); | |
| 275 | - } | |
| 367 | + @Test | |
| 368 | + public void testValidateRoleList_success() { | |
| 369 | + // mock 数据 | |
| 370 | + RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | |
| 371 | + roleMapper.insert(roleDO); | |
| 372 | + // 准备参数 | |
| 373 | + List<Long> ids = singletonList(roleDO.getId()); | |
| 276 | 374 | |
| 277 | - private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) { | |
| 278 | - return createRoleDO(null, name, typeEnum, scopeEnum, status, code); | |
| 375 | + // 调用,无需断言 | |
| 376 | + roleService.validateRoleList(ids); | |
| 279 | 377 | } |
| 280 | 378 | |
| 281 | - private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum) { | |
| 282 | - return createRoleDO(null, name, typeEnum, scopeEnum, randomCommonStatus(), randomString()); | |
| 283 | - } | |
| 379 | + @Test | |
| 380 | + public void testValidateRoleList_notFound() { | |
| 381 | + // 准备参数 | |
| 382 | + List<Long> ids = singletonList(randomLongId()); | |
| 284 | 383 | |
| 285 | - private RoleDO createRoleDO(Long id, String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) { | |
| 286 | - return randomPojo(RoleDO.class, o -> { | |
| 287 | - o.setId(id); | |
| 288 | - o.setName(name); | |
| 289 | - o.setType(typeEnum.getType()); | |
| 290 | - o.setStatus(status); | |
| 291 | - o.setDataScope(scopeEnum.getScope()); | |
| 292 | - o.setCode(code); | |
| 293 | - }); | |
| 384 | + // 调用, 并断言异常 | |
| 385 | + assertServiceException(() -> roleService.validateRoleList(ids), ROLE_NOT_EXISTS); | |
| 294 | 386 | } |
| 295 | 387 | |
| 296 | - private RoleDO randomRole() { | |
| 297 | - return randomPojo(RoleDO.class, | |
| 298 | - o -> o.setDataScope(RandomUtil.randomEle(DataScopeEnum.values()).getScope())); | |
| 299 | - } | |
| 388 | + @Test | |
| 389 | + public void testValidateRoleList_notEnable() { | |
| 390 | + // mock 数据 | |
| 391 | + RoleDO RoleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); | |
| 392 | + roleMapper.insert(RoleDO); | |
| 393 | + // 准备参数 | |
| 394 | + List<Long> ids = singletonList(RoleDO.getId()); | |
| 300 | 395 | |
| 396 | + // 调用, 并断言异常 | |
| 397 | + assertServiceException(() -> roleService.validateRoleList(ids), ROLE_IS_DISABLE, RoleDO.getName()); | |
| 398 | + } | |
| 301 | 399 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImplTest.java
| ... | ... | @@ -196,7 +196,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest { |
| 196 | 196 | role100.setTenantId(dbTenant.getId()); |
| 197 | 197 | RoleDO role101 = randomPojo(RoleDO.class, o -> o.setId(101L)); |
| 198 | 198 | role101.setTenantId(dbTenant.getId()); |
| 199 | - when(roleService.getRoles(isNull())).thenReturn(asList(role100, role101)); | |
| 199 | + when(roleService.getRoleList(isNull())).thenReturn(asList(role100, role101)); | |
| 200 | 200 | // mock 每个角色的权限 |
| 201 | 201 | when(permissionService.getRoleMenuIds(eq(101L))).thenReturn(asSet(201L, 202L)); |
| 202 | 202 | ... | ... |