File size: 2,456 Bytes
a718a03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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"));
    }
}