package com.anthony.failsafeapi.controller; import com.anthony.failsafeapi.model.AuditSummary; import com.anthony.failsafeapi.service.AuditService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.web.servlet.MockMvc; import java.math.BigDecimal; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @WebMvcTest(AuditController.class) @DisplayName("AuditController - Endpoint de auditoría y costos") class AuditControllerTest { @Autowired private MockMvc mockMvc; @MockitoBean private AuditService auditService; @Test @DisplayName("GET /api/v1/audit/costs devuelve el resumen de auditoría con HTTP 200") void shouldReturnAuditSummary() throws Exception { AuditSummary summary = new AuditSummary( 1500L, new BigDecimal("0.00123"), 10L, 2L, 350L, 800L, "CLOSED" ); when(auditService.getCostSummary()).thenReturn(summary); mockMvc.perform(get("/api/v1/audit/costs")) .andExpect(status().isOk()) .andExpect(jsonPath("$.totalTokensConsumed").value(1500)) .andExpect(jsonPath("$.successfulCalls").value(10)) .andExpect(jsonPath("$.fallbackCalls").value(2)) .andExpect(jsonPath("$.avgLatencyMs").value(350)) .andExpect(jsonPath("$.maxLatencyMs").value(800)) .andExpect(jsonPath("$.circuitBreakerState").value("CLOSED")); } @Test @DisplayName("GET /api/v1/audit/costs devuelve ceros cuando no hay datos") void shouldReturnZeroSummaryWhenEmpty() throws Exception { AuditSummary emptySummary = new AuditSummary( 0L, BigDecimal.ZERO, 0L, 0L, 0L, 0L, "UNKNOWN" ); when(auditService.getCostSummary()).thenReturn(emptySummary); mockMvc.perform(get("/api/v1/audit/costs")) .andExpect(status().isOk()) .andExpect(jsonPath("$.totalTokensConsumed").value(0)) .andExpect(jsonPath("$.circuitBreakerState").value("UNKNOWN")); } }