Commit b8734fdadc9116a3ec800b8aeb63f594897dc2cd
Committed by
Gitee
!26 完成role单测
Merge pull request !26 from timfruit/feature/ut-role
Showing
3 changed files
with
316 additions
and
5 deletions
src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java
| ... | ... | @@ -30,8 +30,8 @@ public interface SysErrorCodeConstants { |
| 30 | 30 | |
| 31 | 31 | // ========== 角色模块 1002003000 ========== |
| 32 | 32 | ErrorCode ROLE_NOT_EXISTS = new ErrorCode(1002003000, "角色不存在"); |
| 33 | - ErrorCode ROLE_NAME_DUPLICATE = new ErrorCode(1002003001, "已经存在名为【{}}】的角色"); | |
| 34 | - ErrorCode ROLE_CODE_DUPLICATE = new ErrorCode(1002003002, "已经存在编码为【{}}】的角色"); | |
| 33 | + ErrorCode ROLE_NAME_DUPLICATE = new ErrorCode(1002003001, "已经存在名为【{}】的角色"); | |
| 34 | + ErrorCode ROLE_CODE_DUPLICATE = new ErrorCode(1002003002, "已经存在编码为【{}】的角色"); | |
| 35 | 35 | ErrorCode ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE = new ErrorCode(1002003004, "不能操作类型为系统内置的角色"); |
| 36 | 36 | |
| 37 | 37 | // ========== 用户模块 1002004000 ========== | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java
| ... | ... | @@ -18,6 +18,7 @@ import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum; |
| 18 | 18 | import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysRoleProducer; |
| 19 | 19 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; |
| 20 | 20 | import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; |
| 21 | +import com.google.common.annotations.VisibleForTesting; | |
| 21 | 22 | import com.google.common.collect.ImmutableMap; |
| 22 | 23 | import lombok.extern.slf4j.Slf4j; |
| 23 | 24 | import org.springframework.lang.Nullable; |
| ... | ... | @@ -245,7 +246,8 @@ public class SysRoleServiceImpl implements SysRoleService { |
| 245 | 246 | * @param code 角色额编码 |
| 246 | 247 | * @param id 角色编号 |
| 247 | 248 | */ |
| 248 | - private void checkDuplicateRole(String name, String code, Long id) { | |
| 249 | + @VisibleForTesting | |
| 250 | + public void checkDuplicateRole(String name, String code, Long id) { | |
| 249 | 251 | // 1. 该 name 名字被其它角色所使用 |
| 250 | 252 | SysRoleDO role = roleMapper.selectByName(name); |
| 251 | 253 | if (role != null && !role.getId().equals(id)) { |
| ... | ... | @@ -258,7 +260,7 @@ public class SysRoleServiceImpl implements SysRoleService { |
| 258 | 260 | // 该 code 编码被其它角色所使用 |
| 259 | 261 | role = roleMapper.selectByCode(code); |
| 260 | 262 | if (role != null && !role.getId().equals(id)) { |
| 261 | - throw ServiceExceptionUtil.exception(ROLE_CODE_DUPLICATE, name); | |
| 263 | + throw ServiceExceptionUtil.exception(ROLE_CODE_DUPLICATE, code); | |
| 262 | 264 | } |
| 263 | 265 | } |
| 264 | 266 | |
| ... | ... | @@ -267,7 +269,8 @@ public class SysRoleServiceImpl implements SysRoleService { |
| 267 | 269 | * |
| 268 | 270 | * @param id 角色编号 |
| 269 | 271 | */ |
| 270 | - private void checkUpdateRole(Long id) { | |
| 272 | + @VisibleForTesting | |
| 273 | + public void checkUpdateRole(Long id) { | |
| 271 | 274 | SysRoleDO roleDO = roleMapper.selectById(id); |
| 272 | 275 | if (roleDO == null) { |
| 273 | 276 | throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS); | ... | ... |
src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleServiceTest.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.system.service.permission; | |
| 2 | + | |
| 3 | +import cn.hutool.core.bean.BeanUtil; | |
| 4 | +import cn.iocoder.dashboard.BaseDbUnitTest; | |
| 5 | +import cn.iocoder.dashboard.common.enums.CommonStatusEnum; | |
| 6 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 7 | +import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum; | |
| 8 | +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleCreateReqVO; | |
| 9 | +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRolePageReqVO; | |
| 10 | +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO; | |
| 11 | +import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysRoleDO; | |
| 12 | +import cn.iocoder.dashboard.modules.system.dal.mysql.permission.SysRoleMapper; | |
| 13 | +import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum; | |
| 14 | +import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysRoleProducer; | |
| 15 | +import cn.iocoder.dashboard.modules.system.service.permission.impl.SysRoleServiceImpl; | |
| 16 | +import cn.iocoder.dashboard.util.AopTargetUtils; | |
| 17 | +import cn.iocoder.dashboard.util.AssertUtils; | |
| 18 | +import cn.iocoder.dashboard.util.RandomUtils; | |
| 19 | +import cn.iocoder.dashboard.util.object.ObjectUtils; | |
| 20 | +import com.google.common.collect.Sets; | |
| 21 | +import org.junit.jupiter.api.Assertions; | |
| 22 | +import org.junit.jupiter.api.Test; | |
| 23 | +import org.mockito.Mockito; | |
| 24 | +import org.springframework.boot.test.mock.mockito.MockBean; | |
| 25 | +import org.springframework.context.annotation.Import; | |
| 26 | + | |
| 27 | +import javax.annotation.Resource; | |
| 28 | +import java.util.*; | |
| 29 | +import java.util.stream.Collectors; | |
| 30 | + | |
| 31 | +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; | |
| 32 | +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | |
| 33 | +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; | |
| 34 | +import static cn.iocoder.dashboard.util.RandomUtils.*; | |
| 35 | +import static cn.iocoder.dashboard.util.object.ObjectUtils.max; | |
| 36 | +import static org.junit.jupiter.api.Assertions.*; | |
| 37 | +import static org.mockito.Mockito.verify; | |
| 38 | + | |
| 39 | +@Import(SysRoleServiceImpl.class) | |
| 40 | +public class SysRoleServiceTest extends BaseDbUnitTest { | |
| 41 | + | |
| 42 | + @Resource | |
| 43 | + private SysRoleServiceImpl sysRoleService; | |
| 44 | + | |
| 45 | + @Resource | |
| 46 | + private SysRoleMapper roleMapper; | |
| 47 | + | |
| 48 | + @MockBean | |
| 49 | + private SysPermissionService sysPermissionService; | |
| 50 | + | |
| 51 | + @MockBean | |
| 52 | + private SysRoleProducer sysRoleProducer; | |
| 53 | + | |
| 54 | + @Test | |
| 55 | + public void testInitLocalCache_success() throws Exception { | |
| 56 | + SysRoleDO roleDO1 = createRoleDO("role1", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 57 | + roleMapper.insert(roleDO1); | |
| 58 | + SysRoleDO roleDO2 = createRoleDO("role2", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 59 | + roleMapper.insert(roleDO2); | |
| 60 | + | |
| 61 | + //调用 | |
| 62 | + sysRoleService.initLocalCache(); | |
| 63 | + | |
| 64 | + //断言 | |
| 65 | + //获取代理对象 | |
| 66 | + SysRoleServiceImpl target = (SysRoleServiceImpl) AopTargetUtils.getTarget(sysRoleService); | |
| 67 | + | |
| 68 | + Map<Long, SysRoleDO> roleCache = (Map<Long, SysRoleDO>) BeanUtil.getFieldValue(target, "roleCache"); | |
| 69 | + assertPojoEquals(roleDO1, roleCache.get(roleDO1.getId())); | |
| 70 | + assertPojoEquals(roleDO2, roleCache.get(roleDO2.getId())); | |
| 71 | + | |
| 72 | + Date maxUpdateTime = (Date) BeanUtil.getFieldValue(target, "maxUpdateTime"); | |
| 73 | + assertEquals(max(roleDO1.getUpdateTime(), roleDO2.getUpdateTime()), maxUpdateTime); | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Test | |
| 77 | + public void testCreateRole_success() { | |
| 78 | + SysRoleCreateReqVO reqVO = randomPojo(SysRoleCreateReqVO.class, o -> { | |
| 79 | + o.setCode("role_code"); | |
| 80 | + o.setName("role_name"); | |
| 81 | + o.setRemark("remark"); | |
| 82 | + o.setType(SysRoleTypeEnum.CUSTOM.getType()); | |
| 83 | + o.setSort(1); | |
| 84 | + }); | |
| 85 | + Long roleId = sysRoleService.createRole(reqVO); | |
| 86 | + | |
| 87 | + //断言 | |
| 88 | + assertNotNull(roleId); | |
| 89 | + SysRoleDO roleDO = roleMapper.selectById(roleId); | |
| 90 | + assertPojoEquals(reqVO, roleDO); | |
| 91 | + | |
| 92 | + verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 93 | + } | |
| 94 | + | |
| 95 | + @Test | |
| 96 | + public void testUpdateRole_success() { | |
| 97 | + SysRoleDO roleDO = createRoleDO("role_name", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 98 | + roleMapper.insert(roleDO); | |
| 99 | + Long roleId = roleDO.getId(); | |
| 100 | + | |
| 101 | + //调用 | |
| 102 | + SysRoleUpdateReqVO reqVO = randomPojo(SysRoleUpdateReqVO.class, o -> { | |
| 103 | + o.setId(roleId); | |
| 104 | + o.setCode("role_code"); | |
| 105 | + o.setName("update_name"); | |
| 106 | + o.setType(SysRoleTypeEnum.SYSTEM.getType()); | |
| 107 | + o.setSort(999); | |
| 108 | + }); | |
| 109 | + sysRoleService.updateRole(reqVO); | |
| 110 | + | |
| 111 | + //断言 | |
| 112 | + SysRoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 113 | + assertPojoEquals(reqVO, newRoleDO); | |
| 114 | + | |
| 115 | + verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 116 | + } | |
| 117 | + | |
| 118 | + @Test | |
| 119 | + public void testUpdateRoleStatus_success() { | |
| 120 | + SysRoleDO roleDO = createRoleDO("role_name", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL, CommonStatusEnum.ENABLE.getStatus()); | |
| 121 | + roleMapper.insert(roleDO); | |
| 122 | + Long roleId = roleDO.getId(); | |
| 123 | + | |
| 124 | + //调用 | |
| 125 | + sysRoleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus()); | |
| 126 | + | |
| 127 | + //断言 | |
| 128 | + SysRoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 129 | + assertEquals(CommonStatusEnum.DISABLE.getStatus(), newRoleDO.getStatus()); | |
| 130 | + | |
| 131 | + verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 132 | + } | |
| 133 | + | |
| 134 | + @Test | |
| 135 | + public void testUpdateRoleDataScope_success() { | |
| 136 | + SysRoleDO roleDO = createRoleDO("role_name", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 137 | + roleMapper.insert(roleDO); | |
| 138 | + Long roleId = roleDO.getId(); | |
| 139 | + | |
| 140 | + //调用 | |
| 141 | + Set<Long> deptIdSet = Arrays.asList(1L, 2L, 3L, 4L, 5L).stream().collect(Collectors.toSet()); | |
| 142 | + sysRoleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScore(), deptIdSet); | |
| 143 | + | |
| 144 | + //断言 | |
| 145 | + SysRoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 146 | + assertEquals(DataScopeEnum.DEPT_CUSTOM.getScore(), newRoleDO.getDataScope()); | |
| 147 | + | |
| 148 | + Set<Long> newDeptIdSet = newRoleDO.getDataScopeDeptIds(); | |
| 149 | + assertTrue(deptIdSet.size() == newDeptIdSet.size()); | |
| 150 | + deptIdSet.stream().forEach(d -> assertTrue(newDeptIdSet.contains(d))); | |
| 151 | + | |
| 152 | + verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 153 | + } | |
| 154 | + | |
| 155 | + @Test | |
| 156 | + public void testDeleteRole_success() { | |
| 157 | + SysRoleDO roleDO = createRoleDO("role_name", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 158 | + roleMapper.insert(roleDO); | |
| 159 | + Long roleId = roleDO.getId(); | |
| 160 | + | |
| 161 | + //调用 | |
| 162 | + sysRoleService.deleteRole(roleId); | |
| 163 | + | |
| 164 | + //断言 | |
| 165 | + SysRoleDO newRoleDO = roleMapper.selectById(roleId); | |
| 166 | + assertNull(newRoleDO); | |
| 167 | + | |
| 168 | + verify(sysRoleProducer).sendRoleRefreshMessage(); | |
| 169 | + } | |
| 170 | + | |
| 171 | + @Test | |
| 172 | + public void testGetRoles_success() { | |
| 173 | + Map<Long, SysRoleDO> idRoleMap = new HashMap<>(); | |
| 174 | + // 验证查询状态为1的角色 | |
| 175 | + SysRoleDO roleDO1 = createRoleDO("role1", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL, 1); | |
| 176 | + roleMapper.insert(roleDO1); | |
| 177 | + idRoleMap.put(roleDO1.getId(), roleDO1); | |
| 178 | + | |
| 179 | + SysRoleDO roleDO2 = createRoleDO("role2", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL, 1); | |
| 180 | + roleMapper.insert(roleDO2); | |
| 181 | + idRoleMap.put(roleDO2.getId(), roleDO2); | |
| 182 | + | |
| 183 | + // 以下是排除的角色 | |
| 184 | + SysRoleDO roleDO3 = createRoleDO("role3", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL, 2); | |
| 185 | + roleMapper.insert(roleDO3); | |
| 186 | + | |
| 187 | + //调用 | |
| 188 | + List<SysRoleDO> roles = sysRoleService.getRoles(Arrays.asList(1)); | |
| 189 | + | |
| 190 | + //断言 | |
| 191 | + assertEquals(2, roles.size()); | |
| 192 | + roles.stream().forEach(r -> assertPojoEquals(idRoleMap.get(r.getId()), r)); | |
| 193 | + | |
| 194 | + } | |
| 195 | + | |
| 196 | + @Test | |
| 197 | + public void testGetRolePage_success() { | |
| 198 | + Map<Long, SysRoleDO> idRoleMap = new HashMap<>(); | |
| 199 | + // 验证名称包含"role", 状态为1,code为"code"的角色 | |
| 200 | + // 第一页 | |
| 201 | + SysRoleDO roleDO = createRoleDO("role1", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL, 1, "code"); | |
| 202 | + roleMapper.insert(roleDO); | |
| 203 | + idRoleMap.put(roleDO.getId(), roleDO); | |
| 204 | + // 第二页 | |
| 205 | + roleDO = createRoleDO("role2", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL, 1, "code"); | |
| 206 | + roleMapper.insert(roleDO); | |
| 207 | + | |
| 208 | + // 以下是排除的角色 | |
| 209 | + roleDO = createRoleDO("role3", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL, 2, "code"); | |
| 210 | + roleMapper.insert(roleDO); | |
| 211 | + roleDO = createRoleDO("role4", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL, 1, "xxxxx"); | |
| 212 | + roleMapper.insert(roleDO); | |
| 213 | + | |
| 214 | + //调用 | |
| 215 | + SysRolePageReqVO reqVO = randomPojo(SysRolePageReqVO.class, o -> { | |
| 216 | + o.setName("role"); | |
| 217 | + o.setCode("code"); | |
| 218 | + o.setStatus(1); | |
| 219 | + o.setPageNo(1); | |
| 220 | + o.setPageSize(1); | |
| 221 | + o.setBeginTime(null); | |
| 222 | + o.setEndTime(null); | |
| 223 | + }); | |
| 224 | + PageResult<SysRoleDO> result = sysRoleService.getRolePage(reqVO); | |
| 225 | + assertEquals(2, result.getTotal()); | |
| 226 | + result.getList().stream().forEach(r -> assertPojoEquals(idRoleMap.get(r.getId()), r)); | |
| 227 | + } | |
| 228 | + | |
| 229 | + @Test | |
| 230 | + public void testCheckDuplicateRole_success() { | |
| 231 | + sysRoleService.checkDuplicateRole(randomString(), randomString(), null); | |
| 232 | + } | |
| 233 | + | |
| 234 | + @Test | |
| 235 | + public void testCheckDuplicateRole_nameDuplicate() { | |
| 236 | + SysRoleDO roleDO = createRoleDO("role_name", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 237 | + roleMapper.insert(roleDO); | |
| 238 | + | |
| 239 | + String duplicateName = "role_name"; | |
| 240 | + | |
| 241 | + assertServiceException(() -> sysRoleService.checkDuplicateRole(duplicateName, randomString(), null), ROLE_NAME_DUPLICATE, duplicateName); | |
| 242 | + } | |
| 243 | + | |
| 244 | + @Test | |
| 245 | + public void testCheckDuplicateRole_codeDuplicate() { | |
| 246 | + SysRoleDO roleDO = randomPojo(SysRoleDO.class, o -> { | |
| 247 | + o.setName("role_999"); | |
| 248 | + o.setCode("code"); | |
| 249 | + o.setType(SysRoleTypeEnum.CUSTOM.getType()); | |
| 250 | + o.setStatus(1); | |
| 251 | + o.setDataScope(DataScopeEnum.ALL.getScore()); | |
| 252 | + }); | |
| 253 | + roleMapper.insert(roleDO); | |
| 254 | + | |
| 255 | + String randomName = randomString(); | |
| 256 | + String duplicateCode = "code"; | |
| 257 | + | |
| 258 | + assertServiceException(() -> sysRoleService.checkDuplicateRole(randomName, duplicateCode, null), ROLE_CODE_DUPLICATE, duplicateCode); | |
| 259 | + } | |
| 260 | + | |
| 261 | + @Test | |
| 262 | + public void testCheckUpdateRole_success() { | |
| 263 | + SysRoleDO roleDO = createRoleDO("role_name", SysRoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | |
| 264 | + roleMapper.insert(roleDO); | |
| 265 | + Long roleId = roleDO.getId(); | |
| 266 | + | |
| 267 | + sysRoleService.checkUpdateRole(roleId); | |
| 268 | + } | |
| 269 | + | |
| 270 | + @Test | |
| 271 | + public void testCheckUpdateRole_roleIdNotExist() { | |
| 272 | + assertServiceException(() -> sysRoleService.checkUpdateRole(randomLongId()), ROLE_NOT_EXISTS); | |
| 273 | + } | |
| 274 | + | |
| 275 | + @Test | |
| 276 | + public void testCheckUpdateRole_systemRoleCanNotBeUpdate() { | |
| 277 | + SysRoleDO roleDO = createRoleDO("role_name", SysRoleTypeEnum.SYSTEM, DataScopeEnum.ALL); | |
| 278 | + roleMapper.insert(roleDO); | |
| 279 | + Long roleId = roleDO.getId(); | |
| 280 | + | |
| 281 | + assertServiceException(() -> sysRoleService.checkUpdateRole(roleId), ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | |
| 282 | + } | |
| 283 | + | |
| 284 | + private SysRoleDO createRoleDO(String name, SysRoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status) { | |
| 285 | + return createRoleDO( name, typeEnum, scopeEnum, status, randomString()); | |
| 286 | + } | |
| 287 | + | |
| 288 | + private SysRoleDO createRoleDO(String name, SysRoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) { | |
| 289 | + return createRoleDO(null, name, typeEnum, scopeEnum, status, code); | |
| 290 | + } | |
| 291 | + | |
| 292 | + private SysRoleDO createRoleDO(String name, SysRoleTypeEnum typeEnum, DataScopeEnum scopeEnum) { | |
| 293 | + return createRoleDO(null, name, typeEnum, scopeEnum, randomCommonStatus(), randomString()); | |
| 294 | + } | |
| 295 | + | |
| 296 | + private SysRoleDO createRoleDO(Long id, String name, SysRoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) { | |
| 297 | + SysRoleDO roleDO = randomPojo(SysRoleDO.class, o -> { | |
| 298 | + o.setId(id); | |
| 299 | + o.setName(name); | |
| 300 | + o.setType(typeEnum.getType()); | |
| 301 | + o.setStatus(status); | |
| 302 | + o.setDataScope(scopeEnum.getScore()); | |
| 303 | + o.setCode(code); | |
| 304 | + }); | |
| 305 | + return roleDO; | |
| 306 | + } | |
| 307 | + | |
| 308 | +} | ... | ... |