Skip to content

Commit

Permalink
test bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunrisea committed Nov 21, 2024
1 parent b8dea6a commit 042db53
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ void before() {
connectionControlManagerMockedStatic = Mockito.mockStatic(ControlManagerCenter.class);
connectionControlManagerMockedStatic.when(() -> ControlManagerCenter.getInstance()).thenReturn(controlManagerCenter);
Mockito.when(controlManagerCenter.getConnectionControlManager()).thenReturn(connectionControlManager);

}

@AfterEach
Expand All @@ -111,6 +110,9 @@ void testAddLongPollingClientHasNotEqualsMd5() throws IOException {
clientMd5Map.put(groupKeyEquals, md5Equals0);
String md5NotEquals1 = MD5Utils.md5Hex("countNotEquals", "UTF-8");
clientMd5Map.put(groupKeyNotEquals, md5NotEquals1);
MockedStatic<MD5Util> 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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -49,20 +51,26 @@ class MD5UtilTest {

MockedStatic<ConfigCacheService> configCacheServiceMockedStatic;

MockedStatic<Md5ComparatorDelegate> 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);

Expand All @@ -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<String> changedGroupKeys = MD5Util.compareMd5(request, response, clientMd5Map);

assertEquals(1, changedGroupKeys.size());
assertEquals("test", changedGroupKeys.get(0));
verify(md5ComparatorDelegate, times(1)).compareMd5(request, response, clientMd5Map);

}

Expand Down

0 comments on commit 042db53

Please sign in to comment.