houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.service.permission;
H 2
3 import cn.hutool.extra.spring.SpringUtil;
4 import com.iailab.framework.common.enums.CommonStatusEnum;
5 import com.iailab.framework.common.pojo.PageResult;
6 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
7 import com.iailab.module.system.controller.admin.permission.vo.role.RolePageReqVO;
8 import com.iailab.module.system.controller.admin.permission.vo.role.RoleSaveReqVO;
9 import com.iailab.module.system.dal.dataobject.permission.RoleDO;
10 import com.iailab.module.system.dal.mysql.permission.RoleMapper;
11 import com.iailab.module.system.enums.permission.DataScopeEnum;
12 import com.iailab.module.system.enums.permission.RoleTypeEnum;
13 import org.junit.jupiter.api.Test;
14 import org.mockito.MockedStatic;
15 import org.springframework.boot.test.mock.mockito.MockBean;
16 import org.springframework.context.annotation.Import;
17
18 import javax.annotation.Resource;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Set;
22
23 import static cn.hutool.core.util.RandomUtil.randomEle;
24 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
25 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime;
26 import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
27 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
28 import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException;
29 import static com.iailab.framework.test.core.util.RandomUtils.*;
30 import static com.iailab.module.system.enums.ErrorCodeConstants.*;
31 import static java.util.Collections.singleton;
32 import static java.util.Collections.singletonList;
33 import static org.junit.jupiter.api.Assertions.*;
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.mockStatic;
36 import static org.mockito.Mockito.verify;
37
38 @Import(RoleServiceImpl.class)
39 public class RoleServiceImplTest extends BaseDbUnitTest {
40
41     @Resource
42     private RoleServiceImpl roleService;
43
44     @Resource
45     private RoleMapper roleMapper;
46
47     @MockBean
48     private PermissionService permissionService;
49
50     @Test
51     public void testCreateRole() {
52         // 准备参数
53         RoleSaveReqVO reqVO = randomPojo(RoleSaveReqVO.class)
54                 .setId(null); // 防止 id 被赋值
55
56         // 调用
57         Long roleId = roleService.createRole(reqVO, null);
58         // 断言
59         RoleDO roleDO = roleMapper.selectById(roleId);
60         assertPojoEquals(reqVO, roleDO, "id");
61         assertEquals(RoleTypeEnum.CUSTOM.getType(), roleDO.getType());
62         assertEquals(CommonStatusEnum.ENABLE.getStatus(), roleDO.getStatus());
63         assertEquals(DataScopeEnum.ALL.getScope(), roleDO.getDataScope());
64     }
65
66     @Test
67     public void testUpdateRole() {
68         // mock 数据
69         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType()));
70         roleMapper.insert(roleDO);
71         // 准备参数
72         Long id = roleDO.getId();
73         RoleSaveReqVO reqVO = randomPojo(RoleSaveReqVO.class, o -> o.setId(id));
74
75         // 调用
76         roleService.updateRole(reqVO);
77         // 断言
78         RoleDO newRoleDO = roleMapper.selectById(id);
79         assertPojoEquals(reqVO, newRoleDO);
80     }
81
82     @Test
83     public void testUpdateRoleDataScope() {
84         // mock 数据
85         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType()));
86         roleMapper.insert(roleDO);
87         // 准备参数
88         Long id = roleDO.getId();
89         Integer dataScope = randomEle(DataScopeEnum.values()).getScope();
90         Set<Long> dataScopeRoleIds = randomSet(Long.class);
91
92         // 调用
93         roleService.updateRoleDataScope(id, dataScope, dataScopeRoleIds);
94         // 断言
95         RoleDO dbRoleDO = roleMapper.selectById(id);
96         assertEquals(dataScope, dbRoleDO.getDataScope());
97         assertEquals(dataScopeRoleIds, dbRoleDO.getDataScopeDeptIds());
98     }
99
100     @Test
101     public void testDeleteRole() {
102         // mock 数据
103         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType()));
104         roleMapper.insert(roleDO);
105         // 参数准备
106         Long id = roleDO.getId();
107
108         // 调用
109         roleService.deleteRole(id);
110         // 断言
111         assertNull(roleMapper.selectById(id));
112         // verify 删除相关数据
113         verify(permissionService).processRoleDeleted(id);
114     }
115
116     @Test
117     public void testValidateRoleDuplicate_success() {
118         // 调用,不会抛异常
119         roleService.validateRoleDuplicate(randomString(), randomString(), null);
120     }
121
122     @Test
123     public void testValidateRoleDuplicate_nameDuplicate() {
124         // mock 数据
125         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setName("role_name"));
126         roleMapper.insert(roleDO);
127         // 准备参数
128         String name = "role_name";
129
130         // 调用,并断言异常
131         assertServiceException(() -> roleService.validateRoleDuplicate(name, randomString(), null),
132                 ROLE_NAME_DUPLICATE, name);
133     }
134
135     @Test
136     public void testValidateRoleDuplicate_codeDuplicate() {
137         // mock 数据
138         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setCode("code"));
139         roleMapper.insert(roleDO);
140         // 准备参数
141         String code = "code";
142
143         // 调用,并断言异常
144         assertServiceException(() -> roleService.validateRoleDuplicate(randomString(), code, null),
145                 ROLE_CODE_DUPLICATE, code);
146     }
147
148     @Test
149     public void testValidateUpdateRole_success() {
150         RoleDO roleDO = randomPojo(RoleDO.class);
151         roleMapper.insert(roleDO);
152         // 准备参数
153         Long id = roleDO.getId();
154
155         // 调用,无异常
156         roleService.validateRoleForUpdate(id);
157     }
158
159     @Test
160     public void testValidateUpdateRole_roleIdNotExist() {
161         assertServiceException(() -> roleService.validateRoleForUpdate(randomLongId()), ROLE_NOT_EXISTS);
162     }
163
164     @Test
165     public void testValidateUpdateRole_systemRoleCanNotBeUpdate() {
166         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.SYSTEM.getType()));
167         roleMapper.insert(roleDO);
168         // 准备参数
169         Long id = roleDO.getId();
170
171         assertServiceException(() -> roleService.validateRoleForUpdate(id),
172                 ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
173     }
174
175     @Test
176     public void testGetRole() {
177         // mock 数据
178         RoleDO roleDO = randomPojo(RoleDO.class);
179         roleMapper.insert(roleDO);
180         // 参数准备
181         Long id = roleDO.getId();
182
183         // 调用
184         RoleDO dbRoleDO = roleService.getRole(id);
185         // 断言
186         assertPojoEquals(roleDO, dbRoleDO);
187     }
188
189     @Test
190     public void testGetRoleFromCache() {
191         // mock 数据(缓存)
192         RoleDO roleDO = randomPojo(RoleDO.class);
193         roleMapper.insert(roleDO);
194         // 参数准备
195         Long id = roleDO.getId();
196
197         // 调用
198         RoleDO dbRoleDO = roleService.getRoleFromCache(id);
199         // 断言
200         assertPojoEquals(roleDO, dbRoleDO);
201     }
202
203     @Test
204     public void testGetRoleListByStatus() {
205         // mock 数据
206         RoleDO dbRole01 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
207         roleMapper.insert(dbRole01);
208         RoleDO dbRole02 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
209         roleMapper.insert(dbRole02);
210
211         // 调用
212         List<RoleDO> list = roleService.getRoleListByStatus(
213                 singleton(CommonStatusEnum.ENABLE.getStatus()));
214         // 断言
215         assertEquals(1, list.size());
216         assertPojoEquals(dbRole01, list.get(0));
217     }
218
219     @Test
220     public void testGetRoleList() {
221         // mock 数据
222         RoleDO dbRole01 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
223         roleMapper.insert(dbRole01);
224         RoleDO dbRole02 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
225         roleMapper.insert(dbRole02);
226
227         // 调用
228         List<RoleDO> list = roleService.getRoleList();
229         // 断言
230         assertEquals(2, list.size());
231         assertPojoEquals(dbRole01, list.get(0));
232         assertPojoEquals(dbRole02, list.get(1));
233     }
234
235     @Test
236     public void testGetRoleList_ids() {
237         // mock 数据
238         RoleDO dbRole01 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
239         roleMapper.insert(dbRole01);
240         RoleDO dbRole02 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
241         roleMapper.insert(dbRole02);
242         // 准备参数
243         Collection<Long> ids = singleton(dbRole01.getId());
244
245         // 调用
246         List<RoleDO> list = roleService.getRoleList(ids);
247         // 断言
248         assertEquals(1, list.size());
249         assertPojoEquals(dbRole01, list.get(0));
250     }
251
252     @Test
253     public void testGetRoleListFromCache() {
254         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
255             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(RoleServiceImpl.class)))
256                     .thenReturn(roleService);
257
258             // mock 数据
259             RoleDO dbRole = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
260             roleMapper.insert(dbRole);
261             // 测试 id 不匹配
262             roleMapper.insert(cloneIgnoreId(dbRole, o -> {}));
263             // 准备参数
264             Collection<Long> ids = singleton(dbRole.getId());
265
266             // 调用
267             List<RoleDO> list = roleService.getRoleListFromCache(ids);
268             // 断言
269             assertEquals(1, list.size());
270             assertPojoEquals(dbRole, list.get(0));
271         }
272     }
273
274     @Test
275     public void testGetRolePage() {
276         // mock 数据
277         RoleDO dbRole = randomPojo(RoleDO.class, o -> { // 等会查询到
278             o.setName("土豆");
279             o.setCode("tudou");
280             o.setStatus(CommonStatusEnum.ENABLE.getStatus());
281             o.setCreateTime(buildTime(2022, 2, 8));
282         });
283         roleMapper.insert(dbRole);
284         // 测试 name 不匹配
285         roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setName("红薯")));
286         // 测试 code 不匹配
287         roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCode("hong")));
288         // 测试 createTime 不匹配
289         roleMapper.insert(cloneIgnoreId(dbRole, o -> o.setCreateTime(buildTime(2022, 2, 16))));
290         // 准备参数
291         RolePageReqVO reqVO = new RolePageReqVO();
292         reqVO.setName("土豆");
293         reqVO.setCode("tu");
294         reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
295         reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12));
296
297         // 调用
298         PageResult<RoleDO> pageResult = roleService.getRolePage(reqVO);
299         // 断言
300         assertEquals(1, pageResult.getTotal());
301         assertEquals(1, pageResult.getList().size());
302         assertPojoEquals(dbRole, pageResult.getList().get(0));
303     }
304
305     @Test
306     public void testHasAnySuperAdmin_true() {
307         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
308             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(RoleServiceImpl.class)))
309                     .thenReturn(roleService);
310
311             // mock 数据
312             RoleDO dbRole = randomPojo(RoleDO.class).setCode("super_admin");
313             roleMapper.insert(dbRole);
314             // 准备参数
315             Long id = dbRole.getId();
316
317             // 调用,并调用
318             assertTrue(roleService.hasAnySuperAdmin(singletonList(id)));
319         }
320     }
321
322     @Test
323     public void testHasAnySuperAdmin_false() {
324         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
325             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(RoleServiceImpl.class)))
326                     .thenReturn(roleService);
327
328             // mock 数据
329             RoleDO dbRole = randomPojo(RoleDO.class).setCode("tenant_admin");
330             roleMapper.insert(dbRole);
331             // 准备参数
332             Long id = dbRole.getId();
333
334             // 调用,并调用
335             assertFalse(roleService.hasAnySuperAdmin(singletonList(id)));
336         }
337     }
338
339     @Test
340     public void testValidateRoleList_success() {
341         // mock 数据
342         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
343         roleMapper.insert(roleDO);
344         // 准备参数
345         List<Long> ids = singletonList(roleDO.getId());
346
347         // 调用,无需断言
348         roleService.validateRoleList(ids);
349     }
350
351     @Test
352     public void testValidateRoleList_notFound() {
353         // 准备参数
354         List<Long> ids = singletonList(randomLongId());
355
356         // 调用, 并断言异常
357         assertServiceException(() -> roleService.validateRoleList(ids), ROLE_NOT_EXISTS);
358     }
359
360     @Test
361     public void testValidateRoleList_notEnable() {
362         // mock 数据
363         RoleDO RoleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
364         roleMapper.insert(RoleDO);
365         // 准备参数
366         List<Long> ids = singletonList(RoleDO.getId());
367
368         // 调用, 并断言异常
369         assertServiceException(() -> roleService.validateRoleList(ids), ROLE_IS_DISABLE, RoleDO.getName());
370     }
371 }