Files
eapim-online/src/test/java/com/eactive/eai/custom/inflow/ClientInflowTargetMetricControllerTest.java
T
curry772 31171d0187 feat: 클라이언트 유량제어/RequestProcessor custom 패키지로 분리
- DJBRequestProcessor: inbound.processor → custom.inbound.processor 이동
- ClientInflowTargetMetricService/Controller: custom.inflow 패키지 신규 생성
  클라이언트 메트릭 API(/manage/inflow/client/**)를 InflowTargetMetric*에서 분리
- InflowTargetMetricService/Controller: 클라이언트 관련 메서드/엔드포인트 제거
- InflowTargetBucketService: getClientDualManager() 분리 반영
- standard-message-config.properties: requestProcessor.class 경로 업데이트
- 단위 테스트: ClientInflowTargetMetric{Service,Controller}Test 추가,
  기존 테스트에서 클라이언트 케이스 분리

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-07 18:02:51 +09:00

145 lines
5.0 KiB
Java

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 단위 테스트.
*
* <p>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<String, Object> body(ResponseEntity<?> response) {
return (Map<String, Object>) response.getBody();
}
// =========================================================================
// 단일 조회
// =========================================================================
@Test
void getClientMetric_found_successTrueAndDataPresent() {
TargetMetricDTO dto = makeDTO("C1", "CLIENT", 300, 50, 20);
when(mockService.getClientMetric("C1")).thenReturn(dto);
Map<String, Object> 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<String, Object> body = body(controller.getClientMetric("GHOST"));
assertEquals(Boolean.FALSE, body.get("success"));
assertTrue(body.get("message").toString().contains("GHOST"));
}
// =========================================================================
// 전체 조회
// =========================================================================
@Test
void getAllClientMetrics_returnsListAndCount() {
List<TargetMetricDTO> 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<String, Object> 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<String, Object> 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<String, Object> 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<String, Object> 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();
}
}