Commit 4779413bc7c25cf9402b8977721e63ec6e4cfe06
Committed by
Gitee
!24 完成menu单测
Merge pull request !24 from timfruit/feature/ut-menu
Showing
3 changed files
with
420 additions
and
6 deletions
src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java
| @@ -15,6 +15,7 @@ import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysMenuProduce | @@ -15,6 +15,7 @@ import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysMenuProduce | ||
| 15 | import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService; | 15 | import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService; |
| 16 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; | 16 | import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; |
| 17 | import cn.iocoder.dashboard.util.collection.CollectionUtils; | 17 | import cn.iocoder.dashboard.util.collection.CollectionUtils; |
| 18 | +import com.google.common.annotations.VisibleForTesting; | ||
| 18 | import com.google.common.collect.ImmutableMap; | 19 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableMultimap; | 20 | import com.google.common.collect.ImmutableMultimap; |
| 20 | import com.google.common.collect.Multimap; | 21 | import com.google.common.collect.Multimap; |
| @@ -168,10 +169,6 @@ public class SysMenuServiceImpl implements SysMenuService { | @@ -168,10 +169,6 @@ public class SysMenuServiceImpl implements SysMenuService { | ||
| 168 | */ | 169 | */ |
| 169 | @Transactional(rollbackFor = Exception.class) | 170 | @Transactional(rollbackFor = Exception.class) |
| 170 | public void deleteMenu(Long menuId) { | 171 | public void deleteMenu(Long menuId) { |
| 171 | - // 校验更新的菜单是否存在 | ||
| 172 | - if (menuMapper.selectById(menuId) == null) { | ||
| 173 | - throw ServiceExceptionUtil.exception(MENU_NOT_EXISTS); | ||
| 174 | - } | ||
| 175 | // 校验是否还有子菜单 | 172 | // 校验是否还有子菜单 |
| 176 | if (menuMapper.selectCountByParentId(menuId) > 0) { | 173 | if (menuMapper.selectCountByParentId(menuId) > 0) { |
| 177 | throw ServiceExceptionUtil.exception(MENU_EXISTS_CHILDREN); | 174 | throw ServiceExceptionUtil.exception(MENU_EXISTS_CHILDREN); |
| @@ -250,7 +247,8 @@ public class SysMenuServiceImpl implements SysMenuService { | @@ -250,7 +247,8 @@ public class SysMenuServiceImpl implements SysMenuService { | ||
| 250 | * @param parentId 父菜单编号 | 247 | * @param parentId 父菜单编号 |
| 251 | * @param childId 当前菜单编号 | 248 | * @param childId 当前菜单编号 |
| 252 | */ | 249 | */ |
| 253 | - private void checkParentResource(Long parentId, Long childId) { | 250 | + @VisibleForTesting |
| 251 | + public void checkParentResource(Long parentId, Long childId) { | ||
| 254 | if (parentId == null || MenuIdEnum.ROOT.getId().equals(parentId)) { | 252 | if (parentId == null || MenuIdEnum.ROOT.getId().equals(parentId)) { |
| 255 | return; | 253 | return; |
| 256 | } | 254 | } |
| @@ -279,7 +277,8 @@ public class SysMenuServiceImpl implements SysMenuService { | @@ -279,7 +277,8 @@ public class SysMenuServiceImpl implements SysMenuService { | ||
| 279 | * @param parentId 父菜单编号 | 277 | * @param parentId 父菜单编号 |
| 280 | * @param id 菜单编号 | 278 | * @param id 菜单编号 |
| 281 | */ | 279 | */ |
| 282 | - private void checkResource(Long parentId, String name, Long id) { | 280 | + @VisibleForTesting |
| 281 | + public void checkResource(Long parentId, String name, Long id) { | ||
| 283 | SysMenuDO menu = menuMapper.selectByParentIdAndName(parentId, name); | 282 | SysMenuDO menu = menuMapper.selectByParentIdAndName(parentId, name); |
| 284 | if (menu == null) { | 283 | if (menu == null) { |
| 285 | return; | 284 | return; |
src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.system.service.permission; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.bean.BeanUtil; | ||
| 4 | +import cn.hutool.core.lang.Assert; | ||
| 5 | +import cn.iocoder.dashboard.BaseDbUnitTest; | ||
| 6 | +import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO; | ||
| 7 | +import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuListReqVO; | ||
| 8 | +import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuUpdateReqVO; | ||
| 9 | +import cn.iocoder.dashboard.modules.system.dal.dataobject.permission.SysMenuDO; | ||
| 10 | +import cn.iocoder.dashboard.modules.system.dal.mysql.permission.SysMenuMapper; | ||
| 11 | +import cn.iocoder.dashboard.modules.system.enums.permission.MenuTypeEnum; | ||
| 12 | +import cn.iocoder.dashboard.modules.system.mq.producer.permission.SysMenuProducer; | ||
| 13 | +import cn.iocoder.dashboard.modules.system.service.permission.impl.SysMenuServiceImpl; | ||
| 14 | +import cn.iocoder.dashboard.util.AopTargetUtils; | ||
| 15 | +import cn.iocoder.dashboard.util.AssertUtils; | ||
| 16 | +import cn.iocoder.dashboard.util.RandomUtils; | ||
| 17 | +import cn.iocoder.dashboard.util.object.ObjectUtils; | ||
| 18 | +import com.google.common.collect.Multimap; | ||
| 19 | +import org.junit.jupiter.api.Assertions; | ||
| 20 | +import org.junit.jupiter.api.Test; | ||
| 21 | +import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 22 | +import org.springframework.context.annotation.Import; | ||
| 23 | + | ||
| 24 | +import javax.annotation.Resource; | ||
| 25 | + | ||
| 26 | +import java.util.*; | ||
| 27 | + | ||
| 28 | +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; | ||
| 29 | +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; | ||
| 30 | +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; | ||
| 31 | +import static cn.iocoder.dashboard.util.RandomUtils.*; | ||
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 33 | +import static org.mockito.Mockito.verify; | ||
| 34 | + | ||
| 35 | +@Import(SysMenuServiceImpl.class) | ||
| 36 | +public class SysMenuServiceTest extends BaseDbUnitTest { | ||
| 37 | + | ||
| 38 | + @Resource | ||
| 39 | + private SysMenuServiceImpl sysMenuService; | ||
| 40 | + | ||
| 41 | + @MockBean | ||
| 42 | + private SysPermissionService sysPermissionService; | ||
| 43 | + | ||
| 44 | + @MockBean | ||
| 45 | + private SysMenuProducer sysMenuProducer; | ||
| 46 | + | ||
| 47 | + @Resource | ||
| 48 | + private SysMenuMapper menuMapper; | ||
| 49 | + | ||
| 50 | + @Test | ||
| 51 | + public void testInitLocalCache_success() throws Exception { | ||
| 52 | + SysMenuDO menuDO1 = createMenuDO(MenuTypeEnum.MENU, "xxxx", 0L); | ||
| 53 | + menuMapper.insert(menuDO1); | ||
| 54 | + SysMenuDO menuDO2 = createMenuDO(MenuTypeEnum.MENU, "xxxx", 0L); | ||
| 55 | + menuMapper.insert(menuDO2); | ||
| 56 | + | ||
| 57 | + //调用 | ||
| 58 | + sysMenuService.initLocalCache(); | ||
| 59 | + | ||
| 60 | + // 获取代理对象 | ||
| 61 | + SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService); | ||
| 62 | + | ||
| 63 | + Map<Long, SysMenuDO> menuCache = (Map<Long, SysMenuDO>) BeanUtil.getFieldValue(target, "menuCache"); | ||
| 64 | + Assert.isTrue(menuCache.size() == 2); | ||
| 65 | + assertPojoEquals(menuDO1, menuCache.get(menuDO1.getId())); | ||
| 66 | + assertPojoEquals(menuDO2, menuCache.get(menuDO2.getId())); | ||
| 67 | + | ||
| 68 | + Multimap<String, SysMenuDO> permissionMenuCache = (Multimap<String, SysMenuDO>) BeanUtil.getFieldValue(target, "permissionMenuCache"); | ||
| 69 | + Assert.isTrue(permissionMenuCache.size() == 2); | ||
| 70 | + assertPojoEquals(menuDO1, permissionMenuCache.get(menuDO1.getPermission())); | ||
| 71 | + assertPojoEquals(menuDO2, permissionMenuCache.get(menuDO2.getPermission())); | ||
| 72 | + | ||
| 73 | + Date maxUpdateTime = (Date) BeanUtil.getFieldValue(target, "maxUpdateTime"); | ||
| 74 | + assertEquals(ObjectUtils.max(menuDO1.getUpdateTime(), menuDO2.getUpdateTime()), maxUpdateTime); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + @Test | ||
| 78 | + public void testCreateMenu_success() { | ||
| 79 | + //构造父目录 | ||
| 80 | + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | ||
| 81 | + menuMapper.insert(menuDO); | ||
| 82 | + Long parentId = menuDO.getId(); | ||
| 83 | + | ||
| 84 | + //调用 | ||
| 85 | + SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> { | ||
| 86 | + o.setParentId(parentId); | ||
| 87 | + o.setName("testSonName"); | ||
| 88 | + o.setType(MenuTypeEnum.MENU.getType()); | ||
| 89 | + o.setStatus(RandomUtils.randomCommonStatus()); | ||
| 90 | + }); | ||
| 91 | + Long menuId = sysMenuService.createMenu(vo); | ||
| 92 | + | ||
| 93 | + //断言 | ||
| 94 | + Assertions.assertNotNull(menuId); | ||
| 95 | + // 校验记录的属性是否正确 | ||
| 96 | + SysMenuDO ret = menuMapper.selectById(menuId); | ||
| 97 | + assertPojoEquals(vo, ret); | ||
| 98 | + // 校验调用 | ||
| 99 | + verify(sysMenuProducer).sendMenuRefreshMessage(); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + @Test | ||
| 103 | + public void testUpdateMenu_success() { | ||
| 104 | + //构造父子目录 | ||
| 105 | + SysMenuDO sonMenuDO = initParentAndSonMenuDO(); | ||
| 106 | + Long sonId = sonMenuDO.getId(); | ||
| 107 | + Long parentId = sonMenuDO.getParentId(); | ||
| 108 | + | ||
| 109 | + //调用 | ||
| 110 | + SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { | ||
| 111 | + o.setId(sonId); | ||
| 112 | + o.setParentId(parentId); | ||
| 113 | + o.setType(MenuTypeEnum.MENU.getType()); | ||
| 114 | + o.setStatus(RandomUtils.randomCommonStatus()); | ||
| 115 | + o.setName("pppppp"); //修改名字 | ||
| 116 | + }); | ||
| 117 | + sysMenuService.updateMenu(vo); | ||
| 118 | + | ||
| 119 | + //断言 | ||
| 120 | + // 校验记录的属性是否正确 | ||
| 121 | + SysMenuDO ret = menuMapper.selectById(sonId); | ||
| 122 | + assertPojoEquals(vo, ret); | ||
| 123 | + // 校验调用 | ||
| 124 | + verify(sysMenuProducer).sendMenuRefreshMessage(); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + @Test | ||
| 128 | + public void testUpdateMenu_sonIdNotExist() { | ||
| 129 | + Long sonId = 99999L; | ||
| 130 | + Long parentId = 10000L; | ||
| 131 | + | ||
| 132 | + //调用 | ||
| 133 | + SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> { | ||
| 134 | + o.setId(sonId); | ||
| 135 | + o.setParentId(parentId); | ||
| 136 | + o.setType(MenuTypeEnum.MENU.getType()); | ||
| 137 | + o.setStatus(RandomUtils.randomCommonStatus()); | ||
| 138 | + }); | ||
| 139 | + //断言 | ||
| 140 | + assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NOT_EXISTS); | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + @Test | ||
| 144 | + public void testDeleteMenu_success() { | ||
| 145 | + SysMenuDO sonMenuDO = initParentAndSonMenuDO(); | ||
| 146 | + Long sonId = sonMenuDO.getId(); | ||
| 147 | + | ||
| 148 | + //调用 | ||
| 149 | + sysMenuService.deleteMenu(sonId); | ||
| 150 | + | ||
| 151 | + //断言 | ||
| 152 | + SysMenuDO menuDO = menuMapper.selectById(sonId); | ||
| 153 | + Assert.isNull(menuDO); | ||
| 154 | + verify(sysPermissionService).processMenuDeleted(sonId); | ||
| 155 | + verify(sysMenuProducer).sendMenuRefreshMessage(); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + @Test | ||
| 159 | + public void testDeleteMenu_menuNotExist() { | ||
| 160 | + Long sonId = 99999L; | ||
| 161 | + | ||
| 162 | + assertServiceException(() -> sysMenuService.deleteMenu(sonId), MENU_NOT_EXISTS); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + @Test | ||
| 166 | + public void testDeleteMenu_existChildren() { | ||
| 167 | + SysMenuDO sonMenu = initParentAndSonMenuDO(); | ||
| 168 | + Long parentId = sonMenu.getParentId(); | ||
| 169 | + | ||
| 170 | + assertServiceException(() -> sysMenuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + @Test | ||
| 174 | + public void testGetMenus_success() { | ||
| 175 | + Map<Long, SysMenuDO> idMenuMap = new HashMap<>(); | ||
| 176 | + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | ||
| 177 | + menuMapper.insert(menuDO); | ||
| 178 | + idMenuMap.put(menuDO.getId(), menuDO); | ||
| 179 | + | ||
| 180 | + SysMenuDO sonMenu = createMenuDO(MenuTypeEnum.MENU, "son", menuDO.getId()); | ||
| 181 | + menuMapper.insert(sonMenu); | ||
| 182 | + idMenuMap.put(sonMenu.getId(), sonMenu); | ||
| 183 | + | ||
| 184 | + //调用 | ||
| 185 | + List<SysMenuDO> menuDOS = sysMenuService.getMenus(); | ||
| 186 | + | ||
| 187 | + //断言 | ||
| 188 | + Assert.isTrue(menuDOS.size() == idMenuMap.size()); | ||
| 189 | + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + | ||
| 193 | + @Test | ||
| 194 | + public void testGetMenusReqVo_success() { | ||
| 195 | + Map<Long, SysMenuDO> idMenuMap = new HashMap<>(); | ||
| 196 | + //用于验证可以模糊搜索名称包含"name",状态为1的menu | ||
| 197 | + SysMenuDO menu = createMenuDO(MenuTypeEnum.MENU, "name2", 0L, 1); | ||
| 198 | + menuMapper.insert(menu); | ||
| 199 | + idMenuMap.put(menu.getId(), menu); | ||
| 200 | + | ||
| 201 | + menu = createMenuDO(MenuTypeEnum.MENU, "11name111", 0L, 1); | ||
| 202 | + menuMapper.insert(menu); | ||
| 203 | + idMenuMap.put(menu.getId(), menu); | ||
| 204 | + | ||
| 205 | + menu = createMenuDO(MenuTypeEnum.MENU, "name", 0L, 1); | ||
| 206 | + menuMapper.insert(menu); | ||
| 207 | + idMenuMap.put(menu.getId(), menu); | ||
| 208 | + | ||
| 209 | + //以下是不符合搜索条件的的menu | ||
| 210 | + menu = createMenuDO(MenuTypeEnum.MENU, "xxxxxx", 0L, 1); | ||
| 211 | + menuMapper.insert(menu); | ||
| 212 | + menu = createMenuDO(MenuTypeEnum.MENU, "name", 0L, 2); | ||
| 213 | + menuMapper.insert(menu); | ||
| 214 | + | ||
| 215 | + //调用 | ||
| 216 | + SysMenuListReqVO reqVO = new SysMenuListReqVO(); | ||
| 217 | + reqVO.setStatus(1); | ||
| 218 | + reqVO.setName("name"); | ||
| 219 | + List<SysMenuDO> menuDOS = sysMenuService.getMenus(reqVO); | ||
| 220 | + | ||
| 221 | + //断言 | ||
| 222 | + Assert.isTrue(menuDOS.size() == idMenuMap.size()); | ||
| 223 | + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + @Test | ||
| 227 | + public void testListMenusFromCache_success() throws Exception { | ||
| 228 | + Map<Long, SysMenuDO> mockCacheMap = new HashMap<>(); | ||
| 229 | + //获取代理对象 | ||
| 230 | + SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService); | ||
| 231 | + BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); | ||
| 232 | + | ||
| 233 | + Map<Long, SysMenuDO> idMenuMap = new HashMap<>(); | ||
| 234 | + //用于验证搜索类型为MENU,状态为1的menu | ||
| 235 | + SysMenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1); | ||
| 236 | + mockCacheMap.put(menuDO.getId(), menuDO); | ||
| 237 | + idMenuMap.put(menuDO.getId(), menuDO); | ||
| 238 | + | ||
| 239 | + menuDO = createMenuDO(2L, MenuTypeEnum.MENU, "name", 0L, 1); | ||
| 240 | + mockCacheMap.put(menuDO.getId(), menuDO); | ||
| 241 | + idMenuMap.put(menuDO.getId(), menuDO); | ||
| 242 | + | ||
| 243 | + //以下是不符合搜索条件的menu | ||
| 244 | + menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1); | ||
| 245 | + mockCacheMap.put(menuDO.getId(), menuDO); | ||
| 246 | + menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); | ||
| 247 | + mockCacheMap.put(menuDO.getId(), menuDO); | ||
| 248 | + | ||
| 249 | + List<SysMenuDO> menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1)); | ||
| 250 | + Assert.isTrue(menuDOS.size() == idMenuMap.size()); | ||
| 251 | + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m)); | ||
| 252 | + } | ||
| 253 | + | ||
| 254 | + | ||
| 255 | + @Test | ||
| 256 | + public void testListMenusFromCache2_success() throws Exception { | ||
| 257 | + Map<Long, SysMenuDO> mockCacheMap = new HashMap<>(); | ||
| 258 | + //获取代理对象 | ||
| 259 | + SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService); | ||
| 260 | + BeanUtil.setFieldValue(target, "menuCache", mockCacheMap); | ||
| 261 | + | ||
| 262 | + Map<Long, SysMenuDO> idMenuMap = new HashMap<>(); | ||
| 263 | + //验证搜索id为1, 类型为MENU, 状态为1 的menu | ||
| 264 | + SysMenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1); | ||
| 265 | + mockCacheMap.put(menuDO.getId(), menuDO); | ||
| 266 | + idMenuMap.put(menuDO.getId(), menuDO); | ||
| 267 | + | ||
| 268 | + //以下是不符合搜索条件的menu | ||
| 269 | + menuDO = createMenuDO(2L, MenuTypeEnum.MENU, "name", 0L, 1); | ||
| 270 | + mockCacheMap.put(menuDO.getId(), menuDO); | ||
| 271 | + menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1); | ||
| 272 | + mockCacheMap.put(menuDO.getId(), menuDO); | ||
| 273 | + menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2); | ||
| 274 | + mockCacheMap.put(menuDO.getId(), menuDO); | ||
| 275 | + | ||
| 276 | + List<SysMenuDO> menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(1L), | ||
| 277 | + Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1)); | ||
| 278 | + Assert.isTrue(menuDOS.size() == idMenuMap.size()); | ||
| 279 | + menuDOS.stream().forEach(menu -> assertPojoEquals(idMenuMap.get(menu.getId()), menu)); | ||
| 280 | + } | ||
| 281 | + | ||
| 282 | + @Test | ||
| 283 | + public void testCheckParentResource_success() { | ||
| 284 | + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | ||
| 285 | + menuMapper.insert(menuDO); | ||
| 286 | + Long parentId = menuDO.getId(); | ||
| 287 | + | ||
| 288 | + sysMenuService.checkParentResource(parentId, null); | ||
| 289 | + } | ||
| 290 | + | ||
| 291 | + @Test | ||
| 292 | + public void testCheckParentResource_canNotSetSelfToBeParent() { | ||
| 293 | + assertServiceException(() -> sysMenuService.checkParentResource(1L, 1L), MENU_PARENT_ERROR); | ||
| 294 | + } | ||
| 295 | + | ||
| 296 | + @Test | ||
| 297 | + public void testCheckParentResource_parentNotExist() { | ||
| 298 | + assertServiceException(() -> sysMenuService.checkParentResource(randomLongId(), null), MENU_PARENT_NOT_EXISTS); | ||
| 299 | + } | ||
| 300 | + | ||
| 301 | + @Test | ||
| 302 | + public void testCheckParentResource_parentTypeError() { | ||
| 303 | + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L); | ||
| 304 | + menuMapper.insert(menuDO); | ||
| 305 | + Long parentId = menuDO.getId(); | ||
| 306 | + | ||
| 307 | + assertServiceException(() -> sysMenuService.checkParentResource(parentId, null), MENU_PARENT_NOT_DIR_OR_MENU); | ||
| 308 | + } | ||
| 309 | + | ||
| 310 | + @Test | ||
| 311 | + public void testCheckResource_success(){ | ||
| 312 | + SysMenuDO sonMenu=initParentAndSonMenuDO(); | ||
| 313 | + Long parentId=sonMenu.getParentId(); | ||
| 314 | + | ||
| 315 | + Long otherSonMenuId=randomLongId(); | ||
| 316 | + String otherSonMenuName=randomString(); | ||
| 317 | + | ||
| 318 | + sysMenuService.checkResource(parentId,otherSonMenuName,otherSonMenuId); | ||
| 319 | + } | ||
| 320 | + | ||
| 321 | + @Test | ||
| 322 | + public void testCheckResource_sonMenuNameDuplicate(){ | ||
| 323 | + SysMenuDO sonMenu=initParentAndSonMenuDO(); | ||
| 324 | + Long parentId=sonMenu.getParentId(); | ||
| 325 | + | ||
| 326 | + Long otherSonMenuId=randomLongId(); | ||
| 327 | + String otherSonMenuName=sonMenu.getName(); //相同名称 | ||
| 328 | + | ||
| 329 | + assertServiceException(()->sysMenuService.checkResource(parentId,otherSonMenuName,otherSonMenuId), MENU_NAME_DUPLICATE); | ||
| 330 | + } | ||
| 331 | + | ||
| 332 | + /** | ||
| 333 | + * 构造父子目录,返回子目录 | ||
| 334 | + * | ||
| 335 | + * @return | ||
| 336 | + */ | ||
| 337 | + private SysMenuDO initParentAndSonMenuDO() { | ||
| 338 | + //构造父子目录 | ||
| 339 | + SysMenuDO menuDO = createMenuDO(MenuTypeEnum.MENU, "parent", 0L); | ||
| 340 | + menuMapper.insert(menuDO); | ||
| 341 | + Long parentId = menuDO.getId(); | ||
| 342 | + | ||
| 343 | + SysMenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "testSonName", parentId); | ||
| 344 | + menuMapper.insert(sonMenuDO); | ||
| 345 | + return sonMenuDO; | ||
| 346 | + } | ||
| 347 | + | ||
| 348 | + private SysMenuDO createMenuDO(MenuTypeEnum typeEnum, String menuName, Long parentId) { | ||
| 349 | + return createMenuDO(typeEnum, menuName, parentId, RandomUtils.randomCommonStatus()); | ||
| 350 | + } | ||
| 351 | + | ||
| 352 | + private SysMenuDO createMenuDO(MenuTypeEnum typeEnum, String menuName, Long parentId, Integer status) { | ||
| 353 | + return createMenuDO(null, typeEnum, menuName, parentId, status); | ||
| 354 | + } | ||
| 355 | + | ||
| 356 | + private SysMenuDO createMenuDO(Long id, MenuTypeEnum typeEnum, String menuName, Long parentId, Integer status) { | ||
| 357 | + SysMenuDO sonMenuDO = RandomUtils.randomPojo(SysMenuDO.class, o -> { | ||
| 358 | + o.setId(id); | ||
| 359 | + o.setParentId(parentId); | ||
| 360 | + o.setType(typeEnum.getType()); | ||
| 361 | + o.setStatus(status); | ||
| 362 | + o.setName(menuName); | ||
| 363 | + }); | ||
| 364 | + return sonMenuDO; | ||
| 365 | + } | ||
| 366 | + | ||
| 367 | + | ||
| 368 | +} |
src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.util; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.bean.BeanUtil; | ||
| 4 | +import org.springframework.aop.framework.AdvisedSupport; | ||
| 5 | +import org.springframework.aop.framework.AopProxy; | ||
| 6 | +import org.springframework.aop.support.AopUtils; | ||
| 7 | + | ||
| 8 | +import java.lang.reflect.Field; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * http://www.bubuko.com/infodetail-3471885.html | ||
| 12 | + */ | ||
| 13 | +public class AopTargetUtils { | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 获取 目标对象 | ||
| 17 | + * | ||
| 18 | + * @param proxy 代理对象 | ||
| 19 | + * @return | ||
| 20 | + * @throws Exception | ||
| 21 | + */ | ||
| 22 | + public static Object getTarget(Object proxy) throws Exception { | ||
| 23 | + if (!AopUtils.isAopProxy(proxy)) { | ||
| 24 | + return proxy; //不是代理对象 | ||
| 25 | + } | ||
| 26 | + if (AopUtils.isJdkDynamicProxy(proxy)) { | ||
| 27 | + return getJdkDynamicProxyTargetObject(proxy); | ||
| 28 | + } else { //cglib | ||
| 29 | + return getCglibProxyTargetObject(proxy); | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + private static Object getCglibProxyTargetObject(Object proxy) throws Exception { | ||
| 34 | + Object dynamicAdvisedInterceptor = BeanUtil.getFieldValue(proxy, "CGLIB$CALLBACK_0"); | ||
| 35 | + AdvisedSupport advisedSupport = (AdvisedSupport) BeanUtil.getFieldValue(dynamicAdvisedInterceptor, "advised"); | ||
| 36 | + Object target = advisedSupport.getTargetSource().getTarget(); | ||
| 37 | + return target; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception { | ||
| 41 | + AopProxy aopProxy = (AopProxy) BeanUtil.getFieldValue(proxy, "h"); | ||
| 42 | + AdvisedSupport advisedSupport = (AdvisedSupport) BeanUtil.getFieldValue(aopProxy, "advised"); | ||
| 43 | + Object target = advisedSupport.getTargetSource().getTarget(); | ||
| 44 | + return target; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | +} | ||
| 0 | \ No newline at end of file | 48 | \ No newline at end of file |