mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-01 02:38:43 +08:00 
			
		
		
		
	增加 UserRole 的缓存,完善权限相关的单元测试
This commit is contained in:
		| @@ -1,10 +1,13 @@ | ||||
| package cn.iocoder.yudao.module.system.service.permission; | ||||
|  | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
| import cn.hutool.core.map.MapUtil; | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | ||||
| import cn.iocoder.yudao.framework.datapermission.core.dept.service.dto.DeptDataPermissionRespDTO; | ||||
| import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; | ||||
| 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.RoleMenuDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO; | ||||
| @@ -17,20 +20,24 @@ import cn.iocoder.yudao.module.system.enums.permission.DataScopeEnum; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.permission.PermissionProducer; | ||||
| import cn.iocoder.yudao.module.system.service.dept.DeptService; | ||||
| import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||||
| import com.google.common.collect.ImmutableMultimap; | ||||
| import com.google.common.collect.Multimap; | ||||
| 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.List; | ||||
| import java.util.*; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||||
| import static java.util.Arrays.asList; | ||||
| import static java.util.Collections.singleton; | ||||
| import static java.util.Collections.singletonList; | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| import static org.mockito.ArgumentMatchers.eq; | ||||
| import static org.mockito.ArgumentMatchers.same; | ||||
| import static org.mockito.Mockito.verify; | ||||
| import static org.mockito.Mockito.when; | ||||
|  | ||||
| @@ -62,6 +69,226 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|     @MockBean | ||||
|     private PermissionProducer permissionProducer; | ||||
|  | ||||
|     @Test | ||||
|     public void testInitRoleMenuLocalCache() { | ||||
|         // mock 数据 | ||||
|         RoleMenuDO roleMenuDO01 = randomPojo(RoleMenuDO.class, o -> o.setRoleId(1L).setMenuId(10L)); | ||||
|         roleMenuMapper.insert(roleMenuDO01); | ||||
|         RoleMenuDO roleMenuDO02 = randomPojo(RoleMenuDO.class, o -> o.setRoleId(1L).setMenuId(20L)); | ||||
|         roleMenuMapper.insert(roleMenuDO02); | ||||
|  | ||||
|         // 调用 | ||||
|         permissionService.initRoleMenuLocalCache(); | ||||
|         // 断言 roleMenuCache 缓存 | ||||
|         assertEquals(1, permissionService.getRoleMenuCache().keySet().size()); | ||||
|         assertEquals(asList(10L, 20L), permissionService.getRoleMenuCache().get(1L)); | ||||
|         // 断言 menuRoleCache 缓存 | ||||
|         assertEquals(2, permissionService.getMenuRoleCache().size()); | ||||
|         assertEquals(singletonList(1L), permissionService.getMenuRoleCache().get(10L)); | ||||
|         assertEquals(singletonList(1L), permissionService.getMenuRoleCache().get(20L)); | ||||
|         // 断言 maxUpdateTime 缓存 | ||||
|         Date maxUpdateTime = permissionService.getRoleMenuMaxUpdateTime(); | ||||
|         assertEquals(ObjectUtils.max(roleMenuDO01.getUpdateTime(), roleMenuDO02.getUpdateTime()), maxUpdateTime); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testInitUserRoleLocalCache() { | ||||
|         // mock 数据 | ||||
|         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(10L)); | ||||
|         userRoleMapper.insert(userRoleDO01); | ||||
|         UserRoleDO roleMenuDO02 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(20L)); | ||||
|         userRoleMapper.insert(roleMenuDO02); | ||||
|  | ||||
|         // 调用 | ||||
|         permissionService.initUserRoleLocalCache(); | ||||
|         // 断言 roleMenuCache 缓存 | ||||
|         assertEquals(1, permissionService.getUserRoleCache().size()); | ||||
|         assertEquals(asSet(10L, 20L), permissionService.getUserRoleCache().get(1L)); | ||||
|         // 断言 maxUpdateTime 缓存 | ||||
|         Date maxUpdateTime = permissionService.getUserRoleMaxUpdateTime(); | ||||
|         assertEquals(ObjectUtils.max(userRoleDO01.getUpdateTime(), roleMenuDO02.getUpdateTime()), maxUpdateTime); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetRoleMenuListFromCache_superAdmin() { | ||||
|         // 准备参数 | ||||
|         Collection<Long> roleIds = singletonList(100L); | ||||
|         Collection<Integer> menuTypes = asList(2, 3); | ||||
|         Collection<Integer> menusStatuses = asList(0, 1); | ||||
|         // mock 方法 | ||||
|         List<RoleDO> roleList = singletonList(randomPojo(RoleDO.class, o -> o.setId(100L))); | ||||
|         when(roleService.getRolesFromCache(eq(roleIds))).thenReturn(roleList); | ||||
|         when(roleService.hasAnySuperAdmin(same(roleList))).thenReturn(true); | ||||
|         List<MenuDO> menuList = randomPojoList(MenuDO.class); | ||||
|         when(menuService.getMenuListFromCache(eq(menuTypes), eq(menusStatuses))).thenReturn(menuList); | ||||
|  | ||||
|         // 调用 | ||||
|         List<MenuDO> result = permissionService.getRoleMenuListFromCache(roleIds, menuTypes, menusStatuses); | ||||
|         // 断言 | ||||
|         assertSame(menuList, result); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetRoleMenuListFromCache_normal() { | ||||
|         // 准备参数 | ||||
|         Collection<Long> roleIds = asSet(100L, 200L); | ||||
|         Collection<Integer> menuTypes = asList(2, 3); | ||||
|         Collection<Integer> menusStatuses = asList(0, 1); | ||||
|         // mock 方法 | ||||
|         Multimap<Long, Long> roleMenuCache = ImmutableMultimap.<Long, Long>builder().put(100L, 1000L) | ||||
|                 .put(200L, 2000L).put(200L, 2001L).build(); | ||||
|         permissionService.setRoleMenuCache(roleMenuCache); | ||||
|         List<MenuDO> menuList = randomPojoList(MenuDO.class); | ||||
|         when(menuService.getMenuListFromCache(eq(asList(1000L, 2000L, 2001L)), eq(menuTypes), eq(menusStatuses))).thenReturn(menuList); | ||||
|  | ||||
|         // 调用 | ||||
|         List<MenuDO> result = permissionService.getRoleMenuListFromCache(roleIds, menuTypes, menusStatuses); | ||||
|         // 断言 | ||||
|         assertSame(menuList, result); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetUserRoleIdsFromCache() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         Collection<Integer> roleStatuses = singleton(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         // mock 方法 | ||||
|         Map<Long, Set<Long>> userRoleCache = MapUtil.<Long, Set<Long>>builder() | ||||
|                 .put(1L, asSet(10L, 20L)).build(); | ||||
|         permissionService.setUserRoleCache(userRoleCache); | ||||
|         RoleDO roleDO01 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         when(roleService.getRoleFromCache(eq(10L))).thenReturn(roleDO01); | ||||
|         RoleDO roleDO02 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); | ||||
|         when(roleService.getRoleFromCache(eq(20L))).thenReturn(roleDO02); | ||||
|  | ||||
|         // 调用 | ||||
|         Set<Long> roleIds = permissionService.getUserRoleIdsFromCache(userId, roleStatuses); | ||||
|         // 断言 | ||||
|         assertEquals(asSet(10L), roleIds); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetRoleMenuIds_superAdmin() { | ||||
|         // 准备参数 | ||||
|         Long roleId = 100L; | ||||
|         // mock 方法 | ||||
|         when(roleService.hasAnySuperAdmin(eq(singleton(100L)))).thenReturn(true); | ||||
|         List<MenuDO> menuList = singletonList(randomPojo(MenuDO.class).setId(1L)); | ||||
|         when(menuService.getMenus()).thenReturn(menuList); | ||||
|  | ||||
|         // 调用 | ||||
|         Set<Long> menuIds = permissionService.getRoleMenuIds(roleId); | ||||
|         // 断言 | ||||
|         assertEquals(singleton(1L), menuIds); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetRoleMenuIds_normal() { | ||||
|         // 准备参数 | ||||
|         Long roleId = 100L; | ||||
|         // mock 数据 | ||||
|         RoleMenuDO roleMenu01 = randomPojo(RoleMenuDO.class).setRoleId(100L).setMenuId(1L); | ||||
|         roleMenuMapper.insert(roleMenu01); | ||||
|         RoleMenuDO roleMenu02 = randomPojo(RoleMenuDO.class).setRoleId(100L).setMenuId(2L); | ||||
|         roleMenuMapper.insert(roleMenu02); | ||||
|  | ||||
|         // 调用 | ||||
|         Set<Long> menuIds = permissionService.getRoleMenuIds(roleId); | ||||
|         // 断言 | ||||
|         assertEquals(asSet(1L, 2L), menuIds); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testAssignRoleMenu() { | ||||
|         // 准备参数 | ||||
|         Long roleId = 1L; | ||||
|         Set<Long> menuIds = asSet(200L, 300L); | ||||
|         // mock 数据 | ||||
|         RoleMenuDO roleMenu01 = randomPojo(RoleMenuDO.class).setRoleId(1L).setMenuId(100L); | ||||
|         roleMenuMapper.insert(roleMenu01); | ||||
|         RoleMenuDO roleMenu02 = randomPojo(RoleMenuDO.class).setRoleId(1L).setMenuId(200L); | ||||
|         roleMenuMapper.insert(roleMenu02); | ||||
|  | ||||
|         // 调用 | ||||
|         permissionService.assignRoleMenu(roleId, menuIds); | ||||
|         // 断言 | ||||
|         List<RoleMenuDO> roleMenuList = roleMenuMapper.selectList(); | ||||
|         assertEquals(2, roleMenuList.size()); | ||||
|         assertEquals(1L, roleMenuList.get(0).getRoleId()); | ||||
|         assertEquals(200L, roleMenuList.get(0).getMenuId()); | ||||
|         assertEquals(1L, roleMenuList.get(1).getRoleId()); | ||||
|         assertEquals(300L, roleMenuList.get(1).getMenuId()); | ||||
|         verify(permissionProducer).sendRoleMenuRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testAssignUserRole() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         Set<Long> roleIds = asSet(200L, 300L); | ||||
|         // mock 数据 | ||||
|         UserRoleDO userRole01 = randomPojo(UserRoleDO.class).setUserId(1L).setRoleId(100L); | ||||
|         userRoleMapper.insert(userRole01); | ||||
|         UserRoleDO userRole02 = randomPojo(UserRoleDO.class).setUserId(1L).setRoleId(200L); | ||||
|         userRoleMapper.insert(userRole02); | ||||
|  | ||||
|         // 调用 | ||||
|         permissionService.assignUserRole(userId, roleIds); | ||||
|         // 断言 | ||||
|         List<UserRoleDO> userRoleDOList = userRoleMapper.selectList(); | ||||
|         assertEquals(2, userRoleDOList.size()); | ||||
|         assertEquals(1L, userRoleDOList.get(0).getUserId()); | ||||
|         assertEquals(200L, userRoleDOList.get(0).getRoleId()); | ||||
|         assertEquals(1L, userRoleDOList.get(1).getUserId()); | ||||
|         assertEquals(300L, userRoleDOList.get(1).getRoleId()); | ||||
|         verify(permissionProducer).sendUserRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetUserRoleIdListByUserId() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 数据 | ||||
|         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(10L)); | ||||
|         userRoleMapper.insert(userRoleDO01); | ||||
|         UserRoleDO roleMenuDO02 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(20L)); | ||||
|         userRoleMapper.insert(roleMenuDO02); | ||||
|  | ||||
|         // 调用 | ||||
|         Set<Long> result = permissionService.getUserRoleIdListByUserId(userId); | ||||
|         // 断言 | ||||
|         assertEquals(asSet(10L, 20L), result); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetUserRoleIdListByRoleIds() { | ||||
|         // 准备参数 | ||||
|         Collection<Long> roleIds = asSet(10L, 20L); | ||||
|         // mock 数据 | ||||
|         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(10L)); | ||||
|         userRoleMapper.insert(userRoleDO01); | ||||
|         UserRoleDO roleMenuDO02 = randomPojo(UserRoleDO.class, o -> o.setUserId(2L).setRoleId(20L)); | ||||
|         userRoleMapper.insert(roleMenuDO02); | ||||
|  | ||||
|         // 调用 | ||||
|         Set<Long> result = permissionService.getUserRoleIdListByRoleIds(roleIds); | ||||
|         // 断言 | ||||
|         assertEquals(asSet(1L, 2L), result); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testAssignRoleDataScope() { | ||||
|         // 准备参数 | ||||
|         Long roleId = 1L; | ||||
|         Integer dataScope = 2; | ||||
|         Set<Long> dataScopeDeptIds = asSet(10L, 20L); | ||||
|  | ||||
|         // 调用 | ||||
|         permissionService.assignRoleDataScope(roleId, dataScope, dataScopeDeptIds); | ||||
|         // 断言 | ||||
|         verify(roleService).updateRoleDataScope(eq(roleId), eq(dataScope), eq(dataScopeDeptIds)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testProcessRoleDeleted() { | ||||
|         // 准备参数 | ||||
| @@ -89,6 +316,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         assertPojoEquals(dbUserRoles.get(0), userRoleDO02); | ||||
|         // 断言调用 | ||||
|         verify(permissionProducer).sendRoleMenuRefreshMessage(); | ||||
|         verify(permissionProducer).sendUserRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -127,14 +355,33 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         List<UserRoleDO> dbUserRoles = userRoleMapper.selectList(); | ||||
|         assertEquals(1, dbUserRoles.size()); | ||||
|         assertPojoEquals(dbUserRoles.get(0), userRoleDO02); | ||||
|         // 断言调用 | ||||
|         verify(permissionProducer).sendUserRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
| //    @Test | ||||
| //    public void testHasAnyRoles_superAdmin() { | ||||
| //        // 准备参数 | ||||
| //        String[] roles = new String[]{"yunai", "tudou"}; | ||||
| //        // mock 方法 | ||||
| //        List<RoleDO> roleList = singletonList(randomPojo(RoleDO.class, o -> o.setId(100L))); | ||||
| //        when(roleService.getRolesFromCache(eq(roleIds))).thenReturn(roleList); | ||||
| //        when(roleService.hasAnySuperAdmin(same(roleList))).thenReturn(true); | ||||
| //        List<MenuDO> menuList = randomPojoList(MenuDO.class); | ||||
| //        when(menuService.getMenuListFromCache(eq(menuTypes), eq(menusStatuses))).thenReturn(menuList); | ||||
| // | ||||
| //        // 调用 | ||||
| //        List<MenuDO> result = permissionService.getRoleMenuListFromCache(roleIds, menuTypes, menusStatuses); | ||||
| //        // 断言 | ||||
| //        assertSame(menuList, result); | ||||
| //    } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDeptDataPermission_All() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         userRoleMapper.insert(new UserRoleDO().setUserId(userId).setRoleId(2L)); | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(2L)).build()); | ||||
|         // mock 获得用户的角色 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.ALL.getScope()) | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
| @@ -154,7 +401,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         userRoleMapper.insert(new UserRoleDO().setUserId(userId).setRoleId(2L)); | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(2L)).build()); | ||||
|         // mock 获得用户的角色 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope()) | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
| @@ -164,7 +411,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(1L); | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|         // 断言 | ||||
|         assertFalse(result.getAll()); | ||||
|         assertFalse(result.getSelf()); | ||||
| @@ -178,7 +425,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         userRoleMapper.insert(new UserRoleDO().setUserId(userId).setRoleId(2L)); | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(2L)).build()); | ||||
|         // mock 获得用户的角色 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_ONLY.getScope()) | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
| @@ -188,7 +435,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(1L); | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|         // 断言 | ||||
|         assertFalse(result.getAll()); | ||||
|         assertFalse(result.getSelf()); | ||||
| @@ -201,7 +448,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         userRoleMapper.insert(new UserRoleDO().setUserId(userId).setRoleId(2L)); | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(2L)).build()); | ||||
|         // mock 获得用户的角色 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope()) | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
| @@ -229,7 +476,7 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         userRoleMapper.insert(new UserRoleDO().setUserId(userId).setRoleId(2L)); | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(2L)).build()); | ||||
|         // mock 获得用户的角色 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.SELF.getScope()) | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV