Search in sources :

Example 81 with Invocation

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));
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 82 with Invocation

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);
}
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 83 with Invocation

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;
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) InvocationRuntimeType(org.apache.servicecomb.core.definition.InvocationRuntimeType) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta)

Example 84 with 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");
}
Also used : Expectations(mockit.Expectations) Invocation(org.apache.servicecomb.core.Invocation) Test(org.junit.Test)

Example 85 with Invocation

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);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Test(org.junit.Test)

Aggregations

Invocation (org.apache.servicecomb.core.Invocation)204 Test (org.junit.Test)125 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)52 ArrayList (java.util.ArrayList)39 HashMap (java.util.HashMap)37 Response (org.apache.servicecomb.swagger.invocation.Response)37 Before (org.junit.Before)29 MockUp (mockit.MockUp)26 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)23 Expectations (mockit.Expectations)20 InvocationFinishEvent (org.apache.servicecomb.core.event.InvocationFinishEvent)20 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)19 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)18 Transport (org.apache.servicecomb.core.Transport)18 RoutingContext (io.vertx.ext.web.RoutingContext)17 Map (java.util.Map)17 ServerAccessLogEvent (org.apache.servicecomb.core.event.ServerAccessLogEvent)17 List (java.util.List)16 Endpoint (org.apache.servicecomb.core.Endpoint)16 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)15