Download src/test/java/com/anthony/failsafeapi/exception/GlobalExceptionHandlerTest.java from tonyy1/tickets: direct link, hf CLI and curl.
- Browser
- Download file 10.2 kB
-
https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/exception/GlobalExceptionHandlerTest.java
- Command line
-
hf download hf://spaces/tonyy1/tickets/src/test/java/com/anthony/failsafeapi/exception/GlobalExceptionHandlerTest.java
-
curl -L -o GlobalExceptionHandlerTest.java https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/exception/GlobalExceptionHandlerTest.java
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; | |
| class GlobalExceptionHandlerTest { | |
| private GlobalExceptionHandler handler; | |
| void setUp() { | |
| handler = new GlobalExceptionHandler(); | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // AIResponseParseException β 500 | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class ParseExceptionTests { | |
| 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 | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class SanitizationExceptionTests { | |
| 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 | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class RateLimitExceptionTests { | |
| 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 | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class IllegalArgumentTests { | |
| 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 | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class FallbackExceptionTests { | |
| 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"); | |
| } | |
| 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"); | |
| } | |
| 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 | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class IllegalStateTests { | |
| 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 | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class GenericExceptionTests { | |
| 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"); | |
| } | |
| } | |
| } | |