Search in sources :

Example 36 with ServiceMetadata

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

the class DefaultServiceMetadataUpgradeHelper method toV1Service.

@Override
public Service toV1Service(Service v1, com.alibaba.nacos.naming.core.v2.pojo.Service v2, ServiceMetadata v2meta) {
    if (null == v1) {
        v1 = new Service(v2.getGroupedServiceName());
        v1.setGroupName(v2.getGroup());
        v1.setNamespaceId(v2.getNamespace());
    }
    v1.setSelector(v2meta.getSelector());
    v1.setProtectThreshold(v2meta.getProtectThreshold());
    v1.setMetadata(v2meta.getExtendData());
    for (Map.Entry<String, ClusterMetadata> entry : v2meta.getClusters().entrySet()) {
        if (!v1.getClusterMap().containsKey(entry.getKey())) {
            v1.addCluster(toV1Cluster(new Cluster(entry.getKey(), v1), entry.getValue()));
        } else {
            toV1Cluster(v1.getClusterMap().get(entry.getKey()), entry.getValue());
        }
    }
    return v1;
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) Service(com.alibaba.nacos.naming.core.Service) Cluster(com.alibaba.nacos.naming.core.Cluster) Map(java.util.Map)

Example 37 with ServiceMetadata

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

the class ServiceQueryRequestHandlerTest method testHandle.

@Test
public void testHandle() throws NacosException {
    Instance instance = new Instance();
    instance.setIp("1.1.1.1");
    List<Instance> instances = Arrays.asList(instance);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setGroupName("A");
    serviceInfo.setGroupName("B");
    serviceInfo.setName("C");
    serviceInfo.setHosts(instances);
    Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(serviceMetadata));
    ServiceQueryRequest serviceQueryRequest = new ServiceQueryRequest();
    serviceQueryRequest.setNamespace("A");
    serviceQueryRequest.setGroupName("B");
    serviceQueryRequest.setServiceName("C");
    serviceQueryRequest.setHealthyOnly(false);
    QueryServiceResponse queryServiceResponse = serviceQueryRequestHandler.handle(serviceQueryRequest, new RequestMeta());
    Assert.assertEquals(queryServiceResponse.getServiceInfo().getName(), "C");
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) QueryServiceResponse(com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse) Instance(com.alibaba.nacos.api.naming.pojo.Instance) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) ServiceQueryRequest(com.alibaba.nacos.api.naming.remote.request.ServiceQueryRequest) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Test(org.junit.Test)

Example 38 with ServiceMetadata

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

the class SubscribeServiceRequestHandlerTest method testHandle.

@Test
public void testHandle() throws NacosException {
    Instance instance = new Instance();
    instance.setIp("1.1.1.1");
    List<Instance> instances = Arrays.asList(instance);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setGroupName("A");
    serviceInfo.setGroupName("B");
    serviceInfo.setName("C");
    serviceInfo.setHosts(instances);
    Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(serviceMetadata));
    SubscribeServiceRequest subscribeServiceRequest = new SubscribeServiceRequest();
    subscribeServiceRequest.setNamespace("A");
    subscribeServiceRequest.setGroupName("B");
    subscribeServiceRequest.setServiceName("C");
    subscribeServiceRequest.setSubscribe(true);
    SubscribeServiceResponse subscribeServiceResponse = subscribeServiceRequestHandler.handle(subscribeServiceRequest, new RequestMeta());
    Assert.assertEquals(subscribeServiceResponse.getServiceInfo().getName(), "C");
    Mockito.verify(clientOperationService).subscribeService(Mockito.any(), Mockito.any(), Mockito.anyString());
    subscribeServiceRequest.setSubscribe(false);
    subscribeServiceResponse = subscribeServiceRequestHandler.handle(subscribeServiceRequest, new RequestMeta());
    Assert.assertEquals(subscribeServiceResponse.getServiceInfo().getName(), "C");
    Mockito.verify(clientOperationService).subscribeService(Mockito.any(), Mockito.any(), Mockito.anyString());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) SubscribeServiceRequest(com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest) Instance(com.alibaba.nacos.api.naming.pojo.Instance) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) SubscribeServiceResponse(com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Test(org.junit.Test)

Example 39 with ServiceMetadata

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

the class NamingMetadataManagerTest method setUp.

@Before
public void setUp() throws Exception {
    namingMetadataManager = new NamingMetadataManager();
    Class<NamingMetadataManager> namingMetadataManagerClass = NamingMetadataManager.class;
    Field serviceMetadataMapField = namingMetadataManagerClass.getDeclaredField("serviceMetadataMap");
    serviceMetadataMapField.setAccessible(true);
    ConcurrentMap<Service, ServiceMetadata> serviceMetadataMap = (ConcurrentMap<Service, ServiceMetadata>) serviceMetadataMapField.get(namingMetadataManager);
    serviceMetadataMap.put(service, serviceMetadata);
    Field instanceMetadataMapField = namingMetadataManagerClass.getDeclaredField("instanceMetadataMap");
    instanceMetadataMapField.setAccessible(true);
    ConcurrentMap<Service, ConcurrentMap<String, InstanceMetadata>> instanceMetadataMap = (ConcurrentMap<Service, ConcurrentMap<String, InstanceMetadata>>) instanceMetadataMapField.get(namingMetadataManager);
    ConcurrentMap<String, InstanceMetadata> concurrentMap = new ConcurrentHashMap<>();
    concurrentMap.put(METADATA_ID, instanceMetadata);
    instanceMetadataMap.put(service, concurrentMap);
}
Also used : Field(java.lang.reflect.Field) ConcurrentMap(java.util.concurrent.ConcurrentMap) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Before(org.junit.Before)

Example 40 with ServiceMetadata

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

the class ServiceMetadataSnapshotOperationTest method testLoadSnapshot.

@Test
public void testLoadSnapshot() {
    ConcurrentMap<Service, ServiceMetadata> map = new ConcurrentHashMap<>();
    Service service = Service.newService("namespace", "group", "name");
    map.put(service, new ServiceMetadata());
    Serializer aDefault = SerializeFactory.getDefault();
    serviceMetadataSnapshotOperation.loadSnapshot(aDefault.serialize(map));
    Map<Service, ServiceMetadata> serviceMetadataSnapshot = namingMetadataManager.getServiceMetadataSnapshot();
    Assert.assertNotNull(serviceMetadataSnapshot);
    Assert.assertEquals(serviceMetadataSnapshot.size(), 1);
}
Also used : Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Serializer(com.alibaba.nacos.consistency.Serializer) 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