tickets / src /test /java /com /anthony /failsafeapi /exception /GlobalExceptionHandlerTest.java
An7h0ny1
src
a718a03
Raw History Blame Contribute Delete
10.2 kB
package com.anthony.failsafeapi.exception;
import com.anthony.failsafeapi.model.ProcessingContext;
import com.anthony.failsafeapi.model.TicketResponse;
import com.anthony.failsafeapi.util.constants.SecurityLayer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("GlobalExceptionHandler - Manejo centralizado de excepciones")
class GlobalExceptionHandlerTest {
private GlobalExceptionHandler handler;
@BeforeEach
void setUp() {
handler = new GlobalExceptionHandler();
}
// ─────────────────────────────────────────────────────────────
// AIResponseParseException β†’ 500
// ─────────────────────────────────────────────────────────────
@Nested
@DisplayName("AIResponseParseException β†’ HTTP 500")
class ParseExceptionTests {
@Test
@DisplayName("Devuelve 500 con fuente AI_PARSER")
void shouldReturn500() {
ResponseEntity<ErrorResponse> response = handler.handleParse(
new AIResponseParseException("parse error", null));
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().source()).isEqualTo("AI_PARSER");
assertThat(response.getBody().status()).isEqualTo("ERROR");
}
}
// ─────────────────────────────────────────────────────────────
// AISanitizationException β†’ 400
// ─────────────────────────────────────────────────────────────
@Nested
@DisplayName("AISanitizationException β†’ HTTP 400")
class SanitizationExceptionTests {
@Test
@DisplayName("Devuelve 400 con fuente SANITIZER y el mensaje original")
void shouldReturn400WithMessage() {
String reason = "SEGURIDAD: inyecciΓ³n detectada";
ResponseEntity<ErrorResponse> response = handler.handleSanitization(
new AISanitizationException(reason));
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().source()).isEqualTo("SANITIZER");
assertThat(response.getBody().status()).isEqualTo("REJECTED");
assertThat(response.getBody().message()).isEqualTo(reason);
}
}
// ─────────────────────────────────────────────────────────────
// RateLimitExceededException β†’ 429
// ─────────────────────────────────────────────────────────────
@Nested
@DisplayName("RateLimitExceededException β†’ HTTP 429")
class RateLimitExceptionTests {
@Test
@DisplayName("Devuelve 429 con fuente RATE_LIMITER")
void shouldReturn429() {
ResponseEntity<ErrorResponse> response = handler.handleRateLimit(
new RateLimitExceededException("IP limit exceeded"));
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.TOO_MANY_REQUESTS);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().source()).isEqualTo("RATE_LIMITER");
assertThat(response.getBody().status()).isEqualTo("REJECTED");
assertThat(response.getBody().message()).isEqualTo("RATE_LIMIT_EXCEEDED");
}
}
// ─────────────────────────────────────────────────────────────
// IllegalArgumentException β†’ 400
// ─────────────────────────────────────────────────────────────
@Nested
@DisplayName("IllegalArgumentException β†’ HTTP 400")
class IllegalArgumentTests {
@Test
@DisplayName("Devuelve 400 con fuente VALIDATOR e informaciΓ³n del error")
void shouldReturn400WithValidatorSource() {
ResponseEntity<ErrorResponse> response = handler.handleIllegalArgument(
new IllegalArgumentException("DescripciΓ³n vacΓ­a"));
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().source()).isEqualTo("VALIDATOR");
assertThat(response.getBody().message()).contains("DescripciΓ³n vacΓ­a");
}
}
// ─────────────────────────────────────────────────────────────
// FallbackActivatedException β†’ 503
// ─────────────────────────────────────────────────────────────
@Nested
@DisplayName("FallbackActivatedException β†’ HTTP 503")
class FallbackExceptionTests {
@Test
@DisplayName("Devuelve 503 con estado DEGRADED y la categorΓ­a inferida")
void shouldReturn503WithDegradedStatus() {
ProcessingContext ctx = new ProcessingContext();
ctx.addSecurityLayer(SecurityLayer.INJECTION_DETECTION);
FallbackActivatedException ex = new FallbackActivatedException("URGENTE", ctx);
ResponseEntity<TicketResponse> response = handler.handleFallback(ex);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
TicketResponse body = response.getBody();
assertThat(body).isNotNull();
assertThat(body.status()).isEqualTo("DEGRADED");
assertThat(body.category()).isEqualTo("URGENTE");
assertThat(body.provider()).isEqualTo("LOCAL_BERT_MODEL");
}
@Test
@DisplayName("Incluye las capas de seguridad del contexto en la respuesta")
void shouldIncludeSecurityLayersFromContext() {
ProcessingContext ctx = new ProcessingContext();
ctx.addSecurityLayer(SecurityLayer.INJECTION_DETECTION);
ctx.addSecurityLayer(SecurityLayer.PII_MASKING_FAILED);
FallbackActivatedException ex = new FallbackActivatedException("NORMAL", ctx);
ResponseEntity<TicketResponse> response = handler.handleFallback(ex);
assertThat(response.getBody().securityLayersPassed())
.contains("INJECTION_DETECTION", "PII_MASKING_FAILED");
}
@Test
@DisplayName("Refleja el estado de PII detectada en la respuesta fallback")
void shouldReflectPiiDetectedState() {
ProcessingContext ctx = new ProcessingContext();
ctx.markPiiDetected();
FallbackActivatedException ex = new FallbackActivatedException("BAJO", ctx);
ResponseEntity<TicketResponse> response = handler.handleFallback(ex);
assertThat(response.getBody().piiDetected()).isTrue();
}
}
// ─────────────────────────────────────────────────────────────
// IllegalStateException β†’ 503
// ─────────────────────────────────────────────────────────────
@Nested
@DisplayName("IllegalStateException β†’ HTTP 503")
class IllegalStateTests {
@Test
@DisplayName("Devuelve 503 con fuente MODEL_LOADER")
void shouldReturn503WithModelLoaderSource() {
ResponseEntity<ErrorResponse> response = handler.handleIllegalState(
new IllegalStateException("Modelos aΓΊn cargando"));
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
assertThat(response.getBody().source()).isEqualTo("MODEL_LOADER");
assertThat(response.getBody().status()).isEqualTo("UNAVAILABLE");
}
}
// ─────────────────────────────────────────────────────────────
// Exception genΓ©rica β†’ 500
// ─────────────────────────────────────────────────────────────
@Nested
@DisplayName("Exception genΓ©rica β†’ HTTP 500")
class GenericExceptionTests {
@Test
@DisplayName("Devuelve 500 con fuente UNKNOWN para excepciones no clasificadas")
void shouldReturn500ForUnknownException() {
ResponseEntity<ErrorResponse> response = handler.handleGenericException(
new RuntimeException("Unexpected"));
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
assertThat(response.getBody().source()).isEqualTo("UNKNOWN");
assertThat(response.getBody().message()).isEqualTo("UNEXPECTED_ERROR");
}
}
}