Search in sources :

Example 1 with User

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

the class TestSchemaMetaCodecRestTemplate method testProtoSchemaOperationUser.

@Test
public void testProtoSchemaOperationUser() throws Exception {
    Invocation consumerInvocation = mockInvocation("user", InvocationType.CONSUMER);
    Invocation providerInvocation = mockInvocation("user", 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;
    // request message
    Map<String, Object> args = new HashMap<>();
    RequestRootSerializer requestSerializer = consumerOperationProtobuf.getRequestRootSerializer();
    user.friends = friends;
    args.put("user", user);
    values = requestSerializer.serialize(args);
    RequestRootDeserializer<Object> requestDeserializer = providerOperationProtobuf.getRequestRootDeserializer();
    Map<String, Object> decodedUserArgs = requestDeserializer.deserialize(values);
    Assert.assertEquals(user.name, ((User) decodedUserArgs.get("user")).name);
    Assert.assertEquals(user.friends.get(0).name, ((User) decodedUserArgs.get("user")).friends.get(0).name);
    // response message
    ResponseRootSerializer responseSerializer = providerOperationProtobuf.findResponseRootSerializer(200);
    values = responseSerializer.serialize(user);
    ResponseRootDeserializer<Object> responseDeserializer = consumerOperationProtobuf.findResponseRootDeserializer(200);
    User decodedUser = (User) responseDeserializer.deserialize(values, TypeFactory.defaultInstance().constructType(User.class));
    Assert.assertEquals(user.name, decodedUser.name);
    Assert.assertEquals(user.friends.get(0).name, decodedUser.friends.get(0).name);
    user.friends = new ArrayList<>();
    values = responseSerializer.serialize(user);
    decodedUser = (User) responseDeserializer.deserialize(values, TypeFactory.defaultInstance().constructType(User.class));
    Assert.assertEquals(user.name, decodedUser.name);
    // proto buffer encode and decode empty list to be null
    Assert.assertEquals(null, decodedUser.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) Test(org.junit.Test)

Example 2 with User

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

the class TestSchemaMetaCodec method testProtoSchemaOperationUserImpl.

private void testProtoSchemaOperationUserImpl() throws IOException {
    Invocation consumerInvocation = mockInvocation("user", InvocationType.CONSUMER);
    Invocation providerInvocation = mockInvocation("user", 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;
    // request message
    Map<String, Object> args = new HashMap<>();
    RequestRootSerializer requestSerializer = consumerOperationProtobuf.getRequestRootSerializer();
    user.friends = friends;
    args.put("user", user);
    values = requestSerializer.serialize(args);
    RequestRootDeserializer<Object> requestDeserializer = providerOperationProtobuf.getRequestRootDeserializer();
    Map<String, Object> decodedUserArgs = requestDeserializer.deserialize(values);
    Assert.assertEquals(user.name, ((User) decodedUserArgs.get("user")).name);
    Assert.assertEquals(user.friends.get(0).name, ((User) decodedUserArgs.get("user")).friends.get(0).name);
    // write request map (pojo)
    args = new HashMap<>();
    Map<String, Object> userMap = new HashMap<>();
    userMap.put("name", "user");
    Map<String, Object> friendMap = new HashMap<>();
    friendMap.put("name", "friend");
    List<Map<String, Object>> friendsList = new ArrayList<>();
    friendsList.add(friendMap);
    userMap.put("friends", friendsList);
    args.put("user", userMap);
    values = requestSerializer.serialize(args);
    decodedUserArgs = requestDeserializer.deserialize(values);
    Assert.assertEquals(user.name, ((User) decodedUserArgs.get("user")).name);
    Assert.assertEquals(user.friends.get(0).name, ((User) decodedUserArgs.get("user")).friends.get(0).name);
    // response message
    ResponseRootSerializer responseSerializer = providerOperationProtobuf.findResponseRootSerializer(200);
    values = responseSerializer.serialize(user);
    ResponseRootDeserializer<Object> responseDeserializer = consumerOperationProtobuf.findResponseRootDeserializer(200);
    User decodedUser = (User) responseDeserializer.deserialize(values, TypeFactory.defaultInstance().constructType(User.class));
    Assert.assertEquals(user.name, decodedUser.name);
    Assert.assertEquals(user.friends.get(0).name, decodedUser.friends.get(0).name);
    user.friends = new ArrayList<>();
    values = responseSerializer.serialize(user);
    decodedUser = (User) responseDeserializer.deserialize(values, TypeFactory.defaultInstance().constructType(User.class));
    Assert.assertEquals(user.name, decodedUser.name);
    // proto buffer encode and decode empty list to be null
    Assert.assertEquals(null, decodedUser.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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with User

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

the class TestNumberBaseSchema method type_invalid.

private void type_invalid(Field field) throws IOException {
    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage(Matchers.is(String.format("not support serialize from %s to proto %s, field=%s:%s", User.class.getName(), field.getTypeName(), ((Type) field.getParent()).getCanonicalName(), field.getName())));
    scbMap = new HashMap<>();
    scbMap.put(field.getName(), new User());
    rootSerializer.serialize(scbMap);
}
Also used : User(org.apache.servicecomb.foundation.test.scaffolding.model.User)

Example 4 with User

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

the class TestPojoOneArg method should_mapper_consumer_bean_to_swagger_body.

@Test
public void should_mapper_consumer_bean_to_swagger_body() {
    SwaggerEnvironment environment = new SwaggerEnvironment();
    Swagger swagger = SwaggerGenerator.generate(PojoOneArg.class);
    SwaggerConsumer swaggerConsumer = environment.createConsumer(ConsumerOneArg.class, swagger);
    ArgumentsMapper mapper = swaggerConsumer.findOperation("bean").getArgumentsMapper();
    Map<String, Object> arguments = new HashMap<>();
    arguments.put("user", new User());
    SwaggerInvocation invocation = new SwaggerInvocation();
    Map<String, Object> result = mapper.invocationArgumentToSwaggerArguments(invocation, arguments);
    Assert.assertEquals(1, result.size());
    Assert.assertSame(arguments.get("user"), result.get("user"));
}
Also used : SwaggerInvocation(org.apache.servicecomb.swagger.invocation.SwaggerInvocation) User(org.apache.servicecomb.foundation.test.scaffolding.model.User) SwaggerConsumer(org.apache.servicecomb.swagger.engine.SwaggerConsumer) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) SwaggerEnvironment(org.apache.servicecomb.swagger.engine.SwaggerEnvironment) ArgumentsMapper(org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper) Test(org.junit.Test)

Example 5 with User

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

the class TestSchemaMetaCodec method testProtoSchemaOperationlistListUserImpl.

private void testProtoSchemaOperationlistListUserImpl(boolean isPojo) throws IOException {
    Invocation consumerInvocation = mockInvocation("listListUser", InvocationType.CONSUMER);
    Invocation providerInvocation = mockInvocation("listListUser", InvocationType.PRODUCER);
    OperationProtobuf providerOperationProtobuf = ProtobufManager.getOrCreateOperation(providerInvocation);
    OperationProtobuf consumerOperationProtobuf = ProtobufManager.getOrCreateOperation(consumerInvocation);
    byte[] values;
    // request message
    RequestRootSerializer requestSerializer = consumerOperationProtobuf.getRequestRootSerializer();
    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;
    List<User> users = new ArrayList<>();
    users.add(user);
    List<List<User>> listOfUsers = new ArrayList<>();
    listOfUsers.add(users);
    Map<String, Object> args = new HashMap<>();
    args.put("value", listOfUsers);
    if (isPojo) {
        Map<String, Object> swaggerArgs = new HashMap<>();
        swaggerArgs.put("value", 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("value");
    } else {
        decodedArgs = decodedSwaggerArgs;
    }
    List<List<?>> listOfUsersRaw = (List<List<?>>) decodedArgs.get("value");
    Assert.assertEquals(1, listOfUsersRaw.size());
    List<?> mapUsersRaw = (List<?>) listOfUsersRaw.get(0);
    Assert.assertEquals(1, mapUsersRaw.size());
    if (isPojo) {
        Map<String, Object> userMap = (Map<String, Object>) mapUsersRaw.get(0);
        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"));
    } else {
        user = (User) mapUsersRaw.get(0);
        Assert.assertEquals("user", user.name);
        // proto buffer encode and decode empty list to be null
        Assert.assertEquals("friend", user.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) 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

User (org.apache.servicecomb.foundation.test.scaffolding.model.User)8 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 OperationProtobuf (org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)5 RequestRootSerializer (org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer)5 Invocation (org.apache.servicecomb.core.Invocation)5 Map (java.util.Map)4 ResponseRootSerializer (org.apache.servicecomb.codec.protobuf.definition.ResponseRootSerializer)3 Test (org.junit.Test)3 List (java.util.List)2 Model (io.swagger.models.Model)1 Swagger (io.swagger.models.Swagger)1 People (org.apache.servicecomb.foundation.test.scaffolding.model.People)1 SwaggerConsumer (org.apache.servicecomb.swagger.engine.SwaggerConsumer)1 SwaggerEnvironment (org.apache.servicecomb.swagger.engine.SwaggerEnvironment)1 SwaggerInvocation (org.apache.servicecomb.swagger.invocation.SwaggerInvocation)1 ArgumentsMapper (org.apache.servicecomb.swagger.invocation.arguments.ArgumentsMapper)1