|
|
@ -1,8 +1,11 @@ |
|
|
package com.crm.auth.service.impl; |
|
|
package com.crm.auth.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.crm.auth.domain.dto.ResourceNode; |
|
|
import com.crm.auth.domain.dto.ResourceNode; |
|
|
import com.crm.auth.domain.entity.SysMenu; |
|
|
import com.crm.auth.domain.entity.SysMenu; |
|
|
|
|
|
import com.crm.auth.domain.entity.SysRoleMenu; |
|
|
import com.crm.auth.domain.enums.MenuType; |
|
|
import com.crm.auth.domain.enums.MenuType; |
|
|
|
|
|
import com.crm.auth.mapper.SysRoleMenuMapper; |
|
|
import com.crm.auth.service.ISysMenuService; |
|
|
import com.crm.auth.service.ISysMenuService; |
|
|
import com.crm.base.domain.exception.BusinessErrorException; |
|
|
import com.crm.base.domain.exception.BusinessErrorException; |
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
@ -29,6 +32,8 @@ class ResourceServiceImplTest { |
|
|
|
|
|
|
|
|
@Mock |
|
|
@Mock |
|
|
private ISysMenuService sysMenuService; |
|
|
private ISysMenuService sysMenuService; |
|
|
|
|
|
@Mock |
|
|
|
|
|
private SysRoleMenuMapper sysRoleMenuMapper; |
|
|
|
|
|
|
|
|
@InjectMocks |
|
|
@InjectMocks |
|
|
private ResourceServiceImpl resourceService; |
|
|
private ResourceServiceImpl resourceService; |
|
|
@ -398,6 +403,64 @@ class ResourceServiceImplTest { |
|
|
assertThat(captor.getValue().getPath()).isEqualTo("/new/path"); |
|
|
assertThat(captor.getValue().getPath()).isEqualTo("/new/path"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ==================== 删除节点 ====================
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
@DisplayName("删除不存在的节点 -> 抛异常") |
|
|
|
|
|
void delete_nodeNotFound_throwsException() { |
|
|
|
|
|
when(sysMenuService.getById(999L)).thenReturn(null); |
|
|
|
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> resourceService.delete(999L)) |
|
|
|
|
|
.isInstanceOf(BusinessErrorException.class) |
|
|
|
|
|
.hasMessageContaining("不存在"); |
|
|
|
|
|
|
|
|
|
|
|
verify(sysMenuService, never()).removeById(any()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
@DisplayName("有子节点 -> 拒绝删除") |
|
|
|
|
|
void delete_hasChildren_rejected() { |
|
|
|
|
|
when(sysMenuService.getById(1L)).thenReturn(catalog); |
|
|
|
|
|
// stub count: parentId=1 有子节点
|
|
|
|
|
|
when(sysMenuService.count(any(LambdaQueryWrapper.class))).thenReturn(3L); |
|
|
|
|
|
|
|
|
|
|
|
assertThatThrownBy(() -> resourceService.delete(1L)) |
|
|
|
|
|
.isInstanceOf(BusinessErrorException.class) |
|
|
|
|
|
.hasMessageContaining("子节点"); |
|
|
|
|
|
|
|
|
|
|
|
verify(sysMenuService, never()).removeById(any()); |
|
|
|
|
|
verify(sysRoleMenuMapper, never()).delete(any(LambdaQueryWrapper.class)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
@DisplayName("叶子节点无授权引用 -> 删除成功") |
|
|
|
|
|
void delete_leafNoAuth_success() { |
|
|
|
|
|
when(sysMenuService.getById(100L)).thenReturn(button); |
|
|
|
|
|
when(sysMenuService.count(any(LambdaQueryWrapper.class))).thenReturn(0L); |
|
|
|
|
|
when(sysMenuService.removeById(100L)).thenReturn(true); |
|
|
|
|
|
|
|
|
|
|
|
resourceService.delete(100L); |
|
|
|
|
|
|
|
|
|
|
|
verify(sysMenuService).removeById(100L); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
@DisplayName("叶子节点有授权引用 -> 删除节点 + 级联清理 sys_role_menu") |
|
|
|
|
|
void delete_leafWithAuth_cascadeCleanup() { |
|
|
|
|
|
when(sysMenuService.getById(100L)).thenReturn(button); |
|
|
|
|
|
when(sysMenuService.count(any(LambdaQueryWrapper.class))).thenReturn(0L); |
|
|
|
|
|
when(sysMenuService.removeById(100L)).thenReturn(true); |
|
|
|
|
|
when(sysRoleMenuMapper.delete(any(LambdaQueryWrapper.class))).thenReturn(2); |
|
|
|
|
|
|
|
|
|
|
|
resourceService.delete(100L); |
|
|
|
|
|
|
|
|
|
|
|
verify(sysMenuService).removeById(100L); |
|
|
|
|
|
// 有引用,级联清理
|
|
|
|
|
|
ArgumentCaptor<LambdaQueryWrapper<SysRoleMenu>> captor = |
|
|
|
|
|
ArgumentCaptor.forClass(LambdaQueryWrapper.class); |
|
|
|
|
|
verify(sysRoleMenuMapper).delete(captor.capture()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// ==================== 辅助方法 ====================
|
|
|
// ==================== 辅助方法 ====================
|
|
|
|
|
|
|
|
|
private SysMenu buildMenu(Long id, Long parentId, String name, int menuType) { |
|
|
private SysMenu buildMenu(Long id, Long parentId, String name, int menuType) { |
|
|
|