mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-01 02:38:43 +08:00 
			
		
		
		
	feature(使用redis缓存): 功能合并
1、缓存改为Redis缓存,不在启动时缓存,使用时候根据数据源缓存 2、前端登录接口调整
This commit is contained in:
		| @@ -1,9 +1,7 @@ | ||||
| package cn.iocoder.yudao.module.system.service.dept; | ||||
|  | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | ||||
| import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | ||||
| import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; | ||||
| import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO; | ||||
| @@ -11,27 +9,20 @@ import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptUpdateRe | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.dept.DeptMapper; | ||||
| import cn.iocoder.yudao.module.system.enums.dept.DeptIdEnum; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.dept.DeptProducer; | ||||
| import com.google.common.collect.Multimap; | ||||
| import org.junit.jupiter.api.BeforeEach; | ||||
| 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.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.function.Consumer; | ||||
| import java.util.Set; | ||||
|  | ||||
| import static cn.hutool.core.util.RandomUtil.randomEle; | ||||
| 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.singletonList; | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| import static org.mockito.Mockito.verify; | ||||
|  | ||||
| /** | ||||
|  * {@link DeptServiceImpl} 的单元测试类 | ||||
| @@ -45,39 +36,163 @@ public class DeptServiceImplTest extends BaseDbUnitTest { | ||||
|     private DeptServiceImpl deptService; | ||||
|     @Resource | ||||
|     private DeptMapper deptMapper; | ||||
|     @MockBean | ||||
|     private DeptProducer deptProducer; | ||||
|  | ||||
|     @BeforeEach | ||||
|     public void setUp() { | ||||
|         // 清理租户上下文 | ||||
|         TenantContextHolder.clear(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCache() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO1 = randomDeptDO(); | ||||
|         deptMapper.insert(deptDO1); | ||||
|         DeptDO deptDO2 = randomDeptDO(); | ||||
|         deptMapper.insert(deptDO2); | ||||
|     public void testCreateDept() { | ||||
|         // 准备参数 | ||||
|         DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> { | ||||
|             o.setParentId(DeptIdEnum.ROOT.getId()); | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|         }); | ||||
|  | ||||
|         // 调用 | ||||
|         deptService.initLocalCache(); | ||||
|         // 断言 deptCache 缓存 | ||||
|         Map<Long, DeptDO> deptCache = deptService.getDeptCache(); | ||||
|         assertEquals(2, deptCache.size()); | ||||
|         assertPojoEquals(deptDO1, deptCache.get(deptDO1.getId())); | ||||
|         assertPojoEquals(deptDO2, deptCache.get(deptDO2.getId())); | ||||
|         // 断言 parentDeptCache 缓存 | ||||
|         Multimap<Long, DeptDO> parentDeptCache = deptService.getParentDeptCache(); | ||||
|         assertEquals(2, parentDeptCache.size()); | ||||
|         assertPojoEquals(deptDO1, parentDeptCache.get(deptDO1.getParentId())); | ||||
|         assertPojoEquals(deptDO2, parentDeptCache.get(deptDO2.getParentId())); | ||||
|         Long deptId = deptService.createDept(reqVO); | ||||
|         // 断言 | ||||
|         assertNotNull(deptId); | ||||
|         // 校验记录的属性是否正确 | ||||
|         DeptDO deptDO = deptMapper.selectById(deptId); | ||||
|         assertPojoEquals(reqVO, deptDO); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testListDepts() { | ||||
|     public void testUpdateDept() { | ||||
|         // mock 数据 | ||||
|         DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); | ||||
|         deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> { | ||||
|             // 设置更新的 ID | ||||
|             o.setParentId(DeptIdEnum.ROOT.getId()); | ||||
|             o.setId(dbDeptDO.getId()); | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|         }); | ||||
|  | ||||
|         // 调用 | ||||
|         deptService.updateDept(reqVO); | ||||
|         // 校验是否更新正确 | ||||
|         DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的 | ||||
|         assertPojoEquals(reqVO, deptDO); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteDept_success() { | ||||
|         // mock 数据 | ||||
|         DeptDO dbDeptDO = randomPojo(DeptDO.class); | ||||
|         deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         Long id = dbDeptDO.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         deptService.deleteDept(id); | ||||
|         // 校验数据不存在了 | ||||
|         assertNull(deptMapper.selectById(id)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteDept_exitsChildren() { | ||||
|         // mock 数据 | ||||
|         DeptDO parentDept = randomPojo(DeptDO.class); | ||||
|         deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         DeptDO childrenDeptDO = randomPojo(DeptDO.class, o -> { | ||||
|             o.setParentId(parentDept.getId()); | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|         }); | ||||
|         // 插入子部门 | ||||
|         deptMapper.insert(childrenDeptDO); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.deleteDept(parentDept.getId()), DEPT_EXITS_CHILDREN); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateDeptExists_notFound() { | ||||
|         // 准备参数 | ||||
|         Long id = randomLongId(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.validateDeptExists(id), DEPT_NOT_FOUND); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateParentDept_parentError() { | ||||
|         // 准备参数 | ||||
|         Long id = randomLongId(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.validateParentDept(id, id), | ||||
|                 DEPT_PARENT_ERROR); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateParentDept_parentIsChild() { | ||||
|         // mock 数据(父节点) | ||||
|         DeptDO parentDept = randomPojo(DeptDO.class); | ||||
|         deptMapper.insert(parentDept); | ||||
|         // mock 数据(子节点) | ||||
|         DeptDO childDept = randomPojo(DeptDO.class, o -> { | ||||
|             o.setParentId(parentDept.getId()); | ||||
|         }); | ||||
|         deptMapper.insert(childDept); | ||||
|  | ||||
|         // 准备参数 | ||||
|         Long id = parentDept.getId(); | ||||
|         Long parentId = childDept.getId(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.validateParentDept(id, parentId), DEPT_PARENT_IS_CHILD); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateNameUnique_duplicate() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO = randomPojo(DeptDO.class); | ||||
|         deptMapper.insert(deptDO); | ||||
|  | ||||
|         // 准备参数 | ||||
|         Long id = randomLongId(); | ||||
|         Long parentId = deptDO.getParentId(); | ||||
|         String name = deptDO.getName(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.validateDeptNameUnique(id, parentId, name), | ||||
|                 DEPT_NAME_DUPLICATE); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDept() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO = randomPojo(DeptDO.class); | ||||
|         deptMapper.insert(deptDO); | ||||
|         // 准备参数 | ||||
|         Long id = deptDO.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDO dbDept = deptService.getDept(id); | ||||
|         // 断言 | ||||
|         assertEquals(deptDO, dbDept); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDeptList_ids() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO01 = randomPojo(DeptDO.class); | ||||
|         deptMapper.insert(deptDO01); | ||||
|         DeptDO deptDO02 = randomPojo(DeptDO.class); | ||||
|         deptMapper.insert(deptDO02); | ||||
|         // 准备参数 | ||||
|         List<Long> ids = Arrays.asList(deptDO01.getId(), deptDO02.getId()); | ||||
|  | ||||
|         // 调用 | ||||
|         List<DeptDO> deptDOList = deptService.getDeptList(ids); | ||||
|         // 断言 | ||||
|         assertEquals(2, deptDOList.size()); | ||||
|         assertEquals(deptDO01, deptDOList.get(0)); | ||||
|         assertEquals(deptDO02, deptDOList.get(1)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDeptList_reqVO() { | ||||
|         // mock 数据 | ||||
|         DeptDO dept = randomPojo(DeptDO.class, o -> { // 等会查询到 | ||||
|             o.setName("开发部"); | ||||
| @@ -101,216 +216,55 @@ public class DeptServiceImplTest extends BaseDbUnitTest { | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateDept_success() { | ||||
|     public void testGetChildDeptList() { | ||||
|         // mock 数据(1 级别子节点) | ||||
|         DeptDO dept1 = randomPojo(DeptDO.class, o -> o.setName("1")); | ||||
|         deptMapper.insert(dept1); | ||||
|         DeptDO dept2 = randomPojo(DeptDO.class, o -> o.setName("2")); | ||||
|         deptMapper.insert(dept2); | ||||
|         // mock 数据(2 级子节点) | ||||
|         DeptDO dept1a = randomPojo(DeptDO.class, o -> o.setName("1-a").setParentId(dept1.getId())); | ||||
|         deptMapper.insert(dept1a); | ||||
|         DeptDO dept2a = randomPojo(DeptDO.class, o -> o.setName("2-a").setParentId(dept2.getId())); | ||||
|         deptMapper.insert(dept2a); | ||||
|         // 准备参数 | ||||
|         DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> { | ||||
|             o.setParentId(DeptIdEnum.ROOT.getId()); | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|         }); | ||||
|         Long id = dept1.getParentId(); | ||||
|  | ||||
|         // 调用 | ||||
|         Long deptId = deptService.createDept(reqVO); | ||||
|         List<DeptDO> result = deptService.getChildDeptList(id); | ||||
|         // 断言 | ||||
|         assertNotNull(deptId); | ||||
|         // 校验记录的属性是否正确 | ||||
|         DeptDO deptDO = deptMapper.selectById(deptId); | ||||
|         assertPojoEquals(reqVO, deptDO); | ||||
|         // 校验调用 | ||||
|         verify(deptProducer).sendDeptRefreshMessage(); | ||||
|         assertEquals(result.size(), 2); | ||||
|         assertPojoEquals(dept1, result.get(0)); | ||||
|         assertPojoEquals(dept1a, result.get(1)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateDept_success() { | ||||
|         // mock 数据 | ||||
|         DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); | ||||
|         deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 | ||||
|     public void testGetChildDeptListFromCache() { | ||||
|         // mock 数据(1 级别子节点) | ||||
|         DeptDO dept1 = randomPojo(DeptDO.class, o -> o.setName("1")); | ||||
|         deptMapper.insert(dept1); | ||||
|         DeptDO dept2 = randomPojo(DeptDO.class, o -> o.setName("2")); | ||||
|         deptMapper.insert(dept2); | ||||
|         // mock 数据(2 级子节点) | ||||
|         DeptDO dept1a = randomPojo(DeptDO.class, o -> o.setName("1-a").setParentId(dept1.getId())); | ||||
|         deptMapper.insert(dept1a); | ||||
|         DeptDO dept2a = randomPojo(DeptDO.class, o -> o.setName("2-a").setParentId(dept2.getId())); | ||||
|         deptMapper.insert(dept2a); | ||||
|         // 准备参数 | ||||
|         DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> { | ||||
|             // 设置更新的 ID | ||||
|             o.setParentId(DeptIdEnum.ROOT.getId()); | ||||
|             o.setId(dbDeptDO.getId()); | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|         }); | ||||
|         Long id = dept1.getParentId(); | ||||
|  | ||||
|         // 调用 | ||||
|         deptService.updateDept(reqVO); | ||||
|         // 校验是否更新正确 | ||||
|         DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的 | ||||
|         assertPojoEquals(reqVO, deptDO); | ||||
|         // 校验调用 | ||||
|         verify(deptProducer).sendDeptRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteDept_success() { | ||||
|         // mock 数据 | ||||
|         DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); | ||||
|         deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         Long id = dbDeptDO.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         deptService.deleteDept(id); | ||||
|         // 校验数据不存在了 | ||||
|         assertNull(deptMapper.selectById(id)); | ||||
|         // 校验调用 | ||||
|         verify(deptProducer).sendDeptRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateDept_nameDuplicateForUpdate() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO = randomDeptDO(); | ||||
|         // 设置根节点部门 | ||||
|         deptDO.setParentId(DeptIdEnum.ROOT.getId()); | ||||
|         deptMapper.insert(deptDO); | ||||
|         // mock 数据 稍后模拟重复它的 name | ||||
|         DeptDO nameDeptDO = randomDeptDO(); | ||||
|         // 设置根节点部门 | ||||
|         nameDeptDO.setParentId(DeptIdEnum.ROOT.getId()); | ||||
|         deptMapper.insert(nameDeptDO); | ||||
|         // 准备参数 | ||||
|         DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> { | ||||
|             // 设置根节点部门 | ||||
|             o.setParentId(DeptIdEnum.ROOT.getId()); | ||||
|             // 设置更新的 ID | ||||
|             o.setId(deptDO.getId()); | ||||
|             // 模拟 name 重复 | ||||
|             o.setName(nameDeptDO.getName()); | ||||
|         }); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.updateDept(reqVO), DEPT_NAME_DUPLICATE); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateDept_parentNotExitsForCreate() { | ||||
|         // 准备参数 | ||||
|         DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, | ||||
|             o -> o.setStatus(randomCommonStatus())); | ||||
|  | ||||
|         // 调用,并断言异常 | ||||
|         assertServiceException(() -> deptService.createDept(reqVO), DEPT_PARENT_NOT_EXITS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateDept_notFoundForDelete() { | ||||
|         // 准备参数 | ||||
|         Long id = randomLongId(); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.deleteDept(id), DEPT_NOT_FOUND); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|    public void testValidateDept_exitsChildrenForDelete() { | ||||
|         // mock 数据 | ||||
|         DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); | ||||
|         deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         DeptDO childrenDeptDO = randomPojo(DeptDO.class, o -> { | ||||
|             o.setParentId(parentDept.getId()); | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|         }); | ||||
|         // 插入子部门 | ||||
|         deptMapper.insert(childrenDeptDO); | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.deleteDept(parentDept.getId()), DEPT_EXITS_CHILDREN); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateDept_parentErrorForUpdate() { | ||||
|         // mock 数据 | ||||
|         DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); | ||||
|         deptMapper.insert(dbDeptDO); | ||||
|         // 准备参数 | ||||
|         DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> { | ||||
|             // 设置自己为父部门 | ||||
|             o.setParentId(dbDeptDO.getId()); | ||||
|             // 设置更新的 ID | ||||
|             o.setId(dbDeptDO.getId()); | ||||
|         }); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_ERROR); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateDept_notEnableForCreate() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); | ||||
|         deptMapper.insert(deptDO); | ||||
|         // 准备参数 | ||||
|         DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> { | ||||
|             // 设置未启用的部门为父部门 | ||||
|             o.setParentId(deptDO.getId()); | ||||
|         }); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.createDept(reqVO), DEPT_NOT_ENABLE); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCheckDept_parentIsChildForUpdate() { | ||||
|         // mock 数据 | ||||
|         DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         deptMapper.insert(parentDept); | ||||
|         DeptDO childDept = randomPojo(DeptDO.class, o -> { | ||||
|             o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|             o.setParentId(parentDept.getId()); | ||||
|         }); | ||||
|         deptMapper.insert(childDept); | ||||
|         // 初始化本地缓存 | ||||
|         deptService.initLocalCache(); | ||||
|  | ||||
|         // 准备参数 | ||||
|         DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> { | ||||
|             // 设置自己的子部门为父部门 | ||||
|             o.setParentId(childDept.getId()); | ||||
|             // 设置更新的 ID | ||||
|             o.setId(parentDept.getId()); | ||||
|         }); | ||||
|  | ||||
|         // 调用, 并断言异常 | ||||
|         assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_IS_CHILD); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDeptList() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO01 = randomDeptDO(); | ||||
|         deptMapper.insert(deptDO01); | ||||
|         DeptDO deptDO02 = randomDeptDO(); | ||||
|         deptMapper.insert(deptDO02); | ||||
|         // 准备参数 | ||||
|         List<Long> ids = Arrays.asList(deptDO01.getId(), deptDO02.getId()); | ||||
|  | ||||
|         // 调用 | ||||
|         List<DeptDO> deptDOList = deptService.getDeptList(ids); | ||||
|         Set<Long> result = deptService.getChildDeptIdListFromCache(id); | ||||
|         // 断言 | ||||
|         assertEquals(2, deptDOList.size()); | ||||
|         assertEquals(deptDO01, deptDOList.get(0)); | ||||
|         assertEquals(deptDO02, deptDOList.get(1)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDept() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO = randomDeptDO(); | ||||
|         deptMapper.insert(deptDO); | ||||
|         // 准备参数 | ||||
|         Long id = deptDO.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDO dbDept = deptService.getDept(id); | ||||
|         // 断言 | ||||
|         assertEquals(deptDO, dbDept); | ||||
|         assertEquals(result.size(), 2); | ||||
|         assertTrue(result.contains(dept1.getId())); | ||||
|         assertTrue(result.contains(dept1a.getId())); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateDeptList_success() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         DeptDO deptDO = randomPojo(DeptDO.class).setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         deptMapper.insert(deptDO); | ||||
|         // 准备参数 | ||||
|         List<Long> ids = singletonList(deptDO.getId()); | ||||
| @@ -331,7 +285,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest { | ||||
|     @Test | ||||
|     public void testValidateDeptList_notEnable() { | ||||
|         // mock 数据 | ||||
|         DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.DISABLE.getStatus()); | ||||
|         DeptDO deptDO = randomPojo(DeptDO.class).setStatus(CommonStatusEnum.DISABLE.getStatus()); | ||||
|         deptMapper.insert(deptDO); | ||||
|         // 准备参数 | ||||
|         List<Long> ids = singletonList(deptDO.getId()); | ||||
| @@ -340,12 +294,4 @@ public class DeptServiceImplTest extends BaseDbUnitTest { | ||||
|         assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_ENABLE, deptDO.getName()); | ||||
|     } | ||||
|  | ||||
|     @SafeVarargs | ||||
|     private static DeptDO randomDeptDO(Consumer<DeptDO>... consumers) { | ||||
|         Consumer<DeptDO> consumer = (o) -> { | ||||
|             o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()); // 保证 status 的范围 | ||||
|         }; | ||||
|         return randomPojo(DeptDO.class, ArrayUtils.append(consumer, consumers)); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -7,14 +7,12 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccou | ||||
| import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | ||||
| 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.Map; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | ||||
| @@ -23,14 +21,13 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS; | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| import static org.mockito.ArgumentMatchers.eq; | ||||
| import static org.mockito.Mockito.verify; | ||||
| import static org.mockito.Mockito.when; | ||||
|  | ||||
| /** | ||||
| * {@link MailAccountServiceImpl} 的单元测试类 | ||||
| * | ||||
| * @author 芋道源码 | ||||
| */ | ||||
|  * {@link MailAccountServiceImpl} 的单元测试类 | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| @Import(MailAccountServiceImpl.class) | ||||
| public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
| @@ -42,23 +39,6 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @MockBean | ||||
|     private MailTemplateService mailTemplateService; | ||||
|     @MockBean | ||||
|     private MailProducer mailProducer; | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCache() { | ||||
|         MailAccountDO accountDO1 = randomPojo(MailAccountDO.class); | ||||
|         mailAccountMapper.insert(accountDO1); | ||||
|         MailAccountDO accountDO02 = randomPojo(MailAccountDO.class); | ||||
|         mailAccountMapper.insert(accountDO02); | ||||
|  | ||||
|         // 调用 | ||||
|         mailAccountService.initLocalCache(); | ||||
|         // 断言 mailAccountCache 缓存 | ||||
|         Map<Long, MailAccountDO> mailAccountCache = mailAccountService.getMailAccountCache(); | ||||
|         assertPojoEquals(accountDO1, mailAccountCache.get(accountDO1.getId())); | ||||
|         assertPojoEquals(accountDO02, mailAccountCache.get(accountDO02.getId())); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateMailAccount_success() { | ||||
| @@ -72,7 +52,6 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验记录的属性是否正确 | ||||
|         MailAccountDO mailAccount = mailAccountMapper.selectById(mailAccountId); | ||||
|         assertPojoEquals(reqVO, mailAccount); | ||||
|         verify(mailProducer).sendMailAccountRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -91,7 +70,6 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验是否更新正确 | ||||
|         MailAccountDO mailAccount = mailAccountMapper.selectById(reqVO.getId()); // 获取最新的 | ||||
|         assertPojoEquals(reqVO, mailAccount); | ||||
|         verify(mailProducer).sendMailAccountRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -115,9 +93,8 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|         // 调用 | ||||
|         mailAccountService.deleteMailAccount(id); | ||||
|        // 校验数据不存在了 | ||||
|        assertNull(mailAccountMapper.selectById(id)); | ||||
|         verify(mailProducer).sendMailAccountRefreshMessage(); | ||||
|         // 校验数据不存在了 | ||||
|         assertNull(mailAccountMapper.selectById(id)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -125,7 +102,6 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|         // mock 数据 | ||||
|         MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); | ||||
|         mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | ||||
|         mailAccountService.initLocalCache(); | ||||
|         // 准备参数 | ||||
|         Long id = dbMailAccount.getId(); | ||||
|  | ||||
| @@ -146,27 +122,27 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailAccountPage() { | ||||
|        // mock 数据 | ||||
|        MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class, o -> { // 等会查询到 | ||||
|            o.setMail("768@qq.com"); | ||||
|            o.setUsername("yunai"); | ||||
|        }); | ||||
|        mailAccountMapper.insert(dbMailAccount); | ||||
|        // 测试 mail 不匹配 | ||||
|        mailAccountMapper.insert(cloneIgnoreId(dbMailAccount, o -> o.setMail("788@qq.com"))); | ||||
|        // 测试 username 不匹配 | ||||
|        mailAccountMapper.insert(cloneIgnoreId(dbMailAccount, o -> o.setUsername("tudou"))); | ||||
|        // 准备参数 | ||||
|        MailAccountPageReqVO reqVO = new MailAccountPageReqVO(); | ||||
|        reqVO.setMail("768"); | ||||
|        reqVO.setUsername("yu"); | ||||
|         // mock 数据 | ||||
|         MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class, o -> { // 等会查询到 | ||||
|             o.setMail("768@qq.com"); | ||||
|             o.setUsername("yunai"); | ||||
|         }); | ||||
|         mailAccountMapper.insert(dbMailAccount); | ||||
|         // 测试 mail 不匹配 | ||||
|         mailAccountMapper.insert(cloneIgnoreId(dbMailAccount, o -> o.setMail("788@qq.com"))); | ||||
|         // 测试 username 不匹配 | ||||
|         mailAccountMapper.insert(cloneIgnoreId(dbMailAccount, o -> o.setUsername("tudou"))); | ||||
|         // 准备参数 | ||||
|         MailAccountPageReqVO reqVO = new MailAccountPageReqVO(); | ||||
|         reqVO.setMail("768"); | ||||
|         reqVO.setUsername("yu"); | ||||
|  | ||||
|        // 调用 | ||||
|        PageResult<MailAccountDO> pageResult = mailAccountService.getMailAccountPage(reqVO); | ||||
|        // 断言 | ||||
|        assertEquals(1, pageResult.getTotal()); | ||||
|        assertEquals(1, pageResult.getList().size()); | ||||
|        assertPojoEquals(dbMailAccount, pageResult.getList().get(0)); | ||||
|         // 调用 | ||||
|         PageResult<MailAccountDO> pageResult = mailAccountService.getMailAccountPage(reqVO); | ||||
|         // 断言 | ||||
|         assertEquals(1, pageResult.getTotal()); | ||||
|         assertEquals(1, pageResult.getList().size()); | ||||
|         assertPojoEquals(dbMailAccount, pageResult.getList().get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|   | ||||
| @@ -8,9 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemp | ||||
| import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| import org.springframework.context.annotation.Import; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| @@ -27,13 +25,12 @@ 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.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS; | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| import static org.mockito.Mockito.verify; | ||||
|  | ||||
| /** | ||||
| * {@link MailTemplateServiceImpl} 的单元测试类 | ||||
| * | ||||
| * @author 芋道源码 | ||||
| */ | ||||
|  * {@link MailTemplateServiceImpl} 的单元测试类 | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| @Import(MailTemplateServiceImpl.class) | ||||
| public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
| @@ -43,24 +40,6 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|     @Resource | ||||
|     private MailTemplateMapper mailTemplateMapper; | ||||
|  | ||||
|     @MockBean | ||||
|     private MailProducer mailProducer; | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCache() { | ||||
|         MailTemplateDO templateDO01 = randomPojo(MailTemplateDO.class); | ||||
|         mailTemplateMapper.insert(templateDO01); | ||||
|         MailTemplateDO templateDO02 = randomPojo(MailTemplateDO.class); | ||||
|         mailTemplateMapper.insert(templateDO02); | ||||
|  | ||||
|         // 调用 | ||||
|         mailTemplateService.initLocalCache(); | ||||
|         // 断言 mailTemplateCache 缓存 | ||||
|         Map<String, MailTemplateDO> mailTemplateCache = mailTemplateService.getMailTemplateCache(); | ||||
|         assertPojoEquals(templateDO01, mailTemplateCache.get(templateDO01.getCode())); | ||||
|         assertPojoEquals(templateDO02, mailTemplateCache.get(templateDO02.getCode())); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateMailTemplate_success() { | ||||
|         // 准备参数 | ||||
| @@ -73,7 +52,6 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验记录的属性是否正确 | ||||
|         MailTemplateDO mailTemplate = mailTemplateMapper.selectById(mailTemplateId); | ||||
|         assertPojoEquals(reqVO, mailTemplate); | ||||
|         verify(mailProducer).sendMailTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -91,7 +69,6 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验是否更新正确 | ||||
|         MailTemplateDO mailTemplate = mailTemplateMapper.selectById(reqVO.getId()); // 获取最新的 | ||||
|         assertPojoEquals(reqVO, mailTemplate); | ||||
|         verify(mailProducer).sendMailTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -115,7 +92,6 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         mailTemplateService.deleteMailTemplate(id); | ||||
|         // 校验数据不存在了 | ||||
|         assertNull(mailTemplateMapper.selectById(id)); | ||||
|         verify(mailProducer).sendMailTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -129,39 +105,39 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailTemplatePage() { | ||||
|        // mock 数据 | ||||
|        MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class, o -> { // 等会查询到 | ||||
|            o.setName("源码"); | ||||
|            o.setCode("test_01"); | ||||
|            o.setAccountId(1L); | ||||
|            o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|            o.setCreateTime(buildTime(2023, 2, 3)); | ||||
|        }); | ||||
|        mailTemplateMapper.insert(dbMailTemplate); | ||||
|        // 测试 name 不匹配 | ||||
|        mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setName("芋道"))); | ||||
|        // 测试 code 不匹配 | ||||
|        mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setCode("test_02"))); | ||||
|        // 测试 accountId 不匹配 | ||||
|        mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setAccountId(2L))); | ||||
|        // 测试 status 不匹配 | ||||
|        mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|        // 测试 createTime 不匹配 | ||||
|        mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setCreateTime(buildTime(2023, 1, 5)))); | ||||
|        // 准备参数 | ||||
|        MailTemplatePageReqVO reqVO = new MailTemplatePageReqVO(); | ||||
|        reqVO.setName("源"); | ||||
|        reqVO.setCode("est_01"); | ||||
|        reqVO.setAccountId(1L); | ||||
|        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 5)); | ||||
|         // mock 数据 | ||||
|         MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class, o -> { // 等会查询到 | ||||
|             o.setName("源码"); | ||||
|             o.setCode("test_01"); | ||||
|             o.setAccountId(1L); | ||||
|             o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|             o.setCreateTime(buildTime(2023, 2, 3)); | ||||
|         }); | ||||
|         mailTemplateMapper.insert(dbMailTemplate); | ||||
|         // 测试 name 不匹配 | ||||
|         mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setName("芋道"))); | ||||
|         // 测试 code 不匹配 | ||||
|         mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setCode("test_02"))); | ||||
|         // 测试 accountId 不匹配 | ||||
|         mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setAccountId(2L))); | ||||
|         // 测试 status 不匹配 | ||||
|         mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|         // 测试 createTime 不匹配 | ||||
|         mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setCreateTime(buildTime(2023, 1, 5)))); | ||||
|         // 准备参数 | ||||
|         MailTemplatePageReqVO reqVO = new MailTemplatePageReqVO(); | ||||
|         reqVO.setName("源"); | ||||
|         reqVO.setCode("est_01"); | ||||
|         reqVO.setAccountId(1L); | ||||
|         reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 5)); | ||||
|  | ||||
|        // 调用 | ||||
|        PageResult<MailTemplateDO> pageResult = mailTemplateService.getMailTemplatePage(reqVO); | ||||
|        // 断言 | ||||
|        assertEquals(1, pageResult.getTotal()); | ||||
|        assertEquals(1, pageResult.getList().size()); | ||||
|        assertPojoEquals(dbMailTemplate, pageResult.getList().get(0)); | ||||
|         // 调用 | ||||
|         PageResult<MailTemplateDO> pageResult = mailTemplateService.getMailTemplatePage(reqVO); | ||||
|         // 断言 | ||||
|         assertEquals(1, pageResult.getTotal()); | ||||
|         assertEquals(1, pageResult.getList().size()); | ||||
|         assertPojoEquals(dbMailTemplate, pageResult.getList().get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -199,7 +175,6 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // mock 数据 | ||||
|         MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | ||||
|         mailTemplateMapper.insert(dbMailTemplate); | ||||
|         mailTemplateService.initLocalCache(); | ||||
|         // 准备参数 | ||||
|         String code = dbMailTemplate.getCode(); | ||||
|  | ||||
|   | ||||
| @@ -8,9 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.notify.vo.template.Notify | ||||
| import cn.iocoder.yudao.module.system.controller.admin.notify.vo.template.NotifyTemplateUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyTemplateMapper; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.notify.NotifyProducer; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| import org.springframework.context.annotation.Import; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| @@ -25,13 +23,12 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTIFY_TEMPLATE_NOT_EXISTS; | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| import static org.mockito.Mockito.verify; | ||||
|  | ||||
| /** | ||||
| * {@link NotifyTemplateServiceImpl} 的单元测试类 | ||||
| * | ||||
| * @author 芋道源码 | ||||
| */ | ||||
|  * {@link NotifyTemplateServiceImpl} 的单元测试类 | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| @Import(NotifyTemplateServiceImpl.class) | ||||
| public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
| @@ -41,9 +38,6 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|     @Resource | ||||
|     private NotifyTemplateMapper notifyTemplateMapper; | ||||
|  | ||||
|     @MockBean | ||||
|     private NotifyProducer notifyProducer; | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateNotifyTemplate_success() { | ||||
|         // 准备参数 | ||||
| @@ -57,7 +51,6 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验记录的属性是否正确 | ||||
|         NotifyTemplateDO notifyTemplate = notifyTemplateMapper.selectById(notifyTemplateId); | ||||
|         assertPojoEquals(reqVO, notifyTemplate); | ||||
|         verify(notifyProducer).sendNotifyTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -76,7 +69,6 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验是否更新正确 | ||||
|         NotifyTemplateDO notifyTemplate = notifyTemplateMapper.selectById(reqVO.getId()); // 获取最新的 | ||||
|         assertPojoEquals(reqVO, notifyTemplate); | ||||
|         verify(notifyProducer).sendNotifyTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -98,9 +90,8 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|         // 调用 | ||||
|         notifyTemplateService.deleteNotifyTemplate(id); | ||||
|        // 校验数据不存在了 | ||||
|        assertNull(notifyTemplateMapper.selectById(id)); | ||||
|        verify(notifyProducer).sendNotifyTemplateRefreshMessage(); | ||||
|         // 校验数据不存在了 | ||||
|         assertNull(notifyTemplateMapper.selectById(id)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -114,35 +105,35 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Test | ||||
|     public void testGetNotifyTemplatePage() { | ||||
|        // mock 数据 | ||||
|        NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class, o -> { // 等会查询到 | ||||
|            o.setName("芋头"); | ||||
|            o.setCode("test_01"); | ||||
|            o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|            o.setCreateTime(buildTime(2022, 2, 3)); | ||||
|        }); | ||||
|        notifyTemplateMapper.insert(dbNotifyTemplate); | ||||
|        // 测试 name 不匹配 | ||||
|        notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setName("投"))); | ||||
|        // 测试 code 不匹配 | ||||
|        notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setCode("test_02"))); | ||||
|        // 测试 status 不匹配 | ||||
|        notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|        // 测试 createTime 不匹配 | ||||
|        notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setCreateTime(buildTime(2022, 1, 5)))); | ||||
|        // 准备参数 | ||||
|        NotifyTemplatePageReqVO reqVO = new NotifyTemplatePageReqVO(); | ||||
|        reqVO.setName("芋"); | ||||
|        reqVO.setCode("est_01"); | ||||
|        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|        reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 5)); | ||||
|         // mock 数据 | ||||
|         NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class, o -> { // 等会查询到 | ||||
|             o.setName("芋头"); | ||||
|             o.setCode("test_01"); | ||||
|             o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|             o.setCreateTime(buildTime(2022, 2, 3)); | ||||
|         }); | ||||
|         notifyTemplateMapper.insert(dbNotifyTemplate); | ||||
|         // 测试 name 不匹配 | ||||
|         notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setName("投"))); | ||||
|         // 测试 code 不匹配 | ||||
|         notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setCode("test_02"))); | ||||
|         // 测试 status 不匹配 | ||||
|         notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|         // 测试 createTime 不匹配 | ||||
|         notifyTemplateMapper.insert(cloneIgnoreId(dbNotifyTemplate, o -> o.setCreateTime(buildTime(2022, 1, 5)))); | ||||
|         // 准备参数 | ||||
|         NotifyTemplatePageReqVO reqVO = new NotifyTemplatePageReqVO(); | ||||
|         reqVO.setName("芋"); | ||||
|         reqVO.setCode("est_01"); | ||||
|         reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 5)); | ||||
|  | ||||
|        // 调用 | ||||
|        PageResult<NotifyTemplateDO> pageResult = notifyTemplateService.getNotifyTemplatePage(reqVO); | ||||
|        // 断言 | ||||
|        assertEquals(1, pageResult.getTotal()); | ||||
|        assertEquals(1, pageResult.getList().size()); | ||||
|        assertPojoEquals(dbNotifyTemplate, pageResult.getList().get(0)); | ||||
|         // 调用 | ||||
|         PageResult<NotifyTemplateDO> pageResult = notifyTemplateService.getNotifyTemplatePage(reqVO); | ||||
|         // 断言 | ||||
|         assertEquals(1, pageResult.getTotal()); | ||||
|         assertEquals(1, pageResult.getList().size()); | ||||
|         assertPojoEquals(dbNotifyTemplate, pageResult.getList().get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -164,7 +155,6 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // mock 数据 | ||||
|         NotifyTemplateDO dbNotifyTemplate = randomPojo(NotifyTemplateDO.class); | ||||
|         notifyTemplateMapper.insert(dbNotifyTemplate); | ||||
|         notifyTemplateService.initLocalCache(); | ||||
|         // 准备参数 | ||||
|         String code = dbNotifyTemplate.getCode(); | ||||
|  | ||||
| @@ -173,7 +163,7 @@ public class NotifyTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // 断言 | ||||
|         assertPojoEquals(dbNotifyTemplate, notifyTemplate); | ||||
|     } | ||||
|      | ||||
|  | ||||
|     @Test | ||||
|     public void testFormatNotifyTemplateContent() { | ||||
|         // 准备参数 | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| package cn.iocoder.yudao.module.system.service.oauth2; | ||||
|  | ||||
| import cn.hutool.core.map.MapUtil; | ||||
| import cn.hutool.extra.spring.SpringUtil; | ||||
| 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; | ||||
| @@ -9,14 +9,12 @@ import cn.iocoder.yudao.module.system.controller.admin.oauth2.vo.client.OAuth2Cl | ||||
| import cn.iocoder.yudao.module.system.controller.admin.oauth2.vo.client.OAuth2ClientUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2ClientDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.oauth2.OAuth2ClientMapper; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.auth.OAuth2ClientProducer; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| import org.mockito.MockedStatic; | ||||
| import org.springframework.context.annotation.Import; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import java.util.Collections; | ||||
| import java.util.Map; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | ||||
| @@ -24,13 +22,14 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | ||||
| 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; | ||||
| import static org.mockito.ArgumentMatchers.eq; | ||||
| import static org.mockito.Mockito.mockStatic; | ||||
|  | ||||
| /** | ||||
| * {@link OAuth2ClientServiceImpl} 的单元测试类 | ||||
| * | ||||
| * @author 芋道源码 | ||||
| */ | ||||
|  * {@link OAuth2ClientServiceImpl} 的单元测试类 | ||||
|  * | ||||
|  * @author 芋道源码 | ||||
|  */ | ||||
| @Import(OAuth2ClientServiceImpl.class) | ||||
| public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
| @@ -40,26 +39,6 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { | ||||
|     @Resource | ||||
|     private OAuth2ClientMapper oauth2ClientMapper; | ||||
|  | ||||
|     @MockBean | ||||
|     private OAuth2ClientProducer oauth2ClientProducer; | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCache() { | ||||
|         // mock 数据 | ||||
|         OAuth2ClientDO clientDO1 = randomPojo(OAuth2ClientDO.class); | ||||
|         oauth2ClientMapper.insert(clientDO1); | ||||
|         OAuth2ClientDO clientDO2 = randomPojo(OAuth2ClientDO.class); | ||||
|         oauth2ClientMapper.insert(clientDO2); | ||||
|  | ||||
|         // 调用 | ||||
|         oauth2ClientService.initLocalCache(); | ||||
|         // 断言 clientCache 缓存 | ||||
|         Map<String, OAuth2ClientDO> clientCache = oauth2ClientService.getClientCache(); | ||||
|         assertEquals(2, clientCache.size()); | ||||
|         assertPojoEquals(clientDO1, clientCache.get(clientDO1.getClientId())); | ||||
|         assertPojoEquals(clientDO2, clientCache.get(clientDO2.getClientId())); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateOAuth2Client_success() { | ||||
|         // 准备参数 | ||||
| @@ -73,7 +52,6 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验记录的属性是否正确 | ||||
|         OAuth2ClientDO oAuth2Client = oauth2ClientMapper.selectById(oauth2ClientId); | ||||
|         assertPojoEquals(reqVO, oAuth2Client); | ||||
|         verify(oauth2ClientProducer).sendOAuth2ClientRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -92,7 +70,6 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验是否更新正确 | ||||
|         OAuth2ClientDO oAuth2Client = oauth2ClientMapper.selectById(reqVO.getId()); // 获取最新的 | ||||
|         assertPojoEquals(reqVO, oAuth2Client); | ||||
|         verify(oauth2ClientProducer).sendOAuth2ClientRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -116,7 +93,6 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { | ||||
|         oauth2ClientService.deleteOAuth2Client(id); | ||||
|         // 校验数据不存在了 | ||||
|         assertNull(oauth2ClientMapper.selectById(id)); | ||||
|         verify(oauth2ClientProducer).sendOAuth2ClientRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -167,62 +143,78 @@ public class OAuth2ClientServiceImplTest extends BaseDbUnitTest { | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetOAuth2ClientPage() { | ||||
|        // mock 数据 | ||||
|        OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class, o -> { // 等会查询到 | ||||
|            o.setName("潜龙"); | ||||
|            o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|        }); | ||||
|        oauth2ClientMapper.insert(dbOAuth2Client); | ||||
|        // 测试 name 不匹配 | ||||
|        oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setName("凤凰"))); | ||||
|        // 测试 status 不匹配 | ||||
|        oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|        // 准备参数 | ||||
|        OAuth2ClientPageReqVO reqVO = new OAuth2ClientPageReqVO(); | ||||
|        reqVO.setName("龙"); | ||||
|        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|     public void testGetOAuth2ClientFromCache() { | ||||
|         // mock 数据 | ||||
|         OAuth2ClientDO clientDO = randomPojo(OAuth2ClientDO.class); | ||||
|         oauth2ClientMapper.insert(clientDO); | ||||
|         // 准备参数 | ||||
|         String clientId = clientDO.getClientId(); | ||||
|  | ||||
|        // 调用 | ||||
|        PageResult<OAuth2ClientDO> pageResult = oauth2ClientService.getOAuth2ClientPage(reqVO); | ||||
|        // 断言 | ||||
|        assertEquals(1, pageResult.getTotal()); | ||||
|        assertEquals(1, pageResult.getList().size()); | ||||
|        assertPojoEquals(dbOAuth2Client, pageResult.getList().get(0)); | ||||
|         // 调用,并断言 | ||||
|         OAuth2ClientDO dbClientDO = oauth2ClientService.getOAuth2ClientFromCache(clientId); | ||||
|         assertPojoEquals(clientDO, dbClientDO); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetOAuth2ClientPage() { | ||||
|         // mock 数据 | ||||
|         OAuth2ClientDO dbOAuth2Client = randomPojo(OAuth2ClientDO.class, o -> { // 等会查询到 | ||||
|             o.setName("潜龙"); | ||||
|             o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         }); | ||||
|         oauth2ClientMapper.insert(dbOAuth2Client); | ||||
|         // 测试 name 不匹配 | ||||
|         oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setName("凤凰"))); | ||||
|         // 测试 status 不匹配 | ||||
|         oauth2ClientMapper.insert(cloneIgnoreId(dbOAuth2Client, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|         // 准备参数 | ||||
|         OAuth2ClientPageReqVO reqVO = new OAuth2ClientPageReqVO(); | ||||
|         reqVO.setName("龙"); | ||||
|         reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|  | ||||
|         // 调用 | ||||
|         PageResult<OAuth2ClientDO> pageResult = oauth2ClientService.getOAuth2ClientPage(reqVO); | ||||
|         // 断言 | ||||
|         assertEquals(1, pageResult.getTotal()); | ||||
|         assertEquals(1, pageResult.getList().size()); | ||||
|         assertPojoEquals(dbOAuth2Client, pageResult.getList().get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidOAuthClientFromCache() { | ||||
|         // mock 方法 | ||||
|         OAuth2ClientDO client = randomPojo(OAuth2ClientDO.class).setClientId("default") | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         OAuth2ClientDO client02 = randomPojo(OAuth2ClientDO.class).setClientId("disable") | ||||
|                 .setStatus(CommonStatusEnum.DISABLE.getStatus()); | ||||
|         Map<String, OAuth2ClientDO> clientCache = MapUtil.<String, OAuth2ClientDO>builder() | ||||
|                 .put(client.getClientId(), client) | ||||
|                 .put(client02.getClientId(), client02).build(); | ||||
|         oauth2ClientService.setClientCache(clientCache); | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(OAuth2ClientServiceImpl.class))) | ||||
|                     .thenReturn(oauth2ClientService); | ||||
|  | ||||
|         // 调用,并断言 | ||||
|         assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache(randomString(), | ||||
|                 null, null, null, null), OAUTH2_CLIENT_NOT_EXISTS); | ||||
|         assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("disable", | ||||
|                 null, null, null, null), OAUTH2_CLIENT_DISABLE); | ||||
|         assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", | ||||
|                 randomString(), null, null, null), OAUTH2_CLIENT_CLIENT_SECRET_ERROR); | ||||
|         assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", | ||||
|                 null, randomString(), null, null), OAUTH2_CLIENT_AUTHORIZED_GRANT_TYPE_NOT_EXISTS); | ||||
|         assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", | ||||
|                 null, null, Collections.singleton(randomString()), null), OAUTH2_CLIENT_SCOPE_OVER); | ||||
|         assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", | ||||
|                 null, null, null, "test"), OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH, "test"); | ||||
|         // 成功调用(1:参数完整) | ||||
|         OAuth2ClientDO result = oauth2ClientService.validOAuthClientFromCache(client.getClientId(), client.getSecret(), | ||||
|                 client.getAuthorizedGrantTypes().get(0), client.getScopes(), client.getRedirectUris().get(0)); | ||||
|         assertPojoEquals(client, result); | ||||
|         // 成功调用(2:只有 clientId 参数) | ||||
|         result = oauth2ClientService.validOAuthClientFromCache(client.getClientId()); | ||||
|         assertPojoEquals(client, result); | ||||
|             // mock 方法 | ||||
|             OAuth2ClientDO client = randomPojo(OAuth2ClientDO.class).setClientId("default") | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|             oauth2ClientMapper.insert(client); | ||||
|             OAuth2ClientDO client02 = randomPojo(OAuth2ClientDO.class).setClientId("disable") | ||||
|                     .setStatus(CommonStatusEnum.DISABLE.getStatus()); | ||||
|             oauth2ClientMapper.insert(client02); | ||||
|  | ||||
|             // 调用,并断言 | ||||
|             assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache(randomString(), | ||||
|                     null, null, null, null), OAUTH2_CLIENT_NOT_EXISTS); | ||||
|             assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("disable", | ||||
|                     null, null, null, null), OAUTH2_CLIENT_DISABLE); | ||||
|             assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", | ||||
|                     randomString(), null, null, null), OAUTH2_CLIENT_CLIENT_SECRET_ERROR); | ||||
|             assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", | ||||
|                     null, randomString(), null, null), OAUTH2_CLIENT_AUTHORIZED_GRANT_TYPE_NOT_EXISTS); | ||||
|             assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", | ||||
|                     null, null, Collections.singleton(randomString()), null), OAUTH2_CLIENT_SCOPE_OVER); | ||||
|             assertServiceException(() -> oauth2ClientService.validOAuthClientFromCache("default", | ||||
|                     null, null, null, "test"), OAUTH2_CLIENT_REDIRECT_URI_NOT_MATCH, "test"); | ||||
|             // 成功调用(1:参数完整) | ||||
|             OAuth2ClientDO result = oauth2ClientService.validOAuthClientFromCache(client.getClientId(), client.getSecret(), | ||||
|                     client.getAuthorizedGrantTypes().get(0), client.getScopes(), client.getRedirectUris().get(0)); | ||||
|             assertPojoEquals(client, result); | ||||
|             // 成功调用(2:只有 clientId 参数) | ||||
|             result = oauth2ClientService.validOAuthClientFromCache(client.getClientId()); | ||||
|             assertPojoEquals(client, result); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -8,10 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuUp | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.permission.MenuMapper; | ||||
| import cn.iocoder.yudao.module.system.enums.permission.MenuTypeEnum; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.permission.MenuProducer; | ||||
| import cn.iocoder.yudao.module.system.service.tenant.TenantService; | ||||
| import com.google.common.collect.LinkedListMultimap; | ||||
| 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; | ||||
| @@ -26,8 +23,6 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||||
| import static cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO.ID_ROOT; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||||
| import static java.util.Arrays.asList; | ||||
| import static java.util.Collections.singletonList; | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||||
| import static org.mockito.ArgumentMatchers.argThat; | ||||
| @@ -46,35 +41,12 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|     @MockBean | ||||
|     private PermissionService permissionService; | ||||
|     @MockBean | ||||
|     private MenuProducer menuProducer; | ||||
|     @MockBean | ||||
|     private TenantService tenantService; | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCache_success() { | ||||
|         MenuDO menuDO1 = randomPojo(MenuDO.class); | ||||
|         menuMapper.insert(menuDO1); | ||||
|         MenuDO menuDO2 = randomPojo(MenuDO.class); | ||||
|         menuMapper.insert(menuDO2); | ||||
|  | ||||
|         // 调用 | ||||
|         menuService.initLocalCache(); | ||||
|         // 校验 menuCache 缓存 | ||||
|         Map<Long, MenuDO> menuCache = menuService.getMenuCache(); | ||||
|         assertEquals(2, menuCache.size()); | ||||
|         assertPojoEquals(menuDO1, menuCache.get(menuDO1.getId())); | ||||
|         assertPojoEquals(menuDO2, menuCache.get(menuDO2.getId())); | ||||
|         // 校验 permissionMenuCache 缓存 | ||||
|         Multimap<String, MenuDO> permissionMenuCache = menuService.getPermissionMenuCache(); | ||||
|         assertEquals(2, permissionMenuCache.size()); | ||||
|         assertPojoEquals(menuDO1, permissionMenuCache.get(menuDO1.getPermission())); | ||||
|         assertPojoEquals(menuDO2, permissionMenuCache.get(menuDO2.getPermission())); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateMenu_success() { | ||||
|         // mock 数据(构造父菜单) | ||||
|         MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, | ||||
|         MenuDO menuDO = buildMenuDO(MenuTypeEnum.MENU, | ||||
|                 "parent", 0L); | ||||
|         menuMapper.insert(menuDO); | ||||
|         Long parentId = menuDO.getId(); | ||||
| @@ -89,14 +61,12 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验记录的属性是否正确 | ||||
|         MenuDO dbMenu = menuMapper.selectById(menuId); | ||||
|         assertPojoEquals(reqVO, dbMenu); | ||||
|         // 校验调用 | ||||
|         verify(menuProducer).sendMenuRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateMenu_success() { | ||||
|         // mock 数据(构造父子菜单) | ||||
|         MenuDO sonMenuDO = initParentAndSonMenu(); | ||||
|         MenuDO sonMenuDO = createParentAndSonMenu(); | ||||
|         Long sonId = sonMenuDO.getId(); | ||||
|         // 准备参数 | ||||
|         MenuUpdateReqVO reqVO = randomPojo(MenuUpdateReqVO.class, o -> { | ||||
| @@ -111,8 +81,6 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验记录的属性是否正确 | ||||
|         MenuDO dbMenu = menuMapper.selectById(sonId); | ||||
|         assertPojoEquals(reqVO, dbMenu); | ||||
|         // 校验调用 | ||||
|         verify(menuProducer).sendMenuRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -137,7 +105,6 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|         MenuDO dbMenuDO = menuMapper.selectById(id); | ||||
|         assertNull(dbMenuDO); | ||||
|         verify(permissionService).processMenuDeleted(id); | ||||
|         verify(menuProducer).sendMenuRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -149,7 +116,7 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|     @Test | ||||
|     public void testDeleteMenu_existChildren() { | ||||
|         // mock 数据(构造父子菜单) | ||||
|         MenuDO sonMenu = initParentAndSonMenu(); | ||||
|         MenuDO sonMenu = createParentAndSonMenu(); | ||||
|         // 准备参数 | ||||
|         Long parentId = sonMenu.getParentId(); | ||||
|  | ||||
| @@ -218,85 +185,6 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|         assertPojoEquals(menu100, result.get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testListMenusFromCache_withoutId() { | ||||
|         // mock 缓存 | ||||
|         Map<Long, MenuDO> menuCache = new HashMap<>(); | ||||
|         // 可被匹配 | ||||
|         MenuDO menuDO = randomPojo(MenuDO.class, o -> o.setId(1L) | ||||
|                 .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         menuCache.put(menuDO.getId(), menuDO); | ||||
|         // 测试 type 不匹配 | ||||
|         menuCache.put(3L, randomPojo(MenuDO.class, o -> o.setId(3L) | ||||
|                 .setType(MenuTypeEnum.BUTTON.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus()))); | ||||
|         // 测试 status 不匹配 | ||||
|         menuCache.put(4L, randomPojo(MenuDO.class, o -> o.setId(4L) | ||||
|                 .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|         menuService.setMenuCache(menuCache); | ||||
|         // 准备参数 | ||||
|         Collection<Integer> menuTypes = singletonList(MenuTypeEnum.MENU.getType()); | ||||
|         Collection<Integer> menusStatuses = singletonList(CommonStatusEnum.ENABLE.getStatus()); | ||||
|  | ||||
|         // 调用 | ||||
|         List<MenuDO> list = menuService.getMenuListFromCache(menuTypes, menusStatuses); | ||||
|         // 断言 | ||||
|         assertEquals(1, list.size()); | ||||
|         assertPojoEquals(menuDO, list.get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testListMenusFromCache_withId() { | ||||
|         // mock 缓存 | ||||
|         Map<Long, MenuDO> menuCache = new HashMap<>(); | ||||
|         // 可被匹配 | ||||
|         MenuDO menuDO = randomPojo(MenuDO.class, o -> o.setId(1L) | ||||
|                 .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         menuCache.put(menuDO.getId(), menuDO); | ||||
|         // 测试 id 不匹配 | ||||
|         menuCache.put(2L, randomPojo(MenuDO.class, o -> o.setId(2L) | ||||
|                 .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus()))); | ||||
|         // 测试 type 不匹配 | ||||
|         menuCache.put(3L, randomPojo(MenuDO.class, o -> o.setId(3L) | ||||
|                 .setType(MenuTypeEnum.BUTTON.getType()).setStatus(CommonStatusEnum.ENABLE.getStatus()))); | ||||
|         // 测试 status 不匹配 | ||||
|         menuCache.put(4L, randomPojo(MenuDO.class, o -> o.setId(4L) | ||||
|                 .setType(MenuTypeEnum.MENU.getType()).setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|         menuService.setMenuCache(menuCache); | ||||
|         // 准备参数 | ||||
|         Collection<Long> menuIds = asList(1L, 3L, 4L); | ||||
|         Collection<Integer> menuTypes = singletonList(MenuTypeEnum.MENU.getType()); | ||||
|         Collection<Integer> menusStatuses = singletonList(CommonStatusEnum.ENABLE.getStatus()); | ||||
|  | ||||
|         // 调用 | ||||
|         List<MenuDO> list = menuService.getMenuListFromCache(menuIds, menuTypes, menusStatuses); | ||||
|         // 断言 | ||||
|         assertEquals(1, list.size()); | ||||
|         assertPojoEquals(menuDO, list.get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMenuListByPermissionFromCache() { | ||||
|         // mock 缓存 | ||||
|         Multimap<String, MenuDO> permissionMenuCache = LinkedListMultimap.create(); | ||||
|         // 可被匹配 | ||||
|         MenuDO menuDO01 = randomPojo(MenuDO.class, o -> o.setId(1L).setPermission("123")); | ||||
|         permissionMenuCache.put(menuDO01.getPermission(), menuDO01); | ||||
|         MenuDO menuDO02 = randomPojo(MenuDO.class, o -> o.setId(2L).setPermission("123")); | ||||
|         permissionMenuCache.put(menuDO02.getPermission(), menuDO02); | ||||
|         // 不可匹配 | ||||
|         permissionMenuCache.put("456", randomPojo(MenuDO.class, o -> o.setId(3L).setPermission("456"))); | ||||
|         menuService.setPermissionMenuCache(permissionMenuCache); | ||||
|         // 准备参数 | ||||
|         String permission = "123"; | ||||
|  | ||||
|         // 调用 | ||||
|         List<MenuDO> list = menuService.getMenuListByPermissionFromCache(permission); | ||||
|         // 断言 | ||||
|         assertEquals(2, list.size()); | ||||
|         assertPojoEquals(menuDO01, list.get(0)); | ||||
|         assertPojoEquals(menuDO02, list.get(1)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMenu() { | ||||
|         // mock 数据 | ||||
| @@ -314,7 +202,7 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|     @Test | ||||
|     public void testValidateParentMenu_success() { | ||||
|         // mock 数据 | ||||
|         MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | ||||
|         MenuDO menuDO = buildMenuDO(MenuTypeEnum.MENU, "parent", 0L); | ||||
|         menuMapper.insert(menuDO); | ||||
|         // 准备参数 | ||||
|         Long parentId = menuDO.getId(); | ||||
| @@ -340,7 +228,7 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|     @Test | ||||
|     public void testValidateParentMenu_parentTypeError() { | ||||
|         // mock 数据 | ||||
|         MenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); | ||||
|         MenuDO menuDO = buildMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); | ||||
|         menuMapper.insert(menuDO); | ||||
|         // 准备参数 | ||||
|         Long parentId = menuDO.getId(); | ||||
| @@ -353,7 +241,7 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|     @Test | ||||
|     public void testValidateMenu_success() { | ||||
|         // mock 父子菜单 | ||||
|         MenuDO sonMenu = initParentAndSonMenu(); | ||||
|         MenuDO sonMenu = createParentAndSonMenu(); | ||||
|         // 准备参数 | ||||
|         Long parentId = sonMenu.getParentId(); | ||||
|         Long otherSonMenuId = randomLongId(); | ||||
| @@ -366,7 +254,7 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|     @Test | ||||
|     public void testValidateMenu_sonMenuNameDuplicate() { | ||||
|         // mock 父子菜单 | ||||
|         MenuDO sonMenu = initParentAndSonMenu(); | ||||
|         MenuDO sonMenu = createParentAndSonMenu(); | ||||
|         // 准备参数 | ||||
|         Long parentId = sonMenu.getParentId(); | ||||
|         Long otherSonMenuId = randomLongId(); | ||||
| @@ -380,26 +268,26 @@ public class MenuServiceImplTest extends BaseDbUnitTest { | ||||
|     // ====================== 初始化方法 ====================== | ||||
|  | ||||
|     /** | ||||
|      * 构造父子菜单,返回子菜单 | ||||
|      * 插入父子菜单,返回子菜单 | ||||
|      * | ||||
|      * @return 子菜单 | ||||
|      */ | ||||
|     private MenuDO initParentAndSonMenu() { | ||||
|     private MenuDO createParentAndSonMenu() { | ||||
|         // 构造父子菜单 | ||||
|         MenuDO parentMenuDO = createMenuDO(MenuTypeEnum.MENU, "parent", ID_ROOT); | ||||
|         MenuDO parentMenuDO = buildMenuDO(MenuTypeEnum.MENU, "parent", ID_ROOT); | ||||
|         menuMapper.insert(parentMenuDO); | ||||
|         // 构建子菜单 | ||||
|         MenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "testSonName", | ||||
|         MenuDO sonMenuDO = buildMenuDO(MenuTypeEnum.MENU, "testSonName", | ||||
|                 parentMenuDO.getParentId()); | ||||
|         menuMapper.insert(sonMenuDO); | ||||
|         return sonMenuDO; | ||||
|     } | ||||
|  | ||||
|     private MenuDO createMenuDO(MenuTypeEnum type, String name, Long parentId) { | ||||
|         return createMenuDO(type, name, parentId, randomCommonStatus()); | ||||
|     private MenuDO buildMenuDO(MenuTypeEnum type, String name, Long parentId) { | ||||
|         return buildMenuDO(type, name, parentId, randomCommonStatus()); | ||||
|     } | ||||
|  | ||||
|     private MenuDO createMenuDO(MenuTypeEnum type, String name, Long parentId, Integer status) { | ||||
|     private MenuDO buildMenuDO(MenuTypeEnum type, String name, Long parentId, Integer status) { | ||||
|         return randomPojo(MenuDO.class, o -> o.setId(null).setName(name).setParentId(parentId) | ||||
|                 .setType(type.getType()).setStatus(status)); | ||||
|     } | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| package cn.iocoder.yudao.module.system.service.permission; | ||||
|  | ||||
| import cn.hutool.core.collection.CollUtil; | ||||
| import cn.hutool.core.map.MapUtil; | ||||
| import cn.hutool.extra.spring.SpringUtil; | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||||
| import cn.iocoder.yudao.module.system.api.permission.dto.DeptDataPermissionRespDTO; | ||||
| @@ -14,32 +14,28 @@ import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.permission.RoleMenuMapper; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.permission.UserRoleMapper; | ||||
| 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.mockito.MockedStatic; | ||||
| 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.collection.ListUtil.toList; | ||||
| 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.*; | ||||
| import static java.util.Arrays.asList; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | ||||
| 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; | ||||
| import static org.mockito.Mockito.*; | ||||
|  | ||||
| @Import({PermissionServiceImpl.class}) | ||||
| public class PermissionServiceTest extends BaseDbUnitTest { | ||||
| @@ -61,131 +57,74 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|     @MockBean | ||||
|     private AdminUserService userService; | ||||
|  | ||||
|     @MockBean | ||||
|     private PermissionProducer permissionProducer; | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCacheForRoleMenu() { | ||||
|         // 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); | ||||
|     public void testHasAnyPermissions_superAdmin() { | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|         // 调用 | ||||
|         permissionService.initLocalCacheForRoleMenu(); | ||||
|         // 断言 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)); | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             String[] roles = new String[]{"system:user:query", "system:user:create"}; | ||||
|             // mock 用户登录的角色 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(100L)); | ||||
|             RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L) | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(singleton(100L)))).thenReturn(toList(role)); | ||||
|             // mock 其它方法 | ||||
|             when(roleService.hasAnySuperAdmin(eq(asSet(100L)))).thenReturn(true); | ||||
|  | ||||
|             // 调用,并断言 | ||||
|             assertTrue(permissionService.hasAnyPermissions(userId, roles)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCacheForUserRole() { | ||||
|         // 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); | ||||
|     public void testHasAnyPermissions_normal() { | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|         // 调用 | ||||
|         permissionService.initLocalCacheForUserRole(); | ||||
|         // 断言 roleMenuCache 缓存 | ||||
|         assertEquals(1, permissionService.getUserRoleCache().size()); | ||||
|         assertEquals(asSet(10L, 20L), permissionService.getUserRoleCache().get(1L)); | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             String[] roles = new String[]{"system:user:query", "system:user:create"}; | ||||
|             // mock 用户登录的角色 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(100L)); | ||||
|             RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L) | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(singleton(100L)))).thenReturn(toList(role)); | ||||
|             // mock 菜单 | ||||
|             Long menuId = 1000L; | ||||
|             when(menuService.getMenuIdListByPermissionFromCache( | ||||
|                     eq("system:user:create"))).thenReturn(singletonList(menuId)); | ||||
|             roleMenuMapper.insert(randomPojo(RoleMenuDO.class).setRoleId(100L).setMenuId(1000L)); | ||||
|  | ||||
|             // 调用,并断言 | ||||
|             assertTrue(permissionService.hasAnyPermissions(userId, roles)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @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.getRoleListFromCache(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); | ||||
|     public void testHasAnyRoles() { | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|         // 调用 | ||||
|         List<MenuDO> result = permissionService.getRoleMenuListFromCache(roleIds, menuTypes, menusStatuses); | ||||
|         // 断言 | ||||
|         assertSame(menuList, result); | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             String[] roles = new String[]{"yunai", "tudou"}; | ||||
|             // mock 用户与角色的缓存 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(100L)); | ||||
|             RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L).setCode("tudou") | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(singleton(100L)))).thenReturn(toList(role)); | ||||
|  | ||||
|             // 调用,并断言 | ||||
|             assertTrue(permissionService.hasAnyRoles(userId, roles)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @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.getMenuList()).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() { | ||||
| @@ -207,75 +146,6 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         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 | ||||
| @@ -303,9 +173,6 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         List<UserRoleDO> dbUserRoles = userRoleMapper.selectList(); | ||||
|         assertEquals(1, dbUserRoles.size()); | ||||
|         assertPojoEquals(dbUserRoles.get(0), userRoleDO02); | ||||
|         // 断言调用 | ||||
|         verify(permissionProducer).sendRoleMenuRefreshMessage(); | ||||
|         verify(permissionProducer).sendUserRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -324,8 +191,77 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|         List<RoleMenuDO> dbRoleMenus = roleMenuMapper.selectList(); | ||||
|         assertEquals(1, dbRoleMenus.size()); | ||||
|         assertPojoEquals(dbRoleMenus.get(0), roleMenuDO02); | ||||
|         // 断言调用 | ||||
|         verify(permissionProducer).sendRoleMenuRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @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.getMenuList()).thenReturn(menuList); | ||||
|  | ||||
|         // 调用 | ||||
|         Set<Long> menuIds = permissionService.getRoleMenuListByRoleId(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.getRoleMenuListByRoleId(roleId); | ||||
|         // 断言 | ||||
|         assertEquals(asSet(1L, 2L), menuIds); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMenuRoleIdListByMenuIdFromCache() { | ||||
|         // 准备参数 | ||||
|         Long menuId = 1L; | ||||
|         // mock 数据 | ||||
|         RoleMenuDO roleMenu01 = randomPojo(RoleMenuDO.class).setRoleId(100L).setMenuId(1L); | ||||
|         roleMenuMapper.insert(roleMenu01); | ||||
|         RoleMenuDO roleMenu02 = randomPojo(RoleMenuDO.class).setRoleId(200L).setMenuId(1L); | ||||
|         roleMenuMapper.insert(roleMenu02); | ||||
|  | ||||
|         // 调用 | ||||
|         Set<Long> roleIds = permissionService.getMenuRoleIdListByMenuIdFromCache(menuId); | ||||
|         // 断言 | ||||
|         assertEquals(asSet(100L, 200L), roleIds); | ||||
|     } | ||||
|  | ||||
|     // ========== 用户-角色的相关方法  ========== | ||||
|  | ||||
|     @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()); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -344,202 +280,248 @@ 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 testHasAnyPermissions_superAdmin() { | ||||
|     public void testGetUserRoleIdListByUserId() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         String[] roles = new String[]{"system:user:query", "system:user:create"}; | ||||
|         // mock 用户与角色的缓存 | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(100L)).build()); | ||||
|         RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L) | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         when(roleService.getRoleFromCache(eq(100L))).thenReturn(role); | ||||
|         // mock 其它方法 | ||||
|         when(roleService.hasAnySuperAdmin(eq(asSet(100L)))).thenReturn(true); | ||||
|         // 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); | ||||
|  | ||||
|         // 调用 | ||||
|         boolean has = permissionService.hasAnyPermissions(userId, roles); | ||||
|         Set<Long> result = permissionService.getUserRoleIdListByUserId(userId); | ||||
|         // 断言 | ||||
|         assertTrue(has); | ||||
|         assertEquals(asSet(10L, 20L), result); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testHasAnyPermissions_normal() { | ||||
|     public void testGetUserRoleIdListByUserIdFromCache() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         String[] roles = new String[]{"system:user:query", "system:user:create"}; | ||||
|         // mock 用户与角色的缓存 | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(100L)).build()); | ||||
|         RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L) | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         when(roleService.getRoleFromCache(eq(100L))).thenReturn(role); | ||||
|         // mock 其它方法 | ||||
|         MenuDO menu = randomPojo(MenuDO.class, o -> o.setId(1000L)); | ||||
|         when(menuService.getMenuListByPermissionFromCache(eq("system:user:create"))).thenReturn(singletonList(menu)); | ||||
|         permissionService.setMenuRoleCache(ImmutableMultimap.<Long, Long>builder().put(1000L, 100L).build()); | ||||
|  | ||||
|         // 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); | ||||
|  | ||||
|         // 调用 | ||||
|         boolean has = permissionService.hasAnyPermissions(userId, roles); | ||||
|         Set<Long> result = permissionService.getUserRoleIdListByUserIdFromCache(userId); | ||||
|         // 断言 | ||||
|         assertTrue(has); | ||||
|         assertEquals(asSet(10L, 20L), result); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testHasAnyRoles_superAdmin() { | ||||
|     public void testGetUserRoleIdsFromCache() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         String[] roles = new String[]{"yunai", "tudou"}; | ||||
|         // mock 用户与角色的缓存 | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(100L)).build()); | ||||
|         RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L) | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         when(roleService.getRoleFromCache(eq(100L))).thenReturn(role); | ||||
|         // mock 其它方法 | ||||
|         when(roleService.hasAnySuperAdmin(eq(asSet(100L)))).thenReturn(true); | ||||
|         // 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); | ||||
|  | ||||
|         // 调用 | ||||
|         boolean has = permissionService.hasAnyRoles(userId, roles); | ||||
|         Set<Long> result = permissionService.getUserRoleIdListByUserIdFromCache(userId); | ||||
|         // 断言 | ||||
|         assertTrue(has); | ||||
|         assertEquals(asSet(10L, 20L), result); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testHasAnyRoles_normal() { | ||||
|     public void testGetUserRoleIdListByRoleId() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         String[] roles = new String[]{"yunai", "tudou"}; | ||||
|         // mock 用户与角色的缓存 | ||||
|         permissionService.setUserRoleCache(MapUtil.<Long, Set<Long>>builder().put(1L, asSet(100L)).build()); | ||||
|         RoleDO role = randomPojo(RoleDO.class, o -> o.setId(100L).setCode("yunai") | ||||
|                 .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         when(roleService.getRoleFromCache(eq(100L))).thenReturn(role); | ||||
|         // mock 其它方法 | ||||
|         when(roleService.getRoleListFromCache(eq(asSet(100L)))).thenReturn(singletonList(role)); | ||||
|         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); | ||||
|  | ||||
|         // 调用 | ||||
|         boolean has = permissionService.hasAnyRoles(userId, roles); | ||||
|         Set<Long> result = permissionService.getUserRoleIdListByRoleId(roleIds); | ||||
|         // 断言 | ||||
|         assertTrue(has); | ||||
|         assertEquals(asSet(1L, 2L), result); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetEnableUserRoleListByUserIdFromCache() { | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             // mock 用户登录的角色 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(100L)); | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(200L)); | ||||
|             RoleDO role01 = randomPojo(RoleDO.class, o -> o.setId(100L) | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             RoleDO role02 = randomPojo(RoleDO.class, o -> o.setId(200L) | ||||
|                     .setStatus(CommonStatusEnum.DISABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(asSet(100L, 200L)))) | ||||
|                     .thenReturn(toList(role01, role02)); | ||||
|  | ||||
|             // 调用 | ||||
|             List<RoleDO> result = permissionService.getEnableUserRoleListByUserIdFromCache(userId); | ||||
|             // 断言 | ||||
|             assertEquals(1, result.size()); | ||||
|             assertPojoEquals(role01, result.get(0)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     // ========== 用户-部门的相关方法  ========== | ||||
|  | ||||
|     @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 testGetDeptDataPermission_All() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         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())); | ||||
|         when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | ||||
|         when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|         // 断言 | ||||
|         assertTrue(result.getAll()); | ||||
|         assertFalse(result.getSelf()); | ||||
|         assertTrue(CollUtil.isEmpty(result.getDeptIds())); | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             // mock 用户的角色编号 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L)); | ||||
|             // mock 获得用户的角色 | ||||
|             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.ALL.getScope()) | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO)); | ||||
|  | ||||
|             // 调用 | ||||
|             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|             // 断言 | ||||
|             assertTrue(result.getAll()); | ||||
|             assertFalse(result.getSelf()); | ||||
|             assertTrue(CollUtil.isEmpty(result.getDeptIds())); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDeptDataPermission_DeptCustom() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         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())); | ||||
|         when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | ||||
|         when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); | ||||
|         // mock 部门的返回 | ||||
|         when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|         // 断言 | ||||
|         assertFalse(result.getAll()); | ||||
|         assertFalse(result.getSelf()); | ||||
|         assertEquals(roleDO.getDataScopeDeptIds().size() + 1, result.getDeptIds().size()); | ||||
|         assertTrue(CollUtil.containsAll(result.getDeptIds(), roleDO.getDataScopeDeptIds())); | ||||
|         assertTrue(CollUtil.contains(result.getDeptIds(), 3L)); | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             // mock 用户的角色编号 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L)); | ||||
|             // mock 获得用户的角色 | ||||
|             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope()) | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO)); | ||||
|             // mock 部门的返回 | ||||
|             when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), | ||||
|                     null, null); // 最后返回 null 的目的,看看会不会重复调用 | ||||
|  | ||||
|             // 调用 | ||||
|             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|             // 断言 | ||||
|             assertFalse(result.getAll()); | ||||
|             assertFalse(result.getSelf()); | ||||
|             assertEquals(roleDO.getDataScopeDeptIds().size() + 1, result.getDeptIds().size()); | ||||
|             assertTrue(CollUtil.containsAll(result.getDeptIds(), roleDO.getDataScopeDeptIds())); | ||||
|             assertTrue(CollUtil.contains(result.getDeptIds(), 3L)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDeptDataPermission_DeptOnly() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         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())); | ||||
|         when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | ||||
|         when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); | ||||
|         // mock 部门的返回 | ||||
|         when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|         // 断言 | ||||
|         assertFalse(result.getAll()); | ||||
|         assertFalse(result.getSelf()); | ||||
|         assertEquals(1, result.getDeptIds().size()); | ||||
|         assertTrue(CollUtil.contains(result.getDeptIds(), 3L)); | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             // mock 用户的角色编号 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L)); | ||||
|             // mock 获得用户的角色 | ||||
|             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_ONLY.getScope()) | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO)); | ||||
|             // mock 部门的返回 | ||||
|             when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), | ||||
|                     null, null); // 最后返回 null 的目的,看看会不会重复调用 | ||||
|  | ||||
|             // 调用 | ||||
|             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|             // 断言 | ||||
|             assertFalse(result.getAll()); | ||||
|             assertFalse(result.getSelf()); | ||||
|             assertEquals(1, result.getDeptIds().size()); | ||||
|             assertTrue(CollUtil.contains(result.getDeptIds(), 3L)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDeptDataPermission_DeptAndChild() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         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())); | ||||
|         when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | ||||
|         when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); | ||||
|         // mock 部门的返回 | ||||
|         when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), null, null); // 最后返回 null 的目的,看看会不会重复调用 | ||||
|         // mock 方法(部门) | ||||
|         DeptDO deptDO = randomPojo(DeptDO.class); | ||||
|         when(deptService.getDeptListByParentIdFromCache(eq(3L), eq(true))) | ||||
|                 .thenReturn(singletonList(deptDO)); | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|         // 断言 | ||||
|         assertFalse(result.getAll()); | ||||
|         assertFalse(result.getSelf()); | ||||
|         assertEquals(2, result.getDeptIds().size()); | ||||
|         assertTrue(CollUtil.contains(result.getDeptIds(), deptDO.getId())); | ||||
|         assertTrue(CollUtil.contains(result.getDeptIds(), 3L)); | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             // mock 用户的角色编号 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L)); | ||||
|             // mock 获得用户的角色 | ||||
|             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope()) | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO)); | ||||
|             // mock 部门的返回 | ||||
|             when(userService.getUser(eq(1L))).thenReturn(new AdminUserDO().setDeptId(3L), | ||||
|                     null, null); // 最后返回 null 的目的,看看会不会重复调用 | ||||
|             // mock 方法(部门) | ||||
|             DeptDO deptDO = randomPojo(DeptDO.class); | ||||
|             when(deptService.getChildDeptIdListFromCache(eq(3L))).thenReturn(singleton(deptDO.getId())); | ||||
|  | ||||
|             // 调用 | ||||
|             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|             // 断言 | ||||
|             assertFalse(result.getAll()); | ||||
|             assertFalse(result.getSelf()); | ||||
|             assertEquals(2, result.getDeptIds().size()); | ||||
|             assertTrue(CollUtil.contains(result.getDeptIds(), deptDO.getId())); | ||||
|             assertTrue(CollUtil.contains(result.getDeptIds(), 3L)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetDeptDataPermission_Self() { | ||||
|         // 准备参数 | ||||
|         Long userId = 1L; | ||||
|         // mock 用户的角色编号 | ||||
|         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())); | ||||
|         when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(singletonList(roleDO)); | ||||
|         when(roleService.getRoleFromCache(eq(2L))).thenReturn(roleDO); | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PermissionServiceImpl.class))) | ||||
|                     .thenReturn(permissionService); | ||||
|  | ||||
|         // 调用 | ||||
|         DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|         // 断言 | ||||
|         assertFalse(result.getAll()); | ||||
|         assertTrue(result.getSelf()); | ||||
|         assertTrue(CollUtil.isEmpty(result.getDeptIds())); | ||||
|             // 准备参数 | ||||
|             Long userId = 1L; | ||||
|             // mock 用户的角色编号 | ||||
|             userRoleMapper.insert(randomPojo(UserRoleDO.class).setUserId(userId).setRoleId(2L)); | ||||
|             // mock 获得用户的角色 | ||||
|             RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setDataScope(DataScopeEnum.SELF.getScope()) | ||||
|                     .setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             when(roleService.getRoleListFromCache(eq(singleton(2L)))).thenReturn(toList(roleDO)); | ||||
|  | ||||
|             // 调用 | ||||
|             DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(userId); | ||||
|             // 断言 | ||||
|             assertFalse(result.getAll()); | ||||
|             assertTrue(result.getSelf()); | ||||
|             assertTrue(CollUtil.isEmpty(result.getDeptIds())); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package cn.iocoder.yudao.module.system.service.permission; | ||||
|  | ||||
| import cn.hutool.extra.spring.SpringUtil; | ||||
| 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; | ||||
| @@ -11,15 +12,14 @@ 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.mockito.MockedStatic; | ||||
| 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; | ||||
| @@ -33,6 +33,8 @@ 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.ArgumentMatchers.eq; | ||||
| import static org.mockito.Mockito.mockStatic; | ||||
| import static org.mockito.Mockito.verify; | ||||
|  | ||||
| @Import(RoleServiceImpl.class) | ||||
| @@ -46,26 +48,9 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @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() { | ||||
|     public void testCreateRole() { | ||||
|         // 准备参数 | ||||
|         RoleCreateReqVO reqVO = randomPojo(RoleCreateReqVO.class); | ||||
|  | ||||
| @@ -77,12 +62,10 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|         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() { | ||||
|     public void testUpdateRole() { | ||||
|         // mock 数据 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | ||||
|         roleMapper.insert(roleDO); | ||||
| @@ -95,12 +78,10 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|         // 断言 | ||||
|         RoleDO newRoleDO = roleMapper.selectById(id); | ||||
|         assertPojoEquals(reqVO, newRoleDO); | ||||
|         // verify 发送刷新消息 | ||||
|         verify(roleProducer).sendRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateRoleStatus_success() { | ||||
|     public void testUpdateRoleStatus() { | ||||
|         // mock 数据 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()) | ||||
|                 .setType(RoleTypeEnum.CUSTOM.getType())); | ||||
| @@ -114,12 +95,10 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|         // 断言 | ||||
|         RoleDO dbRoleDO = roleMapper.selectById(roleId); | ||||
|         assertEquals(CommonStatusEnum.DISABLE.getStatus(), dbRoleDO.getStatus()); | ||||
|         // verify 发送刷新消息 | ||||
|         verify(roleProducer).sendRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateRoleDataScope_success() { | ||||
|     public void testUpdateRoleDataScope() { | ||||
|         // mock 数据 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | ||||
|         roleMapper.insert(roleDO); | ||||
| @@ -134,12 +113,10 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|         RoleDO dbRoleDO = roleMapper.selectById(id); | ||||
|         assertEquals(dataScope, dbRoleDO.getDataScope()); | ||||
|         assertEquals(dataScopeRoleIds, dbRoleDO.getDataScopeDeptIds()); | ||||
|         // verify 发送刷新消息 | ||||
|         verify(roleProducer).sendRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteRole_success() { | ||||
|     public void testDeleteRole() { | ||||
|         // mock 数据 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setType(RoleTypeEnum.CUSTOM.getType())); | ||||
|         roleMapper.insert(roleDO); | ||||
| @@ -152,23 +129,65 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|         assertNull(roleMapper.selectById(id)); | ||||
|         // verify 删除相关数据 | ||||
|         verify(permissionService).processRoleDeleted(id); | ||||
|         // verify 发送刷新消息 | ||||
|         verify(roleProducer).sendRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetRoleFromCache() { | ||||
|         // mock 数据(缓存) | ||||
|     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); | ||||
|         roleService.initLocalCache(); | ||||
|         // 参数准备 | ||||
|         // 准备参数 | ||||
|         Long id = roleDO.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         RoleDO dbRoleDO = roleService.getRoleFromCache(id); | ||||
|         // 断言 | ||||
|         assertPojoEquals(roleDO, dbRoleDO); | ||||
|         // 调用,无异常 | ||||
|         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 | ||||
| @@ -186,22 +205,21 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|     } | ||||
|  | ||||
|     @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()))); | ||||
|     public void testGetRoleFromCache() { | ||||
|         // mock 数据(缓存) | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class); | ||||
|         roleMapper.insert(roleDO); | ||||
|         // 参数准备 | ||||
|         Long id = roleDO.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         List<RoleDO> list = roleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         RoleDO dbRoleDO = roleService.getRoleFromCache(id); | ||||
|         // 断言 | ||||
|         assertEquals(1, list.size()); | ||||
|         assertPojoEquals(dbRole, list.get(0)); | ||||
|         assertPojoEquals(roleDO, dbRoleDO); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetRoleListByStatus_statusEmpty() { | ||||
|     public void testGetRoleListByStatus() { | ||||
|         // mock 数据 | ||||
|         RoleDO dbRole01 = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         roleMapper.insert(dbRole01); | ||||
| @@ -209,29 +227,33 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|         roleMapper.insert(dbRole02); | ||||
|  | ||||
|         // 调用 | ||||
|         List<RoleDO> list = roleService.getRoleListByStatus(null); | ||||
|         List<RoleDO> list = roleService.getRoleListByStatus( | ||||
|                 singleton(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         // 断言 | ||||
|         assertEquals(2, list.size()); | ||||
|         assertEquals(1, 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()); | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(RoleServiceImpl.class))) | ||||
|                     .thenReturn(roleService); | ||||
|  | ||||
|         // 调用 | ||||
|         List<RoleDO> list = roleService.getRoleListFromCache(ids); | ||||
|         // 断言 | ||||
|         assertEquals(1, list.size()); | ||||
|         assertPojoEquals(dbRole, list.get(0)); | ||||
|             // mock 数据 | ||||
|             RoleDO dbRole = randomPojo(RoleDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|             roleMapper.insert(dbRole); | ||||
|             // 测试 id 不匹配 | ||||
|             roleMapper.insert(cloneIgnoreId(dbRole, o -> {})); | ||||
|             // 准备参数 | ||||
|             Collection<Long> ids = singleton(dbRole.getId()); | ||||
|  | ||||
|             // 调用 | ||||
|             List<RoleDO> list = roleService.getRoleListFromCache(ids); | ||||
|             // 断言 | ||||
|             assertEquals(1, list.size()); | ||||
|             assertPojoEquals(dbRole, list.get(0)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -296,72 +318,37 @@ public class RoleServiceImplTest extends BaseDbUnitTest { | ||||
|     } | ||||
|  | ||||
|     @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"))))); | ||||
|     public void testHasAnySuperAdmin_true() { | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(RoleServiceImpl.class))) | ||||
|                     .thenReturn(roleService); | ||||
|  | ||||
|             // mock 数据 | ||||
|             RoleDO dbRole = randomPojo(RoleDO.class).setCode("super_admin"); | ||||
|             roleMapper.insert(dbRole); | ||||
|             // 准备参数 | ||||
|             Long id = dbRole.getId(); | ||||
|  | ||||
|             // 调用,并调用 | ||||
|             assertTrue(roleService.hasAnySuperAdmin(singletonList(id))); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateRoleDuplicate_success() { | ||||
|         // 调用,不会抛异常 | ||||
|         roleService.validateRoleDuplicate(randomString(), randomString(), null); | ||||
|     } | ||||
|     public void testHasAnySuperAdmin_false() { | ||||
|         try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) { | ||||
|             springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(RoleServiceImpl.class))) | ||||
|                     .thenReturn(roleService); | ||||
|  | ||||
|     @Test | ||||
|     public void testValidateRoleDuplicate_nameDuplicate() { | ||||
|         // mock 数据 | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> o.setName("role_name")); | ||||
|         roleMapper.insert(roleDO); | ||||
|         // 准备参数 | ||||
|         String name = "role_name"; | ||||
|             // mock 数据 | ||||
|             RoleDO dbRole = randomPojo(RoleDO.class).setCode("tenant_admin"); | ||||
|             roleMapper.insert(dbRole); | ||||
|             // 准备参数 | ||||
|             Long id = dbRole.getId(); | ||||
|  | ||||
|         // 调用,并断言异常 | ||||
|         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); | ||||
|             // 调用,并调用 | ||||
|             assertFalse(roleService.hasAnySuperAdmin(singletonList(id))); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|   | ||||
| @@ -18,7 +18,6 @@ import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsTemplateDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsTemplateMapper; | ||||
| import cn.iocoder.yudao.module.system.enums.sms.SmsTemplateTypeEnum; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.sms.SmsProducer; | ||||
| import com.google.common.collect.Lists; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| @@ -26,7 +25,6 @@ import org.springframework.context.annotation.Import; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.function.Consumer; | ||||
|  | ||||
| import static cn.hutool.core.util.RandomUtil.randomEle; | ||||
| @@ -55,25 +53,6 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|     private SmsClientFactory smsClientFactory; | ||||
|     @MockBean | ||||
|     private SmsClient smsClient; | ||||
|     @MockBean | ||||
|     private SmsProducer smsProducer; | ||||
|  | ||||
|     @Test | ||||
|     void testInitLocalCache() { | ||||
|         // mock 数据 | ||||
|         SmsTemplateDO smsTemplate01 = randomSmsTemplateDO(); | ||||
|         smsTemplateMapper.insert(smsTemplate01); | ||||
|         SmsTemplateDO smsTemplate02 = randomSmsTemplateDO(); | ||||
|         smsTemplateMapper.insert(smsTemplate02); | ||||
|  | ||||
|         // 调用 | ||||
|         smsTemplateService.initLocalCache(); | ||||
|         // 断言 deptCache 缓存 | ||||
|         Map<String, SmsTemplateDO> smsTemplateCache = smsTemplateService.getSmsTemplateCache(); | ||||
|         assertEquals(2, smsTemplateCache.size()); | ||||
|         assertPojoEquals(smsTemplate01, smsTemplateCache.get(smsTemplate01.getCode())); | ||||
|         assertPojoEquals(smsTemplate02, smsTemplateCache.get(smsTemplate02.getCode())); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testParseTemplateContentParams() { | ||||
| @@ -116,8 +95,6 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         assertPojoEquals(reqVO, smsTemplate); | ||||
|         assertEquals(Lists.newArrayList("operation", "code"), smsTemplate.getParams()); | ||||
|         assertEquals(channelDO.getCode(), smsTemplate.getChannelCode()); | ||||
|         // 校验调用 | ||||
|         verify(smsProducer, times(1)).sendSmsTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -151,8 +128,6 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         assertPojoEquals(reqVO, smsTemplate); | ||||
|         assertEquals(Lists.newArrayList("operation", "code"), smsTemplate.getParams()); | ||||
|         assertEquals(channelDO.getCode(), smsTemplate.getChannelCode()); | ||||
|         // 校验调用 | ||||
|         verify(smsProducer, times(1)).sendSmsTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -174,10 +149,8 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|         // 调用 | ||||
|         smsTemplateService.deleteSmsTemplate(id); | ||||
|        // 校验数据不存在了 | ||||
|        assertNull(smsTemplateMapper.selectById(id)); | ||||
|         // 校验调用 | ||||
|         verify(smsProducer, times(1)).sendSmsTemplateRefreshMessage(); | ||||
|         // 校验数据不存在了 | ||||
|         assertNull(smsTemplateMapper.selectById(id)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -191,47 +164,47 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Test | ||||
|     public void testGetSmsTemplatePage() { | ||||
|        // mock 数据 | ||||
|        SmsTemplateDO dbSmsTemplate = randomPojo(SmsTemplateDO.class, o -> { // 等会查询到 | ||||
|            o.setType(SmsTemplateTypeEnum.PROMOTION.getType()); | ||||
|            o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|            o.setCode("tudou"); | ||||
|            o.setContent("芋道源码"); | ||||
|            o.setApiTemplateId("yunai"); | ||||
|            o.setChannelId(1L); | ||||
|            o.setCreateTime(buildTime(2021, 11, 11)); | ||||
|        }); | ||||
|        smsTemplateMapper.insert(dbSmsTemplate); | ||||
|        // 测试 type 不匹配 | ||||
|        smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setType(SmsTemplateTypeEnum.VERIFICATION_CODE.getType()))); | ||||
|        // 测试 status 不匹配 | ||||
|        smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|        // 测试 code 不匹配 | ||||
|        smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setCode("yuanma"))); | ||||
|        // 测试 content 不匹配 | ||||
|        smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setContent("源码"))); | ||||
|        // 测试 apiTemplateId 不匹配 | ||||
|        smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setApiTemplateId("nai"))); | ||||
|        // 测试 channelId 不匹配 | ||||
|        smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setChannelId(2L))); | ||||
|        // 测试 createTime 不匹配 | ||||
|        smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setCreateTime(buildTime(2021, 12, 12)))); | ||||
|        // 准备参数 | ||||
|        SmsTemplatePageReqVO reqVO = new SmsTemplatePageReqVO(); | ||||
|        reqVO.setType(SmsTemplateTypeEnum.PROMOTION.getType()); | ||||
|        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|        reqVO.setCode("tu"); | ||||
|        reqVO.setContent("芋道"); | ||||
|        reqVO.setApiTemplateId("yu"); | ||||
|        reqVO.setChannelId(1L); | ||||
|        reqVO.setCreateTime(buildBetweenTime(2021, 11, 1, 2021, 12, 1)); | ||||
|         // mock 数据 | ||||
|         SmsTemplateDO dbSmsTemplate = randomPojo(SmsTemplateDO.class, o -> { // 等会查询到 | ||||
|             o.setType(SmsTemplateTypeEnum.PROMOTION.getType()); | ||||
|             o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|             o.setCode("tudou"); | ||||
|             o.setContent("芋道源码"); | ||||
|             o.setApiTemplateId("yunai"); | ||||
|             o.setChannelId(1L); | ||||
|             o.setCreateTime(buildTime(2021, 11, 11)); | ||||
|         }); | ||||
|         smsTemplateMapper.insert(dbSmsTemplate); | ||||
|         // 测试 type 不匹配 | ||||
|         smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setType(SmsTemplateTypeEnum.VERIFICATION_CODE.getType()))); | ||||
|         // 测试 status 不匹配 | ||||
|         smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); | ||||
|         // 测试 code 不匹配 | ||||
|         smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setCode("yuanma"))); | ||||
|         // 测试 content 不匹配 | ||||
|         smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setContent("源码"))); | ||||
|         // 测试 apiTemplateId 不匹配 | ||||
|         smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setApiTemplateId("nai"))); | ||||
|         // 测试 channelId 不匹配 | ||||
|         smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setChannelId(2L))); | ||||
|         // 测试 createTime 不匹配 | ||||
|         smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setCreateTime(buildTime(2021, 12, 12)))); | ||||
|         // 准备参数 | ||||
|         SmsTemplatePageReqVO reqVO = new SmsTemplatePageReqVO(); | ||||
|         reqVO.setType(SmsTemplateTypeEnum.PROMOTION.getType()); | ||||
|         reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|         reqVO.setCode("tu"); | ||||
|         reqVO.setContent("芋道"); | ||||
|         reqVO.setApiTemplateId("yu"); | ||||
|         reqVO.setChannelId(1L); | ||||
|         reqVO.setCreateTime(buildBetweenTime(2021, 11, 1, 2021, 12, 1)); | ||||
|  | ||||
|        // 调用 | ||||
|        PageResult<SmsTemplateDO> pageResult = smsTemplateService.getSmsTemplatePage(reqVO); | ||||
|        // 断言 | ||||
|        assertEquals(1, pageResult.getTotal()); | ||||
|        assertEquals(1, pageResult.getList().size()); | ||||
|        assertPojoEquals(dbSmsTemplate, pageResult.getList().get(0)); | ||||
|         // 调用 | ||||
|         PageResult<SmsTemplateDO> pageResult = smsTemplateService.getSmsTemplatePage(reqVO); | ||||
|         // 断言 | ||||
|         assertEquals(1, pageResult.getTotal()); | ||||
|         assertEquals(1, pageResult.getList().size()); | ||||
|         assertPojoEquals(dbSmsTemplate, pageResult.getList().get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -271,11 +244,11 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         reqVO.setChannelId(1L); | ||||
|         reqVO.setCreateTime(buildBetweenTime(2021, 11, 1, 2021, 12, 1)); | ||||
|  | ||||
|        // 调用 | ||||
|        List<SmsTemplateDO> list = smsTemplateService.getSmsTemplateList(reqVO); | ||||
|        // 断言 | ||||
|        assertEquals(1, list.size()); | ||||
|        assertPojoEquals(dbSmsTemplate, list.get(0)); | ||||
|         // 调用 | ||||
|         List<SmsTemplateDO> list = smsTemplateService.getSmsTemplateList(reqVO); | ||||
|         // 断言 | ||||
|         assertEquals(1, list.size()); | ||||
|         assertPojoEquals(dbSmsTemplate, list.get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|   | ||||
| @@ -199,7 +199,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest { | ||||
|         role101.setTenantId(dbTenant.getId()); | ||||
|         when(roleService.getRoleListByStatus(isNull())).thenReturn(asList(role100, role101)); | ||||
|         // mock 每个角色的权限 | ||||
|         when(permissionService.getRoleMenuIds(eq(101L))).thenReturn(asSet(201L, 202L)); | ||||
|         when(permissionService.getRoleMenuListByRoleId(eq(101L))).thenReturn(asSet(201L, 202L)); | ||||
|  | ||||
|         // 调用 | ||||
|         tenantService.updateTenant(reqVO); | ||||
| @@ -454,7 +454,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest { | ||||
|         TenantContextHolder.setTenantId(dbTenant.getId()); | ||||
|         // mock 菜单 | ||||
|         when(menuService.getMenuList()).thenReturn(Arrays.asList(randomPojo(MenuDO.class, o -> o.setId(100L)), | ||||
|                         randomPojo(MenuDO.class, o -> o.setId(101L)))); | ||||
|                 randomPojo(MenuDO.class, o -> o.setId(101L)))); | ||||
|  | ||||
|         // 调用 | ||||
|         tenantService.handleTenantMenu(handler); | ||||
|   | ||||
| @@ -345,7 +345,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { | ||||
|         reqVO.setDeptId(1L); // 其中,1L 是 2L 的父部门 | ||||
|         // mock 方法 | ||||
|         List<DeptDO> deptList = newArrayList(randomPojo(DeptDO.class, o -> o.setId(2L))); | ||||
|         when(deptService.getDeptListByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList); | ||||
|         when(deptService.getChildDeptList(eq(reqVO.getDeptId()))).thenReturn(deptList); | ||||
|  | ||||
|         // 调用 | ||||
|         PageResult<AdminUserDO> pageResult = userService.getUserPage(reqVO); | ||||
| @@ -368,7 +368,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { | ||||
|         reqVO.setDeptId(1L); // 其中,1L 是 2L 的父部门 | ||||
|         // mock 方法 | ||||
|         List<DeptDO> deptList = newArrayList(randomPojo(DeptDO.class, o -> o.setId(2L))); | ||||
|         when(deptService.getDeptListByParentIdFromCache(eq(reqVO.getDeptId()), eq(true))).thenReturn(deptList); | ||||
|         when(deptService.getChildDeptList(eq(reqVO.getDeptId()))).thenReturn(deptList); | ||||
|  | ||||
|         // 调用 | ||||
|         List<AdminUserDO> list = userService.getUserList(reqVO); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 lwf
					lwf