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