Search in sources :

Example 16 with ServiceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.

the class CatalogServiceV2ImplTest method testPageListServiceDetail.

@Test
public void testPageListServiceDetail() {
    try {
        ServiceMetadata metadata = new ServiceMetadata();
        Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));
        Instance instance = new Instance();
        instance.setServiceName("C");
        instance.setClusterName("D");
        List<Instance> instances = Collections.singletonList(instance);
        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.setHosts(instances);
        Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
        List<ServiceDetailInfo> result = (List<ServiceDetailInfo>) catalogServiceV2Impl.pageListServiceDetail("A", "B", "C", 1, 10);
        Assert.assertEquals(1, result.size());
    } catch (NacosException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ServiceDetailInfo(com.alibaba.nacos.naming.pojo.ServiceDetailInfo) List(java.util.List) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Example 17 with ServiceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.

the class HealthOperatorV2ImplTest method testUpdateHealthStatusForPersistentInstance.

@Test
public void testUpdateHealthStatusForPersistentInstance() {
    try {
        ServiceMetadata metadata = new ServiceMetadata();
        Map<String, ClusterMetadata> clusterMap = new HashMap<>(2);
        ClusterMetadata cluster = Mockito.mock(ClusterMetadata.class);
        clusterMap.put("C", cluster);
        metadata.setClusters(clusterMap);
        Instance instance = new Instance();
        instance.setIp("1.1.1.1");
        instance.setPort(8080);
        Mockito.when(cluster.getHealthyCheckType()).thenReturn(HealthCheckType.NONE.name());
        Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));
        ConnectionBasedClient client = Mockito.mock(ConnectionBasedClient.class);
        Mockito.when(clientManager.getClient(Mockito.anyString())).thenReturn(client);
        InstancePublishInfo instancePublishInfo = new InstancePublishInfo();
        instancePublishInfo.setExtendDatum(new HashMap<>(2));
        Mockito.when(client.getInstancePublishInfo(Mockito.any())).thenReturn(instancePublishInfo);
        healthOperatorV2.updateHealthStatusForPersistentInstance("A", "B", "C", "1.1.1.1", 8080, true);
    } catch (NacosException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) HashMap(java.util.HashMap) ConnectionBasedClient(com.alibaba.nacos.naming.core.v2.client.impl.ConnectionBasedClient) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Example 18 with ServiceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.

the class ServiceMetadataProcessorTest method testOnApply.

@Test
public void testOnApply() throws NoSuchFieldException, IllegalAccessException {
    WriteRequest defaultInstance = WriteRequest.getDefaultInstance();
    Class<WriteRequest> writeRequestClass = WriteRequest.class;
    Field operation = writeRequestClass.getDeclaredField("operation_");
    operation.setAccessible(true);
    operation.set(defaultInstance, "ADD");
    MetadataOperation<ServiceMetadata> metadataOperation = new MetadataOperation<>();
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    metadataOperation.setMetadata(serviceMetadata);
    metadataOperation.setServiceName("nacos");
    metadataOperation.setNamespace("namespace");
    metadataOperation.setGroup("group");
    Serializer aDefault = SerializeFactory.getDefault();
    ByteString bytes = ByteString.copyFrom(aDefault.serialize(metadataOperation));
    Field data = writeRequestClass.getDeclaredField("data_");
    data.setAccessible(true);
    data.set(defaultInstance, bytes);
    // ADD
    Response addResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Service service = Service.newService(metadataOperation.getNamespace(), metadataOperation.getGroup(), metadataOperation.getServiceName(), metadataOperation.getMetadata().isEphemeral());
    Service singleton = ServiceManager.getInstance().getSingleton(service);
    namingMetadataManager.updateServiceMetadata(singleton, metadataOperation.getMetadata());
    Assert.assertTrue(addResponse.getSuccess());
    verify(namingMetadataManager).getServiceMetadata(service);
    verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
    verify(context).getBean(DoubleWriteEventListener.class);
    // CHANGE
    operation.set(defaultInstance, "CHANGE");
    Response changeResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertTrue(changeResponse.getSuccess());
    verify(namingMetadataManager, times(2)).getServiceMetadata(service);
    verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
    verify(context, times(2)).getBean(DoubleWriteEventListener.class);
    // DELETE
    operation.set(defaultInstance, "DELETE");
    Response deleteResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertTrue(deleteResponse.getSuccess());
    verify(namingMetadataManager).removeServiceMetadata(service);
    verify(serviceStorage).removeData(service);
    // VERIFY
    operation.set(defaultInstance, "VERIFY");
    Response otherResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertFalse(otherResponse.getSuccess());
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Field(java.lang.reflect.Field) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteString(com.google.protobuf.ByteString) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Serializer(com.alibaba.nacos.consistency.Serializer) Test(org.junit.Test)

Example 19 with ServiceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.

the class ServiceOperatorV2ImplTest method testCreate.

@Test
public void testCreate() throws NacosException {
    serviceOperatorV2.create("A", "B", new ServiceMetadata());
    Mockito.verify(metadataOperateService).updateServiceMetadata(Mockito.any(), Mockito.any());
}
Also used : ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Test(org.junit.Test)

Example 20 with ServiceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.

the class ServiceOperatorV2ImplTest method testQueryService.

@Test
public void testQueryService() throws NacosException {
    ClusterMetadata clusterMetadata = new ClusterMetadata();
    Map<String, ClusterMetadata> clusterMetadataMap = new HashMap<>(2);
    clusterMetadataMap.put("D", clusterMetadata);
    ServiceMetadata metadata = new ServiceMetadata();
    metadata.setClusters(clusterMetadataMap);
    Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));
    Mockito.when(serviceStorage.getClusters(Mockito.any())).thenReturn(Collections.singleton("D"));
    ObjectNode objectNode = serviceOperatorV2.queryService("A", "C");
    Assert.assertEquals("A", objectNode.get(FieldsConstants.NAME_SPACE_ID).asText());
    Assert.assertEquals("C", objectNode.get(FieldsConstants.NAME).asText());
    Assert.assertEquals(1, objectNode.get(FieldsConstants.CLUSTERS).size());
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Test(org.junit.Test)

Aggregations

ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)33 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)18 Test (org.junit.Test)13 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)11 ClusterMetadata (com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata)8 NacosException (com.alibaba.nacos.api.exception.NacosException)7 Secured (com.alibaba.nacos.auth.annotation.Secured)7 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 Instance (com.alibaba.nacos.api.naming.pojo.Instance)5 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)4 PushDataWrapper (com.alibaba.nacos.naming.push.v2.PushDataWrapper)4 Before (org.junit.Before)4 ServiceDetailInfo (com.alibaba.nacos.naming.pojo.ServiceDetailInfo)3 HashMap (java.util.HashMap)3 PutMapping (org.springframework.web.bind.annotation.PutMapping)3 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)2 Serializer (com.alibaba.nacos.consistency.Serializer)2 Cluster (com.alibaba.nacos.naming.core.Cluster)2 NamingMetadataManager (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager)2 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)2