Download src/test/java/com/anthony/failsafeapi/model/ProcessingContextTest.java from tonyy1/tickets: direct link, hf CLI and curl.
- Browser
- Download file 6.45 kB
-
https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/model/ProcessingContextTest.java
- Command line
-
hf download hf://spaces/tonyy1/tickets/src/test/java/com/anthony/failsafeapi/model/ProcessingContextTest.java
-
curl -L -o ProcessingContextTest.java https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/model/ProcessingContextTest.java
6.45 kB
| package com.anthony.failsafeapi.model; | |
| 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 static org.assertj.core.api.Assertions.assertThat; | |
| class ProcessingContextTest { | |
| private ProcessingContext ctx; | |
| void setUp() { | |
| ctx = new ProcessingContext(); | |
| } | |
| // ───────────────────────────────────────────────────────────── | |
| // Estado inicial | |
| // ───────────────────────────────────────────────────────────── | |
| class InitialStateTests { | |
| void piiDetectedShouldBeFalseByDefault() { | |
| assertThat(ctx.isPiiDetected()).isFalse(); | |
| } | |
| void securityLayersShouldBeEmptyByDefault() { | |
| assertThat(ctx.getSecurityLayersPassed()).isEmpty(); | |
| } | |
| void maskedDescriptionShouldBeNullByDefault() { | |
| assertThat(ctx.getMaskedDescription()).isNull(); | |
| } | |
| } | |
| // ───────────────────────────────────────────────────────────── | |
| // Marcado de PII | |
| // ───────────────────────────────────────────────────────────── | |
| class PiiDetectionTests { | |
| void markPiiDetectedShouldSetFlagToTrue() { | |
| ctx.markPiiDetected(); | |
| assertThat(ctx.isPiiDetected()).isTrue(); | |
| } | |
| void markPiiDetectedMultipleTimesShouldBeIdempotent() { | |
| ctx.markPiiDetected(); | |
| ctx.markPiiDetected(); | |
| assertThat(ctx.isPiiDetected()).isTrue(); | |
| } | |
| } | |
| // ───────────────────────────────────────────────────────────── | |
| // Capas de seguridad | |
| // ───────────────────────────────────────────────────────────── | |
| class SecurityLayersTests { | |
| void shouldAddSingleLayer() { | |
| ctx.addSecurityLayer(SecurityLayer.INJECTION_DETECTION); | |
| assertThat(ctx.getSecurityLayersPassed()).containsExactly("INJECTION_DETECTION"); | |
| } | |
| void shouldAddMultipleLayersInOrder() { | |
| ctx.addSecurityLayer(SecurityLayer.INJECTION_DETECTION); | |
| ctx.addSecurityLayer(SecurityLayer.PII_MASKING); | |
| ctx.addSecurityLayer(SecurityLayer.LLM_CALL); | |
| assertThat(ctx.getSecurityLayersPassed()) | |
| .containsExactly("INJECTION_DETECTION", "PII_MASKING", "LLM_CALL"); | |
| } | |
| void shouldReturnLayersWithTiming() { | |
| ctx.addSecurityLayer(SecurityLayer.INJECTION_DETECTION); | |
| ctx.addSecurityLayer(SecurityLayer.PII_MASKING); | |
| assertThat(ctx.getSecurityLayersWithTiming()).hasSize(2); | |
| assertThat(ctx.getSecurityLayersWithTiming()) | |
| .extracting(ProcessingContext.SecurityLayerRecord::name) | |
| .containsExactly("INJECTION_DETECTION", "PII_MASKING"); | |
| assertThat(ctx.getSecurityLayersWithTiming()) | |
| .extracting(ProcessingContext.SecurityLayerRecord::elapsedMs) | |
| .allMatch(ms -> ms >= 0); | |
| } | |
| void securityLayersShouldBeImmutable() { | |
| ctx.addSecurityLayer(SecurityLayer.INJECTION_DETECTION); | |
| var layers = ctx.getSecurityLayersWithTiming(); | |
| // Intentar modificar la lista devuelta no debe afectar al contexto | |
| org.assertj.core.api.Assertions.assertThatThrownBy( | |
| () -> layers.add(new ProcessingContext.SecurityLayerRecord("EXTRA", 0L)) | |
| ).isInstanceOf(UnsupportedOperationException.class); | |
| } | |
| } | |
| // ───────────────────────────────────────────────────────────── | |
| // Descripción enmascarada | |
| // ───────────────────────────────────────────────────────────── | |
| class MaskedDescriptionTests { | |
| void shouldSetMaskedDescription() { | |
| ctx.setMaskedDescription("Texto con [EMAIL] enmascarado"); | |
| assertThat(ctx.getMaskedDescription()).isEqualTo("Texto con [EMAIL] enmascarado"); | |
| } | |
| void shouldAllowOverwrite() { | |
| ctx.setMaskedDescription("primera versión"); | |
| ctx.setMaskedDescription("segunda versión"); | |
| assertThat(ctx.getMaskedDescription()).isEqualTo("segunda versión"); | |
| } | |
| } | |
| } | |