houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.service.permission;
H 2
3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.extra.spring.SpringUtil;
5 import com.iailab.framework.common.enums.CommonStatusEnum;
6 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
7 import com.iailab.module.system.api.permission.dto.DeptDataPermissionRespDTO;
8 import com.iailab.module.system.dal.dataobject.dept.DeptDO;
9 import com.iailab.module.system.dal.dataobject.permission.MenuDO;
10 import com.iailab.module.system.dal.dataobject.permission.RoleDO;
11 import com.iailab.module.system.dal.dataobject.permission.RoleMenuDO;
12 import com.iailab.module.system.dal.dataobject.permission.UserRoleDO;
13 import com.iailab.module.system.dal.dataobject.user.AdminUserDO;
14 import com.iailab.module.system.dal.mysql.permission.RoleMenuMapper;
15 import com.iailab.module.system.dal.mysql.permission.UserRoleMapper;
16 import com.iailab.module.system.enums.permission.DataScopeEnum;
17 import com.iailab.module.system.service.dept.DeptService;
18 import com.iailab.module.system.service.user.AdminUserService;
19 import org.junit.jupiter.api.Test;
20 import org.mockito.MockedStatic;
21 import org.springframework.boot.test.mock.mockito.MockBean;
22 import org.springframework.context.annotation.Import;
23
24 import javax.annotation.Resource;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Set;
28
29 import static cn.hutool.core.collection.ListUtil.toList;
30 import static com.iailab.framework.common.util.collection.SetUtils.asSet;
31 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
32 import static com.iailab.framework.test.core.util.RandomUtils.randomLongId;
33 import static com.iailab.framework.test.core.util.RandomUtils.randomPojo;
34 import static java.util.Collections.singleton;
35 import static java.util.Collections.singletonList;
36 import static org.junit.jupiter.api.Assertions.*;
37 import static org.mockito.ArgumentMatchers.eq;
38 import static org.mockito.Mockito.*;
39
40 @Import({PermissionServiceImpl.class})
41 public class PermissionServiceTest extends BaseDbUnitTest {
42
43     @Resource
44     private PermissionServiceImpl permissionService;
45
46     @Resource
47     private RoleMenuMapper roleMenuMapper;
48     @Resource
49     private UserRoleMapper userRoleMapper;
50
51     @MockBean
52     private RoleService roleService;
53     @MockBean
54     private MenuService menuService;
55     @MockBean
56     private DeptService deptService;
57     @MockBean
58     private AdminUserService userService;
59
60     @Test
61     public void testHasAnyPermissions_superAdmin() {
62         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
63             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
64                     .thenReturn(permissionService);
65
66             // 准备参数
67             Long userId = 1L;
68             String[] roles = new String[]{"system:user:query", "system:user:create"};
69             // mock 用户登录的角色
70             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(100L));
71             RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L)
72                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
73             when(roleService.getRoleListFromCache(eq(singleton(100L)))).thenReturn(toList(role));
74             // mock 其它方法
75             when(roleService.hasAnySuperAdmin(eq(asSet(100L)))).thenReturn(true);
76
77             // 调用,并断言
78             assertTrue(permissionService.hasAnyPermissions(userId, roles));
79         }
80     }
81
82     @Test
83     public void testHasAnyPermissions_normal() {
84         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
85             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
86                     .thenReturn(permissionService);
87
88             // 准备参数
89             Long userId = 1L;
90             String[] roles = new String[]{"system:user:query", "system:user:create"};
91             // mock 用户登录的角色
92             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(100L));
93             RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L)
94                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
95             when(roleService.getRoleListFromCache(eq(singleton(100L)))).thenReturn(toList(role));
96             // mock 菜单
97             Long menuId = 1000L;
98             when(menuService.getMenuIdListByPermissionFromCache(
99                     eq("system:user:create"))).thenReturn(singletonList(menuId));
100             roleMenuMapper.insert(randomPojo(RoleMenuDO.class).setRoleId(100L).setMenuId(1000L));
101
102             // 调用,并断言
103             assertTrue(permissionService.hasAnyPermissions(userId, roles));
104         }
105     }
106
107     @Test
108     public void testHasAnyRoles() {
109         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
110             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
111                     .thenReturn(permissionService);
112
113             // 准备参数
114             Long userId = 1L;
115             String[] roles = new String[]{"yunai", "tudou"};
116             // mock 用户与角色的缓存
117             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(100L));
118             RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L).setCode("tudou")
119                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
120             when(roleService.getRoleListFromCache(eq(singleton(100L)))).thenReturn(toList(role));
121
122             // 调用,并断言
123             assertTrue(permissionService.hasAnyRoles(userId, roles));
124         }
125     }
126
127     // ========== 角色-菜单的相关方法  ==========
128
129     @Test
130     public void testAssignRoleMenu() {
131         // 准备参数
132         Long roleId = 1L;
133         Set<Long> menuIds = asSet(200L, 300L);
134         // mock 数据
135         RoleMenuDO roleMenu01 = randomPojo(RoleMenuDO.class).setRoleId(1L).setMenuId(100L);
136         roleMenuMapper.insert(roleMenu01);
137         RoleMenuDO roleMenu02 = randomPojo(RoleMenuDO.class).setRoleId(1L).setMenuId(200L);
138         roleMenuMapper.insert(roleMenu02);
139
140         // 调用
141         permissionService.assignRoleMenu(roleId, menuIds);
142         // 断言
143         List<RoleMenuDO> roleMenuList = roleMenuMapper.selectList();
144         assertEquals(2, roleMenuList.size());
145         assertEquals(1L, roleMenuList.get(0).getRoleId());
146         assertEquals(200L, roleMenuList.get(0).getMenuId());
147         assertEquals(1L, roleMenuList.get(1).getRoleId());
148         assertEquals(300L, roleMenuList.get(1).getMenuId());
149     }
150
151     @Test
152     public void testProcessRoleDeleted() {
153         // 准备参数
154         Long roleId = randomLongId();
155         // mock 数据 UserRole
156         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setRoleId(roleId)); // 被删除
157         userRoleMapper.insert(userRoleDO01);
158         UserRoleDO userRoleDO02 = randomPojo(UserRoleDO.class); // 不被删除
159         userRoleMapper.insert(userRoleDO02);
160         // mock 数据 RoleMenu
161         RoleMenuDO roleMenuDO01 = randomPojo(RoleMenuDO.class, o -> o.setRoleId(roleId)); // 被删除
162         roleMenuMapper.insert(roleMenuDO01);
163         RoleMenuDO roleMenuDO02 = randomPojo(RoleMenuDO.class); // 不被删除
164         roleMenuMapper.insert(roleMenuDO02);
165
166         // 调用
167         permissionService.processRoleDeleted(roleId);
168         // 断言数据 RoleMenuDO
169         List<RoleMenuDO> dbRoleMenus = roleMenuMapper.selectList();
170         assertEquals(1, dbRoleMenus.size());
171         assertPojoEquals(dbRoleMenus.get(0), roleMenuDO02);
172         // 断言数据 UserRoleDO
173         List<UserRoleDO> dbUserRoles = userRoleMapper.selectList();
174         assertEquals(1, dbUserRoles.size());
175         assertPojoEquals(dbUserRoles.get(0), userRoleDO02);
176     }
177
178     @Test
179     public void testProcessMenuDeleted() {
180         // 准备参数
181         Long menuId = randomLongId();
182         // mock 数据
183         RoleMenuDO roleMenuDO01 = randomPojo(RoleMenuDO.class, o -> o.setMenuId(menuId)); // 被删除
184         roleMenuMapper.insert(roleMenuDO01);
185         RoleMenuDO roleMenuDO02 = randomPojo(RoleMenuDO.class); // 不被删除
186         roleMenuMapper.insert(roleMenuDO02);
187
188         // 调用
189         permissionService.processMenuDeleted(menuId);
190         // 断言数据
191         List<RoleMenuDO> dbRoleMenus = roleMenuMapper.selectList();
192         assertEquals(1, dbRoleMenus.size());
193         assertPojoEquals(dbRoleMenus.get(0), roleMenuDO02);
194     }
195
196     @Test
197     public void testGetRoleMenuIds_superAdmin() {
198         // 准备参数
199         Long roleId = 100L;
200         // mock 方法
201         when(roleService.hasAnySuperAdmin(eq(singleton(100L)))).thenReturn(true);
202         List<MenuDO> menuList = singletonList(randomPojo(MenuDO.class).setId(1L));
203         when(menuService.getMenuList()).thenReturn(menuList);
204
205         // 调用
206         Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(roleId);
207         // 断言
208         assertEquals(singleton(1L), menuIds);
209     }
210
211     @Test
212     public void testGetRoleMenuIds_normal() {
213         // 准备参数
214         Long roleId = 100L;
215         // mock 数据
216         RoleMenuDO roleMenu01 = randomPojo(RoleMenuDO.class).setRoleId(100L).setMenuId(1L);
217         roleMenuMapper.insert(roleMenu01);
218         RoleMenuDO roleMenu02 = randomPojo(RoleMenuDO.class).setRoleId(100L).setMenuId(2L);
219         roleMenuMapper.insert(roleMenu02);
220
221         // 调用
222         Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(roleId);
223         // 断言
224         assertEquals(asSet(1L, 2L), menuIds);
225     }
226
227     @Test
228     public void testGetMenuRoleIdListByMenuIdFromCache() {
229         // 准备参数
230         Long menuId = 1L;
231         // mock 数据
232         RoleMenuDO roleMenu01 = randomPojo(RoleMenuDO.class).setRoleId(100L).setMenuId(1L);
233         roleMenuMapper.insert(roleMenu01);
234         RoleMenuDO roleMenu02 = randomPojo(RoleMenuDO.class).setRoleId(200L).setMenuId(1L);
235         roleMenuMapper.insert(roleMenu02);
236
237         // 调用
238         Set<Long> roleIds = permissionService.getMenuRoleIdListByMenuIdFromCache(menuId);
239         // 断言
240         assertEquals(asSet(100L, 200L), roleIds);
241     }
242
243     // ========== 用户-角色的相关方法  ==========
244
245     @Test
246     public void testAssignUserRole() {
247         // 准备参数
248         Long userId = 1L;
249         Set<Long> roleIds = asSet(200L, 300L);
250         // mock 数据
251         UserRoleDO userRole01 = randomPojo(UserRoleDO.class).setUserId(1L).setRoleId(100L);
252         userRoleMapper.insert(userRole01);
253         UserRoleDO userRole02 = randomPojo(UserRoleDO.class).setUserId(1L).setRoleId(200L);
254         userRoleMapper.insert(userRole02);
255
256         // 调用
257         permissionService.assignUserRole(userId, roleIds);
258         // 断言
259         List<UserRoleDO> userRoleDOList = userRoleMapper.selectList();
260         assertEquals(2, userRoleDOList.size());
261         assertEquals(1L, userRoleDOList.get(0).getUserId());
262         assertEquals(200L, userRoleDOList.get(0).getRoleId());
263         assertEquals(1L, userRoleDOList.get(1).getUserId());
264         assertEquals(300L, userRoleDOList.get(1).getRoleId());
265     }
266
267     @Test
268     public void testProcessUserDeleted() {
269         // 准备参数
270         Long userId = randomLongId();
271         // mock 数据
272         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setUserId(userId)); // 被删除
273         userRoleMapper.insert(userRoleDO01);
274         UserRoleDO userRoleDO02 = randomPojo(UserRoleDO.class); // 不被删除
275         userRoleMapper.insert(userRoleDO02);
276
277         // 调用
278         permissionService.processUserDeleted(userId);
279         // 断言数据
280         List<UserRoleDO> dbUserRoles = userRoleMapper.selectList();
281         assertEquals(1, dbUserRoles.size());
282         assertPojoEquals(dbUserRoles.get(0), userRoleDO02);
283     }
284
285     @Test
286     public void testGetUserRoleIdListByUserId() {
287         // 准备参数
288         Long userId = 1L;
289         // mock 数据
290         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(10L));
291         userRoleMapper.insert(userRoleDO01);
292         UserRoleDO roleMenuDO02 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(20L));
293         userRoleMapper.insert(roleMenuDO02);
294
295         // 调用
296         Set<Long> result = permissionService.getUserRoleIdListByUserId(userId);
297         // 断言
298         assertEquals(asSet(10L, 20L), result);
299     }
300
301     @Test
302     public void testGetUserRoleIdListByUserIdFromCache() {
303         // 准备参数
304         Long userId = 1L;
305         // mock 数据
306         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(10L));
307         userRoleMapper.insert(userRoleDO01);
308         UserRoleDO roleMenuDO02 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(20L));
309         userRoleMapper.insert(roleMenuDO02);
310
311         // 调用
312         Set<Long> result = permissionService.getUserRoleIdListByUserIdFromCache(userId);
313         // 断言
314         assertEquals(asSet(10L, 20L), result);
315     }
316
317     @Test
318     public void testGetUserRoleIdsFromCache() {
319         // 准备参数
320         Long userId = 1L;
321         // mock 数据
322         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(10L));
323         userRoleMapper.insert(userRoleDO01);
324         UserRoleDO roleMenuDO02 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(20L));
325         userRoleMapper.insert(roleMenuDO02);
326
327         // 调用
328         Set<Long> result = permissionService.getUserRoleIdListByUserIdFromCache(userId);
329         // 断言
330         assertEquals(asSet(10L, 20L), result);
331     }
332
333     @Test
334     public void testGetUserRoleIdListByRoleId() {
335         // 准备参数
336         Collection<Long> roleIds = asSet(10L, 20L);
337         // mock 数据
338         UserRoleDO userRoleDO01 = randomPojo(UserRoleDO.class, o -> o.setUserId(1L).setRoleId(10L));
339         userRoleMapper.insert(userRoleDO01);
340         UserRoleDO roleMenuDO02 = randomPojo(UserRoleDO.class, o -> o.setUserId(2L).setRoleId(20L));
341         userRoleMapper.insert(roleMenuDO02);
342
343         // 调用
344         Set<Long> result = permissionService.getUserRoleIdListByRoleId(roleIds);
345         // 断言
346         assertEquals(asSet(1L, 2L), result);
347     }
348
349     @Test
350     public void testGetEnableUserRoleListByUserIdFromCache() {
351         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
352             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
353                     .thenReturn(permissionService);
354
355             // 准备参数
356             Long userId = 1L;
357             // mock 用户登录的角色
358             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(100L));
359             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(200L));
360             RoleDO role01 = randomPojo(RoleDO.class, o -> o.setId(100L)
361                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
362             RoleDO role02 = randomPojo(RoleDO.class, o -> o.setId(200L)
363                     .setStatus(CommonStatusEnum.DISABLE.getStatus()));
364             when(roleService.getRoleListFromCache(eq(asSet(100L, 200L))))
365                     .thenReturn(toList(role01, role02));
366
367             // 调用
368             List<RoleDO> result = permissionService.getEnableUserRoleListByUserIdFromCache(userId);
369             // 断言
370             assertEquals(1, result.size());
371             assertPojoEquals(role01, result.get(0));
372         }
373     }
374
375     // ========== 用户-部门的相关方法  ==========
376
377     @Test
378     public void testAssignRoleDataScope() {
379         // 准备参数
380         Long roleId = 1L;
381         Integer dataScope = 2;
382         Set<Long> dataScopeDeptIds = asSet(10L, 20L);
383
384         // 调用
385         permissionService.assignRoleDataScope(roleId, dataScope, dataScopeDeptIds);
386         // 断言
387         verify(roleService).updateRoleDataScope(eq(roleId), eq(dataScope), eq(dataScopeDeptIds));
388     }
389
390     @Test
391     public void testGetDeptDataPermission_All() {
392         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
393             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
394                     .thenReturn(permissionService);
395
396             // 准备参数
397             Long userId = 1L;
398             // mock 用户的角色编号
399             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L));
400             // mock 获得用户的角色
401             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.ALL.getScope())
402                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
403             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO));
404
405             // 调用
406             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId);
407             // 断言
408             assertTrue(result.getAll());
409             assertFalse(result.getSelf());
410             assertTrue(CollUtil.isEmpty(result.getDeptIds()));
411         }
412     }
413
414     @Test
415     public void testGetDeptDataPermission_DeptCustom() {
416         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
417             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
418                     .thenReturn(permissionService);
419
420             // 准备参数
421             Long userId = 1L;
422             // mock 用户的角色编号
423             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L));
424             // mock 获得用户的角色
425             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope())
426                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
427             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO));
428             // mock 部门的返回
429             when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L),
430                     null, null); // 最后返回 null 的目的,看看会不会重复调用
431
432             // 调用
433             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId);
434             // 断言
435             assertFalse(result.getAll());
436             assertFalse(result.getSelf());
437             assertEquals(roleDO.getDataScopeDeptIds().size() + 1, result.getDeptIds().size());
438             assertTrue(CollUtil.containsAll(result.getDeptIds(), roleDO.getDataScopeDeptIds()));
439             assertTrue(CollUtil.contains(result.getDeptIds(), 3L));
440         }
441     }
442
443     @Test
444     public void testGetDeptDataPermission_DeptOnly() {
445         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
446             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
447                     .thenReturn(permissionService);
448
449             // 准备参数
450             Long userId = 1L;
451             // mock 用户的角色编号
452             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L));
453             // mock 获得用户的角色
454             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_ONLY.getScope())
455                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
456             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO));
457             // mock 部门的返回
458             when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L),
459                     null, null); // 最后返回 null 的目的,看看会不会重复调用
460
461             // 调用
462             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId);
463             // 断言
464             assertFalse(result.getAll());
465             assertFalse(result.getSelf());
466             assertEquals(1, result.getDeptIds().size());
467             assertTrue(CollUtil.contains(result.getDeptIds(), 3L));
468         }
469     }
470
471     @Test
472     public void testGetDeptDataPermission_DeptAndChild() {
473         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
474             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
475                     .thenReturn(permissionService);
476
477             // 准备参数
478             Long userId = 1L;
479             // mock 用户的角色编号
480             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L));
481             // mock 获得用户的角色
482             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope())
483                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
484             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO));
485             // mock 部门的返回
486             when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L),
487                     null, null); // 最后返回 null 的目的,看看会不会重复调用
488             // mock 方法(部门)
489             DeptDO deptDO = randomPojo(DeptDO.class);
490             when(deptService.getChildDeptIdListFromCache(eq(3L))).thenReturn(singleton(deptDO.getId()));
491
492             // 调用
493             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId);
494             // 断言
495             assertFalse(result.getAll());
496             assertFalse(result.getSelf());
497             assertEquals(2, result.getDeptIds().size());
498             assertTrue(CollUtil.contains(result.getDeptIds(), deptDO.getId()));
499             assertTrue(CollUtil.contains(result.getDeptIds(), 3L));
500         }
501     }
502
503     @Test
504     public void testGetDeptDataPermission_Self() {
505         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
506             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class)))
507                     .thenReturn(permissionService);
508
509             // 准备参数
510             Long userId = 1L;
511             // mock 用户的角色编号
512             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L));
513             // mock 获得用户的角色
514             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.SELF.getScope())
515                     .setStatus(CommonStatusEnum.ENABLE.getStatus()));
516             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO));
517
518             // 调用
519             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId);
520             // 断言
521             assertFalse(result.getAll());
522             assertTrue(result.getSelf());
523             assertTrue(CollUtil.isEmpty(result.getDeptIds()));
524         }
525     }
526
527 }