Search in sources :

Example 1 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project java-chassis by ServiceComb.

the class TestAnySchema method json.

@Test
public void json() throws Throwable {
    Root root = new Root();
    root.setAny("abc");
    scbRootBytes = rootSerializer.serialize(root);
    root = rootDeserializer.deserialize(scbRootBytes);
    Assert.assertEquals("abc", root.getAny());
}
Also used : Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root) ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) Test(org.junit.Test)

Example 2 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project java-chassis by ServiceComb.

the class TestCompatibilityOfImplementations method testEmptyCollection.

@Test
@SuppressWarnings("unchecked")
public void testEmptyCollection() throws Exception {
    ProtobufRoot.Root.Builder builder = ProtobufRoot.Root.newBuilder();
    byte[] values = protobuf.serialize(builder);
    Assert.assertEquals(values.length, 0);
    ProtobufRoot.Root.Builder o = (ProtobufRoot.Root.Builder) protobuf.deserialize(values);
    Assert.assertTrue(o.getFixed32SNotPackedList().isEmpty());
    builder = ProtobufRoot.Root.newBuilder().addFixed32SNotPacked(30);
    values = protobuf.serialize(builder);
    Assert.assertArrayEquals(new byte[] { (byte) -123, (byte) 6, (byte) 30, (byte) 0, (byte) 0, (byte) 0 }, values);
    o = (ProtobufRoot.Root.Builder) protobuf.deserialize(values);
    Assert.assertEquals(30, (int) o.getFixed32SNotPackedList().get(0));
    Root root = new Root();
    root.setFixed32sNotPacked(new ArrayList<>());
    values = scbWeak.serialize(root);
    Assert.assertEquals(values.length, 0);
    Map<String, Object> newRootMap = (Map<String, Object>) scbWeak.deserialize(values);
    Assert.assertEquals(null, // This is different , because depends on default model initializer
    newRootMap.get("fixed32sNotPacked"));
    List<Integer> iValues = new ArrayList<>();
    iValues.add(30);
    root.setFixed32sNotPacked(iValues);
    values = scbWeak.serialize(root);
    Assert.assertArrayEquals(new byte[] { (byte) -123, (byte) 6, (byte) 30, (byte) 0, (byte) 0, (byte) 0 }, values);
    newRootMap = (Map<String, Object>) scbWeak.deserialize(values);
    Assert.assertEquals(30, (int) ((List<Integer>) newRootMap.get("fixed32sNotPacked")).get(0));
}
Also used : ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root) ArrayList(java.util.ArrayList) ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project incubator-servicecomb-java-chassis by apache.

the class TestAnySchema method json_fromMapWithoutType.

@SuppressWarnings("unchecked")
@Test
public void json_fromMapWithoutType() throws Throwable {
    Map<String, Object> map = new HashMap<>();
    map.put("name", "n1");
    Root root = new Root();
    root.setAny(map);
    scbRootBytes = rootSerializer.serialize(root);
    root = rootDeserializer.deserialize(scbRootBytes);
    Assert.assertThat(root.getAny(), Matchers.instanceOf(Map.class));
    Assert.assertThat((Map<? extends String, ? extends String>) root.getAny(), Matchers.hasEntry("name", "n1"));
    RootDeserializer<Map<String, Object>> deserializer = protoMapper.createRootDeserializer("Root", Map.class);
    map = deserializer.deserialize(scbRootBytes);
    Assert.assertThat((Map<? extends String, ? extends String>) map.get("any"), Matchers.hasEntry("name", "n1"));
}
Also used : Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root) ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project incubator-servicecomb-java-chassis by apache.

the class TestAnySchema method json.

@Test
public void json() throws Throwable {
    Root root = new Root();
    root.setAny("abc");
    scbRootBytes = rootSerializer.serialize(root);
    root = rootDeserializer.deserialize(scbRootBytes);
    Assert.assertEquals("abc", root.getAny());
}
Also used : Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root) ProtobufRoot(org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot) Test(org.junit.Test)

Example 5 with Root

use of org.apache.servicecomb.foundation.protobuf.internal.model.Root in project incubator-servicecomb-java-chassis by apache.

the class Protostuff method deserialize.

@Override
public Object deserialize(byte[] bytes) throws IOException {
    Input input = new ByteArrayInput(bytes, false);
    Root result = new Root();
    rootSchema.mergeFrom(input, result);
    return result;
}
Also used : ByteArrayInput(io.protostuff.ByteArrayInput) Input(io.protostuff.Input) ByteArrayInput(io.protostuff.ByteArrayInput) Root(org.apache.servicecomb.foundation.protobuf.internal.model.Root)

Aggregations

Root (org.apache.servicecomb.foundation.protobuf.internal.model.Root)12 ProtobufRoot (org.apache.servicecomb.foundation.protobuf.internal.model.ProtobufRoot)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ByteArrayInput (io.protostuff.ByteArrayInput)2 Input (io.protostuff.Input)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2