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()); + } +}