Fix UT.
This commit is contained in:
parent
48695dab16
commit
e515915895
@ -18,61 +18,65 @@ package com.alibaba.nacos.naming.controllers;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.naming.core.CatalogServiceV2Impl;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CatalogControllerTest {
|
||||
|
||||
private CatalogController catalogController;
|
||||
|
||||
@Before
|
||||
public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosException {
|
||||
catalogController = new CatalogController();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceDetail() throws Exception {
|
||||
Object result = catalogController.serviceDetail(Constants.DEFAULT_NAMESPACE_ID,
|
||||
TEST_GROUP_NAME + Constants.SERVICE_INFO_SPLITER + TEST_SERVICE_NAME);
|
||||
String actual = result.toString();
|
||||
assertTrue(actual.contains("\"service\":{"));
|
||||
assertTrue(actual.contains("\"groupName\":\"test-group-name\""));
|
||||
assertTrue(actual.contains("\"metadata\":{}"));
|
||||
assertTrue(actual.contains("\"name\":\"test-service\""));
|
||||
assertTrue(actual.contains("\"selector\":{\"type\":\"none\",\"contextType\":\"NONE\"}"));
|
||||
assertTrue(actual.contains("\"protectThreshold\":12.34"));
|
||||
assertTrue(actual.contains("\"clusters\":[{"));
|
||||
assertTrue(actual.contains("\"defaultCheckPort\":80"));
|
||||
assertTrue(actual.contains("\"defaultPort\":1"));
|
||||
assertTrue(actual.contains("\"healthChecker\":{\"type\":\"TCP\"}"));
|
||||
assertTrue(actual.contains("\"metadata\":{}"));
|
||||
assertTrue(actual.contains("\"name\":\"test-cluster\""));
|
||||
assertTrue(actual.contains("\"serviceName\":\"test-service\""));
|
||||
assertTrue(actual.contains("\"useIPPort4Check\":true"));
|
||||
}
|
||||
|
||||
@Test(expected = NacosException.class)
|
||||
public void testServiceDetailNotFound() throws Exception {
|
||||
catalogController.serviceDetail(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME);
|
||||
}
|
||||
|
||||
private static final String TEST_CLUSTER_NAME = "test-cluster";
|
||||
|
||||
private static final String TEST_SERVICE_NAME = "test-service";
|
||||
|
||||
private static final String TEST_GROUP_NAME = "test-group-name";
|
||||
|
||||
@Mock
|
||||
private CatalogServiceV2Impl catalogServiceV2;
|
||||
|
||||
@InjectMocks
|
||||
private CatalogController catalogController;
|
||||
|
||||
@Before
|
||||
public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosException {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceDetail() throws Exception {
|
||||
Object expected = new Object();
|
||||
when(catalogServiceV2.getServiceDetail(Constants.DEFAULT_NAMESPACE_ID, TEST_GROUP_NAME, TEST_SERVICE_NAME))
|
||||
.thenReturn(expected);
|
||||
Object actual = catalogController.serviceDetail(Constants.DEFAULT_NAMESPACE_ID,
|
||||
TEST_GROUP_NAME + Constants.SERVICE_INFO_SPLITER + TEST_SERVICE_NAME);
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstanceList() throws NacosException {
|
||||
Instance instance = new Instance();
|
||||
instance.setIp("1.1.1.1");
|
||||
instance.setPort(1234);
|
||||
instance.setClusterName(TEST_CLUSTER_NAME);
|
||||
List list = new ArrayList<>(1);
|
||||
list.add(instance);
|
||||
when(catalogServiceV2
|
||||
.listInstances(Constants.DEFAULT_NAMESPACE_ID, TEST_GROUP_NAME, TEST_SERVICE_NAME, TEST_CLUSTER_NAME))
|
||||
.thenReturn(list);
|
||||
ObjectNode result = catalogController.instanceList(Constants.DEFAULT_NAMESPACE_ID,
|
||||
TEST_GROUP_NAME + Constants.SERVICE_INFO_SPLITER + TEST_SERVICE_NAME, TEST_CLUSTER_NAME, 1, 10);
|
||||
String actual = result.toString();
|
||||
@ -86,8 +90,12 @@ public class CatalogControllerTest {
|
||||
@Test
|
||||
public void testListDetail() {
|
||||
try {
|
||||
Object res = catalogController.listDetail(true, Constants.DEFAULT_NAMESPACE_ID, 1, 10,
|
||||
TEST_GROUP_NAME + Constants.SERVICE_INFO_SPLITER + TEST_SERVICE_NAME, TEST_GROUP_NAME, null, true);
|
||||
when(catalogServiceV2
|
||||
.pageListServiceDetail(Constants.DEFAULT_NAMESPACE_ID, TEST_GROUP_NAME, TEST_SERVICE_NAME, 1, 10))
|
||||
.thenReturn(Collections.emptyList());
|
||||
Object res = catalogController
|
||||
.listDetail(true, Constants.DEFAULT_NAMESPACE_ID, 1, 10, TEST_SERVICE_NAME, TEST_GROUP_NAME, null,
|
||||
true);
|
||||
Assert.assertTrue(res instanceof List);
|
||||
Assert.assertEquals(0, ((List) res).size());
|
||||
} catch (NacosException e) {
|
||||
|
@ -20,8 +20,13 @@ package com.alibaba.nacos.naming.controllers;
|
||||
import com.alibaba.nacos.naming.cluster.ServerStatus;
|
||||
import com.alibaba.nacos.naming.cluster.ServerStatusManager;
|
||||
import com.alibaba.nacos.naming.core.DistroMapper;
|
||||
import com.alibaba.nacos.naming.core.v2.client.Client;
|
||||
import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
|
||||
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager;
|
||||
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
|
||||
import com.alibaba.nacos.naming.core.v2.pojo.Service;
|
||||
import com.alibaba.nacos.naming.misc.SwitchDomain;
|
||||
import com.alibaba.nacos.naming.misc.SwitchManager;
|
||||
import com.alibaba.nacos.naming.monitor.MetricsMonitor;
|
||||
import com.alibaba.nacos.sys.env.Constants;
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
@ -55,6 +60,9 @@ public class OperatorControllerTest {
|
||||
@Mock
|
||||
private SwitchDomain switchDomain;
|
||||
|
||||
@Mock
|
||||
private SwitchManager switchManager;
|
||||
|
||||
@Mock
|
||||
private ServerStatusManager serverStatusManager;
|
||||
|
||||
@ -114,20 +122,22 @@ public class OperatorControllerTest {
|
||||
clients.add("127.0.0.1:8081#true");
|
||||
clients.add("127.0.0.1:8082#false");
|
||||
Mockito.when(clientManager.allClientId()).thenReturn(clients);
|
||||
Mockito.when(clientManager.isResponsibleClient(null)).thenReturn(Boolean.TRUE);
|
||||
Client client = new IpPortBasedClient("127.0.0.1:8081#true", true);
|
||||
client.addServiceInstance(Service.newService("", "", ""), new InstancePublishInfo());
|
||||
Mockito.when(clientManager.getClient("127.0.0.1:8081#true")).thenReturn(client);
|
||||
Mockito.when(clientManager.isResponsibleClient(client)).thenReturn(Boolean.TRUE);
|
||||
|
||||
MockHttpServletRequest servletRequest = new MockHttpServletRequest();
|
||||
servletRequest.addParameter("onlyStatus", "false");
|
||||
ObjectNode objectNode = operatorController.metrics(servletRequest);
|
||||
|
||||
Assert.assertEquals(1, objectNode.get("responsibleServiceCount").asInt());
|
||||
Assert.assertEquals(1, objectNode.get("responsibleInstanceCount").asInt());
|
||||
Assert.assertEquals(ServerStatus.UP.toString(), objectNode.get("status").asText());
|
||||
Assert.assertEquals(3, objectNode.get("clientCount").asInt());
|
||||
Assert.assertEquals(1, objectNode.get("connectionBasedClientCount").asInt());
|
||||
Assert.assertEquals(1, objectNode.get("ephemeralIpPortClientCount").asInt());
|
||||
Assert.assertEquals(1, objectNode.get("persistentIpPortClientCount").asInt());
|
||||
Assert.assertEquals(3, objectNode.get("responsibleClientCount").asInt());
|
||||
Assert.assertEquals(1, objectNode.get("responsibleClientCount").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user