Search in sources :

Example 6 with RequestRootSerializer

use of org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer in project java-chassis by ServiceComb.

the class TestSchemaMetaCodec method testProtoSchemaOperationObjImpl.

private void testProtoSchemaOperationObjImpl(boolean isPojo) throws IOException {
    Invocation consumerInvocation = mockInvocation("obj", InvocationType.CONSUMER);
    Invocation providerInvocation = mockInvocation("obj", InvocationType.PRODUCER);
    OperationProtobuf providerOperationProtobuf = ProtobufManager.getOrCreateOperation(providerInvocation);
    OperationProtobuf consumerOperationProtobuf = ProtobufManager.getOrCreateOperation(consumerInvocation);
    byte[] values;
    // request message
    RequestRootSerializer requestSerializer = consumerOperationProtobuf.getRequestRootSerializer();
    Map<String, Object> args = new HashMap<>();
    args.put("value", 2);
    values = requestSerializer.serialize(args);
    RequestRootDeserializer<Object> requestDeserializer = providerOperationProtobuf.getRequestRootDeserializer();
    Map<String, Object> decodedArgs = requestDeserializer.deserialize(values);
    int result = (int) decodedArgs.get("value");
    Assert.assertEquals(2, result);
    User user = new User();
    user.name = "user";
    User friend = new User();
    friend.name = "friend";
    List<User> friends = new ArrayList<>();
    friends.add(friend);
    user.friends = friends;
    args.put("value", user);
    values = requestSerializer.serialize(args);
    decodedArgs = requestDeserializer.deserialize(values);
    Map<String, Object> userMap = (Map<String, Object>) decodedArgs.get("value");
    Assert.assertEquals("user", userMap.get("name"));
    // proto buffer encode and decode empty list to be null
    friends = (List<User>) userMap.get("friends");
    Map<String, Object> friendMap = (Map<String, Object>) friends.get(0);
    Assert.assertEquals("friend", friendMap.get("name"));
    args.clear();
    People people = new People();
    people.name = "user";
    People pFriend = new People();
    pFriend.name = "friend";
    List<People> pFriends = new ArrayList<>();
    pFriends.add(pFriend);
    people.friends = pFriends;
    args.put("value", people);
    values = requestSerializer.serialize(args);
    decodedArgs = requestDeserializer.deserialize(values);
    people = (People) decodedArgs.get("value");
    Assert.assertEquals("user", people.name);
    // proto buffer encode and decode empty list to be null
    Assert.assertEquals("friend", people.friends.get(0).name);
}
Also used : RequestRootSerializer(org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer) User(org.apache.servicecomb.foundation.test.scaffolding.model.User) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) People(org.apache.servicecomb.foundation.test.scaffolding.model.People) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with RequestRootSerializer

use of org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer in project java-chassis by ServiceComb.

the class TestSchemaMetaCodec method testProtoSchemaOperationBaseImpl.

private void testProtoSchemaOperationBaseImpl(boolean isPojo) throws IOException {
    Invocation consumerInvocation = mockInvocation("base", InvocationType.CONSUMER);
    Invocation providerInvocation = mockInvocation("base", InvocationType.PRODUCER);
    OperationProtobuf providerOperationProtobuf = ProtobufManager.getOrCreateOperation(providerInvocation);
    OperationProtobuf consumerOperationProtobuf = ProtobufManager.getOrCreateOperation(consumerInvocation);
    byte[] values;
    // request message
    RequestRootSerializer requestSerializer = consumerOperationProtobuf.getRequestRootSerializer();
    boolean boolValue = true;
    int iValue = 20;
    long lValue = 30L;
    float fValue = 40f;
    double dValue = 50D;
    String sValue = "hello";
    int[] iArray = new int[] { 60, 70 };
    Color color = Color.BLUE;
    LocalDate localDate = LocalDate.of(2019, 10, 1);
    Date date = new Date();
    Empty empty = new Empty();
    Map<String, Object> args = new HashMap<>();
    args.put("boolValue", boolValue);
    args.put("iValue", iValue);
    args.put("lValue", lValue);
    args.put("fValue", fValue);
    args.put("dValue", dValue);
    args.put("sValue", sValue);
    args.put("iArray", iArray);
    args.put("color", color);
    args.put("localDate", localDate);
    args.put("date", date);
    args.put("empty", empty);
    if (isPojo) {
        Map<String, Object> swaggerArgs = new HashMap<>();
        swaggerArgs.put("baseBody", args);
        values = requestSerializer.serialize(swaggerArgs);
    } else {
        values = requestSerializer.serialize(args);
    }
    RequestRootDeserializer<Object> requestDeserializer = providerOperationProtobuf.getRequestRootDeserializer();
    Map<String, Object> decodedSwaggerArgs = requestDeserializer.deserialize(values);
    Map<String, Object> decodedArgs;
    if (isPojo) {
        Assert.assertEquals(1, decodedSwaggerArgs.size());
        decodedArgs = (Map<String, Object>) decodedSwaggerArgs.get("baseBody");
    } else {
        decodedArgs = decodedSwaggerArgs;
    }
    Assert.assertEquals(boolValue, decodedArgs.get("boolValue"));
    Assert.assertEquals(iValue, decodedArgs.get("iValue"));
    Assert.assertEquals(lValue, decodedArgs.get("lValue"));
    Assert.assertEquals(fValue, decodedArgs.get("fValue"));
    Assert.assertEquals(dValue, decodedArgs.get("dValue"));
    if (isPojo) {
        Assert.assertEquals(2, ((List<Integer>) decodedArgs.get("iArray")).size());
        Assert.assertEquals(60, (((List<Integer>) decodedArgs.get("iArray")).get(0).intValue()));
        Assert.assertEquals(70, (((List<Integer>) decodedArgs.get("iArray")).get(1).intValue()));
        Assert.assertEquals(color.ordinal(), decodedArgs.get("color"));
        Assert.assertEquals(date.getTime(), decodedArgs.get("date"));
        Assert.assertEquals(localDate.getLong(ChronoField.EPOCH_DAY), decodedArgs.get("localDate"));
        Assert.assertEquals(true, ((Map) decodedArgs.get("empty")).isEmpty());
    } else {
        Assert.assertArrayEquals(iArray, (int[]) decodedArgs.get("iArray"));
        Assert.assertEquals(color, decodedArgs.get("color"));
        Assert.assertEquals(date, decodedArgs.get("date"));
        Assert.assertTrue(decodedArgs.get("localDate") instanceof LocalDate);
        Assert.assertEquals(localDate, decodedArgs.get("localDate"));
        Assert.assertTrue(decodedArgs.get("empty") instanceof Empty);
    }
    // default value testing
    args.put("boolValue", false);
    args.put("iValue", 0);
    args.put("lValue", 0L);
    args.put("fValue", 0F);
    args.put("dValue", 0D);
    args.put("sValue", null);
    args.put("iArray", new int[0]);
    args.put("color", null);
    args.put("localDate", null);
    args.put("date", null);
    args.put("empty", null);
    values = requestSerializer.serialize(args);
    decodedArgs = requestDeserializer.deserialize(values);
    Assert.assertEquals(null, decodedArgs.get("boolValue"));
    Assert.assertEquals(null, decodedArgs.get("iValue"));
    Assert.assertEquals(null, decodedArgs.get("lValue"));
    Assert.assertEquals(null, decodedArgs.get("fValue"));
    Assert.assertEquals(null, decodedArgs.get("dValue"));
    Assert.assertEquals(null, decodedArgs.get("iArray"));
    Assert.assertEquals(null, decodedArgs.get("color"));
    Assert.assertEquals(null, decodedArgs.get("localDate"));
    Assert.assertEquals(null, decodedArgs.get("date"));
    Assert.assertEquals(null, decodedArgs.get("empty"));
    // response message
    ResponseRootSerializer responseSerializer = providerOperationProtobuf.findResponseRootSerializer(200);
    values = responseSerializer.serialize(30);
    ResponseRootDeserializer<Object> responseDeserializer = consumerOperationProtobuf.findResponseRootDeserializer(200);
    Object decodedValue = responseDeserializer.deserialize(values, TypeFactory.defaultInstance().constructType(int.class));
    Assert.assertEquals(30, (int) decodedValue);
}
Also used : RequestRootSerializer(org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) Color(org.apache.servicecomb.foundation.test.scaffolding.model.Color) ResponseRootSerializer(org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) Empty(org.apache.servicecomb.foundation.test.scaffolding.model.Empty) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)

Example 8 with RequestRootSerializer

use of org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer in project java-chassis by ServiceComb.

the class TestSchemaMetaCodec method testProtoSchemaOperationmapUserImpl.

private void testProtoSchemaOperationmapUserImpl(boolean isPojo) throws IOException {
    Invocation consumerInvocation = mockInvocation("mapUser", InvocationType.CONSUMER);
    Invocation providerInvocation = mockInvocation("mapUser", InvocationType.PRODUCER);
    OperationProtobuf providerOperationProtobuf = ProtobufManager.getOrCreateOperation(providerInvocation);
    OperationProtobuf consumerOperationProtobuf = ProtobufManager.getOrCreateOperation(consumerInvocation);
    User user = new User();
    user.name = "user";
    User friend = new User();
    friend.name = "friend";
    List<User> friends = new ArrayList<>();
    friends.add(friend);
    user.friends = friends;
    byte[] values;
    Map<String, User> userMap = new HashMap<>();
    userMap.put("test", user);
    // request message
    Map<String, Object> args = new HashMap<>();
    RequestRootSerializer requestSerializer = consumerOperationProtobuf.getRequestRootSerializer();
    user.friends = friends;
    args.put("users", userMap);
    if (isPojo) {
        Map<String, Object> swaggerArgs = new HashMap<>(1);
        swaggerArgs.put("users", args);
        values = requestSerializer.serialize(swaggerArgs);
    } else {
        values = requestSerializer.serialize(args);
    }
    RequestRootDeserializer<Object> requestDeserializer = providerOperationProtobuf.getRequestRootDeserializer();
    Map<String, Object> decodedUserArgs = requestDeserializer.deserialize(values);
    if (isPojo) {
        decodedUserArgs = (Map<String, Object>) decodedUserArgs.get("users");
        Assert.assertEquals(user.name, ((Map<String, Map<String, Object>>) decodedUserArgs.get("users")).get("test").get("name"));
        Assert.assertEquals(user.friends.get(0).name, ((List<Map<String, Object>>) ((Map<String, Map<String, Object>>) decodedUserArgs.get("users")).get("test").get("friends")).get(0).get("name"));
    } else {
        Assert.assertEquals(user.name, ((Map<String, User>) decodedUserArgs.get("users")).get("test").name);
        Assert.assertEquals(user.friends.get(0).name, ((Map<String, User>) decodedUserArgs.get("users")).get("test").friends.get(0).name);
    }
    // response message
    ResponseRootSerializer responseSerializer = providerOperationProtobuf.findResponseRootSerializer(200);
    values = responseSerializer.serialize(userMap);
    ResponseRootDeserializer<Object> responseDeserializer = consumerOperationProtobuf.findResponseRootDeserializer(200);
    Map<String, User> decodedUser = (Map<String, User>) responseDeserializer.deserialize(values, ProtoConst.MAP_TYPE);
    Assert.assertEquals(user.name, decodedUser.get("test").name);
    Assert.assertEquals(user.friends.get(0).name, decodedUser.get("test").friends.get(0).name);
    user.friends = new ArrayList<>();
    values = responseSerializer.serialize(userMap);
    decodedUser = (Map<String, User>) responseDeserializer.deserialize(values, ProtoConst.MAP_TYPE);
    Assert.assertEquals(user.name, decodedUser.get("test").name);
    // proto buffer encode and decode empty list to be null
    Assert.assertEquals(null, decodedUser.get("test").friends);
}
Also used : User(org.apache.servicecomb.foundation.test.scaffolding.model.User) RequestRootSerializer(org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) ResponseRootSerializer(org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer) ArrayList(java.util.ArrayList) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

OperationProtobuf (org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)8 RequestRootSerializer (org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer)8 HashMap (java.util.HashMap)7 Invocation (org.apache.servicecomb.core.Invocation)7 ArrayList (java.util.ArrayList)5 ResponseRootSerializer (org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer)5 User (org.apache.servicecomb.foundation.test.scaffolding.model.User)5 Map (java.util.Map)4 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 List (java.util.List)2 Color (org.apache.servicecomb.foundation.test.scaffolding.model.Color)2 Empty (org.apache.servicecomb.foundation.test.scaffolding.model.Empty)2 Test (org.junit.Test)2 Endpoint (org.apache.servicecomb.core.Endpoint)1 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)1 URIEndpointObject (org.apache.servicecomb.foundation.common.net.URIEndpointObject)1 People (org.apache.servicecomb.foundation.test.scaffolding.model.People)1