use of com.alibaba.nacos.consistency.Serializer 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.consistency.Serializer 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);
}
use of com.alibaba.nacos.consistency.Serializer in project nacos by alibaba.
the class SelectorManagerTest method testSelect.
@Test
public void testSelect() throws NacosException {
Selector selector = selectorManager.parseSelector("mock", "key=value");
Instance instance = new Instance();
instance.setIp("2.2.2.2");
List<Instance> providers = Collections.singletonList(instance);
List<Instance> instances0 = selectorManager.select(selector, "1.1.1.1", providers);
Assert.assertEquals(1, instances0.size());
Assert.assertEquals("2.2.2.2", instances0.get(0).getIp());
// test json serial for Selector
Serializer serializer0 = SerializeFactory.getSerializer("JSON");
byte[] bytes = serializer0.serialize(selector);
Selector jsonSelector = serializer0.deserialize(bytes, Selector.class);
List<Instance> instances1 = selectorManager.select(jsonSelector, "1.1.1.1", providers);
Assert.assertEquals(1, instances1.size());
Assert.assertEquals("2.2.2.2", instances1.get(0).getIp());
// test hessian serial for Selector
Serializer serializer1 = SerializeFactory.getDefault();
byte[] bytes1 = serializer1.serialize(selector);
Selector hessianSelector = serializer1.deserialize(bytes1);
List<Instance> instances2 = selectorManager.select(hessianSelector, "1.1.1.1", providers);
Assert.assertEquals(1, instances2.size());
Assert.assertEquals("2.2.2.2", instances2.get(0).getIp());
}
Aggregations