From 33fb7319b4cc1c53850fdc24706b3f5d913cb02a Mon Sep 17 00:00:00 2001 From: hanbingleixue Date: Thu, 19 Sep 2024 14:53:01 +0800 Subject: [PATCH] Fix the issue of UT failure Signed-off-by: hanbingleixue --- .../FrameworkEventCollectorTest.java | 24 ++++---- .../HotPluggingServiceImplTest.java | 56 ++++++++++++++++--- .../listener/HotPluggingListenerTest.java | 50 ++++++++--------- 3 files changed, 85 insertions(+), 45 deletions(-) diff --git a/sermant-agentcore/sermant-agentcore-core/src/test/java/io/sermant/core/event/collector/FrameworkEventCollectorTest.java b/sermant-agentcore/sermant-agentcore-core/src/test/java/io/sermant/core/event/collector/FrameworkEventCollectorTest.java index 5e839b9410..7b3a8ca2a0 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/test/java/io/sermant/core/event/collector/FrameworkEventCollectorTest.java +++ b/sermant-agentcore/sermant-agentcore-core/src/test/java/io/sermant/core/event/collector/FrameworkEventCollectorTest.java @@ -41,19 +41,19 @@ * @since 2024-09-02 */ public class FrameworkEventCollectorTest { - private static final MockedStatic CONFIG_MANAGER_MOCKED_STATIC = Mockito.mockStatic(ConfigManager.class); + private final MockedStatic configManagerMockedStatic = Mockito.mockStatic(ConfigManager.class); - private static final MockedConstruction MOCKED_CONSTRUCTION = mockConstruction(JarFile.class, + private final MockedConstruction mockedConstruction = mockConstruction(JarFile.class, (mock, context) -> when(mock.getManifest()).thenReturn(new Manifest())); - private static final MockedStatic JAR_FILE_UTILS_MOCKED_STATIC = Mockito.mockStatic(JarFileUtils.class); + private final MockedStatic jarFileUtilsMockedStatic = Mockito.mockStatic(JarFileUtils.class); private static final EventConfig EVENT_CONFIG = new EventConfig(); - @BeforeClass - public static void setUp() throws Exception { - CONFIG_MANAGER_MOCKED_STATIC.when(() -> ConfigManager.getConfig(EventConfig.class)).thenReturn(EVENT_CONFIG); - JAR_FILE_UTILS_MOCKED_STATIC.when(() -> JarFileUtils.getManifestAttr(any(), anyString())).thenReturn("1.0.0"); + @Before + public void setUp() throws Exception { + configManagerMockedStatic.when(() -> ConfigManager.getConfig(EventConfig.class)).thenReturn(EVENT_CONFIG); + jarFileUtilsMockedStatic.when(() -> JarFileUtils.getManifestAttr(any(), anyString())).thenReturn("1.0.0"); } @Test @@ -79,10 +79,10 @@ public void testEventEnabled() { eventQueue.clear(); } - @AfterClass - public static void closeMock() { - CONFIG_MANAGER_MOCKED_STATIC.close(); - MOCKED_CONSTRUCTION.close(); - JAR_FILE_UTILS_MOCKED_STATIC.close(); + @After + public void closeMock() { + configManagerMockedStatic.close(); + mockedConstruction.close(); + jarFileUtilsMockedStatic.close(); } } \ No newline at end of file diff --git a/sermant-agentcore/sermant-agentcore-implement/src/test/java/io/sermant/implement/service/hotplugging/HotPluggingServiceImplTest.java b/sermant-agentcore/sermant-agentcore-implement/src/test/java/io/sermant/implement/service/hotplugging/HotPluggingServiceImplTest.java index ee7f6451d1..b524ffc6bc 100644 --- a/sermant-agentcore/sermant-agentcore-implement/src/test/java/io/sermant/implement/service/hotplugging/HotPluggingServiceImplTest.java +++ b/sermant-agentcore/sermant-agentcore-implement/src/test/java/io/sermant/implement/service/hotplugging/HotPluggingServiceImplTest.java @@ -16,15 +16,19 @@ package io.sermant.implement.service.hotplugging; +import io.sermant.core.config.ConfigManager; import io.sermant.core.operation.OperationManager; import io.sermant.core.operation.converter.api.YamlConverter; +import io.sermant.core.plugin.config.ServiceMeta; import io.sermant.core.service.ServiceManager; import io.sermant.core.service.dynamicconfig.DynamicConfigService; +import io.sermant.core.service.dynamicconfig.config.DynamicConfig; +import io.sermant.core.utils.AesUtil; import io.sermant.core.utils.CollectionUtils; import io.sermant.core.utils.ReflectUtils; -import io.sermant.implement.operation.converter.YamlConverterImpl; -import io.sermant.implement.service.dynamicconfig.nacos.NacosBaseTest; +import io.sermant.implement.operation.converter.YamlConverterImpl; +import io.sermant.implement.service.dynamicconfig.nacos.NacosDynamicConfigService; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -41,17 +45,48 @@ * @author zhp * @since 2024-09-02 */ -public class HotPluggingServiceImplTest extends NacosBaseTest { - private static final MockedStatic operationManagerMockedStatic = Mockito.mockStatic(OperationManager.class); - +public class HotPluggingServiceImplTest { private static final String LISTENERS_KEY = "listeners"; + private MockedStatic dynamicConfigMockedStatic; + + private MockedStatic operationManagerMockedStatic; + + private MockedStatic serviceManagerMockedStatic; + + private NacosDynamicConfigService nacosDynamicConfigService; + + private final DynamicConfig dynamicConfig = new DynamicConfig(); + + private final ServiceMeta serviceMeta = new ServiceMeta(); + @Before public void setUp() { - operationManagerMockedStatic.when(() -> OperationManager.getOperation(YamlConverter.class)) - .thenReturn(new YamlConverterImpl()); + dynamicConfig.setEnableAuth(true); + dynamicConfig.setServerAddress("127.0.0.1:8848"); + dynamicConfig.setTimeoutValue(30000); + Optional optional = AesUtil.generateKey(); + dynamicConfig.setPrivateKey(optional.orElse("")); + dynamicConfig.setUserName("nacos"); + dynamicConfig.setPassword(AesUtil.encrypt(optional.get(), "nacos").orElse("")); + serviceMeta.setProject("testProject2"); + serviceMeta.setApplication("testApplication"); + serviceMeta.setEnvironment("testEnvironment"); + serviceMeta.setCustomLabel("testCustomLabel"); + serviceMeta.setCustomLabelValue("testCustomLabelValue"); + dynamicConfigMockedStatic = Mockito.mockStatic(ConfigManager.class); + dynamicConfigMockedStatic.when(() -> ConfigManager.getConfig(DynamicConfig.class)) + .thenReturn(dynamicConfig); + dynamicConfigMockedStatic.when(() -> ConfigManager.getConfig(ServiceMeta.class)) + .thenReturn(serviceMeta); + nacosDynamicConfigService = new NacosDynamicConfigService(); + nacosDynamicConfigService.start(); + serviceManagerMockedStatic = Mockito.mockStatic(ServiceManager.class); serviceManagerMockedStatic.when(() -> ServiceManager.getService(DynamicConfigService.class)) .thenReturn(nacosDynamicConfigService); + operationManagerMockedStatic = Mockito.mockStatic(OperationManager.class); + operationManagerMockedStatic.when(() -> OperationManager.getOperation(YamlConverter.class)) + .thenReturn(new YamlConverterImpl()); } @Test @@ -67,7 +102,12 @@ public void testStartAndStop() { @After public void closeMock() { - operationManagerMockedStatic.close(); + if (operationManagerMockedStatic != null) { + operationManagerMockedStatic.close(); + } + if (dynamicConfigMockedStatic != null) { + dynamicConfigMockedStatic.close(); + } if (serviceManagerMockedStatic != null) { serviceManagerMockedStatic.close(); } diff --git a/sermant-agentcore/sermant-agentcore-implement/src/test/java/io/sermant/implement/service/hotplugging/listener/HotPluggingListenerTest.java b/sermant-agentcore/sermant-agentcore-implement/src/test/java/io/sermant/implement/service/hotplugging/listener/HotPluggingListenerTest.java index d4e97823e0..8b4b818389 100644 --- a/sermant-agentcore/sermant-agentcore-implement/src/test/java/io/sermant/implement/service/hotplugging/listener/HotPluggingListenerTest.java +++ b/sermant-agentcore/sermant-agentcore-implement/src/test/java/io/sermant/implement/service/hotplugging/listener/HotPluggingListenerTest.java @@ -28,8 +28,8 @@ import io.sermant.implement.operation.converter.YamlConverterImpl; import io.sermant.implement.service.hotplugging.entity.HotPluggingConfig; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.mockito.MockedConstruction; import org.mockito.MockedStatic; @@ -49,7 +49,7 @@ /** - * Unit Tests for HotPluggingListener + * Unit Tests for HotPluggingListener * * @author zhp * @since 2024-09-02 @@ -65,25 +65,25 @@ public class HotPluggingListenerTest { private static final String GROUP = "sermant-hot-plugging"; - private static final MockedStatic OPERATION_MANAGER_MOCKED_STATIC = Mockito.mockStatic(OperationManager.class); - private static final YamlConverter YAML_CONVERTER = new YamlConverterImpl(); - private static final MockedStatic COMMAND_PROCESSOR_MOCKED_STATIC = Mockito.mockStatic(CommandProcessor.class); + private final MockedStatic operationManagerMockedStatic = Mockito.mockStatic(OperationManager.class); + + private final MockedStatic commandProcessorMockedStatic = Mockito.mockStatic(CommandProcessor.class); - private static final MockedStatic JAR_FILE_UTILS_MOCKED_STATIC = Mockito.mockStatic(JarFileUtils.class); + private final MockedStatic jarFileUtilsMockedStatic = Mockito.mockStatic(JarFileUtils.class); - private static final MockedConstruction MOCKED_CONSTRUCTION = mockConstruction(JarFile.class, + private final MockedConstruction mockedConstruction = mockConstruction(JarFile.class, (mock, context) -> when(mock.getManifest()).thenReturn(new Manifest())); private final Yaml yaml = new Yaml(); - @BeforeClass - public static void setUp() throws Exception { - OPERATION_MANAGER_MOCKED_STATIC.when(() -> OperationManager.getOperation(YamlConverter.class)) + @Before + public void setUp() throws Exception { + operationManagerMockedStatic.when(() -> OperationManager.getOperation(YamlConverter.class)) .thenReturn(YAML_CONVERTER); - COMMAND_PROCESSOR_MOCKED_STATIC.verify(() -> CommandProcessor.process(any()), times(0)); - JAR_FILE_UTILS_MOCKED_STATIC.when(() -> JarFileUtils.getManifestAttr(any(), anyString())).thenReturn("1.0.0"); + commandProcessorMockedStatic.verify(() -> CommandProcessor.process(any()), times(0)); + jarFileUtilsMockedStatic.when(() -> JarFileUtils.getManifestAttr(any(), anyString())).thenReturn("1.0.0"); Map argsMap = new HashMap<>(); argsMap.put(CommonConstant.CORE_IMPLEMENT_DIR_KEY, StringUtils.EMPTY); argsMap.put(CommonConstant.CORE_CONFIG_FILE_KEY, StringUtils.EMPTY); @@ -102,7 +102,7 @@ public void testHandleInitEvent() { HotPluggingListener hotPluggingListener = new HotPluggingListener(); DynamicConfigEvent event = new DynamicConfigEvent(KEY, GROUP, CONTENT, DynamicConfigEventType.INIT); hotPluggingListener.process(event); - COMMAND_PROCESSOR_MOCKED_STATIC.verify(() -> CommandProcessor.process(any()), times(0)); + commandProcessorMockedStatic.verify(() -> CommandProcessor.process(any()), times(0)); } @Test @@ -110,7 +110,7 @@ public void testHandleDeleteEvent() { HotPluggingListener hotPluggingListener = new HotPluggingListener(); DynamicConfigEvent event = new DynamicConfigEvent(KEY, GROUP, CONTENT, DynamicConfigEventType.DELETE); hotPluggingListener.process(event); - COMMAND_PROCESSOR_MOCKED_STATIC.verify(() -> CommandProcessor.process(any()), times(0)); + commandProcessorMockedStatic.verify(() -> CommandProcessor.process(any()), times(0)); } @Test @@ -118,7 +118,7 @@ public void testProcessWithEmptyConfig() { HotPluggingListener hotPluggingListener = new HotPluggingListener(); DynamicConfigEvent event = new DynamicConfigEvent(KEY, GROUP, "", DynamicConfigEventType.CREATE); hotPluggingListener.process(event); - COMMAND_PROCESSOR_MOCKED_STATIC.verify(() -> CommandProcessor.process(any()), times(0)); + commandProcessorMockedStatic.verify(() -> CommandProcessor.process(any()), times(0)); } @Test @@ -131,7 +131,7 @@ public void testProcessWithConfigWithoutInstanceId() { DynamicConfigEvent event = new DynamicConfigEvent(KEY, GROUP, yaml.dumpAsMap(hotPluggingConfig), DynamicConfigEventType.CREATE); hotPluggingListener.process(event); - COMMAND_PROCESSOR_MOCKED_STATIC.verify(() -> CommandProcessor.process(any()), times(0)); + commandProcessorMockedStatic.verify(() -> CommandProcessor.process(any()), times(0)); } @Test @@ -144,15 +144,15 @@ public void testExecute() { DynamicConfigEvent event = new DynamicConfigEvent(KEY, GROUP, yaml.dumpAsMap(hotPluggingConfig), DynamicConfigEventType.CREATE); hotPluggingListener.process(event); - COMMAND_PROCESSOR_MOCKED_STATIC.verify(() -> CommandProcessor.process(any()), times(1)); - COMMAND_PROCESSOR_MOCKED_STATIC.clearInvocations(); + commandProcessorMockedStatic.verify(() -> CommandProcessor.process(any()), times(1)); + commandProcessorMockedStatic.clearInvocations(); } - @AfterClass - public static void closeMock() { - COMMAND_PROCESSOR_MOCKED_STATIC.close(); - OPERATION_MANAGER_MOCKED_STATIC.close(); - MOCKED_CONSTRUCTION.close(); - JAR_FILE_UTILS_MOCKED_STATIC.close(); + @After + public void closeMock() { + commandProcessorMockedStatic.close(); + operationManagerMockedStatic.close(); + mockedConstruction.close(); + jarFileUtilsMockedStatic.close(); } } \ No newline at end of file