Search in sources :

Example 1 with Color

use of org.apache.servicecomb.foundation.test.scaffolding.model.Color in project java-chassis by ServiceComb.

the class TestSchemaMetaCodecRestTemplate method testProtoSchemaOperationBase.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testProtoSchemaOperationBase() throws Exception {
    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);
    values = requestSerializer.serialize(args);
    RequestRootDeserializer<Object> requestDeserializer = providerOperationProtobuf.getRequestRootDeserializer();
    Map<String, Object> decodedArgs = requestDeserializer.deserialize(values);
    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"));
    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) Test(org.junit.Test)

Example 2 with Color

use of org.apache.servicecomb.foundation.test.scaffolding.model.Color 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)

Aggregations

LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 OperationProtobuf (org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)2 RequestRootSerializer (org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer)2 ResponseRootSerializer (org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer)2 Invocation (org.apache.servicecomb.core.Invocation)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)1