Skip to content

Commit

Permalink
[ISSUE #12940] add some unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouchunhai committed Jan 7, 2025
1 parent f66be47 commit efc5ee4
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -47,6 +48,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -142,4 +144,30 @@ void testGetPublishedClientList() throws Exception {
.param("groupName", baseTestKey).param("serviceName", baseTestKey).param("ip", "127.0.0.1").param("port", "8848");
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
}

@Test
void testGetSubscribeClientList() throws Exception {
String baseTestKey = "nacos-getSubScribedClientList-test";
// ip port match
Service service = Service.newService(baseTestKey, baseTestKey, baseTestKey);
when(clientServiceIndexesManager.getAllClientsSubscribeService(service)).thenReturn(Arrays.asList("test"));
when(clientManager.getClient("test")).thenReturn(ipPortBasedClient);
Subscriber subscriber = mock(Subscriber.class);
when(subscriber.getIp()).thenReturn("127.0.0.1");
when(subscriber.getPort()).thenReturn(8848);
ipPortBasedClient.addServiceSubscriber(service, subscriber);
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey)
.param("ip", "127.0.0.1").param("port", "8848");
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
// ip port not match
mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey)
.param("ip", "127.0.0.1").param("port", "8849");
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(0));
// ip port is null
mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/subscriber/list")
.param("namespaceId", baseTestKey).param("groupName", baseTestKey).param("serviceName", baseTestKey);
mockmvc.perform(mockHttpServletRequestBuilder).andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
}
}

0 comments on commit efc5ee4

Please sign in to comment.