Skip to content

Commit

Permalink
Merge pull request #1626 from hanbingleixue/modify-ut
Browse files Browse the repository at this point in the history
Fix the issue of UT failure
  • Loading branch information
Sherlockhan authored Sep 19, 2024
2 parents acda541 + 33fb731 commit ebc79d4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@
* @since 2024-09-02
*/
public class FrameworkEventCollectorTest {
private static final MockedStatic<ConfigManager> CONFIG_MANAGER_MOCKED_STATIC = Mockito.mockStatic(ConfigManager.class);
private final MockedStatic<ConfigManager> configManagerMockedStatic = Mockito.mockStatic(ConfigManager.class);

private static final MockedConstruction<JarFile> MOCKED_CONSTRUCTION = mockConstruction(JarFile.class,
private final MockedConstruction<JarFile> mockedConstruction = mockConstruction(JarFile.class,
(mock, context) -> when(mock.getManifest()).thenReturn(new Manifest()));

private static final MockedStatic<JarFileUtils> JAR_FILE_UTILS_MOCKED_STATIC = Mockito.mockStatic(JarFileUtils.class);
private final MockedStatic<JarFileUtils> 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
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,17 +45,48 @@
* @author zhp
* @since 2024-09-02
*/
public class HotPluggingServiceImplTest extends NacosBaseTest {
private static final MockedStatic<OperationManager> operationManagerMockedStatic = Mockito.mockStatic(OperationManager.class);

public class HotPluggingServiceImplTest {
private static final String LISTENERS_KEY = "listeners";

private MockedStatic<ConfigManager> dynamicConfigMockedStatic;

private MockedStatic<OperationManager> operationManagerMockedStatic;

private MockedStatic<ServiceManager> 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<String> 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
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,7 +49,7 @@


/**
* Unit Tests for HotPluggingListener
* Unit Tests for HotPluggingListener
*
* @author zhp
* @since 2024-09-02
Expand All @@ -65,25 +65,25 @@ public class HotPluggingListenerTest {

private static final String GROUP = "sermant-hot-plugging";

private static final MockedStatic<OperationManager> OPERATION_MANAGER_MOCKED_STATIC = Mockito.mockStatic(OperationManager.class);

private static final YamlConverter YAML_CONVERTER = new YamlConverterImpl();

private static final MockedStatic<CommandProcessor> COMMAND_PROCESSOR_MOCKED_STATIC = Mockito.mockStatic(CommandProcessor.class);
private final MockedStatic<OperationManager> operationManagerMockedStatic = Mockito.mockStatic(OperationManager.class);

private final MockedStatic<CommandProcessor> commandProcessorMockedStatic = Mockito.mockStatic(CommandProcessor.class);

private static final MockedStatic<JarFileUtils> JAR_FILE_UTILS_MOCKED_STATIC = Mockito.mockStatic(JarFileUtils.class);
private final MockedStatic<JarFileUtils> jarFileUtilsMockedStatic = Mockito.mockStatic(JarFileUtils.class);

private static final MockedConstruction<JarFile> MOCKED_CONSTRUCTION = mockConstruction(JarFile.class,
private final MockedConstruction<JarFile> 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<String, Object> argsMap = new HashMap<>();
argsMap.put(CommonConstant.CORE_IMPLEMENT_DIR_KEY, StringUtils.EMPTY);
argsMap.put(CommonConstant.CORE_CONFIG_FILE_KEY, StringUtils.EMPTY);
Expand All @@ -102,23 +102,23 @@ 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
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
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
Expand All @@ -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
Expand All @@ -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();
}
}

0 comments on commit ebc79d4

Please sign in to comment.