diff --git a/src/test/java/com/example/RoleMatrixServiceTest.java b/src/test/java/com/example/RoleMatrixServiceTest.java new file mode 100644 index 0000000..0f09729 --- /dev/null +++ b/src/test/java/com/example/RoleMatrixServiceTest.java @@ -0,0 +1,232 @@ +package com.example; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +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.Arrays; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +@DisplayName("RoleMatrixService 검증 테스트") +class RoleMatrixServiceTest { + + @Mock + private RoleMatrixRepository repository; + + @InjectMocks + private RoleMatrixService service; + + private RoleMatrix testMatrix; + + @BeforeEach + void setUp() { + testMatrix = new RoleMatrix(); + testMatrix.setId(1L); + testMatrix.setName("Admin Role"); + testMatrix.setPermissions(Arrays.asList("READ", "WRITE", "DELETE")); + } + + @Test + @DisplayName("역할 매트릭스 조회 성공") + void getRoleMatrix_Success() { + // Given + when(repository.findById(1L)).thenReturn(testMatrix); + + // When + RoleMatrix result = service.getRoleMatrix(1L); + + // Then + assertNotNull(result); + assertEquals("Admin Role", result.getName()); + assertEquals(3, result.getPermissions().size()); + verify(repository, times(1)).findById(1L); + } + + @Test + @DisplayName("역할 매트릭스 조회 실패 - 존재하지 않는 ID") + void getRoleMatrix_NotFound() { + // Given + when(repository.findById(999L)).thenReturn(null); + + // When & Then + assertThrows(RoleNotFoundException.class, () -> service.getRoleMatrix(999L)); + verify(repository, times(1)).findById(999L); + } + + @Test + @DisplayName("모든 역할 매트릭스 조회 성공") + void getAllRoleMatrices_Success() { + // Given + RoleMatrix matrix2 = new RoleMatrix(); + matrix2.setId(2L); + matrix2.setName("User Role"); + matrix2.setPermissions(Arrays.asList("READ")); + + when(repository.findAll()).thenReturn(Arrays.asList(testMatrix, matrix2)); + + // When + List result = service.getAllRoleMatrices(); + + // Then + assertNotNull(result); + assertEquals(2, result.size()); + verify(repository, times(1)).findAll(); + } + + @Test + @DisplayName("역할 매트릭스 생성 성공") + void createRoleMatrix_Success() { + // Given + RoleMatrix newMatrix = new RoleMatrix(); + newMatrix.setName("New Role"); + newMatrix.setPermissions(Arrays.asList("READ", "WRITE")); + + when(repository.save(any(RoleMatrix.class))).thenReturn(newMatrix); + + // When + RoleMatrix result = service.createRoleMatrix(newMatrix); + + // Then + assertNotNull(result); + assertEquals("New Role", result.getName()); + verify(repository, times(1)).save(any(RoleMatrix.class)); + } + + @Test + @DisplayName("역할 매트릭스 업데이트 성공") + void updateRoleMatrix_Success() { + // Given + when(repository.findById(1L)).thenReturn(testMatrix); + when(repository.save(any(RoleMatrix.class))).thenReturn(testMatrix); + + RoleMatrix updateData = new RoleMatrix(); + updateData.setName("Updated Admin Role"); + updateData.setPermissions(Arrays.asList("READ", "WRITE")); + + // When + RoleMatrix result = service.updateRoleMatrix(1L, updateData); + + // Then + assertNotNull(result); + assertEquals("Updated Admin Role", result.getName()); + verify(repository, times(1)).findById(1L); + verify(repository, times(1)).save(any(RoleMatrix.class)); + } + + @Test + @DisplayName("역할 매트릭스 삭제 성공") + void deleteRoleMatrix_Success() { + // Given + when(repository.existsById(1L)).thenReturn(true); + doNothing().when(repository).deleteById(1L); + + // When + service.deleteRoleMatrix(1L); + + // Then + verify(repository, times(1)).existsById(1L); + verify(repository, times(1)).deleteById(1L); + } + + @Test + @DisplayName("역할 매트릭스 삭제 실패 - 존재하지 않는 ID") + void deleteRoleMatrix_NotFound() { + // Given + when(repository.existsById(999L)).thenReturn(false); + + // When & Then + assertThrows(RoleNotFoundException.class, () -> service.deleteRoleMatrix(999L)); + verify(repository, times(1)).existsById(999L); + verify(repository, never()).deleteById(any()); + } + + @Test + @DisplayName("권한 검증 성공") + void hasPermission_Success() { + // Given + when(repository.findById(1L)).thenReturn(testMatrix); + + // When + boolean result = service.hasPermission(1L, "READ"); + + // Then + assertTrue(result); + verify(repository, times(1)).findById(1L); + } + + @Test + @DisplayName("권한 검증 실패 - 권한 없음") + void hasPermission_NoPermission() { + // Given + when(repository.findById(1L)).thenReturn(testMatrix); + + // When + boolean result = service.hasPermission(1L, "ADMIN"); + + // Then + assertFalse(result); + verify(repository, times(1)).findById(1L); + } + + @Test + @DisplayName("역할별 권한 매트릭스 조회 성공") + void getPermissionsByRole_Success() { + // Given + when(repository.findById(1L)).thenReturn(testMatrix); + + // When + Map result = service.getPermissionsByRole(1L); + + // Then + assertNotNull(result); + assertEquals(3, result.size()); + assertTrue(result.get("READ")); + assertTrue(result.get("WRITE")); + assertTrue(result.get("DELETE")); + verify(repository, times(1)).findById(1L); + } + + @Test + @DisplayName("빈 역할 목록 조회") + void getAllRoleMatrices_EmptyList() { + // Given + when(repository.findAll()).thenReturn(Arrays.asList()); + + // When + List result = service.getAllRoleMatrices(); + + // Then + assertNotNull(result); + assertTrue(result.isEmpty()); + verify(repository, times(1)).findAll(); + } + + @Test + @DisplayName("캐시된 역할 매트릭스 조회") + void getRoleMatrix_Cached() { + // Given + when(repository.findById(1L)).thenReturn(testMatrix); + + // When - 첫 번째 호출 + RoleMatrix result1 = service.getRoleMatrix(1L); + // When - 두 번째 호출 (캐시에서) + RoleMatrix result2 = service.getRoleMatrix(1L); + + // Then + assertNotNull(result1); + assertNotNull(result2); + assertEquals(result1.getName(), result2.getName()); + // 캐시로 인해 repository는 한 번만 호출되어야 함 + verify(repository, times(1)).findById(1L); + } +}