Search in sources :

Example 61 with Invocation

use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class TestAbstractRestInvocation method testDoSendResponseResultOKFilter.

@Test
public void testDoSendResponseResultOKFilter(@Mocked Response response) {
    MultiMap headers = MultiMap.caseInsensitiveMultiMap();
    headers.set("Content-Type", "application/json");
    new Expectations() {

        {
            response.getHeaders();
            result = headers;
            response.getStatusCode();
            result = 123;
            response.getReasonPhrase();
            result = "reason";
            response.getResult();
            result = "ok";
        }
    };
    Buffer buffer = Buffer.buffer();
    responseEx = new MockUp<HttpServletResponseEx>() {

        private Map<String, Object> attributes = new HashMap<>();

        @Mock
        public void setAttribute(String key, Object value) {
            this.attributes.put(key, value);
        }

        @Mock
        public Object getAttribute(String key) {
            return this.attributes.get(key);
        }

        @Mock
        void setBodyBuffer(Buffer bodyBuffer) {
            buffer.appendBuffer(bodyBuffer);
        }
    }.getMockInstance();
    HttpServerFilter filter = new HttpServerFilterBaseForTest() {

        @Override
        public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
            buffer.appendString("-filter");
        }
    };
    invocation.onStart(0);
    initRestInvocation();
    List<HttpServerFilter> httpServerFilters = SPIServiceUtils.loadSortedService(HttpServerFilter.class);
    httpServerFilters.add(filter);
    restInvocation.setHttpServerFilters(httpServerFilters);
    restInvocation.sendResponse(response);
    assertEquals("\"ok\"-filter", buffer.toString());
}
Also used : Expectations(mockit.Expectations) Buffer(io.vertx.core.buffer.Buffer) HttpServerFilter(org.apache.servicecomb.common.rest.filter.HttpServerFilter) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) MultiMap(io.vertx.core.MultiMap) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) MultiMap(io.vertx.core.MultiMap) Map(java.util.Map) HashMap(java.util.HashMap) HttpServerFilterBaseForTest(org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest) Test(org.junit.Test)

Example 62 with Invocation

use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class VertxRestInvocationTest method testCreateInvocation.

@Test
public void testCreateInvocation() {
    new MockUp<RestProducerInvocation>() {

        /**
         * Mock this method to avoid error
         */
        @Mock
        void createInvocation() {
        }
    };
    VertxRestInvocation vertxRestInvocation = new VertxRestInvocation();
    VertxServerRequestToHttpServletRequest requestEx = Mockito.mock(VertxServerRequestToHttpServletRequest.class);
    RoutingContext routingContext = Mockito.mock(RoutingContext.class);
    Invocation invocation = Mockito.mock(Invocation.class);
    Deencapsulation.setField(vertxRestInvocation, "requestEx", requestEx);
    Deencapsulation.setField(vertxRestInvocation, "invocation", invocation);
    Mockito.when(requestEx.getContext()).thenReturn(routingContext);
    Deencapsulation.invoke(vertxRestInvocation, "createInvocation");
    Mockito.verify(routingContext, Mockito.times(1)).put(RestConst.REST_INVOCATION_CONTEXT, invocation);
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) VertxServerRequestToHttpServletRequest(org.apache.servicecomb.foundation.vertx.http.VertxServerRequestToHttpServletRequest) Test(org.junit.Test)

Example 63 with Invocation

use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class TestHttpServerFilterBeforeSendResponseExecutor method runFail.

@Test
public void runFail() throws InterruptedException, ExecutionException {
    httpServerFilters.add(new HttpServerFilterBaseForTest() {

        @Override
        public CompletableFuture<Void> beforeSendResponseAsync(Invocation invocation, HttpServletResponseEx responseEx) {
            throw new RuntimeExceptionWithoutStackTrace();
        }
    });
    CompletableFuture<Void> result = executor.run();
    expectedException.expect(ExecutionException.class);
    expectedException.expectCause(Matchers.instanceOf(RuntimeExceptionWithoutStackTrace.class));
    result.get();
}
Also used : RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) CompletableFuture(java.util.concurrent.CompletableFuture) Invocation(org.apache.servicecomb.core.Invocation) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) Test(org.junit.Test)

Example 64 with Invocation

use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class EdgeInvocationCreator method createInstance.

@Override
protected Invocation createInstance() {
    ReferenceConfig referenceConfig = microserviceReferenceConfig.createReferenceConfig(restOperationMeta.getOperationMeta());
    Invocation invocation = InvocationFactory.forConsumer(referenceConfig, restOperationMeta.getOperationMeta(), restOperationMeta.getOperationMeta().buildBaseConsumerRuntimeType(), null);
    invocation.setSync(false);
    invocation.setEdge(true);
    invocation.addLocalContext(EDGE_INVOCATION_CONTEXT, Vertx.currentContext());
    return invocation;
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) MicroserviceReferenceConfig(org.apache.servicecomb.core.provider.consumer.MicroserviceReferenceConfig)

Example 65 with Invocation

use of org.apache.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class TestCustomCommandGroupKey method testOOM.

@Test
public void testOOM() {
    Invocation invocation1 = Mockito.mock(Invocation.class);
    Invocation invocation2 = Mockito.mock(Invocation.class);
    CustomizeCommandGroupKey customizeCommandGroupKey1 = (CustomizeCommandGroupKey) CustomizeCommandGroupKey.asKey("key", invocation1);
    CustomizeCommandGroupKey customizeCommandGroupKey2 = (CustomizeCommandGroupKey) CustomizeCommandGroupKey.asKey("key", invocation2);
    Assert.assertEquals(customizeCommandGroupKey1, customizeCommandGroupKey2);
}
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