Download src/test/java/com/anthony/failsafeapi/service/AuditPersistenceServiceTest.java from tonyy1/tickets: direct link, hf CLI and curl.
- Browser
- Download file 5.35 kB
-
https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/AuditPersistenceServiceTest.java
- Command line
-
hf download hf://spaces/tonyy1/tickets/src/test/java/com/anthony/failsafeapi/service/AuditPersistenceServiceTest.java
-
curl -L -o AuditPersistenceServiceTest.java https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/AuditPersistenceServiceTest.java
5.35 kB
| package com.anthony.failsafeapi.service; | |
| import com.anthony.failsafeapi.audit.TokenAuditRecord; | |
| import com.anthony.failsafeapi.audit.TokenAuditRepository; | |
| import com.anthony.failsafeapi.event.AiCallEvent; | |
| import com.anthony.failsafeapi.util.constants.AuditStatus; | |
| 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.math.BigDecimal; | |
| import java.time.Instant; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| import static org.mockito.Mockito.*; | |
| class AuditPersistenceServiceTest { | |
| private TokenAuditRepository repository; | |
| private AuditPersistenceService auditPersistenceService; | |
| // ───────────────────────────────────────────────────────────── | |
| // Evento SUCCESS | |
| // ───────────────────────────────────────────────────────────── | |
| class SuccessEventTests { | |
| void shouldSaveSuccessRecord() { | |
| AiCallEvent event = new AiCallEvent( | |
| 100, 50, new BigDecimal("0.00005"), | |
| "groq/llama", AuditStatus.SUCCESS, null, 320L, Instant.now() | |
| ); | |
| auditPersistenceService.persist(event); | |
| ArgumentCaptor<TokenAuditRecord> captor = ArgumentCaptor.forClass(TokenAuditRecord.class); | |
| verify(repository, times(1)).save(captor.capture()); | |
| TokenAuditRecord saved = captor.getValue(); | |
| assertThat(saved.getInputTokens()).isEqualTo(100); | |
| assertThat(saved.getOutputTokens()).isEqualTo(50); | |
| assertThat(saved.getEstimatedCostUsd()).isEqualByComparingTo("0.00005"); | |
| assertThat(saved.getProvider()).isEqualTo("groq/llama"); | |
| assertThat(saved.getStatus()).isEqualTo(AuditStatus.SUCCESS); | |
| assertThat(saved.getLatencyMs()).isEqualTo(320L); | |
| } | |
| } | |
| // ───────────────────────────────────────────────────────────── | |
| // Evento EMERGENCY (fallback) | |
| // ───────────────────────────────────────────────────────────── | |
| class FallbackEventTests { | |
| void shouldSaveFallbackRecordWithZeroCost() { | |
| AiCallEvent event = new AiCallEvent( | |
| 0, 0, BigDecimal.ZERO, | |
| "LOCAL_BERT_MODEL", AuditStatus.EMERGENCY, | |
| "CallNotPermittedException", 0L, Instant.now() | |
| ); | |
| auditPersistenceService.persist(event); | |
| ArgumentCaptor<TokenAuditRecord> captor = ArgumentCaptor.forClass(TokenAuditRecord.class); | |
| verify(repository, times(1)).save(captor.capture()); | |
| TokenAuditRecord saved = captor.getValue(); | |
| assertThat(saved.getInputTokens()).isZero(); | |
| assertThat(saved.getOutputTokens()).isZero(); | |
| assertThat(saved.getEstimatedCostUsd()).isEqualByComparingTo(BigDecimal.ZERO); | |
| assertThat(saved.getProvider()).isEqualTo("LOCAL_BERT_MODEL"); | |
| assertThat(saved.getStatus()).isEqualTo(AuditStatus.EMERGENCY); | |
| assertThat(saved.getFallbackReason()).isEqualTo("CallNotPermittedException"); | |
| } | |
| } | |
| // ───────────────────────────────────────────────────────────── | |
| // Recuperación tras fallos | |
| // ───────────────────────────────────────────────────────────── | |
| class RecoveryTests { | |
| void recoverShouldNotThrow() { | |
| AiCallEvent event = new AiCallEvent( | |
| 10, 5, new BigDecimal("0.00001"), | |
| "groq/llama", AuditStatus.SUCCESS, null, 100L, Instant.now() | |
| ); | |
| // recover es un método público; verificamos que no propaga nada | |
| org.assertj.core.api.Assertions.assertThatNoException() | |
| .isThrownBy(() -> auditPersistenceService.recover(new RuntimeException("DB down"), event)); | |
| } | |
| } | |
| } | |