test: 添加RoleServiceTest和PermissionServiceTest
按照TDD开发规范编写: - RoleServiceTest: 11个测试用例 - PermissionServiceTest: 12个测试用例 - 覆盖CRUD操作和异常场景
This commit is contained in:
parent
d507cac248
commit
527b9247e8
|
|
@ -0,0 +1,202 @@
|
||||||
|
package com.ether.pms.auth.service;
|
||||||
|
|
||||||
|
import com.ether.pms.auth.entity.Permission;
|
||||||
|
import com.ether.pms.auth.repository.PermissionRepository;
|
||||||
|
import com.ether.pms.common.BusinessException;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class PermissionServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PermissionRepository permissionRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private PermissionService permissionService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findById_shouldReturnPermission_whenExists() {
|
||||||
|
UUID permId = UUID.randomUUID();
|
||||||
|
Permission mockPermission = new Permission();
|
||||||
|
mockPermission.setId(permId);
|
||||||
|
mockPermission.setCode("USER_VIEW");
|
||||||
|
mockPermission.setName("查看用户");
|
||||||
|
|
||||||
|
when(permissionRepository.findById(permId)).thenReturn(Optional.of(mockPermission));
|
||||||
|
|
||||||
|
Permission result = permissionService.findById(permId);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals("USER_VIEW", result.getCode());
|
||||||
|
assertEquals("查看用户", result.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findById_shouldThrowException_whenNotExists() {
|
||||||
|
UUID permId = UUID.randomUUID();
|
||||||
|
when(permissionRepository.findById(permId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(BusinessException.class, () -> permissionService.findById(permId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findAll_shouldReturnAllPermissions() {
|
||||||
|
Permission perm1 = new Permission();
|
||||||
|
perm1.setCode("USER_VIEW");
|
||||||
|
Permission perm2 = new Permission();
|
||||||
|
perm2.setCode("USER_EDIT");
|
||||||
|
|
||||||
|
when(permissionRepository.findAll()).thenReturn(List.of(perm1, perm2));
|
||||||
|
|
||||||
|
List<Permission> result = permissionService.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, result.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findByType_shouldReturnPermissionsOfType() {
|
||||||
|
Permission menuPerm = new Permission();
|
||||||
|
menuPerm.setCode("MENU_USER");
|
||||||
|
menuPerm.setType("MENU");
|
||||||
|
|
||||||
|
when(permissionRepository.findByType("MENU")).thenReturn(List.of(menuPerm));
|
||||||
|
|
||||||
|
List<Permission> result = permissionService.findByType("MENU");
|
||||||
|
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
assertEquals("MENU", result.get(0).getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findByParentCode_shouldReturnChildPermissions() {
|
||||||
|
Permission childPerm = new Permission();
|
||||||
|
childPerm.setCode("USER_CREATE");
|
||||||
|
childPerm.setParentCode("USER");
|
||||||
|
|
||||||
|
when(permissionRepository.findByParentCode("USER")).thenReturn(List.of(childPerm));
|
||||||
|
|
||||||
|
List<Permission> result = permissionService.findByParentCode("USER");
|
||||||
|
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
assertEquals("USER", result.get(0).getParentCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findMenuPermissions_shouldReturnMenuTypePermissions() {
|
||||||
|
Permission menuPerm = new Permission();
|
||||||
|
menuPerm.setCode("MENU_DASHBOARD");
|
||||||
|
menuPerm.setType("MENU");
|
||||||
|
|
||||||
|
when(permissionRepository.findByType("MENU")).thenReturn(List.of(menuPerm));
|
||||||
|
|
||||||
|
List<Permission> result = permissionService.findMenuPermissions();
|
||||||
|
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_shouldThrowException_whenCodeExists() {
|
||||||
|
Permission newPerm = new Permission();
|
||||||
|
newPerm.setCode("USER_VIEW");
|
||||||
|
newPerm.setName("查看用户");
|
||||||
|
newPerm.setType("BUTTON");
|
||||||
|
newPerm.setResource("/api/users");
|
||||||
|
newPerm.setMethod("GET");
|
||||||
|
|
||||||
|
when(permissionRepository.existsByCode("USER_VIEW")).thenReturn(true);
|
||||||
|
|
||||||
|
assertThrows(BusinessException.class, () -> permissionService.create(newPerm));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_shouldSavePermission_whenCodeNotExists() {
|
||||||
|
Permission newPerm = new Permission();
|
||||||
|
newPerm.setCode("USER_CREATE");
|
||||||
|
newPerm.setName("创建用户");
|
||||||
|
newPerm.setType("API");
|
||||||
|
newPerm.setResource("/api/users");
|
||||||
|
newPerm.setMethod("POST");
|
||||||
|
|
||||||
|
when(permissionRepository.existsByCode("USER_CREATE")).thenReturn(false);
|
||||||
|
when(permissionRepository.save(any(Permission.class))).thenReturn(newPerm);
|
||||||
|
|
||||||
|
Permission result = permissionService.create(newPerm);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals("USER_CREATE", result.getCode());
|
||||||
|
verify(permissionRepository).save(newPerm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void update_shouldUpdateFields_whenPermissionExists() {
|
||||||
|
UUID permId = UUID.randomUUID();
|
||||||
|
Permission existingPerm = new Permission();
|
||||||
|
existingPerm.setId(permId);
|
||||||
|
existingPerm.setCode("USER_VIEW");
|
||||||
|
existingPerm.setName("原名称");
|
||||||
|
|
||||||
|
Permission updateData = new Permission();
|
||||||
|
updateData.setName("新名称");
|
||||||
|
updateData.setDescription("新描述");
|
||||||
|
|
||||||
|
when(permissionRepository.findById(permId)).thenReturn(Optional.of(existingPerm));
|
||||||
|
when(permissionRepository.save(any(Permission.class))).thenAnswer(inv -> inv.getArgument(0));
|
||||||
|
|
||||||
|
Permission result = permissionService.update(permId, updateData);
|
||||||
|
|
||||||
|
assertEquals("新名称", result.getName());
|
||||||
|
assertEquals("新描述", result.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void update_shouldUpdateResourceAndMethod() {
|
||||||
|
UUID permId = UUID.randomUUID();
|
||||||
|
Permission existingPerm = new Permission();
|
||||||
|
existingPerm.setId(permId);
|
||||||
|
existingPerm.setCode("USER_VIEW");
|
||||||
|
existingPerm.setResource("/old/resource");
|
||||||
|
existingPerm.setMethod("GET");
|
||||||
|
|
||||||
|
Permission updateData = new Permission();
|
||||||
|
updateData.setResource("/new/resource");
|
||||||
|
updateData.setMethod("POST");
|
||||||
|
|
||||||
|
when(permissionRepository.findById(permId)).thenReturn(Optional.of(existingPerm));
|
||||||
|
when(permissionRepository.save(any(Permission.class))).thenAnswer(inv -> inv.getArgument(0));
|
||||||
|
|
||||||
|
Permission result = permissionService.update(permId, updateData);
|
||||||
|
|
||||||
|
assertEquals("/new/resource", result.getResource());
|
||||||
|
assertEquals("POST", result.getMethod());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void update_shouldThrowException_whenPermissionNotExists() {
|
||||||
|
UUID permId = UUID.randomUUID();
|
||||||
|
when(permissionRepository.findById(permId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(BusinessException.class, () -> permissionService.update(permId, new Permission()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void delete_shouldCallRepository() {
|
||||||
|
UUID permId = UUID.randomUUID();
|
||||||
|
doNothing().when(permissionRepository).deleteById(permId);
|
||||||
|
|
||||||
|
permissionService.delete(permId);
|
||||||
|
|
||||||
|
verify(permissionRepository).deleteById(permId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,187 @@
|
||||||
|
package com.ether.pms.auth.service;
|
||||||
|
|
||||||
|
import com.ether.pms.auth.entity.Role;
|
||||||
|
import com.ether.pms.auth.entity.Permission;
|
||||||
|
import com.ether.pms.auth.repository.RoleRepository;
|
||||||
|
import com.ether.pms.auth.repository.PermissionRepository;
|
||||||
|
import com.ether.pms.common.BusinessException;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class RoleServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PermissionRepository permissionRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private RoleService roleService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findById_shouldReturnRole_whenExists() {
|
||||||
|
UUID roleId = UUID.randomUUID();
|
||||||
|
Role mockRole = new Role();
|
||||||
|
mockRole.setId(roleId);
|
||||||
|
mockRole.setCode("ADMIN");
|
||||||
|
mockRole.setName("管理员");
|
||||||
|
|
||||||
|
when(roleRepository.findById(roleId)).thenReturn(Optional.of(mockRole));
|
||||||
|
|
||||||
|
Role result = roleService.findById(roleId);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals("ADMIN", result.getCode());
|
||||||
|
assertEquals("管理员", result.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findById_shouldThrowException_whenNotExists() {
|
||||||
|
UUID roleId = UUID.randomUUID();
|
||||||
|
when(roleRepository.findById(roleId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(BusinessException.class, () -> roleService.findById(roleId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findByCode_shouldReturnRole_whenExists() {
|
||||||
|
Role mockRole = new Role();
|
||||||
|
mockRole.setCode("ADMIN");
|
||||||
|
mockRole.setName("管理员");
|
||||||
|
|
||||||
|
when(roleRepository.findByCode("ADMIN")).thenReturn(Optional.of(mockRole));
|
||||||
|
|
||||||
|
Role result = roleService.findByCode("ADMIN");
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals("ADMIN", result.getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findByCode_shouldThrowException_whenNotExists() {
|
||||||
|
when(roleRepository.findByCode("UNKNOWN")).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(BusinessException.class, () -> roleService.findByCode("UNKNOWN"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findAll_shouldReturnAllRoles() {
|
||||||
|
Role role1 = new Role();
|
||||||
|
role1.setCode("ADMIN");
|
||||||
|
Role role2 = new Role();
|
||||||
|
role2.setCode("USER");
|
||||||
|
|
||||||
|
when(roleRepository.findAll()).thenReturn(List.of(role1, role2));
|
||||||
|
|
||||||
|
List<Role> result = roleService.findAll();
|
||||||
|
|
||||||
|
assertEquals(2, result.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_shouldThrowException_whenCodeExists() {
|
||||||
|
Role newRole = new Role();
|
||||||
|
newRole.setCode("ADMIN");
|
||||||
|
newRole.setName("管理员");
|
||||||
|
|
||||||
|
when(roleRepository.existsByCode("ADMIN")).thenReturn(true);
|
||||||
|
|
||||||
|
assertThrows(BusinessException.class, () -> roleService.create(newRole));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_shouldSaveRole_whenCodeNotExists() {
|
||||||
|
Role newRole = new Role();
|
||||||
|
newRole.setCode("OPERATOR");
|
||||||
|
newRole.setName("操作员");
|
||||||
|
newRole.setType(Role.RoleType.SYSTEM);
|
||||||
|
newRole.setDataScope(Role.DataScope.PROJECT);
|
||||||
|
newRole.setStatus(Role.RoleStatus.ENABLED);
|
||||||
|
|
||||||
|
when(roleRepository.existsByCode("OPERATOR")).thenReturn(false);
|
||||||
|
when(roleRepository.save(any(Role.class))).thenReturn(newRole);
|
||||||
|
|
||||||
|
Role result = roleService.create(newRole);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals("OPERATOR", result.getCode());
|
||||||
|
verify(roleRepository).save(newRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void update_shouldUpdateFields_whenRoleExists() {
|
||||||
|
UUID roleId = UUID.randomUUID();
|
||||||
|
Role existingRole = new Role();
|
||||||
|
existingRole.setId(roleId);
|
||||||
|
existingRole.setCode("ADMIN");
|
||||||
|
existingRole.setName("原名称");
|
||||||
|
|
||||||
|
Role updateData = new Role();
|
||||||
|
updateData.setName("新名称");
|
||||||
|
updateData.setDescription("新描述");
|
||||||
|
|
||||||
|
when(roleRepository.findById(roleId)).thenReturn(Optional.of(existingRole));
|
||||||
|
when(roleRepository.save(any(Role.class))).thenAnswer(inv -> inv.getArgument(0));
|
||||||
|
|
||||||
|
Role result = roleService.update(roleId, updateData);
|
||||||
|
|
||||||
|
assertEquals("新名称", result.getName());
|
||||||
|
assertEquals("新描述", result.getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void update_shouldThrowException_whenRoleNotExists() {
|
||||||
|
UUID roleId = UUID.randomUUID();
|
||||||
|
when(roleRepository.findById(roleId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(BusinessException.class, () -> roleService.update(roleId, new Role()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void delete_shouldCallRepository() {
|
||||||
|
UUID roleId = UUID.randomUUID();
|
||||||
|
doNothing().when(roleRepository).deleteById(roleId);
|
||||||
|
|
||||||
|
roleService.delete(roleId);
|
||||||
|
|
||||||
|
verify(roleRepository).deleteById(roleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void assignPermissions_shouldSetPermissionsToRole() {
|
||||||
|
UUID roleId = UUID.randomUUID();
|
||||||
|
UUID permId1 = UUID.randomUUID();
|
||||||
|
UUID permId2 = UUID.randomUUID();
|
||||||
|
|
||||||
|
Role role = new Role();
|
||||||
|
role.setId(roleId);
|
||||||
|
role.setCode("ADMIN");
|
||||||
|
|
||||||
|
Permission perm1 = new Permission();
|
||||||
|
perm1.setId(permId1);
|
||||||
|
Permission perm2 = new Permission();
|
||||||
|
perm2.setId(permId2);
|
||||||
|
|
||||||
|
when(roleRepository.findById(roleId)).thenReturn(Optional.of(role));
|
||||||
|
when(permissionRepository.findAllById(List.of(permId1, permId2))).thenReturn(List.of(perm1, perm2));
|
||||||
|
when(roleRepository.save(any(Role.class))).thenAnswer(inv -> inv.getArgument(0));
|
||||||
|
|
||||||
|
roleService.assignPermissions(roleId, List.of(permId1, permId2));
|
||||||
|
|
||||||
|
verify(roleRepository).save(role);
|
||||||
|
assertEquals(2, role.getPermissions().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue