Search in sources :

Example 1 with Serializer

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());
}
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 2 with Serializer

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

Example 3 with Serializer

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());
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) Selector(com.alibaba.nacos.api.selector.Selector) Serializer(com.alibaba.nacos.consistency.Serializer) Test(org.junit.Test)

Aggregations

Serializer (com.alibaba.nacos.consistency.Serializer)3 Test (org.junit.Test)3 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)2 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 Selector (com.alibaba.nacos.api.selector.Selector)1 Response (com.alibaba.nacos.consistency.entity.Response)1 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)1 ByteString (com.google.protobuf.ByteString)1 Field (java.lang.reflect.Field)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1