package com.eactive.eai.custom.inflow; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.http.ResponseEntity; import org.springframework.test.util.ReflectionTestUtils; import com.eactive.eai.manage.inflow.TargetMetricDTO; /** * ClientInflowTargetMetricController 단위 테스트. * *

Spring MVC를 기동하지 않고 컨트롤러를 직접 인스턴스화하여 * 응답 구조(success, data, count, message)를 검증한다. */ class ClientInflowTargetMetricControllerTest { private ClientInflowTargetMetricController controller; private ClientInflowTargetMetricService mockService; @BeforeEach void setUp() { controller = new ClientInflowTargetMetricController(); mockService = mock(ClientInflowTargetMetricService.class); ReflectionTestUtils.setField(controller, "metricService", mockService); } private TargetMetricDTO makeDTO(String targetId, String targetType, long allowed, long rejPs, long rejTh) { TargetMetricDTO dto = new TargetMetricDTO(); dto.setTargetId(targetId); dto.setTargetType(targetType); dto.setActivate(true); dto.setAllowed(allowed); dto.setRejectedPerSecond(rejPs); dto.setRejectedThreshold(rejTh); long total = allowed + rejPs + rejTh; dto.setTotalRequests(total); dto.setRejectRatio(total > 0 ? (double)(rejPs + rejTh) / total * 100 : 0.0); return dto; } @SuppressWarnings("unchecked") private Map body(ResponseEntity response) { return (Map) response.getBody(); } // ========================================================================= // 단일 조회 // ========================================================================= @Test void getClientMetric_found_successTrueAndDataPresent() { TargetMetricDTO dto = makeDTO("C1", "CLIENT", 300, 50, 20); when(mockService.getClientMetric("C1")).thenReturn(dto); Map body = body(controller.getClientMetric("C1")); assertEquals(Boolean.TRUE, body.get("success")); assertSame(dto, body.get("data")); assertFalse(body.containsKey("message")); } @Test void getClientMetric_notFound_successFalseWithMessage() { when(mockService.getClientMetric("GHOST")).thenReturn(null); Map body = body(controller.getClientMetric("GHOST")); assertEquals(Boolean.FALSE, body.get("success")); assertTrue(body.get("message").toString().contains("GHOST")); } // ========================================================================= // 전체 조회 // ========================================================================= @Test void getAllClientMetrics_returnsListAndCount() { List list = Arrays.asList( makeDTO("C1", "CLIENT", 100, 5, 5), makeDTO("C2", "CLIENT", 200, 0, 0), makeDTO("C3", "CLIENT", 50, 10, 0) ); when(mockService.getAllClientMetrics()).thenReturn(list); Map body = body(controller.getAllClientMetrics()); assertEquals(Boolean.TRUE, body.get("success")); assertSame(list, body.get("data")); assertEquals(3, body.get("count")); } @Test void getAllClientMetrics_emptyList_countZero() { when(mockService.getAllClientMetrics()).thenReturn(Collections.emptyList()); Map body = body(controller.getAllClientMetrics()); assertEquals(Boolean.TRUE, body.get("success")); assertEquals(0, body.get("count")); } // ========================================================================= // reset // ========================================================================= @Test void resetClientMetric_success_successTrueWithTargetId() { when(mockService.resetClientMetric("C1")).thenReturn(true); Map body = body(controller.resetClientMetric("C1")); assertEquals(Boolean.TRUE, body.get("success")); assertTrue(body.get("message").toString().contains("C1")); } @Test void resetClientMetric_notFound_successFalseWithTargetId() { when(mockService.resetClientMetric("GHOST")).thenReturn(false); Map body = body(controller.resetClientMetric("GHOST")); assertEquals(Boolean.FALSE, body.get("success")); assertTrue(body.get("message").toString().contains("GHOST")); } @Test void resetAllClientMetrics_callsDelegateOnce() { doNothing().when(mockService).resetAllClientMetrics(); controller.resetAllClientMetrics(); verify(mockService, times(1)).resetAllClientMetrics(); } }