chore(quarkus-327): bump to Quarkus 3.27.3 LTS, make pom autonomous, fix UserServiceImpl tests (search → searchByUsername), rename deprecated config keys
Some checks failed
CI/CD Pipeline / pipeline (push) Failing after 1m57s
Some checks failed
CI/CD Pipeline / pipeline (push) Failing after 1m57s
This commit is contained in:
@@ -1,233 +1,233 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.dto.audit.AuditLogDTO;
|
||||
import dev.lions.user.manager.dto.common.CountDTO;
|
||||
import dev.lions.user.manager.enums.audit.TypeActionAudit;
|
||||
import dev.lions.user.manager.service.AuditService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AuditResourceTest {
|
||||
|
||||
@Mock
|
||||
AuditService auditService;
|
||||
|
||||
@InjectMocks
|
||||
AuditResource auditResource;
|
||||
|
||||
@org.junit.jupiter.api.BeforeEach
|
||||
void setUp() {
|
||||
auditResource.defaultRealm = "master";
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs() {
|
||||
List<AuditLogDTO> logs = Collections.singletonList(
|
||||
AuditLogDTO.builder().acteurUsername("admin").typeAction(TypeActionAudit.USER_CREATE).build());
|
||||
when(auditService.findByActeur(eq("admin"), any(), any(), eq(0), eq(50))).thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs("admin", null, null, null, null, null, 0, 50);
|
||||
|
||||
assertEquals(logs, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLogsByActor() {
|
||||
List<AuditLogDTO> logs = Collections.singletonList(
|
||||
AuditLogDTO.builder().acteurUsername("admin").build());
|
||||
when(auditService.findByActeur(eq("admin"), isNull(), isNull(), eq(0), eq(100))).thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.getLogsByActor("admin", 100);
|
||||
|
||||
assertEquals(logs, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLogsByResource() {
|
||||
List<AuditLogDTO> logs = Collections.emptyList();
|
||||
when(auditService.findByRessource(eq("USER"), eq("1"), any(), any(), eq(0), eq(100)))
|
||||
.thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.getLogsByResource("USER", "1", 100);
|
||||
|
||||
assertEquals(logs, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLogsByAction() {
|
||||
List<AuditLogDTO> logs = Collections.emptyList();
|
||||
when(auditService.findByTypeAction(eq(TypeActionAudit.USER_CREATE), eq("master"), any(), any(), eq(0), eq(100)))
|
||||
.thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.getLogsByAction(TypeActionAudit.USER_CREATE, null, null, 100);
|
||||
|
||||
assertEquals(logs, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetActionStatistics() {
|
||||
Map<TypeActionAudit, Long> stats = Map.of(TypeActionAudit.USER_CREATE, 10L);
|
||||
when(auditService.countByActionType(eq("master"), any(), any())).thenReturn(stats);
|
||||
|
||||
Map<TypeActionAudit, Long> result = auditResource.getActionStatistics(null, null);
|
||||
|
||||
assertEquals(stats, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserActivityStatistics() {
|
||||
Map<String, Long> stats = Map.of("admin", 100L);
|
||||
when(auditService.countByActeur(eq("master"), any(), any())).thenReturn(stats);
|
||||
|
||||
Map<String, Long> result = auditResource.getUserActivityStatistics(null, null);
|
||||
|
||||
assertEquals(stats, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetFailureCount() {
|
||||
Map<String, Long> successVsFailure = Map.of("failure", 5L, "success", 100L);
|
||||
when(auditService.countSuccessVsFailure(eq("master"), any(), any())).thenReturn(successVsFailure);
|
||||
|
||||
CountDTO result = auditResource.getFailureCount(null, null);
|
||||
|
||||
assertEquals(5L, result.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSuccessCount() {
|
||||
Map<String, Long> successVsFailure = Map.of("failure", 5L, "success", 100L);
|
||||
when(auditService.countSuccessVsFailure(eq("master"), any(), any())).thenReturn(successVsFailure);
|
||||
|
||||
CountDTO result = auditResource.getSuccessCount(null, null);
|
||||
|
||||
assertEquals(100L, result.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExportLogsToCSV() {
|
||||
when(auditService.exportToCSV(eq("master"), any(), any())).thenReturn("csv,data");
|
||||
|
||||
Response response = auditResource.exportLogsToCSV(null, null);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals("csv,data", response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPurgeOldLogs() {
|
||||
when(auditService.purgeOldLogs(any())).thenReturn(0L);
|
||||
|
||||
auditResource.purgeOldLogs(90);
|
||||
|
||||
verify(auditService).purgeOldLogs(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_NullActeur_UsesRealm() {
|
||||
List<AuditLogDTO> logs = Collections.singletonList(
|
||||
AuditLogDTO.builder().acteurUsername("admin").typeAction(TypeActionAudit.USER_CREATE).build());
|
||||
when(auditService.findByRealm(eq("master"), any(), any(), eq(0), eq(50))).thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs(null, null, null, null, null, null, 0, 50);
|
||||
|
||||
assertEquals(logs, result);
|
||||
verify(auditService).findByRealm(eq("master"), any(), any(), eq(0), eq(50));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_BlankActeur_UsesRealm() {
|
||||
List<AuditLogDTO> logs = Collections.emptyList();
|
||||
when(auditService.findByRealm(eq("master"), any(), any(), eq(0), eq(20))).thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs(" ", null, null, null, null, null, 0, 20);
|
||||
|
||||
assertEquals(logs, result);
|
||||
verify(auditService).findByRealm(eq("master"), any(), any(), eq(0), eq(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_WithPostFilter_TypeAction() {
|
||||
AuditLogDTO matchLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.build();
|
||||
AuditLogDTO otherLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_DELETE)
|
||||
.build();
|
||||
when(auditService.findByActeur(eq("admin"), any(), any(), eq(0), eq(50)))
|
||||
.thenReturn(List.of(matchLog, otherLog));
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs("admin", null, null,
|
||||
TypeActionAudit.USER_CREATE, null, null, 0, 50);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(TypeActionAudit.USER_CREATE, result.get(0).getTypeAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_WithPostFilter_RessourceType() {
|
||||
AuditLogDTO matchLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.ressourceType("USER")
|
||||
.build();
|
||||
AuditLogDTO otherLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.ressourceType("ROLE")
|
||||
.build();
|
||||
when(auditService.findByActeur(eq("admin"), any(), any(), eq(0), eq(50)))
|
||||
.thenReturn(List.of(matchLog, otherLog));
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs("admin", null, null,
|
||||
null, "USER", null, 0, 50);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("USER", result.get(0).getRessourceType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_WithPostFilter_Succes() {
|
||||
AuditLogDTO successLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.success(true)
|
||||
.build();
|
||||
AuditLogDTO failLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.success(false)
|
||||
.build();
|
||||
when(auditService.findByActeur(eq("admin"), any(), any(), eq(0), eq(50)))
|
||||
.thenReturn(List.of(successLog, failLog));
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs("admin", null, null,
|
||||
null, null, Boolean.TRUE, 0, 50);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertTrue(result.get(0).isSuccessful());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExportLogsToCSV_Exception() {
|
||||
when(auditService.exportToCSV(eq("master"), any(), any()))
|
||||
.thenThrow(new RuntimeException("Export failed"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> auditResource.exportLogsToCSV(null, null));
|
||||
}
|
||||
}
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.dto.audit.AuditLogDTO;
|
||||
import dev.lions.user.manager.dto.common.CountDTO;
|
||||
import dev.lions.user.manager.enums.audit.TypeActionAudit;
|
||||
import dev.lions.user.manager.service.AuditService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AuditResourceTest {
|
||||
|
||||
@Mock
|
||||
AuditService auditService;
|
||||
|
||||
@InjectMocks
|
||||
AuditResource auditResource;
|
||||
|
||||
@org.junit.jupiter.api.BeforeEach
|
||||
void setUp() {
|
||||
auditResource.defaultRealm = "master";
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs() {
|
||||
List<AuditLogDTO> logs = Collections.singletonList(
|
||||
AuditLogDTO.builder().acteurUsername("admin").typeAction(TypeActionAudit.USER_CREATE).build());
|
||||
when(auditService.findByActeur(eq("admin"), any(), any(), eq(0), eq(50))).thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs("admin", null, null, null, null, null, 0, 50);
|
||||
|
||||
assertEquals(logs, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLogsByActor() {
|
||||
List<AuditLogDTO> logs = Collections.singletonList(
|
||||
AuditLogDTO.builder().acteurUsername("admin").build());
|
||||
when(auditService.findByActeur(eq("admin"), isNull(), isNull(), eq(0), eq(100))).thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.getLogsByActor("admin", 100);
|
||||
|
||||
assertEquals(logs, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLogsByResource() {
|
||||
List<AuditLogDTO> logs = Collections.emptyList();
|
||||
when(auditService.findByRessource(eq("USER"), eq("1"), any(), any(), eq(0), eq(100)))
|
||||
.thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.getLogsByResource("USER", "1", 100);
|
||||
|
||||
assertEquals(logs, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLogsByAction() {
|
||||
List<AuditLogDTO> logs = Collections.emptyList();
|
||||
when(auditService.findByTypeAction(eq(TypeActionAudit.USER_CREATE), eq("master"), any(), any(), eq(0), eq(100)))
|
||||
.thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.getLogsByAction(TypeActionAudit.USER_CREATE, null, null, 100);
|
||||
|
||||
assertEquals(logs, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetActionStatistics() {
|
||||
Map<TypeActionAudit, Long> stats = Map.of(TypeActionAudit.USER_CREATE, 10L);
|
||||
when(auditService.countByActionType(eq("master"), any(), any())).thenReturn(stats);
|
||||
|
||||
Map<TypeActionAudit, Long> result = auditResource.getActionStatistics(null, null);
|
||||
|
||||
assertEquals(stats, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserActivityStatistics() {
|
||||
Map<String, Long> stats = Map.of("admin", 100L);
|
||||
when(auditService.countByActeur(eq("master"), any(), any())).thenReturn(stats);
|
||||
|
||||
Map<String, Long> result = auditResource.getUserActivityStatistics(null, null);
|
||||
|
||||
assertEquals(stats, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetFailureCount() {
|
||||
Map<String, Long> successVsFailure = Map.of("failure", 5L, "success", 100L);
|
||||
when(auditService.countSuccessVsFailure(eq("master"), any(), any())).thenReturn(successVsFailure);
|
||||
|
||||
CountDTO result = auditResource.getFailureCount(null, null);
|
||||
|
||||
assertEquals(5L, result.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSuccessCount() {
|
||||
Map<String, Long> successVsFailure = Map.of("failure", 5L, "success", 100L);
|
||||
when(auditService.countSuccessVsFailure(eq("master"), any(), any())).thenReturn(successVsFailure);
|
||||
|
||||
CountDTO result = auditResource.getSuccessCount(null, null);
|
||||
|
||||
assertEquals(100L, result.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExportLogsToCSV() {
|
||||
when(auditService.exportToCSV(eq("master"), any(), any())).thenReturn("csv,data");
|
||||
|
||||
Response response = auditResource.exportLogsToCSV(null, null);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals("csv,data", response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPurgeOldLogs() {
|
||||
when(auditService.purgeOldLogs(any())).thenReturn(0L);
|
||||
|
||||
auditResource.purgeOldLogs(90);
|
||||
|
||||
verify(auditService).purgeOldLogs(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_NullActeur_UsesRealm() {
|
||||
List<AuditLogDTO> logs = Collections.singletonList(
|
||||
AuditLogDTO.builder().acteurUsername("admin").typeAction(TypeActionAudit.USER_CREATE).build());
|
||||
when(auditService.findByRealm(eq("master"), any(), any(), eq(0), eq(50))).thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs(null, null, null, null, null, null, 0, 50);
|
||||
|
||||
assertEquals(logs, result);
|
||||
verify(auditService).findByRealm(eq("master"), any(), any(), eq(0), eq(50));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_BlankActeur_UsesRealm() {
|
||||
List<AuditLogDTO> logs = Collections.emptyList();
|
||||
when(auditService.findByRealm(eq("master"), any(), any(), eq(0), eq(20))).thenReturn(logs);
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs(" ", null, null, null, null, null, 0, 20);
|
||||
|
||||
assertEquals(logs, result);
|
||||
verify(auditService).findByRealm(eq("master"), any(), any(), eq(0), eq(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_WithPostFilter_TypeAction() {
|
||||
AuditLogDTO matchLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.build();
|
||||
AuditLogDTO otherLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_DELETE)
|
||||
.build();
|
||||
when(auditService.findByActeur(eq("admin"), any(), any(), eq(0), eq(50)))
|
||||
.thenReturn(List.of(matchLog, otherLog));
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs("admin", null, null,
|
||||
TypeActionAudit.USER_CREATE, null, null, 0, 50);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(TypeActionAudit.USER_CREATE, result.get(0).getTypeAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_WithPostFilter_RessourceType() {
|
||||
AuditLogDTO matchLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.ressourceType("USER")
|
||||
.build();
|
||||
AuditLogDTO otherLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.ressourceType("ROLE")
|
||||
.build();
|
||||
when(auditService.findByActeur(eq("admin"), any(), any(), eq(0), eq(50)))
|
||||
.thenReturn(List.of(matchLog, otherLog));
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs("admin", null, null,
|
||||
null, "USER", null, 0, 50);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("USER", result.get(0).getRessourceType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSearchLogs_WithPostFilter_Succes() {
|
||||
AuditLogDTO successLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.success(true)
|
||||
.build();
|
||||
AuditLogDTO failLog = AuditLogDTO.builder()
|
||||
.acteurUsername("admin")
|
||||
.typeAction(TypeActionAudit.USER_CREATE)
|
||||
.success(false)
|
||||
.build();
|
||||
when(auditService.findByActeur(eq("admin"), any(), any(), eq(0), eq(50)))
|
||||
.thenReturn(List.of(successLog, failLog));
|
||||
|
||||
List<AuditLogDTO> result = auditResource.searchLogs("admin", null, null,
|
||||
null, null, Boolean.TRUE, 0, 50);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertTrue(result.get(0).isSuccessful());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExportLogsToCSV_Exception() {
|
||||
when(auditService.exportToCSV(eq("master"), any(), any()))
|
||||
.thenThrow(new RuntimeException("Export failed"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> auditResource.exportLogsToCSV(null, null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,99 +1,99 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.client.KeycloakAdminClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.keycloak.admin.client.Keycloak;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class HealthResourceEndpointTest {
|
||||
|
||||
@Mock
|
||||
KeycloakAdminClient keycloakAdminClient;
|
||||
|
||||
@Mock
|
||||
Keycloak keycloak;
|
||||
|
||||
@InjectMocks
|
||||
HealthResourceEndpoint healthResourceEndpoint;
|
||||
|
||||
@Test
|
||||
void testGetKeycloakHealthConnected() {
|
||||
when(keycloakAdminClient.getInstance()).thenReturn(keycloak);
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("UP", result.get("status"));
|
||||
assertEquals(true, result.get("connected"));
|
||||
assertNotNull(result.get("timestamp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetKeycloakHealthDisconnected() {
|
||||
when(keycloakAdminClient.getInstance()).thenReturn(null);
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("DOWN", result.get("status"));
|
||||
assertEquals(false, result.get("connected"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetKeycloakHealthError() {
|
||||
when(keycloakAdminClient.getInstance()).thenThrow(new RuntimeException("Connection error"));
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("ERROR", result.get("status"));
|
||||
assertEquals(false, result.get("connected"));
|
||||
assertEquals("Connection error", result.get("error"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetServiceStatusConnected() {
|
||||
when(keycloakAdminClient.isConnected()).thenReturn(true);
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getServiceStatus();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("lions-user-manager-server", result.get("service"));
|
||||
assertEquals("1.0.0", result.get("version"));
|
||||
assertEquals("UP", result.get("status"));
|
||||
assertEquals("CONNECTED", result.get("keycloak"));
|
||||
assertNotNull(result.get("timestamp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetServiceStatusDisconnected() {
|
||||
when(keycloakAdminClient.isConnected()).thenReturn(false);
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getServiceStatus();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("UP", result.get("status"));
|
||||
assertEquals("DISCONNECTED", result.get("keycloak"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetServiceStatusKeycloakError() {
|
||||
when(keycloakAdminClient.isConnected()).thenThrow(new RuntimeException("Error"));
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getServiceStatus();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("UP", result.get("status"));
|
||||
assertEquals("ERROR", result.get("keycloak"));
|
||||
assertEquals("Error", result.get("keycloakError"));
|
||||
}
|
||||
}
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.client.KeycloakAdminClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.keycloak.admin.client.Keycloak;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class HealthResourceEndpointTest {
|
||||
|
||||
@Mock
|
||||
KeycloakAdminClient keycloakAdminClient;
|
||||
|
||||
@Mock
|
||||
Keycloak keycloak;
|
||||
|
||||
@InjectMocks
|
||||
HealthResourceEndpoint healthResourceEndpoint;
|
||||
|
||||
@Test
|
||||
void testGetKeycloakHealthConnected() {
|
||||
when(keycloakAdminClient.getInstance()).thenReturn(keycloak);
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("UP", result.get("status"));
|
||||
assertEquals(true, result.get("connected"));
|
||||
assertNotNull(result.get("timestamp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetKeycloakHealthDisconnected() {
|
||||
when(keycloakAdminClient.getInstance()).thenReturn(null);
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("DOWN", result.get("status"));
|
||||
assertEquals(false, result.get("connected"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetKeycloakHealthError() {
|
||||
when(keycloakAdminClient.getInstance()).thenThrow(new RuntimeException("Connection error"));
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getKeycloakHealth();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("ERROR", result.get("status"));
|
||||
assertEquals(false, result.get("connected"));
|
||||
assertEquals("Connection error", result.get("error"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetServiceStatusConnected() {
|
||||
when(keycloakAdminClient.isConnected()).thenReturn(true);
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getServiceStatus();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("lions-user-manager-server", result.get("service"));
|
||||
assertEquals("1.0.0", result.get("version"));
|
||||
assertEquals("UP", result.get("status"));
|
||||
assertEquals("CONNECTED", result.get("keycloak"));
|
||||
assertNotNull(result.get("timestamp"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetServiceStatusDisconnected() {
|
||||
when(keycloakAdminClient.isConnected()).thenReturn(false);
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getServiceStatus();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("UP", result.get("status"));
|
||||
assertEquals("DISCONNECTED", result.get("keycloak"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetServiceStatusKeycloakError() {
|
||||
when(keycloakAdminClient.isConnected()).thenThrow(new RuntimeException("Error"));
|
||||
|
||||
Map<String, Object> result = healthResourceEndpoint.getServiceStatus();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("UP", result.get("status"));
|
||||
assertEquals("ERROR", result.get("keycloak"));
|
||||
assertEquals("Error", result.get("keycloakError"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,222 +1,222 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.dto.realm.AuthorizedRealmsDTO;
|
||||
import dev.lions.user.manager.dto.realm.RealmAccessCheckDTO;
|
||||
import dev.lions.user.manager.dto.realm.RealmAssignmentDTO;
|
||||
import dev.lions.user.manager.service.RealmAuthorizationService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.SecurityContext;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.security.Principal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour RealmAssignmentResource
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RealmAssignmentResourceTest {
|
||||
|
||||
@Mock
|
||||
private RealmAuthorizationService realmAuthorizationService;
|
||||
|
||||
@Mock
|
||||
private SecurityContext securityContext;
|
||||
|
||||
@Mock
|
||||
private Principal principal;
|
||||
|
||||
@InjectMocks
|
||||
private RealmAssignmentResource realmAssignmentResource;
|
||||
|
||||
private RealmAssignmentDTO assignment;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
assignment = RealmAssignmentDTO.builder()
|
||||
.id("assignment-1")
|
||||
.userId("user-1")
|
||||
.username("testuser")
|
||||
.email("test@example.com")
|
||||
.realmName("realm1")
|
||||
.isSuperAdmin(false)
|
||||
.active(true)
|
||||
.assignedAt(LocalDateTime.now())
|
||||
.assignedBy("admin")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllAssignments_Success() {
|
||||
List<RealmAssignmentDTO> assignments = Arrays.asList(assignment);
|
||||
when(realmAuthorizationService.getAllAssignments()).thenReturn(assignments);
|
||||
|
||||
List<RealmAssignmentDTO> result = realmAssignmentResource.getAllAssignments();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAssignmentsByUser_Success() {
|
||||
List<RealmAssignmentDTO> assignments = Arrays.asList(assignment);
|
||||
when(realmAuthorizationService.getAssignmentsByUser("user-1")).thenReturn(assignments);
|
||||
|
||||
List<RealmAssignmentDTO> result = realmAssignmentResource.getAssignmentsByUser("user-1");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAssignmentsByRealm_Success() {
|
||||
List<RealmAssignmentDTO> assignments = Arrays.asList(assignment);
|
||||
when(realmAuthorizationService.getAssignmentsByRealm("realm1")).thenReturn(assignments);
|
||||
|
||||
List<RealmAssignmentDTO> result = realmAssignmentResource.getAssignmentsByRealm("realm1");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAssignmentById_Success() {
|
||||
when(realmAuthorizationService.getAssignmentById("assignment-1")).thenReturn(Optional.of(assignment));
|
||||
|
||||
RealmAssignmentDTO result = realmAssignmentResource.getAssignmentById("assignment-1");
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("assignment-1", result.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAssignmentById_NotFound() {
|
||||
when(realmAuthorizationService.getAssignmentById("non-existent")).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmAssignmentResource.getAssignmentById("non-existent"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCanManageRealm_Success() {
|
||||
when(realmAuthorizationService.canManageRealm("user-1", "realm1")).thenReturn(true);
|
||||
|
||||
RealmAccessCheckDTO result = realmAssignmentResource.canManageRealm("user-1", "realm1");
|
||||
|
||||
assertTrue(result.isCanManage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAuthorizedRealms_Success() {
|
||||
List<String> realms = Arrays.asList("realm1", "realm2");
|
||||
when(realmAuthorizationService.getAuthorizedRealms("user-1")).thenReturn(realms);
|
||||
when(realmAuthorizationService.isSuperAdmin("user-1")).thenReturn(false);
|
||||
|
||||
AuthorizedRealmsDTO result = realmAssignmentResource.getAuthorizedRealms("user-1");
|
||||
|
||||
assertEquals(2, result.getRealms().size());
|
||||
assertFalse(result.isSuperAdmin());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRealmToUser_Success() {
|
||||
// En Quarkus, @Context SecurityContext injecté peut être simulé via Mockito
|
||||
// Mais dans RealmAssignmentResource, l'admin est récupéré du SecurityContext.
|
||||
// Puisque c'est un test unitaire @ExtendWith(MockitoExtension.class),
|
||||
// @Inject SecurityContext securityContext est mocké.
|
||||
|
||||
when(securityContext.getUserPrincipal()).thenReturn(principal);
|
||||
when(principal.getName()).thenReturn("admin");
|
||||
when(realmAuthorizationService.assignRealmToUser(any(RealmAssignmentDTO.class))).thenReturn(assignment);
|
||||
|
||||
Response response = realmAssignmentResource.assignRealmToUser(assignment);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRevokeRealmFromUser_Success() {
|
||||
doNothing().when(realmAuthorizationService).revokeRealmFromUser("user-1", "realm1");
|
||||
|
||||
realmAssignmentResource.revokeRealmFromUser("user-1", "realm1");
|
||||
|
||||
verify(realmAuthorizationService).revokeRealmFromUser("user-1", "realm1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRevokeAllRealmsFromUser_Success() {
|
||||
doNothing().when(realmAuthorizationService).revokeAllRealmsFromUser("user-1");
|
||||
|
||||
realmAssignmentResource.revokeAllRealmsFromUser("user-1");
|
||||
|
||||
verify(realmAuthorizationService).revokeAllRealmsFromUser("user-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeactivateAssignment_Success() {
|
||||
doNothing().when(realmAuthorizationService).deactivateAssignment("assignment-1");
|
||||
|
||||
realmAssignmentResource.deactivateAssignment("assignment-1");
|
||||
|
||||
verify(realmAuthorizationService).deactivateAssignment("assignment-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testActivateAssignment_Success() {
|
||||
doNothing().when(realmAuthorizationService).activateAssignment("assignment-1");
|
||||
|
||||
realmAssignmentResource.activateAssignment("assignment-1");
|
||||
|
||||
verify(realmAuthorizationService).activateAssignment("assignment-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetSuperAdmin_Success() {
|
||||
doNothing().when(realmAuthorizationService).setSuperAdmin("user-1", true);
|
||||
|
||||
realmAssignmentResource.setSuperAdmin("user-1", true);
|
||||
|
||||
verify(realmAuthorizationService).setSuperAdmin("user-1", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRealmToUser_NullPrincipal() {
|
||||
when(securityContext.getUserPrincipal()).thenReturn(null);
|
||||
when(realmAuthorizationService.assignRealmToUser(any(RealmAssignmentDTO.class))).thenReturn(assignment);
|
||||
|
||||
Response response = realmAssignmentResource.assignRealmToUser(assignment);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
// assignedBy n'est pas modifié car le principal est null
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRealmToUser_IllegalArgumentException() {
|
||||
when(securityContext.getUserPrincipal()).thenReturn(principal);
|
||||
when(principal.getName()).thenReturn("admin");
|
||||
when(realmAuthorizationService.assignRealmToUser(any(RealmAssignmentDTO.class)))
|
||||
.thenThrow(new IllegalArgumentException("Affectation déjà existante"));
|
||||
|
||||
Response response = realmAssignmentResource.assignRealmToUser(assignment);
|
||||
|
||||
assertEquals(409, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRealmToUser_GenericException() {
|
||||
when(securityContext.getUserPrincipal()).thenReturn(principal);
|
||||
when(principal.getName()).thenReturn("admin");
|
||||
when(realmAuthorizationService.assignRealmToUser(any(RealmAssignmentDTO.class)))
|
||||
.thenThrow(new RuntimeException("Erreur inattendue"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmAssignmentResource.assignRealmToUser(assignment));
|
||||
}
|
||||
}
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.dto.realm.AuthorizedRealmsDTO;
|
||||
import dev.lions.user.manager.dto.realm.RealmAccessCheckDTO;
|
||||
import dev.lions.user.manager.dto.realm.RealmAssignmentDTO;
|
||||
import dev.lions.user.manager.service.RealmAuthorizationService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.SecurityContext;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.security.Principal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour RealmAssignmentResource
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RealmAssignmentResourceTest {
|
||||
|
||||
@Mock
|
||||
private RealmAuthorizationService realmAuthorizationService;
|
||||
|
||||
@Mock
|
||||
private SecurityContext securityContext;
|
||||
|
||||
@Mock
|
||||
private Principal principal;
|
||||
|
||||
@InjectMocks
|
||||
private RealmAssignmentResource realmAssignmentResource;
|
||||
|
||||
private RealmAssignmentDTO assignment;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
assignment = RealmAssignmentDTO.builder()
|
||||
.id("assignment-1")
|
||||
.userId("user-1")
|
||||
.username("testuser")
|
||||
.email("test@example.com")
|
||||
.realmName("realm1")
|
||||
.isSuperAdmin(false)
|
||||
.active(true)
|
||||
.assignedAt(LocalDateTime.now())
|
||||
.assignedBy("admin")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllAssignments_Success() {
|
||||
List<RealmAssignmentDTO> assignments = Arrays.asList(assignment);
|
||||
when(realmAuthorizationService.getAllAssignments()).thenReturn(assignments);
|
||||
|
||||
List<RealmAssignmentDTO> result = realmAssignmentResource.getAllAssignments();
|
||||
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAssignmentsByUser_Success() {
|
||||
List<RealmAssignmentDTO> assignments = Arrays.asList(assignment);
|
||||
when(realmAuthorizationService.getAssignmentsByUser("user-1")).thenReturn(assignments);
|
||||
|
||||
List<RealmAssignmentDTO> result = realmAssignmentResource.getAssignmentsByUser("user-1");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAssignmentsByRealm_Success() {
|
||||
List<RealmAssignmentDTO> assignments = Arrays.asList(assignment);
|
||||
when(realmAuthorizationService.getAssignmentsByRealm("realm1")).thenReturn(assignments);
|
||||
|
||||
List<RealmAssignmentDTO> result = realmAssignmentResource.getAssignmentsByRealm("realm1");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAssignmentById_Success() {
|
||||
when(realmAuthorizationService.getAssignmentById("assignment-1")).thenReturn(Optional.of(assignment));
|
||||
|
||||
RealmAssignmentDTO result = realmAssignmentResource.getAssignmentById("assignment-1");
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("assignment-1", result.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAssignmentById_NotFound() {
|
||||
when(realmAuthorizationService.getAssignmentById("non-existent")).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmAssignmentResource.getAssignmentById("non-existent"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCanManageRealm_Success() {
|
||||
when(realmAuthorizationService.canManageRealm("user-1", "realm1")).thenReturn(true);
|
||||
|
||||
RealmAccessCheckDTO result = realmAssignmentResource.canManageRealm("user-1", "realm1");
|
||||
|
||||
assertTrue(result.isCanManage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAuthorizedRealms_Success() {
|
||||
List<String> realms = Arrays.asList("realm1", "realm2");
|
||||
when(realmAuthorizationService.getAuthorizedRealms("user-1")).thenReturn(realms);
|
||||
when(realmAuthorizationService.isSuperAdmin("user-1")).thenReturn(false);
|
||||
|
||||
AuthorizedRealmsDTO result = realmAssignmentResource.getAuthorizedRealms("user-1");
|
||||
|
||||
assertEquals(2, result.getRealms().size());
|
||||
assertFalse(result.isSuperAdmin());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRealmToUser_Success() {
|
||||
// En Quarkus, @Context SecurityContext injecté peut être simulé via Mockito
|
||||
// Mais dans RealmAssignmentResource, l'admin est récupéré du SecurityContext.
|
||||
// Puisque c'est un test unitaire @ExtendWith(MockitoExtension.class),
|
||||
// @Inject SecurityContext securityContext est mocké.
|
||||
|
||||
when(securityContext.getUserPrincipal()).thenReturn(principal);
|
||||
when(principal.getName()).thenReturn("admin");
|
||||
when(realmAuthorizationService.assignRealmToUser(any(RealmAssignmentDTO.class))).thenReturn(assignment);
|
||||
|
||||
Response response = realmAssignmentResource.assignRealmToUser(assignment);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRevokeRealmFromUser_Success() {
|
||||
doNothing().when(realmAuthorizationService).revokeRealmFromUser("user-1", "realm1");
|
||||
|
||||
realmAssignmentResource.revokeRealmFromUser("user-1", "realm1");
|
||||
|
||||
verify(realmAuthorizationService).revokeRealmFromUser("user-1", "realm1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRevokeAllRealmsFromUser_Success() {
|
||||
doNothing().when(realmAuthorizationService).revokeAllRealmsFromUser("user-1");
|
||||
|
||||
realmAssignmentResource.revokeAllRealmsFromUser("user-1");
|
||||
|
||||
verify(realmAuthorizationService).revokeAllRealmsFromUser("user-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeactivateAssignment_Success() {
|
||||
doNothing().when(realmAuthorizationService).deactivateAssignment("assignment-1");
|
||||
|
||||
realmAssignmentResource.deactivateAssignment("assignment-1");
|
||||
|
||||
verify(realmAuthorizationService).deactivateAssignment("assignment-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testActivateAssignment_Success() {
|
||||
doNothing().when(realmAuthorizationService).activateAssignment("assignment-1");
|
||||
|
||||
realmAssignmentResource.activateAssignment("assignment-1");
|
||||
|
||||
verify(realmAuthorizationService).activateAssignment("assignment-1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetSuperAdmin_Success() {
|
||||
doNothing().when(realmAuthorizationService).setSuperAdmin("user-1", true);
|
||||
|
||||
realmAssignmentResource.setSuperAdmin("user-1", true);
|
||||
|
||||
verify(realmAuthorizationService).setSuperAdmin("user-1", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRealmToUser_NullPrincipal() {
|
||||
when(securityContext.getUserPrincipal()).thenReturn(null);
|
||||
when(realmAuthorizationService.assignRealmToUser(any(RealmAssignmentDTO.class))).thenReturn(assignment);
|
||||
|
||||
Response response = realmAssignmentResource.assignRealmToUser(assignment);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
// assignedBy n'est pas modifié car le principal est null
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRealmToUser_IllegalArgumentException() {
|
||||
when(securityContext.getUserPrincipal()).thenReturn(principal);
|
||||
when(principal.getName()).thenReturn("admin");
|
||||
when(realmAuthorizationService.assignRealmToUser(any(RealmAssignmentDTO.class)))
|
||||
.thenThrow(new IllegalArgumentException("Affectation déjà existante"));
|
||||
|
||||
Response response = realmAssignmentResource.assignRealmToUser(assignment);
|
||||
|
||||
assertEquals(409, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignRealmToUser_GenericException() {
|
||||
when(securityContext.getUserPrincipal()).thenReturn(principal);
|
||||
when(principal.getName()).thenReturn("admin");
|
||||
when(realmAuthorizationService.assignRealmToUser(any(RealmAssignmentDTO.class)))
|
||||
.thenThrow(new RuntimeException("Erreur inattendue"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmAssignmentResource.assignRealmToUser(assignment));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,88 +1,88 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.client.KeycloakAdminClient;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
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 static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests supplémentaires pour RealmResource pour améliorer la couverture
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RealmResourceAdditionalTest {
|
||||
|
||||
@Mock
|
||||
private KeycloakAdminClient keycloakAdminClient;
|
||||
|
||||
@Mock
|
||||
private SecurityIdentity securityIdentity;
|
||||
|
||||
@InjectMocks
|
||||
private RealmResource realmResource;
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Success() {
|
||||
List<String> realms = Arrays.asList("master", "lions-user-manager", "test-realm");
|
||||
when(keycloakAdminClient.getAllRealms()).thenReturn(realms);
|
||||
|
||||
List<String> result = realmResource.getAllRealms();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(3, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Empty() {
|
||||
when(keycloakAdminClient.getAllRealms()).thenReturn(List.of());
|
||||
|
||||
List<String> result = realmResource.getAllRealms();
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Exception() {
|
||||
when(keycloakAdminClient.getAllRealms()).thenThrow(new RuntimeException("Connection error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmResource.getAllRealms());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmClients_Success() {
|
||||
List<String> clients = Arrays.asList("admin-cli", "account", "lions-app");
|
||||
when(keycloakAdminClient.getRealmClients("test-realm")).thenReturn(clients);
|
||||
|
||||
List<String> result = realmResource.getRealmClients("test-realm");
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(3, result.size());
|
||||
assertTrue(result.contains("admin-cli"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmClients_Empty() {
|
||||
when(keycloakAdminClient.getRealmClients("test-realm")).thenReturn(List.of());
|
||||
|
||||
List<String> result = realmResource.getRealmClients("test-realm");
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmClients_Exception() {
|
||||
when(keycloakAdminClient.getRealmClients("bad-realm")).thenThrow(new RuntimeException("Not found"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmResource.getRealmClients("bad-realm"));
|
||||
}
|
||||
}
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.client.KeycloakAdminClient;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
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 static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests supplémentaires pour RealmResource pour améliorer la couverture
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RealmResourceAdditionalTest {
|
||||
|
||||
@Mock
|
||||
private KeycloakAdminClient keycloakAdminClient;
|
||||
|
||||
@Mock
|
||||
private SecurityIdentity securityIdentity;
|
||||
|
||||
@InjectMocks
|
||||
private RealmResource realmResource;
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Success() {
|
||||
List<String> realms = Arrays.asList("master", "lions-user-manager", "test-realm");
|
||||
when(keycloakAdminClient.getAllRealms()).thenReturn(realms);
|
||||
|
||||
List<String> result = realmResource.getAllRealms();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(3, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Empty() {
|
||||
when(keycloakAdminClient.getAllRealms()).thenReturn(List.of());
|
||||
|
||||
List<String> result = realmResource.getAllRealms();
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Exception() {
|
||||
when(keycloakAdminClient.getAllRealms()).thenThrow(new RuntimeException("Connection error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmResource.getAllRealms());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmClients_Success() {
|
||||
List<String> clients = Arrays.asList("admin-cli", "account", "lions-app");
|
||||
when(keycloakAdminClient.getRealmClients("test-realm")).thenReturn(clients);
|
||||
|
||||
List<String> result = realmResource.getRealmClients("test-realm");
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(3, result.size());
|
||||
assertTrue(result.contains("admin-cli"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmClients_Empty() {
|
||||
when(keycloakAdminClient.getRealmClients("test-realm")).thenReturn(List.of());
|
||||
|
||||
List<String> result = realmResource.getRealmClients("test-realm");
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmClients_Exception() {
|
||||
when(keycloakAdminClient.getRealmClients("bad-realm")).thenThrow(new RuntimeException("Not found"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmResource.getRealmClients("bad-realm"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +1,62 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.client.KeycloakAdminClient;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour RealmResource
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RealmResourceTest {
|
||||
|
||||
@Mock
|
||||
private KeycloakAdminClient keycloakAdminClient;
|
||||
|
||||
@Mock
|
||||
private SecurityIdentity securityIdentity;
|
||||
|
||||
@InjectMocks
|
||||
private RealmResource realmResource;
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Success() {
|
||||
List<String> realms = Arrays.asList("master", "lions-user-manager", "btpxpress");
|
||||
when(keycloakAdminClient.getAllRealms()).thenReturn(realms);
|
||||
|
||||
List<String> result = realmResource.getAllRealms();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("master", result.get(0));
|
||||
verify(keycloakAdminClient).getAllRealms();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_EmptyList() {
|
||||
when(keycloakAdminClient.getAllRealms()).thenReturn(Collections.emptyList());
|
||||
|
||||
List<String> result = realmResource.getAllRealms();
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Exception() {
|
||||
when(keycloakAdminClient.getAllRealms()).thenThrow(new RuntimeException("Keycloak connection error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmResource.getAllRealms());
|
||||
}
|
||||
}
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.client.KeycloakAdminClient;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests unitaires pour RealmResource
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RealmResourceTest {
|
||||
|
||||
@Mock
|
||||
private KeycloakAdminClient keycloakAdminClient;
|
||||
|
||||
@Mock
|
||||
private SecurityIdentity securityIdentity;
|
||||
|
||||
@InjectMocks
|
||||
private RealmResource realmResource;
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Success() {
|
||||
List<String> realms = Arrays.asList("master", "lions-user-manager", "btpxpress");
|
||||
when(keycloakAdminClient.getAllRealms()).thenReturn(realms);
|
||||
|
||||
List<String> result = realmResource.getAllRealms();
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(3, result.size());
|
||||
assertEquals("master", result.get(0));
|
||||
verify(keycloakAdminClient).getAllRealms();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_EmptyList() {
|
||||
when(keycloakAdminClient.getAllRealms()).thenReturn(Collections.emptyList());
|
||||
|
||||
List<String> result = realmResource.getAllRealms();
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealms_Exception() {
|
||||
when(keycloakAdminClient.getAllRealms()).thenThrow(new RuntimeException("Keycloak connection error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> realmResource.getAllRealms());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,370 +1,370 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.dto.role.RoleAssignmentDTO;
|
||||
import dev.lions.user.manager.dto.role.RoleAssignmentRequestDTO;
|
||||
import dev.lions.user.manager.dto.role.RoleDTO;
|
||||
import dev.lions.user.manager.enums.role.TypeRole;
|
||||
import dev.lions.user.manager.service.RoleService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RoleResourceTest {
|
||||
|
||||
@Mock
|
||||
RoleService roleService;
|
||||
|
||||
@InjectMocks
|
||||
RoleResource roleResource;
|
||||
|
||||
private static final String REALM = "test-realm";
|
||||
private static final String CLIENT_ID = "test-client";
|
||||
|
||||
// ============== Realm Role Tests ==============
|
||||
|
||||
@Test
|
||||
void testCreateRealmRole() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").description("desc").build();
|
||||
RoleDTO created = RoleDTO.builder().id("1").name("role").description("desc").build();
|
||||
|
||||
when(roleService.createRealmRole(any(), eq(REALM))).thenReturn(created);
|
||||
|
||||
Response response = roleResource.createRealmRole(input, REALM);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
assertEquals(created, response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateRealmRoleConflict() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
|
||||
when(roleService.createRealmRole(any(), eq(REALM)))
|
||||
.thenThrow(new IllegalArgumentException("Role already exists"));
|
||||
|
||||
Response response = roleResource.createRealmRole(input, REALM);
|
||||
|
||||
assertEquals(409, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmRole() {
|
||||
RoleDTO role = RoleDTO.builder().id("1").name("role").build();
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(role));
|
||||
|
||||
RoleDTO result = roleResource.getRealmRole("role", REALM);
|
||||
|
||||
assertEquals(role, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmRoleNotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.getRealmRole("role", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealmRoles() {
|
||||
List<RoleDTO> roles = Collections.singletonList(RoleDTO.builder().name("role").build());
|
||||
when(roleService.getAllRealmRoles(REALM)).thenReturn(roles);
|
||||
|
||||
List<RoleDTO> result = roleResource.getAllRealmRoles(REALM);
|
||||
|
||||
assertEquals(roles, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateRealmRole() {
|
||||
RoleDTO existingRole = RoleDTO.builder().id("1").name("role").build();
|
||||
RoleDTO input = RoleDTO.builder().description("updated").build();
|
||||
RoleDTO updated = RoleDTO.builder().id("1").name("role").description("updated").build();
|
||||
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(existingRole));
|
||||
when(roleService.updateRole(eq("1"), any(), eq(REALM), eq(TypeRole.REALM_ROLE), isNull()))
|
||||
.thenReturn(updated);
|
||||
|
||||
RoleDTO result = roleResource.updateRealmRole("role", input, REALM);
|
||||
|
||||
assertEquals(updated, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteRealmRole() {
|
||||
RoleDTO existingRole = RoleDTO.builder().id("1").name("role").build();
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(existingRole));
|
||||
doNothing().when(roleService).deleteRole(eq("1"), eq(REALM), eq(TypeRole.REALM_ROLE), isNull());
|
||||
|
||||
roleResource.deleteRealmRole("role", REALM);
|
||||
|
||||
verify(roleService).deleteRole(eq("1"), eq(REALM), eq(TypeRole.REALM_ROLE), isNull());
|
||||
}
|
||||
|
||||
// ============== Client Role Tests ==============
|
||||
|
||||
@Test
|
||||
void testCreateClientRole() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
RoleDTO created = RoleDTO.builder().id("1").name("role").build();
|
||||
|
||||
when(roleService.createClientRole(any(RoleDTO.class), eq(CLIENT_ID), eq(REALM))).thenReturn(created);
|
||||
|
||||
Response response = roleResource.createClientRole(CLIENT_ID, input, REALM);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
assertEquals(created, response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetClientRole() {
|
||||
RoleDTO role = RoleDTO.builder().id("1").name("role").build();
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.CLIENT_ROLE, CLIENT_ID))
|
||||
.thenReturn(Optional.of(role));
|
||||
|
||||
RoleDTO result = roleResource.getClientRole(CLIENT_ID, "role", REALM);
|
||||
|
||||
assertEquals(role, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllClientRoles() {
|
||||
List<RoleDTO> roles = Collections.singletonList(RoleDTO.builder().name("role").build());
|
||||
when(roleService.getAllClientRoles(REALM, CLIENT_ID)).thenReturn(roles);
|
||||
|
||||
List<RoleDTO> result = roleResource.getAllClientRoles(CLIENT_ID, REALM);
|
||||
|
||||
assertEquals(roles, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteClientRole() {
|
||||
RoleDTO existingRole = RoleDTO.builder().id("1").name("role").build();
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.CLIENT_ROLE, CLIENT_ID))
|
||||
.thenReturn(Optional.of(existingRole));
|
||||
doNothing().when(roleService).deleteRole(eq("1"), eq(REALM), eq(TypeRole.CLIENT_ROLE), eq(CLIENT_ID));
|
||||
|
||||
roleResource.deleteClientRole(CLIENT_ID, "role", REALM);
|
||||
|
||||
verify(roleService).deleteRole(eq("1"), eq(REALM), eq(TypeRole.CLIENT_ROLE), eq(CLIENT_ID));
|
||||
}
|
||||
|
||||
// ============== Role Assignment Tests ==============
|
||||
|
||||
@Test
|
||||
void testAssignRealmRoles() {
|
||||
doNothing().when(roleService).assignRolesToUser(any(RoleAssignmentDTO.class));
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("role"))
|
||||
.build();
|
||||
|
||||
roleResource.assignRealmRoles("user1", REALM, request);
|
||||
|
||||
verify(roleService).assignRolesToUser(any(RoleAssignmentDTO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRevokeRealmRoles() {
|
||||
doNothing().when(roleService).revokeRolesFromUser(any(RoleAssignmentDTO.class));
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("role"))
|
||||
.build();
|
||||
|
||||
roleResource.revokeRealmRoles("user1", REALM, request);
|
||||
|
||||
verify(roleService).revokeRolesFromUser(any(RoleAssignmentDTO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignClientRoles() {
|
||||
doNothing().when(roleService).assignRolesToUser(any(RoleAssignmentDTO.class));
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("role"))
|
||||
.build();
|
||||
|
||||
roleResource.assignClientRoles(CLIENT_ID, "user1", REALM, request);
|
||||
|
||||
verify(roleService).assignRolesToUser(any(RoleAssignmentDTO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserRealmRoles() {
|
||||
List<RoleDTO> roles = Collections.singletonList(RoleDTO.builder().name("role").build());
|
||||
when(roleService.getUserRealmRoles("user1", REALM)).thenReturn(roles);
|
||||
|
||||
List<RoleDTO> result = roleResource.getUserRealmRoles("user1", REALM);
|
||||
|
||||
assertEquals(roles, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserClientRoles() {
|
||||
List<RoleDTO> roles = Collections.singletonList(RoleDTO.builder().name("role").build());
|
||||
when(roleService.getUserClientRoles("user1", CLIENT_ID, REALM)).thenReturn(roles);
|
||||
|
||||
List<RoleDTO> result = roleResource.getUserClientRoles(CLIENT_ID, "user1", REALM);
|
||||
|
||||
assertEquals(roles, result);
|
||||
}
|
||||
|
||||
// ============== Composite Role Tests ==============
|
||||
|
||||
@Test
|
||||
void testAddComposites() {
|
||||
RoleDTO parentRole = RoleDTO.builder().id("parent-1").name("role").build();
|
||||
RoleDTO childRole = RoleDTO.builder().id("child-1").name("composite").build();
|
||||
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(parentRole));
|
||||
when(roleService.getRoleByName("composite", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(childRole));
|
||||
doNothing().when(roleService).addCompositeRoles(eq("parent-1"), anyList(), eq(REALM),
|
||||
eq(TypeRole.REALM_ROLE), isNull());
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("composite"))
|
||||
.build();
|
||||
|
||||
roleResource.addComposites("role", REALM, request);
|
||||
|
||||
verify(roleService).addCompositeRoles(eq("parent-1"), anyList(), eq(REALM),
|
||||
eq(TypeRole.REALM_ROLE), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetComposites() {
|
||||
RoleDTO role = RoleDTO.builder().id("1").name("role").build();
|
||||
List<RoleDTO> composites = Collections.singletonList(RoleDTO.builder().name("composite").build());
|
||||
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(role));
|
||||
when(roleService.getCompositeRoles("1", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(composites);
|
||||
|
||||
List<RoleDTO> result = roleResource.getComposites("role", REALM);
|
||||
|
||||
assertEquals(composites, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateRealmRole_GenericException() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
when(roleService.createRealmRole(any(), eq(REALM)))
|
||||
.thenThrow(new RuntimeException("Internal error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.createRealmRole(input, REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateRealmRole_NotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
RoleDTO input = RoleDTO.builder().description("updated").build();
|
||||
assertThrows(RuntimeException.class, () -> roleResource.updateRealmRole("role", input, REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteRealmRole_NotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.deleteRealmRole("role", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateClientRole_IllegalArgumentException() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
when(roleService.createClientRole(any(RoleDTO.class), eq(CLIENT_ID), eq(REALM)))
|
||||
.thenThrow(new IllegalArgumentException("Conflict"));
|
||||
|
||||
Response response = roleResource.createClientRole(CLIENT_ID, input, REALM);
|
||||
|
||||
assertEquals(409, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateClientRole_GenericException() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
when(roleService.createClientRole(any(RoleDTO.class), eq(CLIENT_ID), eq(REALM)))
|
||||
.thenThrow(new RuntimeException("Internal error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.createClientRole(CLIENT_ID, input, REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetClientRole_NotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.CLIENT_ROLE, CLIENT_ID))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.getClientRole(CLIENT_ID, "role", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteClientRole_NotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.CLIENT_ROLE, CLIENT_ID))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.deleteClientRole(CLIENT_ID, "role", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddComposites_ParentNotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("composite"))
|
||||
.build();
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.addComposites("role", REALM, request));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddComposites_ChildNotFound_FilteredOut() {
|
||||
RoleDTO parentRole = RoleDTO.builder().id("parent-1").name("role").build();
|
||||
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(parentRole));
|
||||
when(roleService.getRoleByName("nonexistent", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty()); // will be filtered out (null id)
|
||||
doNothing().when(roleService).addCompositeRoles(eq("parent-1"), anyList(), eq(REALM),
|
||||
eq(TypeRole.REALM_ROLE), isNull());
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("nonexistent"))
|
||||
.build();
|
||||
|
||||
roleResource.addComposites("role", REALM, request);
|
||||
|
||||
// addCompositeRoles called with empty list (filtered out)
|
||||
verify(roleService).addCompositeRoles(eq("parent-1"), eq(Collections.emptyList()), eq(REALM),
|
||||
eq(TypeRole.REALM_ROLE), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetComposites_RoleNotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.getComposites("role", REALM));
|
||||
}
|
||||
}
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.dto.role.RoleAssignmentDTO;
|
||||
import dev.lions.user.manager.dto.role.RoleAssignmentRequestDTO;
|
||||
import dev.lions.user.manager.dto.role.RoleDTO;
|
||||
import dev.lions.user.manager.enums.role.TypeRole;
|
||||
import dev.lions.user.manager.service.RoleService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RoleResourceTest {
|
||||
|
||||
@Mock
|
||||
RoleService roleService;
|
||||
|
||||
@InjectMocks
|
||||
RoleResource roleResource;
|
||||
|
||||
private static final String REALM = "test-realm";
|
||||
private static final String CLIENT_ID = "test-client";
|
||||
|
||||
// ============== Realm Role Tests ==============
|
||||
|
||||
@Test
|
||||
void testCreateRealmRole() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").description("desc").build();
|
||||
RoleDTO created = RoleDTO.builder().id("1").name("role").description("desc").build();
|
||||
|
||||
when(roleService.createRealmRole(any(), eq(REALM))).thenReturn(created);
|
||||
|
||||
Response response = roleResource.createRealmRole(input, REALM);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
assertEquals(created, response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateRealmRoleConflict() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
|
||||
when(roleService.createRealmRole(any(), eq(REALM)))
|
||||
.thenThrow(new IllegalArgumentException("Role already exists"));
|
||||
|
||||
Response response = roleResource.createRealmRole(input, REALM);
|
||||
|
||||
assertEquals(409, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmRole() {
|
||||
RoleDTO role = RoleDTO.builder().id("1").name("role").build();
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(role));
|
||||
|
||||
RoleDTO result = roleResource.getRealmRole("role", REALM);
|
||||
|
||||
assertEquals(role, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRealmRoleNotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.getRealmRole("role", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRealmRoles() {
|
||||
List<RoleDTO> roles = Collections.singletonList(RoleDTO.builder().name("role").build());
|
||||
when(roleService.getAllRealmRoles(REALM)).thenReturn(roles);
|
||||
|
||||
List<RoleDTO> result = roleResource.getAllRealmRoles(REALM);
|
||||
|
||||
assertEquals(roles, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateRealmRole() {
|
||||
RoleDTO existingRole = RoleDTO.builder().id("1").name("role").build();
|
||||
RoleDTO input = RoleDTO.builder().description("updated").build();
|
||||
RoleDTO updated = RoleDTO.builder().id("1").name("role").description("updated").build();
|
||||
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(existingRole));
|
||||
when(roleService.updateRole(eq("1"), any(), eq(REALM), eq(TypeRole.REALM_ROLE), isNull()))
|
||||
.thenReturn(updated);
|
||||
|
||||
RoleDTO result = roleResource.updateRealmRole("role", input, REALM);
|
||||
|
||||
assertEquals(updated, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteRealmRole() {
|
||||
RoleDTO existingRole = RoleDTO.builder().id("1").name("role").build();
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(existingRole));
|
||||
doNothing().when(roleService).deleteRole(eq("1"), eq(REALM), eq(TypeRole.REALM_ROLE), isNull());
|
||||
|
||||
roleResource.deleteRealmRole("role", REALM);
|
||||
|
||||
verify(roleService).deleteRole(eq("1"), eq(REALM), eq(TypeRole.REALM_ROLE), isNull());
|
||||
}
|
||||
|
||||
// ============== Client Role Tests ==============
|
||||
|
||||
@Test
|
||||
void testCreateClientRole() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
RoleDTO created = RoleDTO.builder().id("1").name("role").build();
|
||||
|
||||
when(roleService.createClientRole(any(RoleDTO.class), eq(CLIENT_ID), eq(REALM))).thenReturn(created);
|
||||
|
||||
Response response = roleResource.createClientRole(CLIENT_ID, input, REALM);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
assertEquals(created, response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetClientRole() {
|
||||
RoleDTO role = RoleDTO.builder().id("1").name("role").build();
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.CLIENT_ROLE, CLIENT_ID))
|
||||
.thenReturn(Optional.of(role));
|
||||
|
||||
RoleDTO result = roleResource.getClientRole(CLIENT_ID, "role", REALM);
|
||||
|
||||
assertEquals(role, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllClientRoles() {
|
||||
List<RoleDTO> roles = Collections.singletonList(RoleDTO.builder().name("role").build());
|
||||
when(roleService.getAllClientRoles(REALM, CLIENT_ID)).thenReturn(roles);
|
||||
|
||||
List<RoleDTO> result = roleResource.getAllClientRoles(CLIENT_ID, REALM);
|
||||
|
||||
assertEquals(roles, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteClientRole() {
|
||||
RoleDTO existingRole = RoleDTO.builder().id("1").name("role").build();
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.CLIENT_ROLE, CLIENT_ID))
|
||||
.thenReturn(Optional.of(existingRole));
|
||||
doNothing().when(roleService).deleteRole(eq("1"), eq(REALM), eq(TypeRole.CLIENT_ROLE), eq(CLIENT_ID));
|
||||
|
||||
roleResource.deleteClientRole(CLIENT_ID, "role", REALM);
|
||||
|
||||
verify(roleService).deleteRole(eq("1"), eq(REALM), eq(TypeRole.CLIENT_ROLE), eq(CLIENT_ID));
|
||||
}
|
||||
|
||||
// ============== Role Assignment Tests ==============
|
||||
|
||||
@Test
|
||||
void testAssignRealmRoles() {
|
||||
doNothing().when(roleService).assignRolesToUser(any(RoleAssignmentDTO.class));
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("role"))
|
||||
.build();
|
||||
|
||||
roleResource.assignRealmRoles("user1", REALM, request);
|
||||
|
||||
verify(roleService).assignRolesToUser(any(RoleAssignmentDTO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRevokeRealmRoles() {
|
||||
doNothing().when(roleService).revokeRolesFromUser(any(RoleAssignmentDTO.class));
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("role"))
|
||||
.build();
|
||||
|
||||
roleResource.revokeRealmRoles("user1", REALM, request);
|
||||
|
||||
verify(roleService).revokeRolesFromUser(any(RoleAssignmentDTO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAssignClientRoles() {
|
||||
doNothing().when(roleService).assignRolesToUser(any(RoleAssignmentDTO.class));
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("role"))
|
||||
.build();
|
||||
|
||||
roleResource.assignClientRoles(CLIENT_ID, "user1", REALM, request);
|
||||
|
||||
verify(roleService).assignRolesToUser(any(RoleAssignmentDTO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserRealmRoles() {
|
||||
List<RoleDTO> roles = Collections.singletonList(RoleDTO.builder().name("role").build());
|
||||
when(roleService.getUserRealmRoles("user1", REALM)).thenReturn(roles);
|
||||
|
||||
List<RoleDTO> result = roleResource.getUserRealmRoles("user1", REALM);
|
||||
|
||||
assertEquals(roles, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserClientRoles() {
|
||||
List<RoleDTO> roles = Collections.singletonList(RoleDTO.builder().name("role").build());
|
||||
when(roleService.getUserClientRoles("user1", CLIENT_ID, REALM)).thenReturn(roles);
|
||||
|
||||
List<RoleDTO> result = roleResource.getUserClientRoles(CLIENT_ID, "user1", REALM);
|
||||
|
||||
assertEquals(roles, result);
|
||||
}
|
||||
|
||||
// ============== Composite Role Tests ==============
|
||||
|
||||
@Test
|
||||
void testAddComposites() {
|
||||
RoleDTO parentRole = RoleDTO.builder().id("parent-1").name("role").build();
|
||||
RoleDTO childRole = RoleDTO.builder().id("child-1").name("composite").build();
|
||||
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(parentRole));
|
||||
when(roleService.getRoleByName("composite", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(childRole));
|
||||
doNothing().when(roleService).addCompositeRoles(eq("parent-1"), anyList(), eq(REALM),
|
||||
eq(TypeRole.REALM_ROLE), isNull());
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("composite"))
|
||||
.build();
|
||||
|
||||
roleResource.addComposites("role", REALM, request);
|
||||
|
||||
verify(roleService).addCompositeRoles(eq("parent-1"), anyList(), eq(REALM),
|
||||
eq(TypeRole.REALM_ROLE), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetComposites() {
|
||||
RoleDTO role = RoleDTO.builder().id("1").name("role").build();
|
||||
List<RoleDTO> composites = Collections.singletonList(RoleDTO.builder().name("composite").build());
|
||||
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(role));
|
||||
when(roleService.getCompositeRoles("1", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(composites);
|
||||
|
||||
List<RoleDTO> result = roleResource.getComposites("role", REALM);
|
||||
|
||||
assertEquals(composites, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateRealmRole_GenericException() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
when(roleService.createRealmRole(any(), eq(REALM)))
|
||||
.thenThrow(new RuntimeException("Internal error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.createRealmRole(input, REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateRealmRole_NotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
RoleDTO input = RoleDTO.builder().description("updated").build();
|
||||
assertThrows(RuntimeException.class, () -> roleResource.updateRealmRole("role", input, REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteRealmRole_NotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.deleteRealmRole("role", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateClientRole_IllegalArgumentException() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
when(roleService.createClientRole(any(RoleDTO.class), eq(CLIENT_ID), eq(REALM)))
|
||||
.thenThrow(new IllegalArgumentException("Conflict"));
|
||||
|
||||
Response response = roleResource.createClientRole(CLIENT_ID, input, REALM);
|
||||
|
||||
assertEquals(409, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateClientRole_GenericException() {
|
||||
RoleDTO input = RoleDTO.builder().name("role").build();
|
||||
when(roleService.createClientRole(any(RoleDTO.class), eq(CLIENT_ID), eq(REALM)))
|
||||
.thenThrow(new RuntimeException("Internal error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.createClientRole(CLIENT_ID, input, REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetClientRole_NotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.CLIENT_ROLE, CLIENT_ID))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.getClientRole(CLIENT_ID, "role", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteClientRole_NotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.CLIENT_ROLE, CLIENT_ID))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.deleteClientRole(CLIENT_ID, "role", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddComposites_ParentNotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("composite"))
|
||||
.build();
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.addComposites("role", REALM, request));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddComposites_ChildNotFound_FilteredOut() {
|
||||
RoleDTO parentRole = RoleDTO.builder().id("parent-1").name("role").build();
|
||||
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.of(parentRole));
|
||||
when(roleService.getRoleByName("nonexistent", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty()); // will be filtered out (null id)
|
||||
doNothing().when(roleService).addCompositeRoles(eq("parent-1"), anyList(), eq(REALM),
|
||||
eq(TypeRole.REALM_ROLE), isNull());
|
||||
|
||||
RoleAssignmentRequestDTO request = RoleAssignmentRequestDTO.builder()
|
||||
.roleNames(Collections.singletonList("nonexistent"))
|
||||
.build();
|
||||
|
||||
roleResource.addComposites("role", REALM, request);
|
||||
|
||||
// addCompositeRoles called with empty list (filtered out)
|
||||
verify(roleService).addCompositeRoles(eq("parent-1"), eq(Collections.emptyList()), eq(REALM),
|
||||
eq(TypeRole.REALM_ROLE), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetComposites_RoleNotFound() {
|
||||
when(roleService.getRoleByName("role", REALM, TypeRole.REALM_ROLE, null))
|
||||
.thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> roleResource.getComposites("role", REALM));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,163 +1,163 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.api.SyncResourceApi;
|
||||
import dev.lions.user.manager.dto.sync.HealthStatusDTO;
|
||||
import dev.lions.user.manager.dto.sync.SyncConsistencyDTO;
|
||||
import dev.lions.user.manager.dto.sync.SyncHistoryDTO;
|
||||
import dev.lions.user.manager.dto.sync.SyncResultDTO;
|
||||
import dev.lions.user.manager.service.SyncService;
|
||||
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.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SyncResourceTest {
|
||||
|
||||
@Mock
|
||||
SyncService syncService;
|
||||
|
||||
@InjectMocks
|
||||
SyncResource syncResource;
|
||||
|
||||
private static final String REALM = "test-realm";
|
||||
|
||||
@Test
|
||||
void testCheckKeycloakHealth() {
|
||||
when(syncService.isKeycloakAvailable()).thenReturn(true);
|
||||
when(syncService.getKeycloakHealthInfo()).thenReturn(Map.of("version", "23.0.0"));
|
||||
|
||||
HealthStatusDTO status = syncResource.checkKeycloakHealth();
|
||||
|
||||
assertTrue(status.isKeycloakAccessible());
|
||||
assertTrue(status.isOverallHealthy());
|
||||
assertEquals("23.0.0", status.getKeycloakVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckKeycloakHealthError() {
|
||||
when(syncService.isKeycloakAvailable()).thenThrow(new RuntimeException("Connection refused"));
|
||||
|
||||
HealthStatusDTO status = syncResource.checkKeycloakHealth();
|
||||
|
||||
assertFalse(status.isOverallHealthy());
|
||||
assertTrue(status.getErrorMessage().contains("Connection refused"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSyncUsers() {
|
||||
when(syncService.syncUsersFromRealm(REALM)).thenReturn(10);
|
||||
|
||||
SyncResultDTO result = syncResource.syncUsers(REALM);
|
||||
|
||||
assertTrue(result.isSuccess());
|
||||
assertEquals(10, result.getUsersCount());
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSyncUsersError() {
|
||||
when(syncService.syncUsersFromRealm(REALM)).thenThrow(new RuntimeException("Sync failed"));
|
||||
|
||||
SyncResultDTO result = syncResource.syncUsers(REALM);
|
||||
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("Sync failed", result.getErrorMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSyncRoles() {
|
||||
when(syncService.syncRolesFromRealm(REALM)).thenReturn(5);
|
||||
|
||||
SyncResultDTO result = syncResource.syncRoles(REALM, null);
|
||||
|
||||
assertTrue(result.isSuccess());
|
||||
assertEquals(5, result.getRealmRolesCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSyncRolesError() {
|
||||
when(syncService.syncRolesFromRealm(REALM)).thenThrow(new RuntimeException("Roles sync failed"));
|
||||
|
||||
SyncResultDTO result = syncResource.syncRoles(REALM, null);
|
||||
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("Roles sync failed", result.getErrorMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPing() {
|
||||
String response = syncResource.ping();
|
||||
|
||||
assertNotNull(response);
|
||||
assertTrue(response.contains("pong"));
|
||||
assertTrue(response.contains("SyncResource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDataConsistency_Success() {
|
||||
when(syncService.checkDataConsistency(REALM)).thenReturn(Map.of(
|
||||
"realmName", REALM,
|
||||
"status", "OK",
|
||||
"usersKeycloakCount", 10,
|
||||
"usersLocalCount", 10
|
||||
));
|
||||
|
||||
var result = syncResource.checkDataConsistency(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
assertEquals("OK", result.getStatus());
|
||||
assertEquals(10, result.getUsersKeycloakCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDataConsistency_Exception() {
|
||||
when(syncService.checkDataConsistency(REALM)).thenThrow(new RuntimeException("DB error"));
|
||||
|
||||
var result = syncResource.checkDataConsistency(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("ERROR", result.getStatus());
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
assertEquals("DB error", result.getError());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLastSyncStatus() {
|
||||
var result = syncResource.getLastSyncStatus(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
assertEquals("NEVER_SYNCED", result.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForceSyncRealm_Success() {
|
||||
when(syncService.forceSyncRealm(REALM)).thenReturn(Map.of());
|
||||
|
||||
var result = syncResource.forceSyncRealm(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("SUCCESS", result.getStatus());
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForceSyncRealm_Exception() {
|
||||
doThrow(new RuntimeException("Force sync failed")).when(syncService).forceSyncRealm(REALM);
|
||||
|
||||
var result = syncResource.forceSyncRealm(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("FAILED", result.getStatus());
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
}
|
||||
}
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.api.SyncResourceApi;
|
||||
import dev.lions.user.manager.dto.sync.HealthStatusDTO;
|
||||
import dev.lions.user.manager.dto.sync.SyncConsistencyDTO;
|
||||
import dev.lions.user.manager.dto.sync.SyncHistoryDTO;
|
||||
import dev.lions.user.manager.dto.sync.SyncResultDTO;
|
||||
import dev.lions.user.manager.service.SyncService;
|
||||
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.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class SyncResourceTest {
|
||||
|
||||
@Mock
|
||||
SyncService syncService;
|
||||
|
||||
@InjectMocks
|
||||
SyncResource syncResource;
|
||||
|
||||
private static final String REALM = "test-realm";
|
||||
|
||||
@Test
|
||||
void testCheckKeycloakHealth() {
|
||||
when(syncService.isKeycloakAvailable()).thenReturn(true);
|
||||
when(syncService.getKeycloakHealthInfo()).thenReturn(Map.of("version", "23.0.0"));
|
||||
|
||||
HealthStatusDTO status = syncResource.checkKeycloakHealth();
|
||||
|
||||
assertTrue(status.isKeycloakAccessible());
|
||||
assertTrue(status.isOverallHealthy());
|
||||
assertEquals("23.0.0", status.getKeycloakVersion());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckKeycloakHealthError() {
|
||||
when(syncService.isKeycloakAvailable()).thenThrow(new RuntimeException("Connection refused"));
|
||||
|
||||
HealthStatusDTO status = syncResource.checkKeycloakHealth();
|
||||
|
||||
assertFalse(status.isOverallHealthy());
|
||||
assertTrue(status.getErrorMessage().contains("Connection refused"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSyncUsers() {
|
||||
when(syncService.syncUsersFromRealm(REALM)).thenReturn(10);
|
||||
|
||||
SyncResultDTO result = syncResource.syncUsers(REALM);
|
||||
|
||||
assertTrue(result.isSuccess());
|
||||
assertEquals(10, result.getUsersCount());
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSyncUsersError() {
|
||||
when(syncService.syncUsersFromRealm(REALM)).thenThrow(new RuntimeException("Sync failed"));
|
||||
|
||||
SyncResultDTO result = syncResource.syncUsers(REALM);
|
||||
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("Sync failed", result.getErrorMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSyncRoles() {
|
||||
when(syncService.syncRolesFromRealm(REALM)).thenReturn(5);
|
||||
|
||||
SyncResultDTO result = syncResource.syncRoles(REALM, null);
|
||||
|
||||
assertTrue(result.isSuccess());
|
||||
assertEquals(5, result.getRealmRolesCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSyncRolesError() {
|
||||
when(syncService.syncRolesFromRealm(REALM)).thenThrow(new RuntimeException("Roles sync failed"));
|
||||
|
||||
SyncResultDTO result = syncResource.syncRoles(REALM, null);
|
||||
|
||||
assertFalse(result.isSuccess());
|
||||
assertEquals("Roles sync failed", result.getErrorMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPing() {
|
||||
String response = syncResource.ping();
|
||||
|
||||
assertNotNull(response);
|
||||
assertTrue(response.contains("pong"));
|
||||
assertTrue(response.contains("SyncResource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDataConsistency_Success() {
|
||||
when(syncService.checkDataConsistency(REALM)).thenReturn(Map.of(
|
||||
"realmName", REALM,
|
||||
"status", "OK",
|
||||
"usersKeycloakCount", 10,
|
||||
"usersLocalCount", 10
|
||||
));
|
||||
|
||||
var result = syncResource.checkDataConsistency(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
assertEquals("OK", result.getStatus());
|
||||
assertEquals(10, result.getUsersKeycloakCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckDataConsistency_Exception() {
|
||||
when(syncService.checkDataConsistency(REALM)).thenThrow(new RuntimeException("DB error"));
|
||||
|
||||
var result = syncResource.checkDataConsistency(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("ERROR", result.getStatus());
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
assertEquals("DB error", result.getError());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLastSyncStatus() {
|
||||
var result = syncResource.getLastSyncStatus(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
assertEquals("NEVER_SYNCED", result.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForceSyncRealm_Success() {
|
||||
when(syncService.forceSyncRealm(REALM)).thenReturn(Map.of());
|
||||
|
||||
var result = syncResource.forceSyncRealm(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("SUCCESS", result.getStatus());
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForceSyncRealm_Exception() {
|
||||
doThrow(new RuntimeException("Force sync failed")).when(syncService).forceSyncRealm(REALM);
|
||||
|
||||
var result = syncResource.forceSyncRealm(REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("FAILED", result.getStatus());
|
||||
assertEquals(REALM, result.getRealmName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,95 +1,95 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.client.KeycloakAdminClient;
|
||||
import dev.lions.user.manager.dto.common.UserSessionStatsDTO;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.keycloak.admin.client.resource.RealmResource;
|
||||
import org.keycloak.admin.client.resource.UserResource;
|
||||
import org.keycloak.admin.client.resource.UsersResource;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class UserMetricsResourceTest {
|
||||
|
||||
@Mock
|
||||
KeycloakAdminClient keycloakAdminClient;
|
||||
|
||||
@Mock
|
||||
RealmResource realmResource;
|
||||
|
||||
@Mock
|
||||
UsersResource usersResource;
|
||||
|
||||
@Mock
|
||||
UserResource userResource1;
|
||||
|
||||
@Mock
|
||||
UserResource userResource2;
|
||||
|
||||
@InjectMocks
|
||||
UserMetricsResource userMetricsResource;
|
||||
|
||||
@Test
|
||||
void testGetUserSessionStats() {
|
||||
// Préparer deux utilisateurs avec des sessions différentes
|
||||
UserRepresentation u1 = new UserRepresentation();
|
||||
u1.setId("u1");
|
||||
UserRepresentation u2 = new UserRepresentation();
|
||||
u2.setId("u2");
|
||||
|
||||
when(keycloakAdminClient.getRealm("test-realm")).thenReturn(realmResource);
|
||||
when(realmResource.users()).thenReturn(usersResource);
|
||||
when(usersResource.list()).thenReturn(List.of(u1, u2));
|
||||
|
||||
// u1 a 2 sessions, u2 en a 0
|
||||
when(usersResource.get("u1")).thenReturn(userResource1);
|
||||
when(usersResource.get("u2")).thenReturn(userResource2);
|
||||
when(userResource1.getUserSessions()).thenReturn(List.of(new org.keycloak.representations.idm.UserSessionRepresentation(),
|
||||
new org.keycloak.representations.idm.UserSessionRepresentation()));
|
||||
when(userResource2.getUserSessions()).thenReturn(List.of());
|
||||
|
||||
UserSessionStatsDTO stats = userMetricsResource.getUserSessionStats("test-realm");
|
||||
|
||||
assertNotNull(stats);
|
||||
assertEquals("test-realm", stats.getRealmName());
|
||||
assertEquals(2L, stats.getTotalUsers());
|
||||
assertEquals(2L, stats.getActiveSessions()); // 2 sessions au total
|
||||
assertEquals(1L, stats.getOnlineUsers()); // 1 utilisateur avec au moins une session
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserSessionStats_DefaultRealm() {
|
||||
when(keycloakAdminClient.getRealm("master")).thenReturn(realmResource);
|
||||
when(realmResource.users()).thenReturn(usersResource);
|
||||
when(usersResource.list()).thenReturn(List.of());
|
||||
|
||||
UserSessionStatsDTO stats = userMetricsResource.getUserSessionStats(null);
|
||||
|
||||
assertNotNull(stats);
|
||||
assertEquals("master", stats.getRealmName());
|
||||
assertEquals(0L, stats.getTotalUsers());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserSessionStats_OnError() {
|
||||
when(keycloakAdminClient.getRealm(anyString()))
|
||||
.thenThrow(new RuntimeException("KC error"));
|
||||
|
||||
Assertions.assertThrows(RuntimeException.class,
|
||||
() -> userMetricsResource.getUserSessionStats("realm"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.client.KeycloakAdminClient;
|
||||
import dev.lions.user.manager.dto.common.UserSessionStatsDTO;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.keycloak.admin.client.resource.RealmResource;
|
||||
import org.keycloak.admin.client.resource.UserResource;
|
||||
import org.keycloak.admin.client.resource.UsersResource;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class UserMetricsResourceTest {
|
||||
|
||||
@Mock
|
||||
KeycloakAdminClient keycloakAdminClient;
|
||||
|
||||
@Mock
|
||||
RealmResource realmResource;
|
||||
|
||||
@Mock
|
||||
UsersResource usersResource;
|
||||
|
||||
@Mock
|
||||
UserResource userResource1;
|
||||
|
||||
@Mock
|
||||
UserResource userResource2;
|
||||
|
||||
@InjectMocks
|
||||
UserMetricsResource userMetricsResource;
|
||||
|
||||
@Test
|
||||
void testGetUserSessionStats() {
|
||||
// Préparer deux utilisateurs avec des sessions différentes
|
||||
UserRepresentation u1 = new UserRepresentation();
|
||||
u1.setId("u1");
|
||||
UserRepresentation u2 = new UserRepresentation();
|
||||
u2.setId("u2");
|
||||
|
||||
when(keycloakAdminClient.getRealm("test-realm")).thenReturn(realmResource);
|
||||
when(realmResource.users()).thenReturn(usersResource);
|
||||
when(usersResource.list()).thenReturn(List.of(u1, u2));
|
||||
|
||||
// u1 a 2 sessions, u2 en a 0
|
||||
when(usersResource.get("u1")).thenReturn(userResource1);
|
||||
when(usersResource.get("u2")).thenReturn(userResource2);
|
||||
when(userResource1.getUserSessions()).thenReturn(List.of(new org.keycloak.representations.idm.UserSessionRepresentation(),
|
||||
new org.keycloak.representations.idm.UserSessionRepresentation()));
|
||||
when(userResource2.getUserSessions()).thenReturn(List.of());
|
||||
|
||||
UserSessionStatsDTO stats = userMetricsResource.getUserSessionStats("test-realm");
|
||||
|
||||
assertNotNull(stats);
|
||||
assertEquals("test-realm", stats.getRealmName());
|
||||
assertEquals(2L, stats.getTotalUsers());
|
||||
assertEquals(2L, stats.getActiveSessions()); // 2 sessions au total
|
||||
assertEquals(1L, stats.getOnlineUsers()); // 1 utilisateur avec au moins une session
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserSessionStats_DefaultRealm() {
|
||||
when(keycloakAdminClient.getRealm("master")).thenReturn(realmResource);
|
||||
when(realmResource.users()).thenReturn(usersResource);
|
||||
when(usersResource.list()).thenReturn(List.of());
|
||||
|
||||
UserSessionStatsDTO stats = userMetricsResource.getUserSessionStats(null);
|
||||
|
||||
assertNotNull(stats);
|
||||
assertEquals("master", stats.getRealmName());
|
||||
assertEquals(0L, stats.getTotalUsers());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserSessionStats_OnError() {
|
||||
when(keycloakAdminClient.getRealm(anyString()))
|
||||
.thenThrow(new RuntimeException("KC error"));
|
||||
|
||||
Assertions.assertThrows(RuntimeException.class,
|
||||
() -> userMetricsResource.getUserSessionStats("realm"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,243 +1,243 @@
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.dto.importexport.ImportResultDTO;
|
||||
import dev.lions.user.manager.dto.user.*;
|
||||
import dev.lions.user.manager.service.UserService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class UserResourceTest {
|
||||
|
||||
@Mock
|
||||
UserService userService;
|
||||
|
||||
@InjectMocks
|
||||
UserResource userResource;
|
||||
|
||||
private static final String REALM = "test-realm";
|
||||
|
||||
@Test
|
||||
void testSearchUsers() {
|
||||
UserSearchCriteriaDTO criteria = UserSearchCriteriaDTO.builder()
|
||||
.realmName(REALM)
|
||||
.searchTerm("test")
|
||||
.page(0)
|
||||
.pageSize(20)
|
||||
.build();
|
||||
|
||||
UserSearchResultDTO mockResult = UserSearchResultDTO.builder()
|
||||
.users(Collections.singletonList(UserDTO.builder().username("test").build()))
|
||||
.totalCount(1L)
|
||||
.build();
|
||||
|
||||
when(userService.searchUsers(any())).thenReturn(mockResult);
|
||||
|
||||
UserSearchResultDTO result = userResource.searchUsers(criteria);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.getTotalCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserById() {
|
||||
UserDTO user = UserDTO.builder().id("1").username("testuser").build();
|
||||
when(userService.getUserById("1", REALM)).thenReturn(Optional.of(user));
|
||||
|
||||
UserDTO result = userResource.getUserById("1", REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(user, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserByIdNotFound() {
|
||||
when(userService.getUserById("1", REALM)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> userResource.getUserById("1", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllUsers() {
|
||||
UserSearchResultDTO mockResult = UserSearchResultDTO.builder()
|
||||
.users(Collections.emptyList())
|
||||
.totalCount(0L)
|
||||
.build();
|
||||
when(userService.getAllUsers(REALM, 0, 20)).thenReturn(mockResult);
|
||||
|
||||
UserSearchResultDTO result = userResource.getAllUsers(REALM, 0, 20);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(0, result.getTotalCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateUser() {
|
||||
UserDTO newUser = UserDTO.builder().username("newuser").email("new@test.com").build();
|
||||
UserDTO createdUser = UserDTO.builder().id("123").username("newuser").email("new@test.com").build();
|
||||
|
||||
when(userService.createUser(any(), eq(REALM))).thenReturn(createdUser);
|
||||
|
||||
Response response = userResource.createUser(newUser, REALM);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
assertEquals(createdUser, response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUser() {
|
||||
UserDTO updateUser = UserDTO.builder()
|
||||
.username("updated")
|
||||
.prenom("John")
|
||||
.nom("Doe")
|
||||
.email("john.doe@test.com")
|
||||
.build();
|
||||
UserDTO updatedUser = UserDTO.builder()
|
||||
.id("1")
|
||||
.username("updated")
|
||||
.prenom("John")
|
||||
.nom("Doe")
|
||||
.email("john.doe@test.com")
|
||||
.build();
|
||||
|
||||
when(userService.updateUser(eq("1"), any(), eq(REALM))).thenReturn(updatedUser);
|
||||
|
||||
UserDTO result = userResource.updateUser("1", updateUser, REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(updatedUser, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteUser() {
|
||||
doNothing().when(userService).deleteUser("1", REALM, false);
|
||||
|
||||
userResource.deleteUser("1", REALM, false);
|
||||
|
||||
verify(userService).deleteUser("1", REALM, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testActivateUser() {
|
||||
doNothing().when(userService).activateUser("1", REALM);
|
||||
|
||||
userResource.activateUser("1", REALM);
|
||||
|
||||
verify(userService).activateUser("1", REALM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeactivateUser() {
|
||||
doNothing().when(userService).deactivateUser("1", REALM, "reason");
|
||||
|
||||
userResource.deactivateUser("1", REALM, "reason");
|
||||
|
||||
verify(userService).deactivateUser("1", REALM, "reason");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testResetPassword() {
|
||||
doNothing().when(userService).resetPassword("1", REALM, "newpassword", true);
|
||||
|
||||
PasswordResetRequestDTO request = PasswordResetRequestDTO.builder()
|
||||
.password("newpassword")
|
||||
.temporary(true)
|
||||
.build();
|
||||
|
||||
userResource.resetPassword("1", REALM, request);
|
||||
|
||||
verify(userService).resetPassword("1", REALM, "newpassword", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendVerificationEmail() {
|
||||
doNothing().when(userService).sendVerificationEmail("1", REALM);
|
||||
|
||||
Response response = userResource.sendVerificationEmail("1", REALM);
|
||||
|
||||
verify(userService).sendVerificationEmail("1", REALM);
|
||||
assertNotNull(response);
|
||||
assertEquals(202, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogoutAllSessions() {
|
||||
when(userService.logoutAllSessions("1", REALM)).thenReturn(5);
|
||||
|
||||
SessionsRevokedDTO result = userResource.logoutAllSessions("1", REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(5, result.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetActiveSessions() {
|
||||
when(userService.getActiveSessions("1", REALM)).thenReturn(Collections.singletonList("session-1"));
|
||||
|
||||
List<String> result = userResource.getActiveSessions("1", REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("session-1", result.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateUser_IllegalArgumentException() {
|
||||
UserDTO newUser = UserDTO.builder().username("existinguser").email("existing@test.com").build();
|
||||
when(userService.createUser(any(), eq(REALM))).thenThrow(new IllegalArgumentException("Username exists"));
|
||||
|
||||
Response response = userResource.createUser(newUser, REALM);
|
||||
|
||||
assertEquals(409, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateUser_RuntimeException() {
|
||||
UserDTO newUser = UserDTO.builder().username("user").email("user@test.com").build();
|
||||
when(userService.createUser(any(), eq(REALM))).thenThrow(new RuntimeException("Connection error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> userResource.createUser(newUser, REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExportUsersToCSV() {
|
||||
String csvContent = "username,email,prenom,nom\ntest,test@test.com,Test,User";
|
||||
when(userService.exportUsersToCSV(any())).thenReturn(csvContent);
|
||||
|
||||
Response response = userResource.exportUsersToCSV(REALM);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(csvContent, response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testImportUsersFromCSV() {
|
||||
String csvContent = "username,email,prenom,nom\ntest,test@test.com,Test,User";
|
||||
ImportResultDTO importResult = ImportResultDTO.builder()
|
||||
.successCount(1)
|
||||
.errorCount(0)
|
||||
.totalLines(2)
|
||||
.errors(Collections.emptyList())
|
||||
.build();
|
||||
when(userService.importUsersFromCSV(csvContent, REALM)).thenReturn(importResult);
|
||||
|
||||
ImportResultDTO result = userResource.importUsersFromCSV(REALM, csvContent);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.getSuccessCount());
|
||||
assertEquals(0, result.getErrorCount());
|
||||
}
|
||||
}
|
||||
package dev.lions.user.manager.resource;
|
||||
|
||||
import dev.lions.user.manager.dto.importexport.ImportResultDTO;
|
||||
import dev.lions.user.manager.dto.user.*;
|
||||
import dev.lions.user.manager.service.UserService;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class UserResourceTest {
|
||||
|
||||
@Mock
|
||||
UserService userService;
|
||||
|
||||
@InjectMocks
|
||||
UserResource userResource;
|
||||
|
||||
private static final String REALM = "test-realm";
|
||||
|
||||
@Test
|
||||
void testSearchUsers() {
|
||||
UserSearchCriteriaDTO criteria = UserSearchCriteriaDTO.builder()
|
||||
.realmName(REALM)
|
||||
.searchTerm("test")
|
||||
.page(0)
|
||||
.pageSize(20)
|
||||
.build();
|
||||
|
||||
UserSearchResultDTO mockResult = UserSearchResultDTO.builder()
|
||||
.users(Collections.singletonList(UserDTO.builder().username("test").build()))
|
||||
.totalCount(1L)
|
||||
.build();
|
||||
|
||||
when(userService.searchUsers(any())).thenReturn(mockResult);
|
||||
|
||||
UserSearchResultDTO result = userResource.searchUsers(criteria);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.getTotalCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserById() {
|
||||
UserDTO user = UserDTO.builder().id("1").username("testuser").build();
|
||||
when(userService.getUserById("1", REALM)).thenReturn(Optional.of(user));
|
||||
|
||||
UserDTO result = userResource.getUserById("1", REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(user, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUserByIdNotFound() {
|
||||
when(userService.getUserById("1", REALM)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(RuntimeException.class, () -> userResource.getUserById("1", REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllUsers() {
|
||||
UserSearchResultDTO mockResult = UserSearchResultDTO.builder()
|
||||
.users(Collections.emptyList())
|
||||
.totalCount(0L)
|
||||
.build();
|
||||
when(userService.getAllUsers(REALM, 0, 20)).thenReturn(mockResult);
|
||||
|
||||
UserSearchResultDTO result = userResource.getAllUsers(REALM, 0, 20);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(0, result.getTotalCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateUser() {
|
||||
UserDTO newUser = UserDTO.builder().username("newuser").email("new@test.com").build();
|
||||
UserDTO createdUser = UserDTO.builder().id("123").username("newuser").email("new@test.com").build();
|
||||
|
||||
when(userService.createUser(any(), eq(REALM))).thenReturn(createdUser);
|
||||
|
||||
Response response = userResource.createUser(newUser, REALM);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
assertEquals(createdUser, response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateUser() {
|
||||
UserDTO updateUser = UserDTO.builder()
|
||||
.username("updated")
|
||||
.prenom("John")
|
||||
.nom("Doe")
|
||||
.email("john.doe@test.com")
|
||||
.build();
|
||||
UserDTO updatedUser = UserDTO.builder()
|
||||
.id("1")
|
||||
.username("updated")
|
||||
.prenom("John")
|
||||
.nom("Doe")
|
||||
.email("john.doe@test.com")
|
||||
.build();
|
||||
|
||||
when(userService.updateUser(eq("1"), any(), eq(REALM))).thenReturn(updatedUser);
|
||||
|
||||
UserDTO result = userResource.updateUser("1", updateUser, REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(updatedUser, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteUser() {
|
||||
doNothing().when(userService).deleteUser("1", REALM, false);
|
||||
|
||||
userResource.deleteUser("1", REALM, false);
|
||||
|
||||
verify(userService).deleteUser("1", REALM, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testActivateUser() {
|
||||
doNothing().when(userService).activateUser("1", REALM);
|
||||
|
||||
userResource.activateUser("1", REALM);
|
||||
|
||||
verify(userService).activateUser("1", REALM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeactivateUser() {
|
||||
doNothing().when(userService).deactivateUser("1", REALM, "reason");
|
||||
|
||||
userResource.deactivateUser("1", REALM, "reason");
|
||||
|
||||
verify(userService).deactivateUser("1", REALM, "reason");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testResetPassword() {
|
||||
doNothing().when(userService).resetPassword("1", REALM, "newpassword", true);
|
||||
|
||||
PasswordResetRequestDTO request = PasswordResetRequestDTO.builder()
|
||||
.password("newpassword")
|
||||
.temporary(true)
|
||||
.build();
|
||||
|
||||
userResource.resetPassword("1", REALM, request);
|
||||
|
||||
verify(userService).resetPassword("1", REALM, "newpassword", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSendVerificationEmail() {
|
||||
doNothing().when(userService).sendVerificationEmail("1", REALM);
|
||||
|
||||
Response response = userResource.sendVerificationEmail("1", REALM);
|
||||
|
||||
verify(userService).sendVerificationEmail("1", REALM);
|
||||
assertNotNull(response);
|
||||
assertEquals(202, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogoutAllSessions() {
|
||||
when(userService.logoutAllSessions("1", REALM)).thenReturn(5);
|
||||
|
||||
SessionsRevokedDTO result = userResource.logoutAllSessions("1", REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(5, result.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetActiveSessions() {
|
||||
when(userService.getActiveSessions("1", REALM)).thenReturn(Collections.singletonList("session-1"));
|
||||
|
||||
List<String> result = userResource.getActiveSessions("1", REALM);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("session-1", result.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateUser_IllegalArgumentException() {
|
||||
UserDTO newUser = UserDTO.builder().username("existinguser").email("existing@test.com").build();
|
||||
when(userService.createUser(any(), eq(REALM))).thenThrow(new IllegalArgumentException("Username exists"));
|
||||
|
||||
Response response = userResource.createUser(newUser, REALM);
|
||||
|
||||
assertEquals(409, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateUser_RuntimeException() {
|
||||
UserDTO newUser = UserDTO.builder().username("user").email("user@test.com").build();
|
||||
when(userService.createUser(any(), eq(REALM))).thenThrow(new RuntimeException("Connection error"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> userResource.createUser(newUser, REALM));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExportUsersToCSV() {
|
||||
String csvContent = "username,email,prenom,nom\ntest,test@test.com,Test,User";
|
||||
when(userService.exportUsersToCSV(any())).thenReturn(csvContent);
|
||||
|
||||
Response response = userResource.exportUsersToCSV(REALM);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(csvContent, response.getEntity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testImportUsersFromCSV() {
|
||||
String csvContent = "username,email,prenom,nom\ntest,test@test.com,Test,User";
|
||||
ImportResultDTO importResult = ImportResultDTO.builder()
|
||||
.successCount(1)
|
||||
.errorCount(0)
|
||||
.totalLines(2)
|
||||
.errors(Collections.emptyList())
|
||||
.build();
|
||||
when(userService.importUsersFromCSV(csvContent, REALM)).thenReturn(importResult);
|
||||
|
||||
ImportResultDTO result = userResource.importUsersFromCSV(REALM, csvContent);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.getSuccessCount());
|
||||
assertEquals(0, result.getErrorCount());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user