From 042db538730ff7c0593eb088b14e9d50fd78dafa Mon Sep 17 00:00:00 2001 From: Sunrisea <1336317033@qq.com> Date: Thu, 21 Nov 2024 22:38:54 +0800 Subject: [PATCH] test bug fix --- .../server/service/LongPollingServiceTest.java | 6 ++++-- .../nacos/config/server/utils/MD5UtilTest.java | 15 +++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/LongPollingServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/LongPollingServiceTest.java index 79409b7cfa9..925d22162a1 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/LongPollingServiceTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/LongPollingServiceTest.java @@ -84,7 +84,6 @@ void before() { connectionControlManagerMockedStatic = Mockito.mockStatic(ControlManagerCenter.class); connectionControlManagerMockedStatic.when(() -> ControlManagerCenter.getInstance()).thenReturn(controlManagerCenter); Mockito.when(controlManagerCenter.getConnectionControlManager()).thenReturn(connectionControlManager); - } @AfterEach @@ -111,6 +110,9 @@ void testAddLongPollingClientHasNotEqualsMd5() throws IOException { clientMd5Map.put(groupKeyEquals, md5Equals0); String md5NotEquals1 = MD5Utils.md5Hex("countNotEquals", "UTF-8"); clientMd5Map.put(groupKeyNotEquals, md5NotEquals1); + MockedStatic md5UtilMockedStatic = Mockito.mockStatic(MD5Util.class); + md5UtilMockedStatic.when(() -> MD5Util.compareMd5(any(), any(), any())) + .thenReturn(Arrays.asList(groupKeyNotEquals)); HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class); Mockito.when(httpServletRequest.getHeader(eq(LongPollingService.LONG_POLLING_NO_HANG_UP_HEADER))).thenReturn(null); @@ -131,7 +133,7 @@ void testAddLongPollingClientHasNotEqualsMd5() throws IOException { //expect print not equals group Mockito.verify(printWriter, times(1)).println(eq(responseString)); Mockito.verify(httpServletResponse, times(1)).setStatus(eq(HttpServletResponse.SC_OK)); - + md5UtilMockedStatic.close(); } @Test diff --git a/config/src/test/java/com/alibaba/nacos/config/server/utils/MD5UtilTest.java b/config/src/test/java/com/alibaba/nacos/config/server/utils/MD5UtilTest.java index 4fd19914bb9..1ecdec56130 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/utils/MD5UtilTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/utils/MD5UtilTest.java @@ -41,6 +41,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class MD5UtilTest { @@ -49,20 +51,26 @@ class MD5UtilTest { MockedStatic configCacheServiceMockedStatic; + MockedStatic md5ComparatorDelegateMockedStatic; + @BeforeEach void setUp() { envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class); configCacheServiceMockedStatic = Mockito.mockStatic(ConfigCacheService.class); + md5ComparatorDelegateMockedStatic = Mockito.mockStatic(Md5ComparatorDelegate.class); } @AfterEach void tearDown() { envUtilMockedStatic.close(); configCacheServiceMockedStatic.close(); + md5ComparatorDelegateMockedStatic.close(); } @Test void testCompareMd5() { + Md5ComparatorDelegate md5ComparatorDelegate = Mockito.mock(Md5ComparatorDelegate.class); + when(Md5ComparatorDelegate.getInstance()).thenReturn(md5ComparatorDelegate); when(ConfigCacheService.isUptodate(anyString(), anyString(), anyString(), anyString())).thenReturn(false); @@ -74,11 +82,10 @@ void testCompareMd5() { MockHttpServletResponse response = new MockHttpServletResponse(); envUtilMockedStatic.when(() -> EnvUtil.getProperty("nacos.config.cache.type", "nacos")).thenReturn("nacos"); + when(md5ComparatorDelegate.compareMd5(request, response, clientMd5Map)).thenReturn(new ArrayList<>()); + MD5Util.compareMd5(request, response, clientMd5Map); - List changedGroupKeys = MD5Util.compareMd5(request, response, clientMd5Map); - - assertEquals(1, changedGroupKeys.size()); - assertEquals("test", changedGroupKeys.get(0)); + verify(md5ComparatorDelegate, times(1)).compareMd5(request, response, clientMd5Map); }