use of org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer 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);
}
use of org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer 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);
}
use of org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer 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);
}
use of org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer in project java-chassis by ServiceComb.
the class TestHighwayTransport method commonHighwayMock.
private void commonHighwayMock(Invocation invocation) {
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
Endpoint lEndpoint = Mockito.mock(Endpoint.class);
Mockito.when(invocation.getEndpoint()).thenReturn(lEndpoint);
RequestRootSerializer lWrapSchema = Mockito.mock(RequestRootSerializer.class);
Mockito.when(operationProtobuf.getRequestRootSerializer()).thenReturn(lWrapSchema);
URIEndpointObject ep = Mockito.mock(URIEndpointObject.class);
Mockito.when(lEndpoint.getAddress()).thenReturn(ep);
Mockito.when(ep.getHostOrIp()).thenReturn("127.0.0.1");
Mockito.when(ep.getPort()).thenReturn(80);
}
use of org.apache.servicecomb.codec.protobuf.definition.RequestRootSerializer 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);
}
}
Aggregations