mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	修复所有单元测试
This commit is contained in:
		| @@ -3,6 +3,8 @@ package cn.iocoder.yudao.module.system.service.permission; | ||||
| import cn.hutool.core.bean.BeanUtil; | ||||
| import cn.hutool.core.lang.Assert; | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | ||||
| import cn.iocoder.yudao.framework.common.util.spring.SpringAopUtils; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuUpdateReqVO; | ||||
| @@ -10,8 +12,7 @@ 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.framework.common.util.spring.SpringAopUtils; | ||||
| import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; | ||||
| import cn.iocoder.yudao.module.system.service.tenant.TenantService; | ||||
| import cn.iocoder.yudao.module.system.test.BaseDbUnitTest; | ||||
| import com.google.common.collect.Multimap; | ||||
| import org.junit.jupiter.api.Test; | ||||
| @@ -21,28 +22,33 @@ import org.springframework.context.annotation.Import; | ||||
| import javax.annotation.Resource; | ||||
| import java.util.*; | ||||
|  | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||||
| 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.AssertUtils.assertServiceException; | ||||
| 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.ArgumentMatchers.argThat; | ||||
| import static org.mockito.Mockito.doNothing; | ||||
| import static org.mockito.Mockito.verify; | ||||
|  | ||||
| // TODO @芋艿:单测的代码质量可以提升下 | ||||
| @Import(MenuServiceImpl.class) | ||||
| public class MenuServiceTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Resource | ||||
|     private MenuServiceImpl sysMenuService; | ||||
|  | ||||
|     @MockBean | ||||
|     private PermissionService sysPermissionService; | ||||
|  | ||||
|     @MockBean | ||||
|     private MenuProducer sysMenuProducer; | ||||
|     private MenuServiceImpl menuService; | ||||
|  | ||||
|     @Resource | ||||
|     private MenuMapper menuMapper; | ||||
|  | ||||
|     @MockBean | ||||
|     private PermissionService permissionService; | ||||
|     @MockBean | ||||
|     private MenuProducer menuProducer; | ||||
|     @MockBean | ||||
|     private TenantService tenantService; | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCache_success() throws Exception { | ||||
|         MenuDO menuDO1 = createMenuDO(MenuTypeEnum.MENU, "xxxx", 0L); | ||||
| @@ -51,10 +57,10 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         menuMapper.insert(menuDO2); | ||||
|  | ||||
|         // 调用 | ||||
|         sysMenuService.initLocalCache(); | ||||
|         menuService.initLocalCache(); | ||||
|  | ||||
|         // 获取代理对象 | ||||
|         MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(sysMenuService); | ||||
|         MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(menuService); | ||||
|  | ||||
|         Map<Long, MenuDO> menuCache = | ||||
|                 (Map<Long, MenuDO>) BeanUtil.getFieldValue(target, "menuCache"); | ||||
| @@ -86,7 +92,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|             o.setType(MenuTypeEnum.MENU.getType()); | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|         }); | ||||
|         Long menuId = sysMenuService.createMenu(vo); | ||||
|         Long menuId = menuService.createMenu(vo); | ||||
|  | ||||
|         //断言 | ||||
|         assertNotNull(menuId); | ||||
| @@ -94,7 +100,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         MenuDO ret = menuMapper.selectById(menuId); | ||||
|         assertPojoEquals(vo, ret); | ||||
|         // 校验调用 | ||||
|         verify(sysMenuProducer).sendMenuRefreshMessage(); | ||||
|         verify(menuProducer).sendMenuRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -112,14 +118,14 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|             o.setName("pppppp"); //修改名字 | ||||
|         }); | ||||
|         sysMenuService.updateMenu(vo); | ||||
|         menuService.updateMenu(vo); | ||||
|  | ||||
|         //断言 | ||||
|         // 校验记录的属性是否正确 | ||||
|         MenuDO ret = menuMapper.selectById(sonId); | ||||
|         assertPojoEquals(vo, ret); | ||||
|         // 校验调用 | ||||
|         verify(sysMenuProducer).sendMenuRefreshMessage(); | ||||
|         verify(menuProducer).sendMenuRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -135,7 +141,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|             o.setStatus(randomCommonStatus()); | ||||
|         }); | ||||
|         //断言 | ||||
|         assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NOT_EXISTS); | ||||
|         assertServiceException(() -> menuService.updateMenu(vo), MENU_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -144,20 +150,20 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         Long sonId = sonMenuDO.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         sysMenuService.deleteMenu(sonId); | ||||
|         menuService.deleteMenu(sonId); | ||||
|  | ||||
|         // 断言 | ||||
|         MenuDO menuDO = menuMapper.selectById(sonId); | ||||
|         assertNull(menuDO); | ||||
|         verify(sysPermissionService).processMenuDeleted(sonId); | ||||
|         verify(sysMenuProducer).sendMenuRefreshMessage(); | ||||
|         verify(permissionService).processMenuDeleted(sonId); | ||||
|         verify(menuProducer).sendMenuRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteMenu_menuNotExist() { | ||||
|         Long sonId = 99999L; | ||||
|  | ||||
|         assertServiceException(() -> sysMenuService.deleteMenu(sonId), MENU_NOT_EXISTS); | ||||
|         assertServiceException(() -> menuService.deleteMenu(sonId), MENU_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -165,26 +171,50 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         MenuDO sonMenu = initParentAndSonMenuDO(); | ||||
|         Long parentId = sonMenu.getParentId(); | ||||
|  | ||||
|         assertServiceException(() -> sysMenuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); | ||||
|         assertServiceException(() -> menuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMenus_success() { | ||||
|         Map<Long, MenuDO> idMenuMap = new HashMap<>(); | ||||
|         MenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | ||||
|         menuMapper.insert(menuDO); | ||||
|         idMenuMap.put(menuDO.getId(), menuDO); | ||||
|  | ||||
|         MenuDO sonMenu = createMenuDO(MenuTypeEnum.MENU, "son", menuDO.getId()); | ||||
|         menuMapper.insert(sonMenu); | ||||
|         idMenuMap.put(sonMenu.getId(), sonMenu); | ||||
|     public void testGetMenus() { | ||||
|         // mock 数据 | ||||
|         MenuDO menu100 = randomPojo(MenuDO.class, o -> o.setId(100L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         menuMapper.insert(menu100); | ||||
|         MenuDO menu101 = randomPojo(MenuDO.class, o -> o.setId(101L).setStatus(CommonStatusEnum.DISABLE.getStatus())); | ||||
|         menuMapper.insert(menu101); | ||||
|         // 准备参数 | ||||
|         MenuListReqVO reqVO = new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|  | ||||
|         // 调用 | ||||
|         List<MenuDO> menuDOS = sysMenuService.getMenus(); | ||||
|  | ||||
|         List<MenuDO> result = menuService.getMenus(reqVO); | ||||
|         // 断言 | ||||
|         assertEquals(menuDOS.size(), idMenuMap.size()); | ||||
|         menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); | ||||
|         assertEquals(1, result.size()); | ||||
|         assertPojoEquals(menu100, result.get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testTenantMenus() { | ||||
|         // mock 数据 | ||||
|         MenuDO menu100 = randomPojo(MenuDO.class, o -> o.setId(100L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         menuMapper.insert(menu100); | ||||
|         MenuDO menu101 = randomPojo(MenuDO.class, o -> o.setId(101L).setStatus(CommonStatusEnum.DISABLE.getStatus())); | ||||
|         menuMapper.insert(menu101); | ||||
|         MenuDO menu102 = randomPojo(MenuDO.class, o -> o.setId(102L).setStatus(CommonStatusEnum.ENABLE.getStatus())); | ||||
|         menuMapper.insert(menu102); | ||||
|         // mock 过滤菜单 | ||||
|         // mock 账户额度充足 | ||||
|         Set<Long> menuIds = asSet(100L, 101L); | ||||
|         doNothing().when(tenantService).handleTenantMenu(argThat(handler -> { | ||||
|             handler.handle(menuIds); | ||||
|             return true; | ||||
|         })); | ||||
|         // 准备参数 | ||||
|         MenuListReqVO reqVO = new MenuListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|  | ||||
|         // 调用 | ||||
|         List<MenuDO> result = menuService.getTenantMenus(reqVO); | ||||
|         // 断言 | ||||
|         assertEquals(1, result.size()); | ||||
|         assertPojoEquals(menu100, result.get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -213,7 +243,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         MenuListReqVO reqVO = new MenuListReqVO(); | ||||
|         reqVO.setStatus(1); | ||||
|         reqVO.setName("name"); | ||||
|         List<MenuDO> menuDOS = sysMenuService.getMenus(reqVO); | ||||
|         List<MenuDO> menuDOS = menuService.getMenus(reqVO); | ||||
|  | ||||
|         // 断言 | ||||
|         assertEquals(menuDOS.size(), idMenuMap.size()); | ||||
| @@ -224,7 +254,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|     public void testListMenusFromCache_success() throws Exception { | ||||
|         Map<Long, MenuDO> mockCacheMap = new HashMap<>(); | ||||
|         // 获取代理对象 | ||||
|         MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(sysMenuService); | ||||
|         MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(menuService); | ||||
|         BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); | ||||
|  | ||||
|         Map<Long, MenuDO> idMenuMap = new HashMap<>(); | ||||
| @@ -243,7 +273,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); | ||||
|         mockCacheMap.put(menuDO.getId(), menuDO); | ||||
|  | ||||
|         List<MenuDO> menuDOS = sysMenuService.getMenuListFromCache(Collections.singletonList(MenuTypeEnum.MENU.getType()), | ||||
|         List<MenuDO> menuDOS = menuService.getMenuListFromCache(Collections.singletonList(MenuTypeEnum.MENU.getType()), | ||||
|                 Collections.singletonList(CommonStatusEnum.DISABLE.getStatus())); | ||||
|         assertEquals(menuDOS.size(), idMenuMap.size()); | ||||
|         menuDOS.forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); | ||||
| @@ -253,7 +283,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|     public void testListMenusFromCache2_success() throws Exception { | ||||
|         Map<Long, MenuDO> mockCacheMap = new HashMap<>(); | ||||
|         // 获取代理对象 | ||||
|         MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(sysMenuService); | ||||
|         MenuServiceImpl target = (MenuServiceImpl) SpringAopUtils.getTarget(menuService); | ||||
|         BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); | ||||
|  | ||||
|         Map<Long, MenuDO> idMenuMap = new HashMap<>(); | ||||
| @@ -270,7 +300,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); | ||||
|         mockCacheMap.put(menuDO.getId(), menuDO); | ||||
|  | ||||
|         List<MenuDO> menuDOS = sysMenuService.getMenuListFromCache(Collections.singletonList(1L), | ||||
|         List<MenuDO> menuDOS = menuService.getMenuListFromCache(Collections.singletonList(1L), | ||||
|                 Collections.singletonList(MenuTypeEnum.MENU.getType()), Collections.singletonList(1)); | ||||
|         assertEquals(menuDOS.size(), idMenuMap.size()); | ||||
|         menuDOS.forEach(menu -> assertPojoEquals(idMenuMap.get(menu.getId()), menu)); | ||||
| @@ -282,17 +312,17 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         menuMapper.insert(menuDO); | ||||
|         Long parentId = menuDO.getId(); | ||||
|  | ||||
|         sysMenuService.checkParentResource(parentId, null); | ||||
|         menuService.checkParentResource(parentId, null); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCheckParentResource_canNotSetSelfToBeParent() { | ||||
|         assertServiceException(() -> sysMenuService.checkParentResource(1L, 1L), MENU_PARENT_ERROR); | ||||
|         assertServiceException(() -> menuService.checkParentResource(1L, 1L), MENU_PARENT_ERROR); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCheckParentResource_parentNotExist() { | ||||
|         assertServiceException(() -> sysMenuService.checkParentResource(randomLongId(), null), MENU_PARENT_NOT_EXISTS); | ||||
|         assertServiceException(() -> menuService.checkParentResource(randomLongId(), null), MENU_PARENT_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -301,7 +331,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         menuMapper.insert(menuDO); | ||||
|         Long parentId = menuDO.getId(); | ||||
|  | ||||
|         assertServiceException(() -> sysMenuService.checkParentResource(parentId, null), MENU_PARENT_NOT_DIR_OR_MENU); | ||||
|         assertServiceException(() -> menuService.checkParentResource(parentId, null), MENU_PARENT_NOT_DIR_OR_MENU); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -312,7 +342,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         Long otherSonMenuId = randomLongId(); | ||||
|         String otherSonMenuName = randomString(); | ||||
|  | ||||
|         sysMenuService.checkResource(parentId, otherSonMenuName, otherSonMenuId); | ||||
|         menuService.checkResource(parentId, otherSonMenuName, otherSonMenuId); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -323,7 +353,7 @@ public class MenuServiceTest extends BaseDbUnitTest { | ||||
|         Long otherSonMenuId=randomLongId(); | ||||
|         String otherSonMenuName=sonMenu.getName(); //相同名称 | ||||
|  | ||||
|         assertServiceException(() -> sysMenuService.checkResource(parentId, otherSonMenuName, otherSonMenuId), MENU_NAME_DUPLICATE); | ||||
|         assertServiceException(() -> menuService.checkResource(parentId, otherSonMenuName, otherSonMenuId), MENU_NAME_DUPLICATE); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|   | ||||
| @@ -47,7 +47,6 @@ public class PermissionServiceTest extends BaseDbUnitTest { | ||||
|     private MenuService menuService; | ||||
|     @MockBean | ||||
|     private DeptService deptService; | ||||
|  | ||||
|     @MockBean | ||||
|     private PermissionProducer permissionProducer; | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,6 @@ | ||||
| package cn.iocoder.yudao.module.system.service.permission; | ||||
|  | ||||
| import cn.hutool.core.bean.BeanUtil; | ||||
| import cn.hutool.core.util.RandomUtil; | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||||
| import cn.iocoder.yudao.framework.security.core.enums.DataScopeEnum; | ||||
| @@ -11,7 +11,6 @@ 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.RoleTypeEnum; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.permission.RoleProducer; | ||||
| import cn.iocoder.yudao.framework.common.util.spring.SpringAopUtils; | ||||
| import cn.iocoder.yudao.module.system.test.BaseDbUnitTest; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| @@ -21,71 +20,67 @@ import javax.annotation.Resource; | ||||
| import java.util.*; | ||||
| import java.util.stream.Collectors; | ||||
|  | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||||
| import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max; | ||||
| 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.common.util.object.ObjectUtils.max; | ||||
| 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; | ||||
|  | ||||
| // TODO @芋艿:单测的代码质量可以提升下 | ||||
| @Import(RoleServiceImpl.class) | ||||
| public class RoleServiceTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @Resource | ||||
|     private RoleServiceImpl sysRoleService; | ||||
|     private RoleServiceImpl roleService; | ||||
|  | ||||
|     @Resource | ||||
|     private RoleMapper roleMapper; | ||||
|  | ||||
|     @MockBean | ||||
|     private PermissionService sysPermissionService; | ||||
|  | ||||
|     private PermissionService permissionService; | ||||
|     @MockBean | ||||
|     private RoleProducer sysRoleProducer; | ||||
|     private RoleProducer roleProducer; | ||||
|  | ||||
|     @Test | ||||
|     public void testInitLocalCache_success() throws Exception { | ||||
|         RoleDO roleDO1 = createRoleDO("role1", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | ||||
|     public void testInitLocalCache() { | ||||
|         RoleDO roleDO1 = randomRole(); | ||||
|         roleMapper.insert(roleDO1); | ||||
|         RoleDO roleDO2 = createRoleDO("role2", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | ||||
|         RoleDO roleDO2 = randomRole(); | ||||
|         roleMapper.insert(roleDO2); | ||||
|  | ||||
|         //调用 | ||||
|         sysRoleService.initLocalCache(); | ||||
|  | ||||
|         //断言 | ||||
|         //获取代理对象 | ||||
|         RoleServiceImpl target = (RoleServiceImpl) SpringAopUtils.getTarget(sysRoleService); | ||||
|  | ||||
|         Map<Long, RoleDO> roleCache = (Map<Long, RoleDO>) BeanUtil.getFieldValue(target, "roleCache"); | ||||
|         // 调用 | ||||
|         roleService.initLocalCache(); | ||||
|         // 断言 roleCache 缓存 | ||||
|         Map<Long, RoleDO> roleCache = roleService.getRoleCache(); | ||||
|         assertPojoEquals(roleDO1, roleCache.get(roleDO1.getId())); | ||||
|         assertPojoEquals(roleDO2, roleCache.get(roleDO2.getId())); | ||||
|  | ||||
|         Date maxUpdateTime = (Date) BeanUtil.getFieldValue(target, "maxUpdateTime"); | ||||
|         assertEquals(max(roleDO1.getUpdateTime(), roleDO2.getUpdateTime()), maxUpdateTime); | ||||
|         // 断言 maxUpdateTime 缓存 | ||||
|         assertEquals(max(roleDO1.getUpdateTime(), roleDO2.getUpdateTime()), roleService.getMaxUpdateTime()); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCreateRole_success() { | ||||
|         RoleCreateReqVO reqVO = randomPojo(RoleCreateReqVO.class, o -> { | ||||
|             o.setCode("role_code"); | ||||
|             o.setName("role_name"); | ||||
|             o.setRemark("remark"); | ||||
|             o.setSort(1); | ||||
|         }); | ||||
|         Long roleId = sysRoleService.createRole(reqVO, null); | ||||
|         // 准备参数 | ||||
|         RoleCreateReqVO reqVO = randomPojo(RoleCreateReqVO.class); | ||||
|  | ||||
|         //断言 | ||||
|         // 调用 | ||||
|         Long roleId = roleService.createRole(reqVO, null); | ||||
|         // 断言 | ||||
|         assertNotNull(roleId); | ||||
|         RoleDO roleDO = roleMapper.selectById(roleId); | ||||
|         assertPojoEquals(reqVO, roleDO); | ||||
|  | ||||
|         verify(sysRoleProducer).sendRoleRefreshMessage(); | ||||
|         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() { | ||||
|         // mock 数据 | ||||
|         RoleDO roleDO = createRoleDO("role_name", RoleTypeEnum.CUSTOM, DataScopeEnum.ALL); | ||||
|         roleMapper.insert(roleDO); | ||||
|         Long roleId = roleDO.getId(); | ||||
| @@ -97,13 +92,13 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|             o.setName("update_name"); | ||||
|             o.setSort(999); | ||||
|         }); | ||||
|         sysRoleService.updateRole(reqVO); | ||||
|         roleService.updateRole(reqVO); | ||||
|  | ||||
|         //断言 | ||||
|         RoleDO newRoleDO = roleMapper.selectById(roleId); | ||||
|         assertPojoEquals(reqVO, newRoleDO); | ||||
|  | ||||
|         verify(sysRoleProducer).sendRoleRefreshMessage(); | ||||
|         verify(roleProducer).sendRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -113,13 +108,13 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|         Long roleId = roleDO.getId(); | ||||
|  | ||||
|         //调用 | ||||
|         sysRoleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus()); | ||||
|         roleService.updateRoleStatus(roleId, CommonStatusEnum.DISABLE.getStatus()); | ||||
|  | ||||
|         //断言 | ||||
|         RoleDO newRoleDO = roleMapper.selectById(roleId); | ||||
|         assertEquals(CommonStatusEnum.DISABLE.getStatus(), newRoleDO.getStatus()); | ||||
|  | ||||
|         verify(sysRoleProducer).sendRoleRefreshMessage(); | ||||
|         verify(roleProducer).sendRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -130,7 +125,7 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|  | ||||
|         //调用 | ||||
|         Set<Long> deptIdSet = Arrays.asList(1L, 2L, 3L, 4L, 5L).stream().collect(Collectors.toSet()); | ||||
|         sysRoleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScope(), deptIdSet); | ||||
|         roleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScope(), deptIdSet); | ||||
|  | ||||
|         //断言 | ||||
|         RoleDO newRoleDO = roleMapper.selectById(roleId); | ||||
| @@ -140,7 +135,7 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|         assertTrue(deptIdSet.size() == newDeptIdSet.size()); | ||||
|         deptIdSet.stream().forEach(d -> assertTrue(newDeptIdSet.contains(d))); | ||||
|  | ||||
|         verify(sysRoleProducer).sendRoleRefreshMessage(); | ||||
|         verify(roleProducer).sendRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -150,13 +145,13 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|         Long roleId = roleDO.getId(); | ||||
|  | ||||
|         //调用 | ||||
|         sysRoleService.deleteRole(roleId); | ||||
|         roleService.deleteRole(roleId); | ||||
|  | ||||
|         //断言 | ||||
|         RoleDO newRoleDO = roleMapper.selectById(roleId); | ||||
|         assertNull(newRoleDO); | ||||
|  | ||||
|         verify(sysRoleProducer).sendRoleRefreshMessage(); | ||||
|         verify(roleProducer).sendRoleRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -176,7 +171,7 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|         roleMapper.insert(roleDO3); | ||||
|  | ||||
|         //调用 | ||||
|         List<RoleDO> roles = sysRoleService.getRoles(Arrays.asList(1)); | ||||
|         List<RoleDO> roles = roleService.getRoles(Arrays.asList(1)); | ||||
|  | ||||
|         //断言 | ||||
|         assertEquals(2, roles.size()); | ||||
| @@ -212,14 +207,14 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|             o.setBeginTime(null); | ||||
|             o.setEndTime(null); | ||||
|         }); | ||||
|         PageResult<RoleDO> result = sysRoleService.getRolePage(reqVO); | ||||
|         PageResult<RoleDO> result = roleService.getRolePage(reqVO); | ||||
|         assertEquals(2, result.getTotal()); | ||||
|         result.getList().stream().forEach(r -> assertPojoEquals(idRoleMap.get(r.getId()), r)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCheckDuplicateRole_success() { | ||||
|         sysRoleService.checkDuplicateRole(randomString(), randomString(), null); | ||||
|         roleService.checkDuplicateRole(randomString(), randomString(), null); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -229,7 +224,7 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|  | ||||
|         String duplicateName = "role_name"; | ||||
|  | ||||
|         assertServiceException(() -> sysRoleService.checkDuplicateRole(duplicateName, randomString(), null), ROLE_NAME_DUPLICATE, duplicateName); | ||||
|         assertServiceException(() -> roleService.checkDuplicateRole(duplicateName, randomString(), null), ROLE_NAME_DUPLICATE, duplicateName); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -246,7 +241,7 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|         String randomName = randomString(); | ||||
|         String duplicateCode = "code"; | ||||
|  | ||||
|         assertServiceException(() -> sysRoleService.checkDuplicateRole(randomName, duplicateCode, null), ROLE_CODE_DUPLICATE, duplicateCode); | ||||
|         assertServiceException(() -> roleService.checkDuplicateRole(randomName, duplicateCode, null), ROLE_CODE_DUPLICATE, duplicateCode); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -255,12 +250,12 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|         roleMapper.insert(roleDO); | ||||
|         Long roleId = roleDO.getId(); | ||||
|  | ||||
|         sysRoleService.checkUpdateRole(roleId); | ||||
|         roleService.checkUpdateRole(roleId); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCheckUpdateRole_roleIdNotExist() { | ||||
|         assertServiceException(() -> sysRoleService.checkUpdateRole(randomLongId()), ROLE_NOT_EXISTS); | ||||
|         assertServiceException(() -> roleService.checkUpdateRole(randomLongId()), ROLE_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -269,7 +264,7 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|         roleMapper.insert(roleDO); | ||||
|         Long roleId = roleDO.getId(); | ||||
|  | ||||
|         assertServiceException(() -> sysRoleService.checkUpdateRole(roleId), ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | ||||
|         assertServiceException(() -> roleService.checkUpdateRole(roleId), ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); | ||||
|     } | ||||
|  | ||||
|     private RoleDO createRoleDO(String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status) { | ||||
| @@ -285,7 +280,7 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|     } | ||||
|  | ||||
|     private RoleDO createRoleDO(Long id, String name, RoleTypeEnum typeEnum, DataScopeEnum scopeEnum, Integer status, String code) { | ||||
|         RoleDO roleDO = randomPojo(RoleDO.class, o -> { | ||||
|         return randomPojo(RoleDO.class, o -> { | ||||
|             o.setId(id); | ||||
|             o.setName(name); | ||||
|             o.setType(typeEnum.getType()); | ||||
| @@ -293,7 +288,11 @@ public class RoleServiceTest extends BaseDbUnitTest { | ||||
|             o.setDataScope(scopeEnum.getScope()); | ||||
|             o.setCode(code); | ||||
|         }); | ||||
|         return roleDO; | ||||
|     } | ||||
|  | ||||
|     private RoleDO randomRole() { | ||||
|         return randomPojo(RoleDO.class, | ||||
|                 o -> o.setDataScope(RandomUtil.randomEle(DataScopeEnum.values()).getScope())); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -13,12 +13,14 @@ import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfi | ||||
| import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.dept.PostDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; | ||||
| import cn.iocoder.yudao.module.system.enums.common.SexEnum; | ||||
| import cn.iocoder.yudao.module.system.service.dept.DeptService; | ||||
| import cn.iocoder.yudao.module.system.service.dept.PostService; | ||||
| import cn.iocoder.yudao.module.system.service.permission.PermissionService; | ||||
| import cn.iocoder.yudao.module.system.service.tenant.TenantService; | ||||
| import cn.iocoder.yudao.module.system.test.BaseDbUnitTest; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.mockito.stubbing.Answer; | ||||
| @@ -55,7 +57,6 @@ public class UserServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|     @MockBean | ||||
|     private DeptService deptService; | ||||
|  | ||||
|     @MockBean | ||||
|     private PostService postService; | ||||
|     @MockBean | ||||
| @@ -63,6 +64,8 @@ public class UserServiceImplTest extends BaseDbUnitTest { | ||||
|     @MockBean | ||||
|     private PasswordEncoder passwordEncoder; | ||||
|     @MockBean | ||||
|     private TenantService tenantService; | ||||
|     @MockBean | ||||
|     private FileApi fileApi; | ||||
|  | ||||
|     @Test | ||||
| @@ -72,6 +75,12 @@ public class UserServiceImplTest extends BaseDbUnitTest { | ||||
|             o.setSex(RandomUtil.randomEle(SexEnum.values()).getSex()); | ||||
|             o.setMobile(randomString()); | ||||
|         }); | ||||
|         // mock 账户额度充足 | ||||
|         TenantDO tenant = randomPojo(TenantDO.class, o -> o.setAccountCount(1)); | ||||
|         doNothing().when(tenantService).handleTenantInfo(argThat(handler -> { | ||||
|             handler.handle(tenant); | ||||
|             return true; | ||||
|         })); | ||||
|         // mock deptService 的方法 | ||||
|         DeptDO dept = randomPojo(DeptDO.class, o -> { | ||||
|             o.setId(reqVO.getDeptId()); | ||||
| @@ -97,6 +106,21 @@ public class UserServiceImplTest extends BaseDbUnitTest { | ||||
|         assertEquals(CommonStatusEnum.ENABLE.getStatus(), user.getStatus()); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCreatUser_max() { | ||||
|         // 准备参数 | ||||
|         UserCreateReqVO reqVO = randomPojo(UserCreateReqVO.class); | ||||
|         // mock 账户额度不足 | ||||
|         TenantDO tenant = randomPojo(TenantDO.class, o -> o.setAccountCount(-1)); | ||||
|         doNothing().when(tenantService).handleTenantInfo(argThat(handler -> { | ||||
|             handler.handle(tenant); | ||||
|             return true; | ||||
|         })); | ||||
|  | ||||
|         // 调用,并断言异常 | ||||
|         assertServiceException(() -> userService.createUser(reqVO), USER_COUNT_MAX, -1); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testUpdateUser_success() { | ||||
|         // mock 数据 | ||||
|   | ||||
| @@ -49,6 +49,7 @@ CREATE TABLE IF NOT EXISTS "system_role" ( | ||||
|     "updater" varchar(64) DEFAULT '', | ||||
|     "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||||
|     "deleted" bit NOT NULL DEFAULT FALSE, | ||||
|     "tenant_id" bigint not null default  '0', | ||||
|     PRIMARY KEY ("id") | ||||
| ) COMMENT '角色信息表'; | ||||
|  | ||||
| @@ -61,6 +62,7 @@ CREATE TABLE IF NOT EXISTS "system_role_menu" ( | ||||
|     "updater" varchar(64) DEFAULT '', | ||||
|     "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||||
|     "deleted" bit NOT NULL DEFAULT FALSE, | ||||
|     "tenant_id" bigint not null default  '0', | ||||
|     PRIMARY KEY ("id") | ||||
| ) COMMENT '角色和菜单关联表'; | ||||
|  | ||||
| @@ -92,7 +94,8 @@ CREATE TABLE IF NOT EXISTS "system_user_role" ( | ||||
|      "updater" varchar(64) DEFAULT '', | ||||
|      "update_time" timestamp DEFAULT NULL, | ||||
|      "deleted" bit DEFAULT FALSE, | ||||
|      PRIMARY KEY ("id") | ||||
|     "tenant_id" bigint not null default  '0', | ||||
|     PRIMARY KEY ("id") | ||||
| ) COMMENT '用户和角色关联表'; | ||||
|  | ||||
| CREATE TABLE IF NOT EXISTS "system_dict_type" ( | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV