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;
}
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");
}
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());
}
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);
}
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);
}
Aggregations