use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class InvocationContextItemTest method serverGetFormattedItemOnInvocationContextValueNotFound.
@Test
public void serverGetFormattedItemOnInvocationContextValueNotFound() {
Map<String, Object> routingContextData = new HashMap<>();
Invocation invocation = Mockito.mock(Invocation.class);
when(routingContext.data()).thenReturn(routingContextData);
routingContextData.put(RestConst.REST_INVOCATION_CONTEXT, invocation);
when(invocation.getContext(INVOCATION_CONTEXT_KEY)).thenReturn(null);
ITEM.appendServerFormattedItem(accessLogEvent, strBuilder);
Assert.assertThat(strBuilder.toString(), Matchers.is(InvocationContextAccessItem.NOT_FOUND));
}
use of org.apache.servicecomb.core.Invocation 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.core.Invocation in project java-chassis by ServiceComb.
the class TestSchemaMetaCodec method mockInvocation.
private Invocation mockInvocation(String operation, InvocationType invocationType) {
OperationMeta operationMeta;
boolean isConsumer;
Invocation invocation = Mockito.mock(Invocation.class);
InvocationRuntimeType invocationRuntimeType;
if (InvocationType.CONSUMER == invocationType) {
operationMeta = consumerSchemaMeta.getOperations().get(operation);
isConsumer = true;
Mockito.when(invocation.getSchemaMeta()).thenReturn(consumerSchemaMeta);
invocationRuntimeType = operationMeta.buildBaseConsumerRuntimeType();
} else {
operationMeta = providerSchemaMeta.getOperations().get(operation);
isConsumer = false;
Mockito.when(invocation.getSchemaMeta()).thenReturn(providerSchemaMeta);
invocationRuntimeType = operationMeta.buildBaseProviderRuntimeType();
}
MicroserviceMeta microserviceMeta = operationMeta.getMicroserviceMeta();
Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
Mockito.when(invocation.getInvocationRuntimeType()).thenReturn(invocationRuntimeType);
Mockito.when(invocation.findResponseType(200)).thenReturn(invocationRuntimeType.findResponseType(200));
Mockito.when(invocation.getInvocationType()).thenReturn(invocationType);
Mockito.when(invocation.getMicroserviceMeta()).thenReturn(microserviceMeta);
Mockito.when(invocation.isConsumer()).thenReturn(isConsumer);
return invocation;
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class RestProducerInvocationCreatorTest method should_merge_invocation_context_from_request.
@Test
public void should_merge_invocation_context_from_request() {
mockGetServicePathManager();
new Expectations() {
{
requestEx.getHeader(Const.CSE_CONTEXT);
result = "{\"k\":\"v\"}";
}
};
Invocation invocation = creator.createAsync().join();
assertThat(invocation.getContext("k")).isEqualTo("v");
}
use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.
the class RestProducerInvocationCreatorTest method should_save_requestEx_in_invocation_context.
@Test
public void should_save_requestEx_in_invocation_context() {
mockGetServicePathManager();
Invocation invocation = creator.createAsync().join();
Object request = invocation.getLocalContext(RestConst.REST_REQUEST);
assertThat(request).isSameAs(requestEx);
}
Aggregations