From 9589bc635aac190ddea60f065fe397231fbfcd7b Mon Sep 17 00:00:00 2001 From: curry772 Date: Wed, 29 Apr 2026 15:15:11 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=ED=81=B4=EB=9D=BC=EC=9D=B4=EC=96=B8?= =?UTF-8?q?=ED=8A=B8=20=EC=9C=A0=EB=9F=89=EC=A0=9C=EC=96=B4=20Command=20?= =?UTF-8?q?=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- .../ReloadInflowClientControlCommandTest.java | 104 ++++++++++++++++++ .../RemoveInflowClientControlCommandTest.java | 98 +++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 src/test/java/com/eactive/eai/agent/inflow/ReloadInflowClientControlCommandTest.java create mode 100644 src/test/java/com/eactive/eai/agent/inflow/RemoveInflowClientControlCommandTest.java diff --git a/src/test/java/com/eactive/eai/agent/inflow/ReloadInflowClientControlCommandTest.java b/src/test/java/com/eactive/eai/agent/inflow/ReloadInflowClientControlCommandTest.java new file mode 100644 index 0000000..c666b33 --- /dev/null +++ b/src/test/java/com/eactive/eai/agent/inflow/ReloadInflowClientControlCommandTest.java @@ -0,0 +1,104 @@ +package com.eactive.eai.agent.inflow; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.test.util.ReflectionTestUtils; + +import com.eactive.eai.agent.command.CommandException; +import com.eactive.eai.common.inflow.InflowControlManager; +import com.eactive.eai.common.inflow.dual.DualInflowControlManager; +import com.eactive.eai.common.util.ApplicationContextProvider; + +/** + * ReloadInflowClientControlCommand 단위 테스트. + * + *

ApplicationContextProvider의 static context 필드에 mock ApplicationContext를 주입하여 + * InflowControlManager.getInstance() 반환값을 제어한다. + */ +class ReloadInflowClientControlCommandTest { + + private ApplicationContext mockContext; + private DualInflowControlManager mockDualManager; + + @BeforeEach + void setUp() { + mockContext = mock(ApplicationContext.class); + mockDualManager = mock(DualInflowControlManager.class); + ReflectionTestUtils.setField(ApplicationContextProvider.class, "context", mockContext); + } + + private ReloadInflowClientControlCommand commandWith(Object args) { + ReloadInflowClientControlCommand cmd = new ReloadInflowClientControlCommand(); + cmd.setArgs(args); + return cmd; + } + + // ========================================================================= + // args 타입 검증 실패 + // ========================================================================= + + @Test + void execute_argsIsInteger_throwsCommandException() { + ReloadInflowClientControlCommand cmd = commandWith(Integer.valueOf(1)); + + assertThrows(CommandException.class, cmd::execute); + verifyNoInteractions(mockContext); + } + + // ========================================================================= + // DualInflowControlManager 비활성 + // ========================================================================= + + @Test + void execute_notDualManager_throwsCommandException() { + InflowControlManager baseMgr = mock(InflowControlManager.class); + when(mockContext.getBean(InflowControlManager.class)).thenReturn(baseMgr); + + ReloadInflowClientControlCommand cmd = commandWith("CLIENT_A"); + + assertThrows(CommandException.class, cmd::execute); + } + + // ========================================================================= + // 전체 재로드 (ALL) + // ========================================================================= + + @Test + void execute_argsIsALL_callsReloadClientNoArgs() throws Exception { + when(mockContext.getBean(InflowControlManager.class)).thenReturn(mockDualManager); + + Object result = commandWith("ALL").execute(); + + assertEquals("success", result); + verify(mockDualManager).reloadClient(); + verify(mockDualManager, never()).reloadClient(anyString()); + } + + // ========================================================================= + // 개별 재로드 (ClientId) + // ========================================================================= + + @Test + void execute_argsIsClientId_callsReloadClientWithId() throws Exception { + when(mockContext.getBean(InflowControlManager.class)).thenReturn(mockDualManager); + + Object result = commandWith("CLIENT_A").execute(); + + assertEquals("success", result); + verify(mockDualManager).reloadClient("CLIENT_A"); + verify(mockDualManager, never()).reloadClient(); + } + + @Test + void execute_differentClientId_callsReloadClientWithThatId() throws Exception { + when(mockContext.getBean(InflowControlManager.class)).thenReturn(mockDualManager); + + commandWith("CLIENT_B").execute(); + + verify(mockDualManager).reloadClient("CLIENT_B"); + } +} diff --git a/src/test/java/com/eactive/eai/agent/inflow/RemoveInflowClientControlCommandTest.java b/src/test/java/com/eactive/eai/agent/inflow/RemoveInflowClientControlCommandTest.java new file mode 100644 index 0000000..c55e07e --- /dev/null +++ b/src/test/java/com/eactive/eai/agent/inflow/RemoveInflowClientControlCommandTest.java @@ -0,0 +1,98 @@ +package com.eactive.eai.agent.inflow; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.test.util.ReflectionTestUtils; + +import com.eactive.eai.agent.command.CommandException; +import com.eactive.eai.common.inflow.InflowControlManager; +import com.eactive.eai.common.inflow.dual.DualInflowControlManager; +import com.eactive.eai.common.util.ApplicationContextProvider; + +/** + * RemoveInflowClientControlCommand 단위 테스트. + * + *

ApplicationContextProvider의 static context 필드에 mock ApplicationContext를 주입하여 + * InflowControlManager.getInstance() 반환값을 제어한다. + */ +class RemoveInflowClientControlCommandTest { + + private ApplicationContext mockContext; + private DualInflowControlManager mockDualManager; + + @BeforeEach + void setUp() { + mockContext = mock(ApplicationContext.class); + mockDualManager = mock(DualInflowControlManager.class); + ReflectionTestUtils.setField(ApplicationContextProvider.class, "context", mockContext); + } + + private RemoveInflowClientControlCommand commandWith(Object args) { + RemoveInflowClientControlCommand cmd = new RemoveInflowClientControlCommand(); + cmd.setArgs(args); + return cmd; + } + + // ========================================================================= + // args 타입 검증 실패 + // ========================================================================= + + @Test + void execute_argsIsInteger_throwsCommandException() { + RemoveInflowClientControlCommand cmd = commandWith(Integer.valueOf(1)); + + assertThrows(CommandException.class, cmd::execute); + verifyNoInteractions(mockContext); + } + + // ========================================================================= + // DualInflowControlManager 비활성 + // ========================================================================= + + @Test + void execute_notDualManager_throwsCommandException() { + InflowControlManager baseMgr = mock(InflowControlManager.class); + when(mockContext.getBean(InflowControlManager.class)).thenReturn(baseMgr); + + RemoveInflowClientControlCommand cmd = commandWith("CLIENT_A"); + + assertThrows(CommandException.class, cmd::execute); + } + + // ========================================================================= + // 정상 제거 + // ========================================================================= + + @Test + void execute_validClientId_callsRemoveClient() throws CommandException { + when(mockContext.getBean(InflowControlManager.class)).thenReturn(mockDualManager); + + Object result = commandWith("CLIENT_A").execute(); + + assertEquals("success", result); + verify(mockDualManager).removeClient("CLIENT_A"); + } + + @Test + void execute_differentClientId_callsRemoveClientWithThatId() throws Exception { + when(mockContext.getBean(InflowControlManager.class)).thenReturn(mockDualManager); + + commandWith("CLIENT_B").execute(); + + verify(mockDualManager).removeClient("CLIENT_B"); + } + + @Test + void execute_validClientId_doesNotCallReload() throws Exception { + when(mockContext.getBean(InflowControlManager.class)).thenReturn(mockDualManager); + + commandWith("CLIENT_A").execute(); + + verify(mockDualManager, never()).reloadClient(); + verify(mockDualManager, never()).reloadClient(anyString()); + } +}