完善 RoleServiceImpl 单元测试
parent
71550a3c0e
commit
9185e5d2a0
|
@ -213,7 +213,7 @@ public class BpmTaskAssignRuleServiceImpl implements BpmTaskAssignRuleService {
|
||||||
|
|
||||||
private void validTaskAssignRuleOptions(Integer type, Set<Long> options) {
|
private void validTaskAssignRuleOptions(Integer type, Set<Long> options) {
|
||||||
if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.ROLE.getType())) {
|
if (Objects.equals(type, BpmTaskAssignRuleTypeEnum.ROLE.getType())) {
|
||||||
roleApi.validRoles(options);
|
roleApi.validRoleList(options);
|
||||||
} else if (ObjectUtils.equalsAny(type, BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(),
|
} else if (ObjectUtils.equalsAny(type, BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(),
|
||||||
BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) {
|
BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType())) {
|
||||||
deptApi.validateDeptList(options);
|
deptApi.validateDeptList(options);
|
||||||
|
|
|
@ -20,6 +20,6 @@ public interface RoleApi {
|
||||||
@GetMapping(PREFIX + "/valid")
|
@GetMapping(PREFIX + "/valid")
|
||||||
@ApiOperation("校验角色是否合法")
|
@ApiOperation("校验角色是否合法")
|
||||||
@ApiImplicitParam(name = "ids", value = "角色编号数组", example = "1,2", required = true, allowMultiple = true)
|
@ApiImplicitParam(name = "ids", value = "角色编号数组", example = "1,2", required = true, allowMultiple = true)
|
||||||
CommonResult<Boolean> validRoles(@RequestParam("ids") Collection<Long> ids);
|
CommonResult<Boolean> validRoleList(@RequestParam("ids") Collection<Long> ids);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
package cn.iocoder.yudao.module.system.enums.permission;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Menu 编号枚举
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor
|
|
||||||
public enum MenuIdEnum {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根节点
|
|
||||||
*/
|
|
||||||
ROOT(0L);
|
|
||||||
|
|
||||||
private final Long id;
|
|
||||||
|
|
||||||
}
|
|
|
@ -22,8 +22,8 @@ public class RoleApiImpl implements RoleApi {
|
||||||
private RoleService roleService;
|
private RoleService roleService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommonResult<Boolean> validRoles(Collection<Long> ids) {
|
public CommonResult<Boolean> validRoleList(Collection<Long> ids) {
|
||||||
roleService.validRoles(ids);
|
roleService.validateRoleList(ids);
|
||||||
return success(true);
|
return success(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ public class AuthController {
|
||||||
}
|
}
|
||||||
// 获得角色列表
|
// 获得角色列表
|
||||||
Set<Long> roleIds = permissionService.getUserRoleIdsFromCache(getLoginUserId(), singleton(CommonStatusEnum.ENABLE.getStatus()));
|
Set<Long> roleIds = permissionService.getUserRoleIdsFromCache(getLoginUserId(), singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
List<RoleDO> roleList = roleService.getRolesFromCache(roleIds);
|
List<RoleDO> roleList = roleService.getRoleListFromCache(roleIds);
|
||||||
// 获得菜单列表
|
// 获得菜单列表
|
||||||
List<MenuDO> menuList = permissionService.getRoleMenuListFromCache(roleIds,
|
List<MenuDO> menuList = permissionService.getRoleMenuListFromCache(roleIds,
|
||||||
SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType(), MenuTypeEnum.BUTTON.getType()),
|
SetUtils.asSet(MenuTypeEnum.DIR.getType(), MenuTypeEnum.MENU.getType(), MenuTypeEnum.BUTTON.getType()),
|
||||||
|
|
|
@ -20,12 +20,12 @@ import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||||
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
|
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
|
||||||
|
import static java.util.Collections.singleton;
|
||||||
|
|
||||||
@Api(tags = "管理后台 - 角色")
|
@Api(tags = "管理后台 - 角色")
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -85,9 +85,9 @@ public class RoleController {
|
||||||
|
|
||||||
@GetMapping("/list-all-simple")
|
@GetMapping("/list-all-simple")
|
||||||
@ApiOperation(value = "获取角色精简信息列表", notes = "只包含被开启的角色,主要用于前端的下拉选项")
|
@ApiOperation(value = "获取角色精简信息列表", notes = "只包含被开启的角色,主要用于前端的下拉选项")
|
||||||
public CommonResult<List<RoleSimpleRespVO>> getSimpleRoles() {
|
public CommonResult<List<RoleSimpleRespVO>> getSimpleRoleList() {
|
||||||
// 获得角色列表,只要开启状态的
|
// 获得角色列表,只要开启状态的
|
||||||
List<RoleDO> list = roleService.getRoles(Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
|
List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
// 排序后,返回给前端
|
// 排序后,返回给前端
|
||||||
list.sort(Comparator.comparing(RoleDO::getSort));
|
list.sort(Comparator.comparing(RoleDO::getSort));
|
||||||
return success(RoleConvert.INSTANCE.convertList02(list));
|
return success(RoleConvert.INSTANCE.convertList02(list));
|
||||||
|
|
|
@ -63,7 +63,7 @@ public class UserProfileController {
|
||||||
AdminUserDO user = userService.getUser(getLoginUserId());
|
AdminUserDO user = userService.getUser(getLoginUserId());
|
||||||
UserProfileRespVO resp = UserConvert.INSTANCE.convert03(user);
|
UserProfileRespVO resp = UserConvert.INSTANCE.convert03(user);
|
||||||
// 获得用户角色
|
// 获得用户角色
|
||||||
List<RoleDO> userRoles = roleService.getRolesFromCache(permissionService.getUserRoleIdListByUserId(user.getId()));
|
List<RoleDO> userRoles = roleService.getRoleListFromCache(permissionService.getUserRoleIdListByUserId(user.getId()));
|
||||||
resp.setRoles(UserConvert.INSTANCE.convertList(userRoles));
|
resp.setRoles(UserConvert.INSTANCE.convertList(userRoles));
|
||||||
// 获得部门信息
|
// 获得部门信息
|
||||||
if (user.getDeptId() != null) {
|
if (user.getDeptId() != null) {
|
||||||
|
|
|
@ -9,13 +9,15 @@ import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
|
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
||||||
import cn.iocoder.yudao.module.system.enums.permission.MenuIdEnum;
|
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.factory.Mappers;
|
import org.mapstruct.factory.Mappers;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
|
||||||
|
import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface AuthConvert {
|
public interface AuthConvert {
|
||||||
|
|
||||||
|
@ -26,8 +28,8 @@ public interface AuthConvert {
|
||||||
default AuthPermissionInfoRespVO convert(AdminUserDO user, List<RoleDO> roleList, List<MenuDO> menuList) {
|
default AuthPermissionInfoRespVO convert(AdminUserDO user, List<RoleDO> roleList, List<MenuDO> menuList) {
|
||||||
return AuthPermissionInfoRespVO.builder()
|
return AuthPermissionInfoRespVO.builder()
|
||||||
.user(AuthPermissionInfoRespVO.UserVO.builder().id(user.getId()).nickname(user.getNickname()).avatar(user.getAvatar()).build())
|
.user(AuthPermissionInfoRespVO.UserVO.builder().id(user.getId()).nickname(user.getNickname()).avatar(user.getAvatar()).build())
|
||||||
.roles(CollectionUtils.convertSet(roleList, RoleDO::getCode))
|
.roles(convertSet(roleList, RoleDO::getCode))
|
||||||
.permissions(CollectionUtils.convertSet(menuList, MenuDO::getPermission))
|
.permissions(convertSet(menuList, MenuDO::getPermission))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +49,7 @@ public interface AuthConvert {
|
||||||
Map<Long, AuthMenuRespVO> treeNodeMap = new LinkedHashMap<>();
|
Map<Long, AuthMenuRespVO> treeNodeMap = new LinkedHashMap<>();
|
||||||
menuList.forEach(menu -> treeNodeMap.put(menu.getId(), AuthConvert.INSTANCE.convertTreeNode(menu)));
|
menuList.forEach(menu -> treeNodeMap.put(menu.getId(), AuthConvert.INSTANCE.convertTreeNode(menu)));
|
||||||
// 处理父子关系
|
// 处理父子关系
|
||||||
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(MenuIdEnum.ROOT.getId())).forEach(childNode -> {
|
treeNodeMap.values().stream().filter(node -> !node.getParentId().equals(ID_ROOT)).forEach(childNode -> {
|
||||||
// 获得父节点
|
// 获得父节点
|
||||||
AuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId());
|
AuthMenuRespVO parentNode = treeNodeMap.get(childNode.getParentId());
|
||||||
if (parentNode == null) {
|
if (parentNode == null) {
|
||||||
|
@ -62,7 +64,7 @@ public interface AuthConvert {
|
||||||
parentNode.getChildren().add(childNode);
|
parentNode.getChildren().add(childNode);
|
||||||
});
|
});
|
||||||
// 获得到所有的根节点
|
// 获得到所有的根节点
|
||||||
return CollectionUtils.filterList(treeNodeMap.values(), node -> MenuIdEnum.ROOT.getId().equals(node.getParentId()));
|
return filterList(treeNodeMap.values(), node -> ID_ROOT.equals(node.getParentId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
SocialUserBindReqDTO convert(Long userId, Integer userType, AuthSocialBindLoginReqVO reqVO);
|
SocialUserBindReqDTO convert(Long userId, Integer userType, AuthSocialBindLoginReqVO reqVO);
|
||||||
|
|
|
@ -158,7 +158,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断角色是否包含超级管理员。如果是超级管理员,获取到全部
|
// 判断角色是否包含超级管理员。如果是超级管理员,获取到全部
|
||||||
List<RoleDO> roleList = roleService.getRolesFromCache(roleIds);
|
List<RoleDO> roleList = roleService.getRoleListFromCache(roleIds);
|
||||||
if (roleService.hasAnySuperAdmin(roleList)) {
|
if (roleService.hasAnySuperAdmin(roleList)) {
|
||||||
return menuService.getMenuListFromCache(menuTypes, menusStatuses);
|
return menuService.getMenuListFromCache(menuTypes, menusStatuses);
|
||||||
}
|
}
|
||||||
|
@ -371,7 +371,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||||
if (roleService.hasAnySuperAdmin(roleIds)) {
|
if (roleService.hasAnySuperAdmin(roleIds)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Set<String> userRoles = convertSet(roleService.getRolesFromCache(roleIds),
|
Set<String> userRoles = convertSet(roleService.getRoleListFromCache(roleIds),
|
||||||
RoleDO::getCode);
|
RoleDO::getCode);
|
||||||
return CollUtil.containsAny(userRoles, Sets.newHashSet(roles));
|
return CollUtil.containsAny(userRoles, Sets.newHashSet(roles));
|
||||||
}
|
}
|
||||||
|
@ -388,7 +388,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||||
result.setSelf(true);
|
result.setSelf(true);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
List<RoleDO> roles = roleService.getRolesFromCache(roleIds);
|
List<RoleDO> roles = roleService.getRoleListFromCache(roleIds);
|
||||||
|
|
||||||
// 获得用户的部门编号的缓存,通过 Guava 的 Suppliers 惰性求值,即有且仅有第一次发起 DB 的查询
|
// 获得用户的部门编号的缓存,通过 Guava 的 Suppliers 惰性求值,即有且仅有第一次发起 DB 的查询
|
||||||
Supplier<Long> userDeptIdCache = Suppliers.memoize(() -> userService.getUser(userId).getDeptId());
|
Supplier<Long> userDeptIdCache = Suppliers.memoize(() -> userService.getUser(userId).getDeptId());
|
||||||
|
|
|
@ -79,7 +79,7 @@ public interface RoleService {
|
||||||
* @param statuses 筛选的状态。允许空,空时不筛选
|
* @param statuses 筛选的状态。允许空,空时不筛选
|
||||||
* @return 角色列表
|
* @return 角色列表
|
||||||
*/
|
*/
|
||||||
List<RoleDO> getRoles(@Nullable Collection<Integer> statuses);
|
List<RoleDO> getRoleListByStatus(@Nullable Collection<Integer> statuses);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得角色数组,从缓存中
|
* 获得角色数组,从缓存中
|
||||||
|
@ -87,7 +87,7 @@ public interface RoleService {
|
||||||
* @param ids 角色编号数组
|
* @param ids 角色编号数组
|
||||||
* @return 角色数组
|
* @return 角色数组
|
||||||
*/
|
*/
|
||||||
List<RoleDO> getRolesFromCache(Collection<Long> ids);
|
List<RoleDO> getRoleListFromCache(Collection<Long> ids);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断角色数组中,是否有超级管理员
|
* 判断角色数组中,是否有超级管理员
|
||||||
|
@ -104,7 +104,7 @@ public interface RoleService {
|
||||||
* @return 是否有管理员
|
* @return 是否有管理员
|
||||||
*/
|
*/
|
||||||
default boolean hasAnySuperAdmin(Set<Long> ids) {
|
default boolean hasAnySuperAdmin(Set<Long> ids) {
|
||||||
return hasAnySuperAdmin(getRolesFromCache(ids));
|
return hasAnySuperAdmin(getRoleListFromCache(ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -138,6 +138,6 @@ public interface RoleService {
|
||||||
*
|
*
|
||||||
* @param ids 角色编号数组
|
* @param ids 角色编号数组
|
||||||
*/
|
*/
|
||||||
void validRoles(Collection<Long> ids);
|
void validateRoleList(Collection<Long> ids);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
||||||
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO;
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO;
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleExportReqVO;
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleExportReqVO;
|
||||||
|
@ -34,6 +33,7 @@ import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
||||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -76,7 +76,7 @@ public class RoleServiceImpl implements RoleService {
|
||||||
log.info("[initLocalCache][缓存角色,数量为:{}]", roleList.size());
|
log.info("[initLocalCache][缓存角色,数量为:{}]", roleList.size());
|
||||||
|
|
||||||
// 第二步:构建缓存
|
// 第二步:构建缓存
|
||||||
roleCache = CollectionUtils.convertMap(roleList, RoleDO::getId);
|
roleCache = convertMap(roleList, RoleDO::getId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ public class RoleServiceImpl implements RoleService {
|
||||||
@Transactional
|
@Transactional
|
||||||
public Long createRole(RoleCreateReqVO reqVO, Integer type) {
|
public Long createRole(RoleCreateReqVO reqVO, Integer type) {
|
||||||
// 校验角色
|
// 校验角色
|
||||||
checkDuplicateRole(reqVO.getName(), reqVO.getCode(), null);
|
validateRoleDuplicate(reqVO.getName(), reqVO.getCode(), null);
|
||||||
// 插入到数据库
|
// 插入到数据库
|
||||||
RoleDO role = RoleConvert.INSTANCE.convert(reqVO);
|
RoleDO role = RoleConvert.INSTANCE.convert(reqVO);
|
||||||
role.setType(ObjectUtil.defaultIfNull(type, RoleTypeEnum.CUSTOM.getType()));
|
role.setType(ObjectUtil.defaultIfNull(type, RoleTypeEnum.CUSTOM.getType()));
|
||||||
|
@ -105,13 +105,13 @@ public class RoleServiceImpl implements RoleService {
|
||||||
@Override
|
@Override
|
||||||
public void updateRole(RoleUpdateReqVO reqVO) {
|
public void updateRole(RoleUpdateReqVO reqVO) {
|
||||||
// 校验是否可以更新
|
// 校验是否可以更新
|
||||||
checkUpdateRole(reqVO.getId());
|
validateRoleForUpdate(reqVO.getId());
|
||||||
// 校验角色的唯一字段是否重复
|
// 校验角色的唯一字段是否重复
|
||||||
checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId());
|
validateRoleDuplicate(reqVO.getName(), reqVO.getCode(), reqVO.getId());
|
||||||
|
|
||||||
// 更新到数据库
|
// 更新到数据库
|
||||||
RoleDO updateObject = RoleConvert.INSTANCE.convert(reqVO);
|
RoleDO updateObj = RoleConvert.INSTANCE.convert(reqVO);
|
||||||
roleMapper.updateById(updateObject);
|
roleMapper.updateById(updateObj);
|
||||||
// 发送刷新消息
|
// 发送刷新消息
|
||||||
roleProducer.sendRoleRefreshMessage();
|
roleProducer.sendRoleRefreshMessage();
|
||||||
}
|
}
|
||||||
|
@ -119,12 +119,11 @@ public class RoleServiceImpl implements RoleService {
|
||||||
@Override
|
@Override
|
||||||
public void updateRoleStatus(Long id, Integer status) {
|
public void updateRoleStatus(Long id, Integer status) {
|
||||||
// 校验是否可以更新
|
// 校验是否可以更新
|
||||||
checkUpdateRole(id);
|
validateRoleForUpdate(id);
|
||||||
|
|
||||||
// 更新状态
|
// 更新状态
|
||||||
RoleDO updateObject = new RoleDO();
|
RoleDO updateObj = new RoleDO().setId(id).setStatus(status);
|
||||||
updateObject.setId(id);
|
roleMapper.updateById(updateObj);
|
||||||
updateObject.setStatus(status);
|
|
||||||
roleMapper.updateById(updateObject);
|
|
||||||
// 发送刷新消息
|
// 发送刷新消息
|
||||||
roleProducer.sendRoleRefreshMessage();
|
roleProducer.sendRoleRefreshMessage();
|
||||||
}
|
}
|
||||||
|
@ -132,7 +131,8 @@ public class RoleServiceImpl implements RoleService {
|
||||||
@Override
|
@Override
|
||||||
public void updateRoleDataScope(Long id, Integer dataScope, Set<Long> dataScopeDeptIds) {
|
public void updateRoleDataScope(Long id, Integer dataScope, Set<Long> dataScopeDeptIds) {
|
||||||
// 校验是否可以更新
|
// 校验是否可以更新
|
||||||
checkUpdateRole(id);
|
validateRoleForUpdate(id);
|
||||||
|
|
||||||
// 更新数据范围
|
// 更新数据范围
|
||||||
RoleDO updateObject = new RoleDO();
|
RoleDO updateObject = new RoleDO();
|
||||||
updateObject.setId(id);
|
updateObject.setId(id);
|
||||||
|
@ -147,7 +147,7 @@ public class RoleServiceImpl implements RoleService {
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void deleteRole(Long id) {
|
public void deleteRole(Long id) {
|
||||||
// 校验是否可以更新
|
// 校验是否可以更新
|
||||||
this.checkUpdateRole(id);
|
validateRoleForUpdate(id);
|
||||||
// 标记删除
|
// 标记删除
|
||||||
roleMapper.deleteById(id);
|
roleMapper.deleteById(id);
|
||||||
// 删除相关数据
|
// 删除相关数据
|
||||||
|
@ -169,7 +169,7 @@ public class RoleServiceImpl implements RoleService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<RoleDO> getRoles(@Nullable Collection<Integer> statuses) {
|
public List<RoleDO> getRoleListByStatus(@Nullable Collection<Integer> statuses) {
|
||||||
if (CollUtil.isEmpty(statuses)) {
|
if (CollUtil.isEmpty(statuses)) {
|
||||||
return roleMapper.selectList();
|
return roleMapper.selectList();
|
||||||
}
|
}
|
||||||
|
@ -177,7 +177,7 @@ public class RoleServiceImpl implements RoleService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<RoleDO> getRolesFromCache(Collection<Long> ids) {
|
public List<RoleDO> getRoleListFromCache(Collection<Long> ids) {
|
||||||
if (CollectionUtil.isEmpty(ids)) {
|
if (CollectionUtil.isEmpty(ids)) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
@ -219,7 +219,7 @@ public class RoleServiceImpl implements RoleService {
|
||||||
* @param id 角色编号
|
* @param id 角色编号
|
||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public void checkDuplicateRole(String name, String code, Long id) {
|
void validateRoleDuplicate(String name, String code, Long id) {
|
||||||
// 0. 超级管理员,不允许创建
|
// 0. 超级管理员,不允许创建
|
||||||
if (RoleCodeEnum.isSuperAdmin(code)) {
|
if (RoleCodeEnum.isSuperAdmin(code)) {
|
||||||
throw exception(ROLE_ADMIN_CODE_ERROR, code);
|
throw exception(ROLE_ADMIN_CODE_ERROR, code);
|
||||||
|
@ -246,7 +246,7 @@ public class RoleServiceImpl implements RoleService {
|
||||||
* @param id 角色编号
|
* @param id 角色编号
|
||||||
*/
|
*/
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
public void checkUpdateRole(Long id) {
|
void validateRoleForUpdate(Long id) {
|
||||||
RoleDO roleDO = roleMapper.selectById(id);
|
RoleDO roleDO = roleMapper.selectById(id);
|
||||||
if (roleDO == null) {
|
if (roleDO == null) {
|
||||||
throw exception(ROLE_NOT_EXISTS);
|
throw exception(ROLE_NOT_EXISTS);
|
||||||
|
@ -258,13 +258,13 @@ public class RoleServiceImpl implements RoleService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validRoles(Collection<Long> ids) {
|
public void validateRoleList(Collection<Long> ids) {
|
||||||
if (CollUtil.isEmpty(ids)) {
|
if (CollUtil.isEmpty(ids)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 获得角色信息
|
// 获得角色信息
|
||||||
List<RoleDO> roles = roleMapper.selectBatchIds(ids);
|
List<RoleDO> roles = roleMapper.selectBatchIds(ids);
|
||||||
Map<Long, RoleDO> roleMap = CollectionUtils.convertMap(roles, RoleDO::getId);
|
Map<Long, RoleDO> roleMap = convertMap(roles, RoleDO::getId);
|
||||||
// 校验
|
// 校验
|
||||||
ids.forEach(id -> {
|
ids.forEach(id -> {
|
||||||
RoleDO role = roleMap.get(id);
|
RoleDO role = roleMap.get(id);
|
||||||
|
|
|
@ -158,7 +158,7 @@ public class TenantServiceImpl implements TenantService {
|
||||||
public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) {
|
public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) {
|
||||||
TenantUtils.execute(tenantId, () -> {
|
TenantUtils.execute(tenantId, () -> {
|
||||||
// 获得所有角色
|
// 获得所有角色
|
||||||
List<RoleDO> roles = roleService.getRoles(null);
|
List<RoleDO> roles = roleService.getRoleListByStatus(null);
|
||||||
roles.forEach(role -> Assert.isTrue(tenantId.equals(role.getTenantId()), "角色({}/{}) 租户不匹配",
|
roles.forEach(role -> Assert.isTrue(tenantId.equals(role.getTenantId()), "角色({}/{}) 租户不匹配",
|
||||||
role.getId(), role.getTenantId(), tenantId)); // 兜底校验
|
role.getId(), role.getTenantId(), tenantId)); // 兜底校验
|
||||||
// 重新分配每个角色的权限
|
// 重新分配每个角色的权限
|
||||||
|
|
|
@ -106,7 +106,7 @@ public class PermissionServiceTest extends BaseDbUnitTest {
|
||||||
Collection<Integer> menusStatuses = asList(0, 1);
|
Collection<Integer> menusStatuses = asList(0, 1);
|
||||||
// mock 方法
|
// mock 方法
|
||||||
List<RoleDO> roleList = singletonList(randomPojo(RoleDO.class, o -> o.setId(100L)));
|
List<RoleDO> roleList = singletonList(randomPojo(RoleDO.class, o -> o.setId(100L)));
|
||||||
when(roleService.getRolesFromCache(eq(roleIds))).thenReturn(roleList);
|
when(roleService.getRoleListFromCache(eq(roleIds))).thenReturn(roleList);
|
||||||
when(roleService.hasAnySuperAdmin(same(roleList))).thenReturn(true);
|
when(roleService.hasAnySuperAdmin(same(roleList))).thenReturn(true);
|
||||||
List<MenuDO> menuList = randomPojoList(MenuDO.class);
|
List<MenuDO> menuList = randomPojoList(MenuDO.class);
|
||||||
when(menuService.getMenuListFromCache(eq(menuTypes), eq(menusStatuses))).thenReturn(menuList);
|
when(menuService.getMenuListFromCache(eq(menuTypes), eq(menusStatuses))).thenReturn(menuList);
|
||||||
|
@ -419,7 +419,7 @@ public class PermissionServiceTest extends BaseDbUnitTest {
|
||||||
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
when(roleService.getRoleFromCache(eq(100L))).thenReturn(role);
|
when(roleService.getRoleFromCache(eq(100L))).thenReturn(role);
|
||||||
// mock 其它方法
|
// mock 其它方法
|
||||||
when(roleService.getRolesFromCache(eq(asSet(100L)))).thenReturn(singletonList(role));
|
when(roleService.getRoleListFromCache(eq(asSet(100L)))).thenReturn(singletonList(role));
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
boolean has = permissionService.hasAnyRoles(userId, roles);
|
boolean has = permissionService.hasAnyRoles(userId, roles);
|
||||||
|
@ -436,7 +436,7 @@ public class PermissionServiceTest extends BaseDbUnitTest {
|
||||||
// mock 获得用户的角色
|
// mock 获得用户的角色
|
||||||
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.ALL.getScope())
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.ALL.getScope())
|
||||||
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
||||||
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
|
@ -456,7 +456,7 @@ public class PermissionServiceTest extends BaseDbUnitTest {
|
||||||
// mock 获得用户的角色
|
// mock 获得用户的角色
|
||||||
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope())
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope())
|
||||||
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
||||||
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
||||||
// mock 部门的返回
|
// mock 部门的返回
|
||||||
when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用
|
when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用
|
||||||
|
@ -480,7 +480,7 @@ public class PermissionServiceTest extends BaseDbUnitTest {
|
||||||
// mock 获得用户的角色
|
// mock 获得用户的角色
|
||||||
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_ONLY.getScope())
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_ONLY.getScope())
|
||||||
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
||||||
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
||||||
// mock 部门的返回
|
// mock 部门的返回
|
||||||
when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用
|
when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用
|
||||||
|
@ -503,7 +503,7 @@ public class PermissionServiceTest extends BaseDbUnitTest {
|
||||||
// mock 获得用户的角色
|
// mock 获得用户的角色
|
||||||
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope())
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope())
|
||||||
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
||||||
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
||||||
// mock 部门的返回
|
// mock 部门的返回
|
||||||
when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用
|
when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用
|
||||||
|
@ -531,7 +531,7 @@ public class PermissionServiceTest extends BaseDbUnitTest {
|
||||||
// mock 获得用户的角色
|
// mock 获得用户的角色
|
||||||
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.SELF.getScope())
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.SELF.getScope())
|
||||||
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
when(roleService.getRolesFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO));
|
||||||
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO);
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
|
|
|
@ -0,0 +1,399 @@
|
||||||
|
package cn.iocoder.yudao.module.system.service.permission;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
||||||
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleExportReqVO;
|
||||||
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RolePageReqVO;
|
||||||
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleUpdateReqVO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
||||||
|
import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMapper;
|
||||||
|
import cn.iocoder.yudao.module.system.enums.permission.DataScopeEnum;
|
||||||
|
import cn.iocoder.yudao.module.system.enums.permission.RoleTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.system.mq.producer.permission.RoleProducer;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static cn.hutool.core.util.RandomUtil.randomEle;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||||
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||||
|
import static java.util.Collections.singleton;
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
@Import(RoleServiceImpl.class)
|
||||||
|
public class RoleServiceImplTest extends BaseDbUnitTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RoleServiceImpl roleService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RoleMapper roleMapper;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private PermissionService permissionService;
|
||||||
|
@MockBean
|
||||||
|
private RoleProducer roleProducer;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInitLocalCache() {
|
||||||
|
RoleDO roleDO1 = randomPojo(RoleDO.class);
|
||||||
|
roleMapper.insert(roleDO1);
|
||||||
|
RoleDO roleDO2 = randomPojo(RoleDO.class);
|
||||||
|
roleMapper.insert(roleDO2);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
roleService.initLocalCache();
|
||||||
|
// 断言 roleCache 缓存
|
||||||
|
Map<Long, RoleDO> roleCache = roleService.getRoleCache();
|
||||||
|
assertPojoEquals(roleDO1, roleCache.get(roleDO1.getId()));
|
||||||
|
assertPojoEquals(roleDO2, roleCache.get(roleDO2.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateRole_success() {
|
||||||
|
// 准备参数
|
||||||
|
RoleCreateReqVO reqVO = randomPojo(RoleCreateReqVO.class);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
Long roleId = roleService.createRole(reqVO, null);
|
||||||
|
// 断言
|
||||||
|
RoleDO roleDO = roleMapper.selectById(roleId);
|
||||||
|
assertPojoEquals(reqVO, roleDO);
|
||||||
|
assertEquals(RoleTypeEnum.CUSTOM.getType(), roleDO.getType());
|
||||||
|
assertEquals(CommonStatusEnum.ENABLE.getStatus(), roleDO.getStatus());
|
||||||
|
assertEquals(DataScopeEnum.ALL.getScope(), roleDO.getDataScope());
|
||||||
|
// verify 发送刷新消息
|
||||||
|
verify(roleProducer).sendRoleRefreshMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateRole_success() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType()));
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 准备参数
|
||||||
|
Long id = roleDO.getId();
|
||||||
|
RoleUpdateReqVO reqVO = randomPojo(RoleUpdateReqVO.class, o -> o.setId(id));
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
roleService.updateRole(reqVO);
|
||||||
|
// 断言
|
||||||
|
RoleDO newRoleDO = roleMapper.selectById(id);
|
||||||
|
assertPojoEquals(reqVO, newRoleDO);
|
||||||
|
// verify 发送刷新消息
|
||||||
|
verify(roleProducer).sendRoleRefreshMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateRoleStatus_success() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())
|
||||||
|
.setType(RoleTypeEnum.CUSTOM.getType()));
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
|
||||||
|
// 准备参数
|
||||||
|
Long roleId = roleDO.getId();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
roleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus());
|
||||||
|
// 断言
|
||||||
|
RoleDO dbRoleDO = roleMapper.selectById(roleId);
|
||||||
|
assertEquals(CommonStatusEnum.DISABLE.getStatus(), dbRoleDO.getStatus());
|
||||||
|
// verify 发送刷新消息
|
||||||
|
verify(roleProducer).sendRoleRefreshMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateRoleDataScope_success() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType()));
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 准备参数
|
||||||
|
Long id = roleDO.getId();
|
||||||
|
Integer dataScope = randomEle(DataScopeEnum.values()).getScope();
|
||||||
|
Set<Long> dataScopeRoleIds = randomSet(Long.class);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
roleService.updateRoleDataScope(id, dataScope, dataScopeRoleIds);
|
||||||
|
// 断言
|
||||||
|
RoleDO dbRoleDO = roleMapper.selectById(id);
|
||||||
|
assertEquals(dataScope, dbRoleDO.getDataScope());
|
||||||
|
assertEquals(dataScopeRoleIds, dbRoleDO.getDataScopeDeptIds());
|
||||||
|
// verify 发送刷新消息
|
||||||
|
verify(roleProducer).sendRoleRefreshMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteRole_success() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType()));
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 参数准备
|
||||||
|
Long id = roleDO.getId();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
roleService.deleteRole(id);
|
||||||
|
// 断言
|
||||||
|
assertNull(roleMapper.selectById(id));
|
||||||
|
// verify 删除相关数据
|
||||||
|
verify(permissionService).processRoleDeleted(id);
|
||||||
|
// verify 发送刷新消息
|
||||||
|
verify(roleProducer).sendRoleRefreshMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRoleFromCache() {
|
||||||
|
// mock 数据(缓存)
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class);
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
roleService.initLocalCache();
|
||||||
|
// 参数准备
|
||||||
|
Long id = roleDO.getId();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
RoleDO dbRoleDO = roleService.getRoleFromCache(id);
|
||||||
|
// 断言
|
||||||
|
assertPojoEquals(roleDO, dbRoleDO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRole() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class);
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 参数准备
|
||||||
|
Long id = roleDO.getId();
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
RoleDO dbRoleDO = roleService.getRole(id);
|
||||||
|
// 断言
|
||||||
|
assertPojoEquals(roleDO, dbRoleDO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRoleListByStatus_statusNotEmpty() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO dbRole = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
|
roleMapper.insert(dbRole);
|
||||||
|
// 测试 status 不匹配
|
||||||
|
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
|
// 断言
|
||||||
|
assertEquals(1, list.size());
|
||||||
|
assertPojoEquals(dbRole, list.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRoleListByStatus_statusEmpty() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO dbRole01 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
|
roleMapper.insert(dbRole01);
|
||||||
|
RoleDO dbRole02 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
|
||||||
|
roleMapper.insert(dbRole02);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
List<RoleDO> list = roleService.getRoleListByStatus(null);
|
||||||
|
// 断言
|
||||||
|
assertEquals(2, list.size());
|
||||||
|
assertPojoEquals(dbRole01, list.get(0));
|
||||||
|
assertPojoEquals(dbRole02, list.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRoleListFromCache() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO dbRole = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
|
roleMapper.insert(dbRole);
|
||||||
|
// 测试 id 不匹配
|
||||||
|
roleMapper.insert(cloneIgnoreId(dbRole, o -> {}));
|
||||||
|
roleService.initLocalCache();
|
||||||
|
// 准备参数
|
||||||
|
Collection<Long> ids = singleton(dbRole.getId());
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
List<RoleDO> list = roleService.getRoleListFromCache(ids);
|
||||||
|
// 断言
|
||||||
|
assertEquals(1, list.size());
|
||||||
|
assertPojoEquals(dbRole, list.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRoleList() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO dbRole = randomPojo(RoleDO.class, o -> { // 等会查询到
|
||||||
|
o.setName("土豆");
|
||||||
|
o.setCode("tudou");
|
||||||
|
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||||
|
o.setCreateTime(buildTime(2022, 2, 8));
|
||||||
|
});
|
||||||
|
roleMapper.insert(dbRole);
|
||||||
|
// 测试 name 不匹配
|
||||||
|
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setName("红薯")));
|
||||||
|
// 测试 code 不匹配
|
||||||
|
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCode("hong")));
|
||||||
|
// 测试 createTime 不匹配
|
||||||
|
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCreateTime(buildTime(2022, 2, 16))));
|
||||||
|
// 准备参数
|
||||||
|
RoleExportReqVO reqVO = new RoleExportReqVO();
|
||||||
|
reqVO.setName("土豆");
|
||||||
|
reqVO.setCode("tu");
|
||||||
|
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||||
|
reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12));
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
List<RoleDO> list = roleService.getRoleList(reqVO);
|
||||||
|
// 断言
|
||||||
|
assertEquals(1, list.size());
|
||||||
|
assertPojoEquals(dbRole, list.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetRolePage() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO dbRole = randomPojo(RoleDO.class, o -> { // 等会查询到
|
||||||
|
o.setName("土豆");
|
||||||
|
o.setCode("tudou");
|
||||||
|
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||||
|
o.setCreateTime(buildTime(2022, 2, 8));
|
||||||
|
});
|
||||||
|
roleMapper.insert(dbRole);
|
||||||
|
// 测试 name 不匹配
|
||||||
|
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setName("红薯")));
|
||||||
|
// 测试 code 不匹配
|
||||||
|
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCode("hong")));
|
||||||
|
// 测试 createTime 不匹配
|
||||||
|
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCreateTime(buildTime(2022, 2, 16))));
|
||||||
|
// 准备参数
|
||||||
|
RolePageReqVO reqVO = new RolePageReqVO();
|
||||||
|
reqVO.setName("土豆");
|
||||||
|
reqVO.setCode("tu");
|
||||||
|
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||||
|
reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12));
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
PageResult<RoleDO> pageResult = roleService.getRolePage(reqVO);
|
||||||
|
// 断言
|
||||||
|
assertEquals(1, pageResult.getTotal());
|
||||||
|
assertEquals(1, pageResult.getList().size());
|
||||||
|
assertPojoEquals(dbRole, pageResult.getList().get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHasAnySuperAdmin() {
|
||||||
|
// 是超级
|
||||||
|
assertTrue(roleService.hasAnySuperAdmin(singletonList(randomPojo(RoleDO.class,
|
||||||
|
o -> o.setCode("super_admin")))));
|
||||||
|
// 非超级
|
||||||
|
assertFalse(roleService.hasAnySuperAdmin(singletonList(randomPojo(RoleDO.class,
|
||||||
|
o -> o.setCode("tenant_admin")))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateRoleDuplicate_success() {
|
||||||
|
// 调用,不会抛异常
|
||||||
|
roleService.validateRoleDuplicate(randomString(), randomString(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateRoleDuplicate_nameDuplicate() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setName("role_name"));
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 准备参数
|
||||||
|
String name = "role_name";
|
||||||
|
|
||||||
|
// 调用,并断言异常
|
||||||
|
assertServiceException(() -> roleService.validateRoleDuplicate(name, randomString(), null),
|
||||||
|
ROLE_NAME_DUPLICATE, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateRoleDuplicate_codeDuplicate() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setCode("code"));
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 准备参数
|
||||||
|
String code = "code";
|
||||||
|
|
||||||
|
// 调用,并断言异常
|
||||||
|
assertServiceException(() -> roleService.validateRoleDuplicate(randomString(), code, null),
|
||||||
|
ROLE_CODE_DUPLICATE, code);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateUpdateRole_success() {
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class);
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 准备参数
|
||||||
|
Long id = roleDO.getId();
|
||||||
|
|
||||||
|
// 调用,无异常
|
||||||
|
roleService.validateRoleForUpdate(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateUpdateRole_roleIdNotExist() {
|
||||||
|
assertServiceException(() -> roleService.validateRoleForUpdate(randomLongId()), ROLE_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateUpdateRole_systemRoleCanNotBeUpdate() {
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.SYSTEM.getType()));
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 准备参数
|
||||||
|
Long id = roleDO.getId();
|
||||||
|
|
||||||
|
assertServiceException(() -> roleService.validateRoleForUpdate(id),
|
||||||
|
ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateRoleList_success() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
||||||
|
roleMapper.insert(roleDO);
|
||||||
|
// 准备参数
|
||||||
|
List<Long> ids = singletonList(roleDO.getId());
|
||||||
|
|
||||||
|
// 调用,无需断言
|
||||||
|
roleService.validateRoleList(ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateRoleList_notFound() {
|
||||||
|
// 准备参数
|
||||||
|
List<Long> ids = singletonList(randomLongId());
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> roleService.validateRoleList(ids), ROLE_NOT_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidateRoleList_notEnable() {
|
||||||
|
// mock 数据
|
||||||
|
RoleDO RoleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
|
||||||
|
roleMapper.insert(RoleDO);
|
||||||
|
// 准备参数
|
||||||
|
List<Long> ids = singletonList(RoleDO.getId());
|
||||||
|
|
||||||
|
// 调用, 并断言异常
|
||||||
|
assertServiceException(() -> roleService.validateRoleList(ids), ROLE_IS_DISABLE, RoleDO.getName());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,301 +0,0 @@
|
||||||
package cn.iocoder.yudao.module.system.service.permission;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.RandomUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleExportReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RolePageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleUpdateReqVO;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
|
|
||||||
import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMapper;
|
|
||||||
import cn.iocoder.yudao.module.system.enums.permission.DataScopeEnum;
|
|
||||||
import cn.iocoder.yudao.module.system.enums.permission.RoleTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.system.mq.producer.permission.RoleProducer;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
|
||||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
|
|
||||||
// TODO @芋艿:单测的代码质量可以提升下
|
|
||||||
@Import(RoleServiceImpl.class)
|
|
||||||
public class RoleServiceTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private RoleServiceImpl roleService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private RoleMapper roleMapper;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private PermissionService permissionService;
|
|
||||||
@MockBean
|
|
||||||
private RoleProducer roleProducer;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testInitLocalCache() {
|
|
||||||
RoleDO roleDO1 = randomRole();
|
|
||||||
roleMapper.insert(roleDO1);
|
|
||||||
RoleDO roleDO2 = randomRole();
|
|
||||||
roleMapper.insert(roleDO2);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
roleService.initLocalCache();
|
|
||||||
// 断言 roleCache 缓存
|
|
||||||
Map<Long, RoleDO> roleCache = roleService.getRoleCache();
|
|
||||||
assertPojoEquals(roleDO1, roleCache.get(roleDO1.getId()));
|
|
||||||
assertPojoEquals(roleDO2, roleCache.get(roleDO2.getId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateRole_success() {
|
|
||||||
// 准备参数
|
|
||||||
RoleCreateReqVO reqVO = randomPojo(RoleCreateReqVO.class);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Long roleId = roleService.createRole(reqVO, null);
|
|
||||||
// 断言
|
|
||||||
assertNotNull(roleId);
|
|
||||||
RoleDO roleDO = roleMapper.selectById(roleId);
|
|
||||||
assertPojoEquals(reqVO, roleDO);
|
|
||||||
assertEquals(RoleTypeEnum.CUSTOM.getType(), roleDO.getType());
|
|
||||||
assertEquals(CommonStatusEnum.ENABLE.getStatus(), roleDO.getStatus());
|
|
||||||
assertEquals(DataScopeEnum.ALL.getScope(), roleDO.getDataScope());
|
|
||||||
// verify 发送刷新消息
|
|
||||||
verify(roleProducer).sendRoleRefreshMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateRole_success() {
|
|
||||||
// mock 数据
|
|
||||||
RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL);
|
|
||||||
roleMapper.insert(roleDO);
|
|
||||||
Long roleId = roleDO.getId();
|
|
||||||
|
|
||||||
//调用
|
|
||||||
RoleUpdateReqVO reqVO = randomPojo(RoleUpdateReqVO.class, o -> {
|
|
||||||
o.setId(roleId);
|
|
||||||
o.setCode("role_code");
|
|
||||||
o.setName("update_name");
|
|
||||||
o.setSort(999);
|
|
||||||
});
|
|
||||||
roleService.updateRole(reqVO);
|
|
||||||
|
|
||||||
//断言
|
|
||||||
RoleDO newRoleDO = roleMapper.selectById(roleId);
|
|
||||||
assertPojoEquals(reqVO, newRoleDO);
|
|
||||||
|
|
||||||
verify(roleProducer).sendRoleRefreshMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateRoleStatus_success() {
|
|
||||||
RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL, CommonStatusEnum.ENABLE.getStatus());
|
|
||||||
roleMapper.insert(roleDO);
|
|
||||||
Long roleId = roleDO.getId();
|
|
||||||
|
|
||||||
//调用
|
|
||||||
roleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus());
|
|
||||||
|
|
||||||
//断言
|
|
||||||
RoleDO newRoleDO = roleMapper.selectById(roleId);
|
|
||||||
assertEquals(CommonStatusEnum.DISABLE.getStatus(), newRoleDO.getStatus());
|
|
||||||
|
|
||||||
verify(roleProducer).sendRoleRefreshMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateRoleDataScope_success() {
|
|
||||||
RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL);
|
|
||||||
roleMapper.insert(roleDO);
|
|
||||||
Long roleId = roleDO.getId();
|
|
||||||
|
|
||||||
//调用
|
|
||||||
Set<Long> deptIdSet = new HashSet<>(Arrays.asList(1L, 2L, 3L, 4L, 5L));
|
|
||||||
roleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScope(), deptIdSet);
|
|
||||||
|
|
||||||
//断言
|
|
||||||
RoleDO newRoleDO = roleMapper.selectById(roleId);
|
|
||||||
assertEquals(DataScopeEnum.DEPT_CUSTOM.getScope(), newRoleDO.getDataScope());
|
|
||||||
|
|
||||||
Set<Long> newDeptIdSet = newRoleDO.getDataScopeDeptIds();
|
|
||||||
assertEquals(deptIdSet.size(), newDeptIdSet.size());
|
|
||||||
deptIdSet.stream().forEach(d -> assertTrue(newDeptIdSet.contains(d)));
|
|
||||||
|
|
||||||
verify(roleProducer).sendRoleRefreshMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteRole_success() {
|
|
||||||
RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL);
|
|
||||||
roleMapper.insert(roleDO);
|
|
||||||
Long roleId = roleDO.getId();
|
|
||||||
|
|
||||||
//调用
|
|
||||||
roleService.deleteRole(roleId);
|
|
||||||
|
|
||||||
//断言
|
|
||||||
RoleDO newRoleDO = roleMapper.selectById(roleId);
|
|
||||||
assertNull(newRoleDO);
|
|
||||||
|
|
||||||
verify(roleProducer).sendRoleRefreshMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetRoles() {
|
|
||||||
// mock 数据
|
|
||||||
RoleDO dbRole = randomPojo(RoleDO.class, o -> { // 等会查询到
|
|
||||||
o.setName("土豆");
|
|
||||||
o.setCode("tudou");
|
|
||||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
|
||||||
o.setCreateTime(buildTime(2022, 2, 8));
|
|
||||||
});
|
|
||||||
roleMapper.insert(dbRole);
|
|
||||||
// 测试 name 不匹配
|
|
||||||
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setName("红薯")));
|
|
||||||
// 测试 code 不匹配
|
|
||||||
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCode("hong")));
|
|
||||||
// 测试 createTime 不匹配
|
|
||||||
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCreateTime(buildTime(2022, 2, 16))));
|
|
||||||
// 准备参数
|
|
||||||
RoleExportReqVO reqVO = new RoleExportReqVO();
|
|
||||||
reqVO.setName("土豆");
|
|
||||||
reqVO.setCode("tu");
|
|
||||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
|
||||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)}));
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<RoleDO> list = roleService.getRoleList(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, list.size());
|
|
||||||
assertPojoEquals(dbRole, list.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetRolePage() {
|
|
||||||
// mock 数据
|
|
||||||
RoleDO dbRole = randomPojo(RoleDO.class, o -> { // 等会查询到
|
|
||||||
o.setName("土豆");
|
|
||||||
o.setCode("tudou");
|
|
||||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
|
||||||
o.setCreateTime(buildTime(2022, 2, 8));
|
|
||||||
});
|
|
||||||
roleMapper.insert(dbRole);
|
|
||||||
// 测试 name 不匹配
|
|
||||||
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setName("红薯")));
|
|
||||||
// 测试 code 不匹配
|
|
||||||
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCode("hong")));
|
|
||||||
// 测试 createTime 不匹配
|
|
||||||
roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCreateTime(buildTime(2022, 2, 16))));
|
|
||||||
// 准备参数
|
|
||||||
RolePageReqVO reqVO = new RolePageReqVO();
|
|
||||||
reqVO.setName("土豆");
|
|
||||||
reqVO.setCode("tu");
|
|
||||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
|
||||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)}));
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PageResult<RoleDO> pageResult = roleService.getRolePage(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, pageResult.getTotal());
|
|
||||||
assertEquals(1, pageResult.getList().size());
|
|
||||||
assertPojoEquals(dbRole, pageResult.getList().get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCheckDuplicateRole_success() {
|
|
||||||
roleService.checkDuplicateRole(randomString(), randomString(), null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCheckDuplicateRole_nameDuplicate() {
|
|
||||||
RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL);
|
|
||||||
roleMapper.insert(roleDO);
|
|
||||||
|
|
||||||
String duplicateName = "role_name";
|
|
||||||
|
|
||||||
assertServiceException(() -> roleService.checkDuplicateRole(duplicateName, randomString(), null), ROLE_NAME_DUPLICATE, duplicateName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCheckDuplicateRole_codeDuplicate() {
|
|
||||||
RoleDO roleDO = randomPojo(RoleDO.class, o -> {
|
|
||||||
o.setName("role_999");
|
|
||||||
o.setCode("code");
|
|
||||||
o.setType(RoleTypeEnum.CUSTOM.getType());
|
|
||||||
o.setStatus(1);
|
|
||||||
o.setDataScope(DataScopeEnum.ALL.getScope());
|
|
||||||
});
|
|
||||||
roleMapper.insert(roleDO);
|
|
||||||
|
|
||||||
String randomName = randomString();
|
|
||||||
String duplicateCode = "code";
|
|
||||||
|
|
||||||
assertServiceException(() -> roleService.checkDuplicateRole(randomName, duplicateCode, null), ROLE_CODE_DUPLICATE, duplicateCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCheckUpdateRole_success() {
|
|
||||||
RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL);
|
|
||||||
roleMapper.insert(roleDO);
|
|
||||||
Long roleId = roleDO.getId();
|
|
||||||
|
|
||||||
roleService.checkUpdateRole(roleId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCheckUpdateRole_roleIdNotExist() {
|
|
||||||
assertServiceException(() -> roleService.checkUpdateRole(randomLongId()), ROLE_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCheckUpdateRole_systemRoleCanNotBeUpdate() {
|
|
||||||
RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.SYSTEM, DataScopeEnum.ALL);
|
|
||||||
roleMapper.insert(roleDO);
|
|
||||||
Long roleId = roleDO.getId();
|
|
||||||
|
|
||||||
assertServiceException(() -> roleService.checkUpdateRole(roleId), ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status) {
|
|
||||||
return createRoleDO( name, typeEnum, scopeEnum, status, randomString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) {
|
|
||||||
return createRoleDO(null, name, typeEnum, scopeEnum, status, code);
|
|
||||||
}
|
|
||||||
|
|
||||||
private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum) {
|
|
||||||
return createRoleDO(null, name, typeEnum, scopeEnum, randomCommonStatus(), randomString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private RoleDO createRoleDO(Long id, String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) {
|
|
||||||
return randomPojo(RoleDO.class, o -> {
|
|
||||||
o.setId(id);
|
|
||||||
o.setName(name);
|
|
||||||
o.setType(typeEnum.getType());
|
|
||||||
o.setStatus(status);
|
|
||||||
o.setDataScope(scopeEnum.getScope());
|
|
||||||
o.setCode(code);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private RoleDO randomRole() {
|
|
||||||
return randomPojo(RoleDO.class,
|
|
||||||
o -> o.setDataScope(RandomUtil.randomEle(DataScopeEnum.values()).getScope()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -196,7 +196,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
|
||||||
role100.setTenantId(dbTenant.getId());
|
role100.setTenantId(dbTenant.getId());
|
||||||
RoleDO role101 = randomPojo(RoleDO.class, o -> o.setId(101L));
|
RoleDO role101 = randomPojo(RoleDO.class, o -> o.setId(101L));
|
||||||
role101.setTenantId(dbTenant.getId());
|
role101.setTenantId(dbTenant.getId());
|
||||||
when(roleService.getRoles(isNull())).thenReturn(asList(role100, role101));
|
when(roleService.getRoleList(isNull())).thenReturn(asList(role100, role101));
|
||||||
// mock 每个角色的权限
|
// mock 每个角色的权限
|
||||||
when(permissionService.getRoleMenuIds(eq(101L))).thenReturn(asSet(201L, 202L));
|
when(permissionService.getRoleMenuIds(eq(101L))).thenReturn(asSet(201L, 202L));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue