Commit d2e773e84946aa445cdbd95a107e834ee633f184

Authored by timfruit
1 parent 310a436c

根据艿艿提示修改

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 15 import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService;
16 16 import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
17 17 import cn.iocoder.dashboard.util.collection.CollectionUtils;
  18 +import com.google.common.annotations.VisibleForTesting;
18 19 import com.google.common.collect.ImmutableMap;
19 20 import com.google.common.collect.ImmutableMultimap;
20 21 import com.google.common.collect.Multimap;
... ... @@ -246,7 +247,8 @@ public class SysMenuServiceImpl implements SysMenuService {
246 247 * @param parentId 父菜单编号
247 248 * @param childId 当前菜单编号
248 249 */
249   - private void checkParentResource(Long parentId, Long childId) {
  250 + @VisibleForTesting
  251 + public void checkParentResource(Long parentId, Long childId) {
250 252 if (parentId == null || MenuIdEnum.ROOT.getId().equals(parentId)) {
251 253 return;
252 254 }
... ... @@ -275,7 +277,8 @@ public class SysMenuServiceImpl implements SysMenuService {
275 277 * @param parentId 父菜单编号
276 278 * @param id 菜单编号
277 279 */
278   - private void checkResource(Long parentId, String name, Long id) {
  280 + @VisibleForTesting
  281 + public void checkResource(Long parentId, String name, Long id) {
279 282 SysMenuDO menu = menuMapper.selectByParentIdAndName(parentId, name);
280 283 if (menu == null) {
281 284 return;
... ...
src/test/java/cn/iocoder/dashboard/modules/system/service/permission/SysMenuServiceTest.java
... ... @@ -26,7 +26,9 @@ import javax.annotation.Resource;
26 26 import java.util.*;
27 27  
28 28 import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*;
29   -import static cn.iocoder.dashboard.util.RandomUtils.randomPojo;
  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.*;
30 32 import static org.junit.jupiter.api.Assertions.assertEquals;
31 33 import static org.mockito.Mockito.verify;
32 34  
... ... @@ -59,14 +61,14 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
59 61 SysMenuServiceImpl target = (SysMenuServiceImpl) AopTargetUtils.getTarget(sysMenuService);
60 62  
61 63 Map<Long, SysMenuDO> menuCache = (Map<Long, SysMenuDO>) BeanUtil.getFieldValue(target, "menuCache");
62   - Assert.isTrue(menuCache.size()==2);
63   - AssertUtils.assertPojoEquals(menuDO1,menuCache.get(menuDO1.getId()));
64   - AssertUtils.assertPojoEquals(menuDO2,menuCache.get(menuDO2.getId()));
  64 + Assert.isTrue(menuCache.size() == 2);
  65 + assertPojoEquals(menuDO1, menuCache.get(menuDO1.getId()));
  66 + assertPojoEquals(menuDO2, menuCache.get(menuDO2.getId()));
65 67  
66 68 Multimap<String, SysMenuDO> permissionMenuCache = (Multimap<String, SysMenuDO>) BeanUtil.getFieldValue(target, "permissionMenuCache");
67   - Assert.isTrue(permissionMenuCache.size()==2);
68   - AssertUtils.assertPojoEquals(menuDO1,permissionMenuCache.get(menuDO1.getPermission()));
69   - AssertUtils.assertPojoEquals(menuDO2,permissionMenuCache.get(menuDO2.getPermission()));
  69 + Assert.isTrue(permissionMenuCache.size() == 2);
  70 + assertPojoEquals(menuDO1, permissionMenuCache.get(menuDO1.getPermission()));
  71 + assertPojoEquals(menuDO2, permissionMenuCache.get(menuDO2.getPermission()));
70 72  
71 73 Date maxUpdateTime = (Date) BeanUtil.getFieldValue(target, "maxUpdateTime");
72 74 assertEquals(ObjectUtils.max(menuDO1.getUpdateTime(), menuDO2.getUpdateTime()), maxUpdateTime);
... ... @@ -92,47 +94,12 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
92 94 Assertions.assertNotNull(menuId);
93 95 // 校验记录的属性是否正确
94 96 SysMenuDO ret = menuMapper.selectById(menuId);
95   - AssertUtils.assertPojoEquals(vo, ret);
  97 + assertPojoEquals(vo, ret);
96 98 // 校验调用
97 99 verify(sysMenuProducer).sendMenuRefreshMessage();
98 100 }
99 101  
100 102 @Test
101   - public void testCreateMenu_checkParentExist() {
102   - Long parentId = RandomUtils.randomLongId();
103   -
104   - // 调用
105   - SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> {
106   - o.setParentId(parentId);
107   - o.setName("testSonName");
108   - o.setType(MenuTypeEnum.MENU.getType());
109   - o.setStatus(RandomUtils.randomCommonStatus());
110   - });
111   -
112   - // 调用, 并断言异常
113   - AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_EXISTS);
114   - }
115   -
116   - @Test
117   - public void testCreateMenu_checkParentTypeError() {
118   - //构造父目录
119   - SysMenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L);
120   - menuMapper.insert(menuDO);
121   - Long parentId = menuDO.getId();
122   -
123   - // 调用
124   - SysMenuCreateReqVO vo = randomPojo(SysMenuCreateReqVO.class, o -> {
125   - o.setParentId(parentId);
126   - o.setName("testSonName");
127   - o.setType(MenuTypeEnum.MENU.getType());
128   - o.setStatus(RandomUtils.randomCommonStatus());
129   - });
130   -
131   - // 调用, 并断言异常
132   - AssertUtils.assertServiceException(() -> sysMenuService.createMenu(vo), MENU_PARENT_NOT_DIR_OR_MENU);
133   - }
134   -
135   - @Test
136 103 public void testUpdateMenu_success() {
137 104 //构造父子目录
138 105 SysMenuDO sonMenuDO = initParentAndSonMenuDO();
... ... @@ -152,7 +119,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
152 119 //断言
153 120 // 校验记录的属性是否正确
154 121 SysMenuDO ret = menuMapper.selectById(sonId);
155   - AssertUtils.assertPojoEquals(vo, ret);
  122 + assertPojoEquals(vo, ret);
156 123 // 校验调用
157 124 verify(sysMenuProducer).sendMenuRefreshMessage();
158 125 }
... ... @@ -170,102 +137,14 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
170 137 o.setStatus(RandomUtils.randomCommonStatus());
171 138 });
172 139 //断言
173   - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NOT_EXISTS);
174   - }
175   -
176   - @Test
177   - public void testUpdateMenu_canNotSetSelfToBeParent() {
178   - //构造父子目录
179   - SysMenuDO sonMenuDO = initParentAndSonMenuDO();
180   - Long sonId = sonMenuDO.getId();
181   -
182   - long updateParentId = sonId;
183   -
184   - //调用
185   - SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> {
186   - o.setId(sonId);
187   - o.setParentId(updateParentId);
188   - o.setType(MenuTypeEnum.MENU.getType());
189   - o.setStatus(RandomUtils.randomCommonStatus());
190   - });
191   - //断言
192   - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_ERROR);
193   - }
194   -
195   - @Test
196   - public void testUpdateMenu_parentNotExist() {
197   - //构造父子目录
198   - SysMenuDO sonMenuDO = initParentAndSonMenuDO();
199   - Long sonId = sonMenuDO.getId();
200   -
201   - long updateParentId = -999999;
202   -
203   - //调用
204   - SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> {
205   - o.setId(sonId);
206   - o.setParentId(updateParentId);
207   - o.setType(MenuTypeEnum.MENU.getType());
208   - o.setStatus(RandomUtils.randomCommonStatus());
209   - });
210   - //断言
211   - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_NOT_EXISTS);
212   - }
213   -
214   - @Test
215   - public void testUpdateMenu_parentTypeError() {
216   - SysMenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "son", 999L);
217   - menuMapper.insert(sonMenuDO);
218   - Long sonId = sonMenuDO.getId();
219   -
220   - //button类型
221   - SysMenuDO menuDO = createMenuDO(MenuTypeEnum.BUTTON, "parent", 0L);
222   - menuMapper.insert(menuDO);
223   - Long parentId = menuDO.getId();
224   -
225   - //调用
226   - SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> {
227   - o.setId(sonId);
228   - o.setParentId(parentId);
229   - o.setType(MenuTypeEnum.MENU.getType());
230   - o.setStatus(RandomUtils.randomCommonStatus());
231   - });
232   - //断言
233   - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_PARENT_NOT_DIR_OR_MENU);
234   - }
235   -
236   - @Test
237   - public void testUpdateMenu_sonMenuNameDuplicate() {
238   - SysMenuDO parent = createMenuDO(MenuTypeEnum.MENU, "parent", 999L);
239   - menuMapper.insert(parent);
240   - Long parentId = parent.getId();
241   -
242   -
243   - SysMenuDO sonMenuDO = createMenuDO(MenuTypeEnum.MENU, "son", parentId);
244   - menuMapper.insert(sonMenuDO);
245   -
246   - SysMenuDO sonMenuDO2 = createMenuDO(MenuTypeEnum.MENU, "son2", parentId);
247   - menuMapper.insert(sonMenuDO2);
248   -
249   - Long sonId = sonMenuDO.getId();
250   - String updateName = sonMenuDO2.getName();
251   -
252   - //调用
253   - SysMenuUpdateReqVO vo = RandomUtils.randomPojo(SysMenuUpdateReqVO.class, o -> {
254   - o.setId(sonId);
255   - o.setParentId(parentId);
256   - o.setType(MenuTypeEnum.MENU.getType());
257   - o.setStatus(RandomUtils.randomCommonStatus());
258   - o.setName(updateName);
259   - });
260   - //断言
261   - AssertUtils.assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NAME_DUPLICATE);
  140 + assertServiceException(() -> sysMenuService.updateMenu(vo), MENU_NOT_EXISTS);
262 141 }
263 142  
264 143 @Test
265 144 public void testDeleteMenu_success() {
266 145 SysMenuDO sonMenuDO = initParentAndSonMenuDO();
267   -
268 146 Long sonId = sonMenuDO.getId();
  147 +
269 148 //调用
270 149 sysMenuService.deleteMenu(sonId);
271 150  
... ... @@ -280,7 +159,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
280 159 public void testDeleteMenu_menuNotExist() {
281 160 Long sonId = 99999L;
282 161  
283   - AssertUtils.assertServiceException(() -> sysMenuService.deleteMenu(sonId), MENU_NOT_EXISTS);
  162 + assertServiceException(() -> sysMenuService.deleteMenu(sonId), MENU_NOT_EXISTS);
284 163 }
285 164  
286 165 @Test
... ... @@ -288,7 +167,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
288 167 SysMenuDO sonMenu = initParentAndSonMenuDO();
289 168 Long parentId = sonMenu.getParentId();
290 169  
291   - AssertUtils.assertServiceException(() -> sysMenuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN);
  170 + assertServiceException(() -> sysMenuService.deleteMenu(parentId), MENU_EXISTS_CHILDREN);
292 171 }
293 172  
294 173 @Test
... ... @@ -307,15 +186,14 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
307 186  
308 187 //断言
309 188 Assert.isTrue(menuDOS.size() == idMenuMap.size());
310   - for (SysMenuDO menu : menuDOS) {
311   - AssertUtils.assertPojoEquals(idMenuMap.get(menu.getId()), menu);
312   - }
  189 + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
313 190 }
314 191  
315 192  
316 193 @Test
317 194 public void testGetMenusReqVo_success() {
318 195 Map<Long, SysMenuDO> idMenuMap = new HashMap<>();
  196 + //用于验证可以模糊搜索名称包含"name",状态为1的menu
319 197 SysMenuDO menu = createMenuDO(MenuTypeEnum.MENU, "name2", 0L, 1);
320 198 menuMapper.insert(menu);
321 199 idMenuMap.put(menu.getId(), menu);
... ... @@ -328,13 +206,12 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
328 206 menuMapper.insert(menu);
329 207 idMenuMap.put(menu.getId(), menu);
330 208  
331   -
  209 + //以下是不符合搜索条件的的menu
332 210 menu = createMenuDO(MenuTypeEnum.MENU, "xxxxxx", 0L, 1);
333 211 menuMapper.insert(menu);
334 212 menu = createMenuDO(MenuTypeEnum.MENU, "name", 0L, 2);
335 213 menuMapper.insert(menu);
336 214  
337   -
338 215 //调用
339 216 SysMenuListReqVO reqVO = new SysMenuListReqVO();
340 217 reqVO.setStatus(1);
... ... @@ -343,9 +220,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
343 220  
344 221 //断言
345 222 Assert.isTrue(menuDOS.size() == idMenuMap.size());
346   - for (SysMenuDO m : menuDOS) {
347   - AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m);
348   - }
  223 + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
349 224 }
350 225  
351 226 @Test
... ... @@ -356,6 +231,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
356 231 BeanUtil.setFieldValue(target, "menuCache", mockCacheMap);
357 232  
358 233 Map<Long, SysMenuDO> idMenuMap = new HashMap<>();
  234 + //用于验证搜索类型为MENU,状态为1的menu
359 235 SysMenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1);
360 236 mockCacheMap.put(menuDO.getId(), menuDO);
361 237 idMenuMap.put(menuDO.getId(), menuDO);
... ... @@ -364,7 +240,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
364 240 mockCacheMap.put(menuDO.getId(), menuDO);
365 241 idMenuMap.put(menuDO.getId(), menuDO);
366 242  
367   -
  243 + //以下是不符合搜索条件的menu
368 244 menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1);
369 245 mockCacheMap.put(menuDO.getId(), menuDO);
370 246 menuDO = createMenuDO(4L, MenuTypeEnum.MENU, "name", 0L, 2);
... ... @@ -372,9 +248,7 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
372 248  
373 249 List<SysMenuDO> menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1));
374 250 Assert.isTrue(menuDOS.size() == idMenuMap.size());
375   - for (SysMenuDO m : menuDOS) {
376   - AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m);
377   - }
  251 + menuDOS.stream().forEach(m -> assertPojoEquals(idMenuMap.get(m.getId()), m));
378 252 }
379 253  
380 254  
... ... @@ -386,10 +260,12 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
386 260 BeanUtil.setFieldValue(target, "menuCache", mockCacheMap);
387 261  
388 262 Map<Long, SysMenuDO> idMenuMap = new HashMap<>();
  263 + //验证搜索id为1, 类型为MENU, 状态为1 的menu
389 264 SysMenuDO menuDO = createMenuDO(1L, MenuTypeEnum.MENU, "name", 0L, 1);
390 265 mockCacheMap.put(menuDO.getId(), menuDO);
391 266 idMenuMap.put(menuDO.getId(), menuDO);
392 267  
  268 + //以下是不符合搜索条件的menu
393 269 menuDO = createMenuDO(2L, MenuTypeEnum.MENU, "name", 0L, 1);
394 270 mockCacheMap.put(menuDO.getId(), menuDO);
395 271 menuDO = createMenuDO(3L, MenuTypeEnum.BUTTON, "name", 0L, 1);
... ... @@ -400,9 +276,57 @@ public class SysMenuServiceTest extends BaseDbUnitTest {
400 276 List<SysMenuDO> menuDOS = sysMenuService.listMenusFromCache(Arrays.asList(1L),
401 277 Arrays.asList(MenuTypeEnum.MENU.getType()), Arrays.asList(1));
402 278 Assert.isTrue(menuDOS.size() == idMenuMap.size());
403   - for (SysMenuDO m : menuDOS) {
404   - AssertUtils.assertPojoEquals(idMenuMap.get(m.getId()), m);
405   - }
  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);
406 330 }
407 331  
408 332 /**
... ...
src/test/java/cn/iocoder/dashboard/util/AopTargetUtils.java
1 1 package cn.iocoder.dashboard.util;
2 2  
  3 +import cn.hutool.core.bean.BeanUtil;
3 4 import org.springframework.aop.framework.AdvisedSupport;
4 5 import org.springframework.aop.framework.AopProxy;
5 6 import org.springframework.aop.support.AopUtils;
... ... @@ -11,7 +12,6 @@ import java.lang.reflect.Field;
11 12 */
12 13 public class AopTargetUtils {
13 14  
14   -
15 15 /**
16 16 * 获取 目标对象
17 17 *
... ... @@ -20,43 +20,27 @@ public class AopTargetUtils {
20 20 * @throws Exception
21 21 */
22 22 public static Object getTarget(Object proxy) throws Exception {
23   -
24 23 if (!AopUtils.isAopProxy(proxy)) {
25 24 return proxy; //不是代理对象
26 25 }
27   -
28 26 if (AopUtils.isJdkDynamicProxy(proxy)) {
29 27 return getJdkDynamicProxyTargetObject(proxy);
30 28 } else { //cglib
31 29 return getCglibProxyTargetObject(proxy);
32 30 }
33   -
34 31 }
35 32  
36   -
37 33 private static Object getCglibProxyTargetObject(Object proxy) throws Exception {
38   - Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
39   - h.setAccessible(true);
40   - Object dynamicAdvisedInterceptor = h.get(proxy);
41   -
42   - Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
43   - advised.setAccessible(true);
44   -
45   - Object target = ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
46   -
  34 + Object dynamicAdvisedInterceptor = BeanUtil.getFieldValue(proxy, "CGLIB$CALLBACK_0");
  35 + AdvisedSupport advisedSupport = (AdvisedSupport) BeanUtil.getFieldValue(dynamicAdvisedInterceptor, "advised");
  36 + Object target = advisedSupport.getTargetSource().getTarget();
47 37 return target;
48 38 }
49 39  
50 40 private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
51   - Field h = proxy.getClass().getSuperclass().getDeclaredField("h");
52   - h.setAccessible(true);
53   - AopProxy aopProxy = (AopProxy) h.get(proxy);
54   -
55   - Field advised = aopProxy.getClass().getDeclaredField("advised");
56   - advised.setAccessible(true);
57   -
58   - Object target = ((AdvisedSupport) advised.get(aopProxy)).getTargetSource().getTarget();
59   -
  41 + AopProxy aopProxy = (AopProxy) BeanUtil.getFieldValue(proxy, "h");
  42 + AdvisedSupport advisedSupport = (AdvisedSupport) BeanUtil.getFieldValue(aopProxy, "advised");
  43 + Object target = advisedSupport.getTargetSource().getTarget();
60 44 return target;
61 45 }
62 46  
... ...