Download src/test/java/com/anthony/failsafeapi/service/AuditServiceTest.java from tonyy1/tickets: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/AuditServiceTest.java
- Command line
-
hf download hf://spaces/tonyy1/tickets/src/test/java/com/anthony/failsafeapi/service/AuditServiceTest.java
-
curl -L -o AuditServiceTest.java https://huggingface.co/spaces/tonyy1/tickets/resolve/main/src/test/java/com/anthony/failsafeapi/service/AuditServiceTest.java
6.65 kB
| package com.anthony.failsafeapi.service; | |
| import com.anthony.failsafeapi.audit.TokenAuditRepository; | |
| import com.anthony.failsafeapi.model.AuditSummary; | |
| import com.anthony.failsafeapi.util.constants.AuditStatus; | |
| import io.github.resilience4j.circuitbreaker.CircuitBreaker; | |
| import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; | |
| 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.junit.jupiter.api.extension.ExtendWith; | |
| import org.mockito.InjectMocks; | |
| import org.mockito.Mock; | |
| import org.mockito.junit.jupiter.MockitoExtension; | |
| import java.math.BigDecimal; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| import static org.mockito.Mockito.*; | |
| class AuditServiceTest { | |
| private TokenAuditRepository auditRepository; | |
| private CircuitBreakerRegistry circuitBreakerRegistry; | |
| private CircuitBreaker circuitBreaker; | |
| private AuditService auditService; | |
| void setUp() { | |
| when(circuitBreakerRegistry.circuitBreaker("aiClassification")).thenReturn(circuitBreaker); | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Resumen completo con datos | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class SummaryCalculationTests { | |
| void shouldReturnCorrectSummary() { | |
| when(auditRepository.sumTotalTokens()).thenReturn(1500L); | |
| when(auditRepository.sumTotalCost()).thenReturn(new BigDecimal("0.00123")); | |
| when(auditRepository.countByStatus(AuditStatus.SUCCESS)).thenReturn(10L); | |
| when(auditRepository.countByStatus(AuditStatus.EMERGENCY)).thenReturn(2L); | |
| when(auditRepository.avgLatencyMs()).thenReturn(350.0); | |
| when(auditRepository.maxLatencyMs()).thenReturn(800L); | |
| when(circuitBreakerRegistry.circuitBreaker("aiClassification")).thenReturn(circuitBreaker); | |
| when(circuitBreaker.getState()).thenReturn(CircuitBreaker.State.CLOSED); | |
| AuditSummary summary = auditService.getCostSummary(); | |
| assertThat(summary.totalTokensConsumed()).isEqualTo(1500L); | |
| assertThat(summary.totalCostUsd()).isEqualByComparingTo("0.00123"); | |
| assertThat(summary.successfulCalls()).isEqualTo(10L); | |
| assertThat(summary.fallbackCalls()).isEqualTo(2L); | |
| assertThat(summary.avgLatencyMs()).isEqualTo(350L); | |
| assertThat(summary.maxLatencyMs()).isEqualTo(800L); | |
| assertThat(summary.circuitBreakerState()).isEqualTo("CLOSED"); | |
| } | |
| void shouldReturnZerosWhenRepositoryReturnsNulls() { | |
| when(auditRepository.sumTotalTokens()).thenReturn(null); | |
| when(auditRepository.sumTotalCost()).thenReturn(null); | |
| when(auditRepository.countByStatus(any(AuditStatus.class))).thenReturn(0L); | |
| when(auditRepository.avgLatencyMs()).thenReturn(null); | |
| when(auditRepository.maxLatencyMs()).thenReturn(null); | |
| AuditSummary summary = auditService.getCostSummary(); | |
| assertThat(summary.totalTokensConsumed()).isZero(); | |
| assertThat(summary.totalCostUsd()).isEqualByComparingTo(BigDecimal.ZERO); | |
| assertThat(summary.avgLatencyMs()).isZero(); | |
| assertThat(summary.maxLatencyMs()).isZero(); | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Estado del Circuit Breaker | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class CircuitBreakerStateTests { | |
| void shouldReflectOpenState() { | |
| when(circuitBreaker.getState()).thenReturn(CircuitBreaker.State.OPEN); | |
| stubRepositoryDefaults(); | |
| AuditSummary summary = auditService.getCostSummary(); | |
| assertThat(summary.circuitBreakerState()).isEqualTo("OPEN"); | |
| } | |
| void shouldReturnUnknownWhenCircuitBreakerNotFound() { | |
| when(circuitBreakerRegistry.circuitBreaker("aiClassification")) | |
| .thenThrow(new RuntimeException("Not found")); | |
| stubRepositoryDefaults(); | |
| AuditSummary summary = auditService.getCostSummary(); | |
| assertThat(summary.circuitBreakerState()).isEqualTo("UNKNOWN"); | |
| } | |
| void shouldReflectHalfOpenState() { | |
| when(circuitBreaker.getState()).thenReturn(CircuitBreaker.State.HALF_OPEN); | |
| stubRepositoryDefaults(); | |
| AuditSummary summary = auditService.getCostSummary(); | |
| assertThat(summary.circuitBreakerState()).isEqualTo("HALF_OPEN"); | |
| } | |
| } | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // Helper | |
| // βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| private void stubRepositoryDefaults() { | |
| when(auditRepository.sumTotalTokens()).thenReturn(0L); | |
| when(auditRepository.sumTotalCost()).thenReturn(BigDecimal.ZERO); | |
| when(auditRepository.countByStatus(any(AuditStatus.class))).thenReturn(0L); | |
| when(auditRepository.avgLatencyMs()).thenReturn(null); | |
| when(auditRepository.maxLatencyMs()).thenReturn(null); | |
| } | |
| } | |