Download src/test/java/com/anthony/failsafeapi/service/DataRetentionServiceTest.java from tonyy1/tickets: direct link, hf CLI and curl.
- Browser
- Download file 9.78 kB
-
https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/DataRetentionServiceTest.java
- Command line
-
hf download hf://spaces/tonyy1/tickets/src/test/java/com/anthony/failsafeapi/service/DataRetentionServiceTest.java
-
curl -L -o DataRetentionServiceTest.java https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/DataRetentionServiceTest.java
9.78 kB
| package com.anthony.failsafeapi.service; | |
| import com.anthony.failsafeapi.audit.TokenAuditRepository; | |
| import org.junit.jupiter.api.DisplayName; | |
| import org.junit.jupiter.api.Nested; | |
| import org.junit.jupiter.api.Test; | |
| import org.junit.jupiter.api.extension.ExtendWith; | |
| import org.mockito.ArgumentCaptor; | |
| import org.mockito.InjectMocks; | |
| import org.mockito.Mock; | |
| import org.mockito.junit.jupiter.MockitoExtension; | |
| import java.time.Instant; | |
| import java.time.temporal.ChronoUnit; | |
| import static org.assertj.core.api.Assertions.assertThatNoException; | |
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | |
| import static org.mockito.ArgumentMatchers.any; | |
| import static org.mockito.ArgumentMatchers.anyInt; | |
| import static org.mockito.Mockito.*; | |
| /** | |
| * Tests para DataRetentionService. | |
| * | |
| * la purga se hace en lotes pequeΓ±os | |
| * (deleteBatchBefore con LIMIT 1000) en vez de un DELETE masivo ΓΊnico. | |
| * Estos tests verifican el comportamiento del loop, no la SQL subyacente. | |
| * | |
| * Conceptos clave: | |
| * - MAX_BATCHES_PER_RUN: lΓmite de seguridad (100 lotes por ejecuciΓ³n) | |
| * - Cada lote es independiente: si uno falla, los anteriores se mantienen | |
| * - El loop para cuando un lote devuelve 0 (no hay mΓ‘s registros viejos) | |
| */ | |
| class DataRetentionServiceTest { | |
| private TokenAuditRepository auditRepository; | |
| private DataRetentionService dataRetentionService; | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // CΓ‘lculo del cutoff date (~30 dΓas atrΓ‘s) | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class CutoffDateTests { | |
| void shouldCallDeleteWithCutoff30DaysAgo() { | |
| // Primer lote: devuelve 0 β el loop termina inmediatamente | |
| when(auditRepository.deleteBatchBefore(any(Instant.class), anyInt())) | |
| .thenReturn(0); | |
| dataRetentionService.purgeOldAuditRecords(); | |
| ArgumentCaptor<Instant> cutoffCaptor = ArgumentCaptor.forClass(Instant.class); | |
| verify(auditRepository).deleteBatchBefore(cutoffCaptor.capture(), anyInt()); | |
| Instant capturedCutoff = cutoffCaptor.getValue(); | |
| Instant expected30DaysAgo = Instant.now().minus(30, ChronoUnit.DAYS); | |
| // Tolerancia de 5 segundos para tiempo de ejecuciΓ³n del test | |
| assertThat(capturedCutoff) | |
| .isAfter(expected30DaysAgo.minusSeconds(5)) | |
| .isBefore(expected30DaysAgo.plusSeconds(5)); | |
| } | |
| void shouldPassBatchSizeAsLimit() { | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())).thenReturn(0); | |
| dataRetentionService.purgeOldAuditRecords(); | |
| ArgumentCaptor<Integer> limitCaptor = ArgumentCaptor.forClass(Integer.class); | |
| verify(auditRepository).deleteBatchBefore(any(), limitCaptor.capture()); | |
| // El batch size debe ser positivo (el valor exacto es 1000 pero no | |
| // queremos acoplar el test a una constante mΓ‘gica β solo verificar | |
| // que es razonable) | |
| assertThat(limitCaptor.getValue()).isPositive().isLessThanOrEqualTo(10000); | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Comportamiento del loop de lotes | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class BatchLoopTests { | |
| void shouldNotThrowWhenDeletionSucceeds() { | |
| // 3 lotes de 1000 y luego 0 β loop termina | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())) | |
| .thenReturn(1000, 1000, 1000, 0); | |
| assertThatNoException().isThrownBy(() -> | |
| dataRetentionService.purgeOldAuditRecords()); | |
| } | |
| void shouldNotThrowWhenNothingToDelete() { | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())).thenReturn(0); | |
| assertThatNoException().isThrownBy(() -> | |
| dataRetentionService.purgeOldAuditRecords()); | |
| } | |
| void shouldCallRepositoryOnceWhenFirstBatchIsEmpty() { | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())).thenReturn(0); | |
| dataRetentionService.purgeOldAuditRecords(); | |
| verify(auditRepository, times(1)).deleteBatchBefore(any(), anyInt()); | |
| } | |
| void shouldExecuteMultipleBatchesUntilEmpty() { | |
| // 5 lotes con datos, lote 6 devuelve 0 β loop termina | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())) | |
| .thenReturn(1000, 1000, 1000, 1000, 1000, 0); | |
| dataRetentionService.purgeOldAuditRecords(); | |
| // Debe llamarse 6 veces: 5 con datos + 1 con 0 | |
| verify(auditRepository, times(6)).deleteBatchBefore(any(), anyInt()); | |
| } | |
| void shouldStopLoopWhenBatchReturnsFewerThanBatchSize() { | |
| // Un lote que devuelve 500 (menos de 1000) y luego 0 | |
| // el loop se detiene cuando devuelve 0, no cuando devuelve < BATCH_SIZE | |
| // porque el repositorio puede devolver < 1000 si quedan menos registros | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())) | |
| .thenReturn(500, 0); | |
| dataRetentionService.purgeOldAuditRecords(); | |
| verify(auditRepository, times(2)).deleteBatchBefore(any(), anyInt()); | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // LΓmite de seguridad (MAX_BATCHES_PER_RUN) | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class MaxBatchesLimitTests { | |
| void shouldStopAfterMaxBatchesPerRun() { | |
| // El repositorio siempre devuelve 1000 (nunca se vacΓa) | |
| // El loop debe detenerse en MAX_BATCHES_PER_RUN (100) | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())).thenReturn(1000); | |
| dataRetentionService.purgeOldAuditRecords(); | |
| // Debe llamarse exactamente MAX_BATCHES_PER_RUN veces (100) | |
| // Si este nΓΊmero mΓ‘gico cambia en el cΓ³digo, actualizar el test. | |
| verify(auditRepository, times(100)).deleteBatchBefore(any(), anyInt()); | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Resistencia a fallos | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class RepositoryFailureTests { | |
| void shouldNotPropagateRepositoryExceptionOnFirstBatch() { | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())) | |
| .thenThrow(new RuntimeException("Database connection lost")); | |
| assertThatNoException().isThrownBy(() -> | |
| dataRetentionService.purgeOldAuditRecords()); | |
| } | |
| void shouldKeepPreviousBatchesWhenOneFails() { | |
| // Lote 1 y 2: exitosos (1000 registros cada uno) | |
| // Lote 3: falla | |
| when(auditRepository.deleteBatchBefore(any(), anyInt())) | |
| .thenReturn(1000, 1000) | |
| .thenThrow(new RuntimeException("Connection lost on batch 3")); | |
| // El mΓ©todo debe absorber el error y no lanzar | |
| assertThatNoException().isThrownBy(() -> | |
| dataRetentionService.purgeOldAuditRecords()); | |
| // Lotes 1 y 2 ya se ejecutaron antes del fallo (commit atΓ³mico por lote) | |
| verify(auditRepository, times(3)).deleteBatchBefore(any(), anyInt()); | |
| } | |
| } | |
| } |