diff --git a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/test/java/com/huawei/dubbo/registry/ExtensionLoaderInterceptorTest.java b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/test/java/com/huawei/dubbo/registry/ExtensionLoaderInterceptorTest.java index ce47d06827..6a9c825d80 100644 --- a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/test/java/com/huawei/dubbo/registry/ExtensionLoaderInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/test/java/com/huawei/dubbo/registry/ExtensionLoaderInterceptorTest.java @@ -16,15 +16,15 @@ package com.huawei.dubbo.registry; +import com.huawei.dubbo.registry.alibaba.ServiceCenterRegistryFactory; import com.huawei.dubbo.registry.constants.Constant; import com.huawei.dubbo.registry.interceptor.ExtensionLoaderInterceptor; import com.huawei.dubbo.registry.utils.ReflectUtils; -import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; - import com.alibaba.dubbo.common.extension.ExtensionLoader; import com.alibaba.dubbo.common.extension.SPI; import com.alibaba.dubbo.registry.RegistryFactory; +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import org.junit.Assert; import org.junit.Test; @@ -72,6 +72,7 @@ public void testAlibabaExtensionLoader() { ExtensionLoader alibabaLoader = ExtensionLoader.getExtensionLoader(RegistryFactory.class); ExecuteContext context = ExecuteContext.forMemberMethod(alibabaLoader, null, arguments, null, null); Map> cachedClasses = ReflectUtils.getExtensionClasses(context.getObject()); + cachedClasses.remove("sc", ServiceCenterRegistryFactory.class); // spi名为null interceptor.before(context); @@ -96,7 +97,7 @@ public void testAlibabaExtensionLoader() { @Test public void testInvalidApacheExtensionLoader() { org.apache.dubbo.common.extension.ExtensionLoader apacheLoader = - org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader(ApacheSpiTest.class); + org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader(ApacheSpiTest.class); ExecuteContext context = ExecuteContext.forMemberMethod(apacheLoader, null, arguments, null, null); testExtensionLoader(context); } @@ -109,10 +110,11 @@ public void testInvalidApacheExtensionLoader() { @Test public void testApacheExtensionLoader() { org.apache.dubbo.common.extension.ExtensionLoader apacheLoader = - org.apache.dubbo.common.extension.ExtensionLoader - .getExtensionLoader(org.apache.dubbo.registry.RegistryFactory.class); + org.apache.dubbo.common.extension.ExtensionLoader + .getExtensionLoader(org.apache.dubbo.registry.RegistryFactory.class); ExecuteContext context = ExecuteContext.forMemberMethod(apacheLoader, null, arguments, null, null); Map> cachedClasses = ReflectUtils.getExtensionClasses(context.getObject()); + cachedClasses.remove("sc", com.huawei.dubbo.registry.apache.ServiceCenterRegistryFactory.class); // spi名为null interceptor.before(context); diff --git a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/test/java/com/huawei/dubbo/registry/ReflectUtilsTest.java b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/test/java/com/huawei/dubbo/registry/ReflectUtilsTest.java index 2a4463c5da..f9ccbea71a 100644 --- a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/test/java/com/huawei/dubbo/registry/ReflectUtilsTest.java +++ b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/test/java/com/huawei/dubbo/registry/ReflectUtilsTest.java @@ -146,8 +146,9 @@ public void testAlibabaAbstractInterfaceConfig() { public void testAlibabaExtensionLoader() { ExtensionLoader loader = ExtensionLoader.getExtensionLoader(RegistryFactory.class); - // 初始化 + // 初始化, 其他UT可能已经加载,先移除ServiceCenterRegistryFactory Map> cachedClasses = ReflectUtils.getExtensionClasses(loader); + cachedClasses.remove("sc", ServiceCenterRegistryFactory.class); Assert.assertEquals(0, cachedClasses.size()); // 加载ServiceCenterRegistryFactory @@ -281,8 +282,9 @@ public void testApacheExtensionLoader() { org.apache.dubbo.common.extension.ExtensionLoader .getExtensionLoader(org.apache.dubbo.registry.RegistryFactory.class); - // 初始化 + // 初始化, 其他UT可能已经加载,先移除ServiceCenterRegistryFactory Map> cachedClasses = ReflectUtils.getExtensionClasses(loader); + cachedClasses.remove("sc", com.huawei.dubbo.registry.apache.ServiceCenterRegistryFactory.class); Assert.assertEquals(1, cachedClasses.size()); // 加载ServiceCenterRegistryFactory diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/config/RegistryConfigResolverTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/config/RegistryConfigResolverTest.java index 4fa520da5d..3864d83c1d 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/config/RegistryConfigResolverTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/config/RegistryConfigResolverTest.java @@ -17,13 +17,13 @@ package com.huawei.registry.config; -import com.huaweicloud.sermant.core.utils.ReflectUtils; - import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigEvent; +import com.huaweicloud.sermant.core.utils.ReflectUtils; import org.junit.Assert; import org.junit.Test; +import org.mockito.MockedStatic; import org.mockito.Mockito; import java.util.Optional; @@ -48,39 +48,41 @@ public class RegistryConfigResolverTest { */ @Test public void testUpdateGraceConfig() { - Mockito.mockStatic(PluginConfigManager.class).when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) - .thenReturn(new GraceConfig()); - RegistryConfigResolver configResolver = new GraceConfigResolver(); - final DynamicConfigEvent event = Mockito.mock(DynamicConfigEvent.class); - Mockito.when(event.getContent()).thenReturn("rule:\n" - + " enableSpring: true # springCloud优雅上下线开关\n" - + " startDelayTime: 20 # 优雅上下线启动延迟时间, 单位S\n" - + " enableWarmUp: true # 是否开启预热\n" - + " warmUpTime: 1200 # 预热时间, 单位S\n" - + " enableGraceShutdown: false # 是否开启优雅下线\n" - + " shutdownWaitTime: 300 # 关闭前相关流量检测的最大等待时间, 单位S. 需开启enabledGraceShutdown才会生效\n" - + " enableOfflineNotify: true # 是否开启下线主动通知\n" - + " httpServerPort: 26688 # 开启下线主动通知时的httpServer端口\n" - + " upstreamAddressMaxSize: 5000 # 缓存上游地址的默认大小\n" - + " upstreamAddressExpiredTime: 600 # 缓存上游地址的过期时间"); - Mockito.when(event.getKey()).thenReturn("sermant.agent.grace"); - configResolver.updateConfig(event); - final GraceConfig graceConfig = config(configResolver, GraceConfig.class); - Assert.assertTrue(graceConfig.isEnableSpring()); - Assert.assertEquals(graceConfig.getStartDelayTime(), TEST_START_DELAY_TIME); - Assert.assertTrue(graceConfig.isEnableWarmUp()); - Assert.assertEquals(graceConfig.getWarmUpTime(), TEST_WARM_UP_TIME); - Assert.assertFalse(graceConfig.isEnableGraceShutdown()); - Assert.assertEquals(graceConfig.getShutdownWaitTime(), TEST_SHUTDOWN_WAIT_TIME); - Assert.assertTrue(graceConfig.isEnableOfflineNotify()); - Assert.assertEquals(graceConfig.getHttpServerPort(), TEST_HTTP_SERVER_PORT); - Assert.assertEquals(graceConfig.getUpstreamAddressMaxSize(), TEST_UPSTREAM_ADDRESS_MAXSIZE); - Assert.assertEquals(graceConfig.getUpstreamAddressExpiredTime(), TEST_UPSTREAM_ADDRESS_EXPIRED_TIME); - Mockito.when(event.getContent()).thenReturn("rule:\n" - + " enableSpring: true # springCloud优雅上下线开关"); - configResolver.updateConfig(event); - final GraceConfig config = config(configResolver, GraceConfig.class); - Assert.assertEquals(config.getShutdownWaitTime(), TEST_DEFAULT_SHUTDOWN_WAIT_TIME); + try (MockedStatic pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class)) { + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) + .thenReturn(new GraceConfig()); + RegistryConfigResolver configResolver = new GraceConfigResolver(); + final DynamicConfigEvent event = Mockito.mock(DynamicConfigEvent.class); + Mockito.when(event.getContent()).thenReturn("rule:\n" + + " enableSpring: true # springCloud优雅上下线开关\n" + + " startDelayTime: 20 # 优雅上下线启动延迟时间, 单位S\n" + + " enableWarmUp: true # 是否开启预热\n" + + " warmUpTime: 1200 # 预热时间, 单位S\n" + + " enableGraceShutdown: false # 是否开启优雅下线\n" + + " shutdownWaitTime: 300 # 关闭前相关流量检测的最大等待时间, 单位S. 需开启enabledGraceShutdown才会生效\n" + + " enableOfflineNotify: true # 是否开启下线主动通知\n" + + " httpServerPort: 26688 # 开启下线主动通知时的httpServer端口\n" + + " upstreamAddressMaxSize: 5000 # 缓存上游地址的默认大小\n" + + " upstreamAddressExpiredTime: 600 # 缓存上游地址的过期时间"); + Mockito.when(event.getKey()).thenReturn("sermant.agent.grace"); + configResolver.updateConfig(event); + final GraceConfig graceConfig = config(configResolver, GraceConfig.class); + Assert.assertTrue(graceConfig.isEnableSpring()); + Assert.assertEquals(graceConfig.getStartDelayTime(), TEST_START_DELAY_TIME); + Assert.assertTrue(graceConfig.isEnableWarmUp()); + Assert.assertEquals(graceConfig.getWarmUpTime(), TEST_WARM_UP_TIME); + Assert.assertFalse(graceConfig.isEnableGraceShutdown()); + Assert.assertEquals(graceConfig.getShutdownWaitTime(), TEST_SHUTDOWN_WAIT_TIME); + Assert.assertTrue(graceConfig.isEnableOfflineNotify()); + Assert.assertEquals(graceConfig.getHttpServerPort(), TEST_HTTP_SERVER_PORT); + Assert.assertEquals(graceConfig.getUpstreamAddressMaxSize(), TEST_UPSTREAM_ADDRESS_MAXSIZE); + Assert.assertEquals(graceConfig.getUpstreamAddressExpiredTime(), TEST_UPSTREAM_ADDRESS_EXPIRED_TIME); + Mockito.when(event.getContent()).thenReturn("rule:\n" + + " enableSpring: true # springCloud优雅上下线开关"); + configResolver.updateConfig(event); + final GraceConfig config = config(configResolver, GraceConfig.class); + Assert.assertEquals(config.getShutdownWaitTime(), TEST_DEFAULT_SHUTDOWN_WAIT_TIME); + } } private T config(RegistryConfigResolver configResolver, Class clazz) { diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/RegistryDelayInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/RegistryDelayInterceptorTest.java index cdbd8b53a2..df8f565ca1 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/RegistryDelayInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/RegistryDelayInterceptorTest.java @@ -24,6 +24,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -44,19 +45,28 @@ public class RegistryDelayInterceptorTest { private final GraceConfig graceConfig = new GraceConfig(); + /** + * PluginConfigManager mock对象 + */ + public MockedStatic pluginConfigManagerMockedStatic; + /** * 初始化 */ @Before public void init() { - final MockedStatic pluginConfigManagerMockedStatic = Mockito - .mockStatic(PluginConfigManager.class); + pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) .thenReturn(graceConfig); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) .thenReturn(new RegisterConfig()); } + @After + public void tearDown() { + pluginConfigManagerMockedStatic.close(); + } + /** * 测试开关是否生效 */ diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/ResponseTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/ResponseTest.java index 8b1a9e91c0..5555b2b343 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/ResponseTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/ResponseTest.java @@ -22,6 +22,7 @@ import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; +import org.junit.After; import org.junit.Before; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -38,18 +39,28 @@ public class ResponseTest { */ public static final String SHUTDOWN_ENDPOINT = "localhost:8911"; + /** + * PluginConfigManager mock对象 + */ + public MockedStatic pluginConfigManagerMockedStatic; + /** * 初始化 */ - protected void init() { + @Before + public void init() { final GraceConfig graceConfig = new GraceConfig(); graceConfig.setEnableSpring(true); graceConfig.setEnableGraceShutdown(true); - final MockedStatic pluginConfigManagerMockedStatic = Mockito - .mockStatic(PluginConfigManager.class); + pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) .thenReturn(graceConfig); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) .thenReturn(new RegisterConfig()); } + + @After + public void tearDown() { + pluginConfigManagerMockedStatic.close(); + } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringCacheManagerInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringCacheManagerInterceptorTest.java index 45597ea0d4..332d965076 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringCacheManagerInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringCacheManagerInterceptorTest.java @@ -26,6 +26,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.MockedStatic; @@ -41,6 +42,11 @@ * @since 2022-06-30 */ public class SpringCacheManagerInterceptorTest { + /** + * PluginConfigManager mock对象 + */ + public MockedStatic pluginConfigManagerMockedStatic; + /** * 初始化 */ @@ -48,14 +54,18 @@ public class SpringCacheManagerInterceptorTest { public void init() { final GraceConfig graceConfig = new GraceConfig(); graceConfig.setEnableSpring(true); - final MockedStatic pluginConfigManagerMockedStatic = Mockito - .mockStatic(PluginConfigManager.class); + pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) .thenReturn(graceConfig); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) .thenReturn(new RegisterConfig()); } + @After + public void tearDown() { + pluginConfigManagerMockedStatic.close(); + } + /** * 测试获取缓存管理器 */ diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerFeignResponseInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerFeignResponseInterceptorTest.java index eb706d4054..f178675e8c 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerFeignResponseInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerFeignResponseInterceptorTest.java @@ -25,6 +25,7 @@ import feign.Request; import feign.Request.HttpMethod; import feign.Response; + import org.apache.http.HttpStatus; import org.junit.Assert; import org.junit.Test; @@ -41,11 +42,11 @@ public class SpringLoadbalancerFeignResponseInterceptorTest extends ResponseTest { /** * 测试请求流程 + * * @throws NoSuchMethodException 不会抛出 */ @Test public void testRequest() throws NoSuchMethodException { - init(); final Request request = Request .create(HttpMethod.GET, "http://provider:8888", Collections.emptyMap(), new byte[0], StandardCharsets.UTF_8); @@ -54,14 +55,14 @@ public void testRequest() throws NoSuchMethodException { Collections.singletonList(SHUTDOWN_ENDPOINT))) .status(HttpStatus.SC_OK) .request(request).build(); - Object[] arguments = new Object[] {request}; + Object[] arguments = new Object[]{request}; final SpringLoadbalancerFeignResponseInterceptor interceptor = new SpringLoadbalancerFeignResponseInterceptor(); final ExecuteContext executeContext = ExecuteContext.forMemberMethod(this, this.getClass().getDeclaredMethod("testRequest"), arguments, null, null); interceptor.before(executeContext); executeContext.changeResult(response); interceptor.after(executeContext); - Assert.assertTrue(((Request)arguments[0]).headers().size() > 0); + Assert.assertTrue(((Request) arguments[0]).headers().size() > 0); Assert.assertTrue(GraceContext.INSTANCE.getGraceShutDownManager().isMarkedOffline(SHUTDOWN_ENDPOINT)); } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerRestTemplateResponseInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerRestTemplateResponseInterceptorTest.java index 4ab3114ea5..1a91b073a6 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerRestTemplateResponseInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerRestTemplateResponseInterceptorTest.java @@ -46,7 +46,6 @@ public class SpringLoadbalancerRestTemplateResponseInterceptorTest extends Respo */ @Test public void testRestTemplate() throws NoSuchMethodException { - init(); final HttpRequest request = buildRequest(); final Object[] arguments = {request}; final ExecuteContext executeContext = ExecuteContext @@ -68,6 +67,7 @@ public void testRestTemplate() throws NoSuchMethodException { private HttpRequest buildRequest() { return new HttpRequest() { private final HttpHeaders headers = new HttpHeaders(); + @Override public String getMethodValue() { return "test"; diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerWarmUpInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerWarmUpInterceptorTest.java index 5227185213..dd4f81c262 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerWarmUpInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringLoadbalancerWarmUpInterceptorTest.java @@ -21,9 +21,8 @@ import com.huawei.registry.config.grace.GraceHelper; import com.huawei.registry.entity.MicroServiceInstance; -import com.huaweicloud.sermant.core.utils.ReflectUtils; - import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.utils.ReflectUtils; import org.junit.Assert; import org.junit.Test; @@ -49,12 +48,11 @@ public class SpringLoadbalancerWarmUpInterceptorTest extends WarmUpTest { */ @Test public void testWarmUp() throws NoSuchMethodException { - before(); final SpringLoadbalancerWarmUpInterceptor springRibbonWarmUpInterceptor = new SpringLoadbalancerWarmUpInterceptor(); final List serviceInstances = buildServiceInstances(); final ExecuteContext executeContext = ExecuteContext.forMemberMethod(springRibbonWarmUpInterceptor, SpringRibbonWarmUpInterceptor.class.getDeclaredMethod("doAfter", ExecuteContext.class), - new Object[] {serviceInstances} + new Object[]{serviceInstances} , null, null); executeContext.changeResult(new DefaultResponse(serviceInstances.get(0))); @@ -64,14 +62,14 @@ public void testWarmUp() throws NoSuchMethodException { /** * 执行操作 * - * @param interceptor 拦截器 + * @param interceptor 拦截器 * @param executeContext 上下文 - * @param argument 参数 + * @param argument 参数 */ private void process(GraceSwitchInterceptor interceptor, ExecuteContext executeContext, Object argument) { final HashMap statResult = new HashMap<>(); for (int i = 0; i < REQUEST_COUNT; i++) { - ReflectUtils.setFieldValue(executeContext, "arguments", new Object[] {argument}); + ReflectUtils.setFieldValue(executeContext, "arguments", new Object[]{argument}); interceptor.before(executeContext); interceptor.after(executeContext); final Object result = executeContext.getResult(); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringRibbonWarmUpInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringRibbonWarmUpInterceptorTest.java index a37a2ce215..be788f87e7 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringRibbonWarmUpInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringRibbonWarmUpInterceptorTest.java @@ -42,7 +42,6 @@ public class SpringRibbonWarmUpInterceptorTest extends WarmUpTest { */ @Test public void testWarmUp() throws NoSuchMethodException { - before(); final SpringRibbonWarmUpInterceptor springRibbonWarmUpInterceptor = new SpringRibbonWarmUpInterceptor(); final ExecuteContext executeContext = ExecuteContext.forMemberMethod(springRibbonWarmUpInterceptor, SpringRibbonWarmUpInterceptor.class.getDeclaredMethod("doAfter", ExecuteContext.class), null, null, null); @@ -53,9 +52,9 @@ public void testWarmUp() throws NoSuchMethodException { /** * 执行操作 * - * @param interceptor 拦截器 + * @param interceptor 拦截器 * @param executeContext 上下文 - * @param result 结果 + * @param result 结果 */ private void process(GraceSwitchInterceptor interceptor, ExecuteContext executeContext, Object result) { final HashMap statResult = new HashMap<>(); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringWebHandlerInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringWebHandlerInterceptorTest.java index 6839fa2e2b..5be2616562 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringWebHandlerInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringWebHandlerInterceptorTest.java @@ -17,8 +17,6 @@ package com.huawei.registry.grace.interceptors; -import static org.junit.Assert.*; - import com.huawei.registry.config.GraceConfig; import com.huawei.registry.config.RegisterConfig; import com.huawei.registry.services.GraceService; @@ -27,6 +25,7 @@ import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -45,13 +44,18 @@ public class SpringWebHandlerInterceptorTest { private final GraceConfig graceConfig = new GraceConfig(); + /** + * PluginConfigManager mock对象 + */ + public MockedStatic pluginConfigManagerMockedStatic; + /** * 初始化 */ @Before public void init() { graceConfig.setEnableSpring(true); - final MockedStatic pluginConfigManagerMockedStatic = Mockito + pluginConfigManagerMockedStatic = Mockito .mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) .thenReturn(graceConfig); @@ -72,6 +76,11 @@ public void addAddress(String address) { }); } + @After + public void tearDown() { + pluginConfigManagerMockedStatic.close(); + } + /** * 测试添加拦截器 */ diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringZuulResponseInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringZuulResponseInterceptorTest.java index 84006746eb..9eb2022299 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringZuulResponseInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/SpringZuulResponseInterceptorTest.java @@ -44,7 +44,6 @@ public class SpringZuulResponseInterceptorTest extends ResponseTest { */ @Test public void testZuul() throws NoSuchMethodException { - init(); final RequestContext requestContext = new RequestContext(); RequestContext.testSetCurrentContext(requestContext); final HttpServletResponse response = Mockito.mock(HttpServletResponse.class); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/WarmUpTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/WarmUpTest.java index be37cdb051..8834197ef8 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/WarmUpTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/grace/interceptors/WarmUpTest.java @@ -24,7 +24,9 @@ import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.netflix.loadbalancer.Server; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.springframework.cloud.client.ServiceInstance; @@ -80,21 +82,31 @@ public class WarmUpTest { */ protected final GraceConfig graceConfig = new GraceConfig(); + /** + * PluginConfigManager mock对象 + */ + public MockedStatic pluginConfigManagerMockedStatic; + /** * 前置 */ - protected void before() { + @Before + public void before() { graceConfig.setEnableWarmUp(true); graceConfig.setEnableSpring(true); graceConfig.setWarmUpTime(DEFAULT_WARM_TIME); - final MockedStatic pluginConfigManagerMockedStatic = Mockito - .mockStatic(PluginConfigManager.class); + pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) .thenReturn(graceConfig); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) .thenReturn(new RegisterConfig()); } + @After + public void tearDown() { + pluginConfigManagerMockedStatic.close(); + } + /** * 统计数据 * diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/utils/ZoneUtilsTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/utils/ZoneUtilsTest.java index dcf49e96d1..e9e78c60b4 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/utils/ZoneUtilsTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/utils/ZoneUtilsTest.java @@ -24,6 +24,7 @@ import org.junit.Assert; import org.junit.Test; +import org.mockito.MockedStatic; import org.mockito.Mockito; import java.util.Collections; @@ -41,17 +42,20 @@ public class ZoneUtilsTest { */ @Test public void testSetZone() { - final HashMap meta = new HashMap<>( - Collections.singletonMap(SpringRegistryConstants.LOAD_BALANCER_ZONE_META_KEY, "test")); - ZoneUtils.setZone(meta); - Assert.assertEquals(meta.get(SpringRegistryConstants.LOAD_BALANCER_ZONE_META_KEY), "test"); - final RegisterConfig registerConfig = new RegisterConfig(); - String zone = "registerZone"; - registerConfig.setZone(zone); - Mockito.mockStatic(PluginConfigManager.class).when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) - .thenReturn(registerConfig); - final HashMap map = new HashMap<>(); - ZoneUtils.setZone(map); - Assert.assertEquals(map.get(SpringRegistryConstants.LOAD_BALANCER_ZONE_META_KEY), zone); + try (MockedStatic pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class)) { + final HashMap meta = new HashMap<>( + Collections.singletonMap(SpringRegistryConstants.LOAD_BALANCER_ZONE_META_KEY, "test")); + ZoneUtils.setZone(meta); + Assert.assertEquals(meta.get(SpringRegistryConstants.LOAD_BALANCER_ZONE_META_KEY), "test"); + final RegisterConfig registerConfig = new RegisterConfig(); + String zone = "registerZone"; + registerConfig.setZone(zone); + + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) + .thenReturn(registerConfig); + final HashMap map = new HashMap<>(); + ZoneUtils.setZone(map); + Assert.assertEquals(map.get(SpringRegistryConstants.LOAD_BALANCER_ZONE_META_KEY), zone); + } } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/cache/AddressCache.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/cache/AddressCache.java index 9cfaca4e26..68138e552a 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/cache/AddressCache.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/main/java/com/huawei/registry/service/cache/AddressCache.java @@ -18,10 +18,9 @@ import com.huawei.registry.config.GraceConfig; -import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; - import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -42,9 +41,9 @@ public enum AddressCache { AddressCache() { GraceConfig pluginConfig = PluginConfigManager.getPluginConfig(GraceConfig.class); cache = CacheBuilder.newBuilder() - .maximumSize(pluginConfig.getUpstreamAddressMaxSize()) // 设置缓存的最大容量 - .expireAfterWrite(pluginConfig.getUpstreamAddressExpiredTime(), TimeUnit.SECONDS) // 设置缓存失效时间 - .build(); + .maximumSize(pluginConfig.getUpstreamAddressMaxSize()) // 设置缓存的最大容量 + .expireAfterWrite(pluginConfig.getUpstreamAddressExpiredTime(), TimeUnit.SECONDS) // 设置缓存失效时间 + .build(); } /** @@ -59,4 +58,11 @@ public void addAddress(String address) { public Set getAddressSet() { return cache.asMap().keySet(); } + + /** + * 清除所有缓存 + */ + public void cleanCache() { + cache.invalidateAll(); + } } \ No newline at end of file diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/cache/AddressCacheTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/cache/AddressCacheTest.java index 6aca51a8cb..df592b17fd 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/cache/AddressCacheTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/cache/AddressCacheTest.java @@ -23,7 +23,10 @@ import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import org.mockito.MockedStatic; import org.mockito.Mockito; /** @@ -35,14 +38,29 @@ public class AddressCacheTest { private static final int SIZE = 3; + /** + * PluginConfigManager mock对象 + */ + public MockedStatic pluginConfigManagerMockedStatic; + + @Before + public void setUp() { + pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) + .thenReturn(new GraceConfig()); + } + + @After + public void tearDown() { + pluginConfigManagerMockedStatic.close(); + AddressCache.INSTANCE.cleanCache(); + } + /** * 测试地址缓存 */ @Test public void testAddress() { - Mockito.mockStatic(PluginConfigManager.class) - .when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) - .thenReturn(new GraceConfig()); AddressCache.INSTANCE.addAddress("localhost:1200"); AddressCache.INSTANCE.addAddress("localhost:1300"); AddressCache.INSTANCE.addAddress("localhost:1400"); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/BaseTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/BaseTest.java index 1ee35a6bc1..e9879c4e47 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/BaseTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/BaseTest.java @@ -22,10 +22,6 @@ import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.config.ConfigManager; import com.huaweicloud.sermant.core.config.common.BaseConfig; -import com.huaweicloud.sermant.core.service.ServiceManager; -import com.huaweicloud.sermant.core.service.dynamicconfig.config.DynamicConfig; -import com.huaweicloud.sermant.core.service.heartbeat.config.HeartbeatConfig; -import com.huaweicloud.sermant.core.service.send.config.BackendConfig; import org.junit.Assert; import org.junit.BeforeClass; @@ -50,29 +46,18 @@ public class BaseTest { @BeforeClass public static void init() throws IllegalAccessException, NoSuchFieldException, ClassNotFoundException { currentClassLoader = Thread.currentThread().getContextClassLoader(); - final Class configManagerClass = currentClassLoader.loadClass(BackendConfig.class.getName()); - final Field nettyServerPort = configManagerClass.getDeclaredField("port"); - setFieldValue(nettyServerPort, "6888", null); - - final Field nettyServerIp = configManagerClass.getDeclaredField("ip"); - setFieldValue(nettyServerIp, "127.0.0.1", null); - final Class aClass = currentClassLoader.loadClass(ConfigManager.class.getName()); final Field configMap = aClass.getDeclaredField("CONFIG_MAP"); configMap.setAccessible(true); removeFinalModify(configMap); configManagerMap = (Map) configMap.get(null); - configManagerMap.put("heartbeat", new HeartbeatConfig()); - configManagerMap.put("dynamic.config", new DynamicConfig()); configManagerMap.put("servicecomb.service", new RegisterConfig()); - configMap.set(null, configManagerMap); - ServiceManager.initServices(); final URL logbackSettingURL = BaseTest.class.getResource("/logback-test.xml"); Assert.assertNotNull(logbackSettingURL); - LoggerFactory.init( - Collections.singletonMap(CommonConstant.LOG_SETTING_FILE_KEY, logbackSettingURL.getPath())); + LoggerFactory.init(Collections.singletonMap(CommonConstant.LOG_SETTING_FILE_KEY, + logbackSettingURL.getPath())); } /** @@ -87,17 +72,4 @@ protected static void removeFinalModify(Field field) throws NoSuchFieldException modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); } - - /** - * 反射设置字段值 - * - * @param field 字段 - * @param value 值 - * @param target 目前类 静态属性为null - * @throws IllegalAccessException 找不到抛出异常 - */ - protected static void setFieldValue(Field field, Object value, Object target) throws IllegalAccessException { - field.setAccessible(true); - field.set(target, value); - } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/ScClientTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/ScClientTest.java index 5c5f4342da..a79b255845 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/ScClientTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/client/ScClientTest.java @@ -23,6 +23,7 @@ import org.junit.Before; import org.junit.Test; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; @@ -58,7 +59,7 @@ public void before() { * @throws IllegalAccessException 无法拿到目标对象抛出 */ @Test - public void buildMicro() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + public void buildMicro() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException { scClient.init(); final ServiceCenterOperation rawClient = scClient.getRawClient(); Assert.assertNotNull(rawClient); @@ -67,10 +68,11 @@ public void buildMicro() throws NoSuchMethodException, InvocationTargetException final Object serviceResult = buildMicroService.invoke(scClient); Assert.assertNotNull(serviceResult); final Method buildMicroServiceInstance = scClient.getClass() - .getDeclaredMethod("buildMicroServiceInstance", String.class); + .getDeclaredMethod("buildMicroServiceInstance"); buildMicroServiceInstance.setAccessible(true); - final Object instanceResult = buildMicroServiceInstance - .invoke(scClient, RegisterContext.INSTANCE.getClientInfo().getServiceId()); - Assert.assertNotNull(instanceResult); + buildMicroServiceInstance.invoke(scClient); + final Field microserviceInstanceField = scClient.getClass().getDeclaredField("microserviceInstance"); + microserviceInstanceField.setAccessible(true); + Assert.assertNotNull(microserviceInstanceField.get(scClient)); } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/impl/GraceServiceImplTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/impl/GraceServiceImplTest.java index 40ca33fdae..3900a5d7b8 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/impl/GraceServiceImplTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/impl/GraceServiceImplTest.java @@ -20,11 +20,13 @@ import com.huawei.registry.config.GraceConfig; import com.huawei.registry.config.RegisterConfig; import com.huawei.registry.service.cache.AddressCache; -import com.huaweicloud.sermant.core.utils.ReflectUtils; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; +import com.huaweicloud.sermant.core.utils.ReflectUtils; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -40,16 +42,30 @@ */ public class GraceServiceImplTest { /** - * 测试关闭逻辑以及加载handler逻辑 + * PluginConfigManager mock对象 */ - @Test - public void testShutDown() { - final MockedStatic pluginConfigManagerMockedStatic = Mockito - .mockStatic(PluginConfigManager.class); + public MockedStatic pluginConfigManagerMockedStatic; + + @Before + public void setUp() { + pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) .thenReturn(new GraceConfig()); pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) .thenReturn(new RegisterConfig()); + } + + @After + public void tearDown() { + pluginConfigManagerMockedStatic.close(); + AddressCache.INSTANCE.cleanCache(); + } + + /** + * 测试关闭逻辑以及加载handler逻辑 + */ + @Test + public void testShutDown() { final GraceServiceImpl spy = Mockito.spy(new GraceServiceImpl()); spy.shutdown(); spy.addAddress("test"); @@ -59,6 +75,6 @@ public void testShutDown() { Assert.assertFalse(AddressCache.INSTANCE.getAddressSet().isEmpty()); final Optional shutdown = ReflectUtils.getFieldValue(spy, "SHUTDOWN"); Assert.assertTrue(shutdown.isPresent() && shutdown.get() instanceof AtomicBoolean); - Assert.assertTrue(((AtomicBoolean)shutdown.get()).get()); + Assert.assertTrue(((AtomicBoolean) shutdown.get()).get()); } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/server/GraceHttpServerTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/server/GraceHttpServerTest.java index eefbe10507..1e3c0692c8 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/server/GraceHttpServerTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-service/src/test/java/com/huawei/registry/service/server/GraceHttpServerTest.java @@ -25,13 +25,15 @@ import com.huawei.registry.service.utils.HttpClientResult; import com.huawei.registry.service.utils.HttpClientUtils; import com.huawei.registry.services.GraceService; -import com.huaweicloud.sermant.core.utils.ReflectUtils; import com.alibaba.fastjson.JSONObject; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; +import com.huaweicloud.sermant.core.utils.ReflectUtils; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -47,6 +49,28 @@ * @since 2022-06-29 */ public class GraceHttpServerTest { + /** + * PluginConfigManager mock对象 + */ + public MockedStatic pluginConfigManagerMockedStatic; + + /** + * PluginConfigManager mock对象 + */ + public MockedStatic pluginServiceManagerMockedStatic; + + @Before + public void setUp() { + pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class); + pluginServiceManagerMockedStatic = Mockito.mockStatic(PluginServiceManager.class); + } + + @After + public void tearDown() { + pluginConfigManagerMockedStatic.close(); + pluginServiceManagerMockedStatic.close(); + } + /** * 测试HttpServer有效性 */ @@ -55,20 +79,15 @@ public void testHttpServer() { final GraceConfig graceConfig = new GraceConfig(); graceConfig.setEnableSpring(true); graceConfig.setEnableGraceShutdown(true); - final MockedStatic pluginConfigManagerMockedStatic = Mockito - .mockStatic(PluginConfigManager.class); - pluginConfigManagerMockedStatic - .when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(RegisterConfig.class)) .thenReturn(new RegisterConfig()); - pluginConfigManagerMockedStatic - .when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) + pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(GraceConfig.class)) .thenReturn(graceConfig); - Mockito.mockStatic(PluginServiceManager.class) - .when(() -> PluginServiceManager.getPluginService(GraceService.class)) + pluginServiceManagerMockedStatic.when(() -> PluginServiceManager.getPluginService(GraceService.class)) .thenReturn(new GraceServiceImpl()); final GraceHttpServer graceHttpServer = new GraceHttpServer(); graceHttpServer.start(); - try{ + try { checkHandlers(graceHttpServer); final HashMap notifyHeaders = new HashMap<>(); notifyHeaders.put(GraceConstants.MARK_SHUTDOWN_SERVICE_ENDPOINT, "localhost:28181"); @@ -76,7 +95,7 @@ public void testHttpServer() { final GraceContext instance = GraceContext.INSTANCE; final HttpClientResult notifyResult = HttpClientUtils.INSTANCE .doPost("http://127.0.0.1:16688" + GraceConstants.GRACE_NOTIFY_URL_PATH, - JSONObject.toJSONString(new Object()),notifyHeaders); + JSONObject.toJSONString(new Object()), notifyHeaders); Assert.assertTrue(instance.getGraceShutDownManager().isMarkedOffline("localhost:28181")); Assert.assertEquals(notifyResult.getCode(), GraceConstants.GRACE_HTTP_SUCCESS_CODE); } finally {