Search in sources :

Example 16 with AsyncResponse

use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.

the class TestInvokerUtils method testReactiveInvoke.

@Test
public void testReactiveInvoke() {
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    boolean validAssert;
    try {
        InvokerUtils.reactiveInvoke(invocation, asyncResp);
        validAssert = true;
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Invocation(io.servicecomb.core.Invocation) AsyncResponse(io.servicecomb.core.AsyncResponse) InvocationException(io.servicecomb.core.exception.InvocationException) Test(org.junit.Test)

Example 17 with AsyncResponse

use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.

the class VertxHttpMethod method handleResponse.

protected void handleResponse(Invocation invocation, HttpClientResponse httpResponse, RestOperationMeta restOperation, AsyncResponse asyncResp) {
    ProduceProcessor produceProcessor = null;
    String contentType = httpResponse.getHeader("Content-Type");
    if (contentType != null) {
        String contentTypeForFind = contentType;
        int idx = contentType.indexOf(';');
        if (idx != -1) {
            contentTypeForFind = contentType.substring(0, idx);
        }
        produceProcessor = restOperation.findProduceProcessor(contentTypeForFind);
    }
    if (produceProcessor == null) {
        String msg = String.format("path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", restOperation.getAbsolutePath(), httpResponse.statusCode(), httpResponse.statusMessage(), contentType);
        Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
        asyncResp.fail(invocation.getInvocationType(), exception);
        return;
    }
    ProduceProcessor finalProduceProcessor = produceProcessor;
    httpResponse.bodyHandler(responseBuf -> {
        invocation.getResponseExecutor().execute(() -> {
            try {
                ResponseMeta responseMeta = restOperation.getOperationMeta().findResponseMeta(httpResponse.statusCode());
                Object result = finalProduceProcessor.decodeResponse(responseBuf, responseMeta.getJavaType());
                Response response = Response.create(httpResponse.statusCode(), httpResponse.statusMessage(), result);
                for (String headerName : responseMeta.getHeaders().keySet()) {
                    List<String> headerValues = httpResponse.headers().getAll(headerName);
                    for (String headerValue : headerValues) {
                        response.getHeaders().addHeader(headerName, headerValue);
                    }
                }
                asyncResp.complete(response);
            } catch (Throwable e) {
                asyncResp.fail(invocation.getInvocationType(), e);
            }
        });
    });
}
Also used : AsyncResponse(io.servicecomb.core.AsyncResponse) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Response(io.servicecomb.core.Response) ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData)

Example 18 with AsyncResponse

use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.

the class TestVertxGetMethod method testVertxGetMethod.

@Test
public void testVertxGetMethod() {
    HttpClient client = Mockito.mock(HttpClient.class);
    Invocation invocation = Mockito.mock(Invocation.class);
    IpPort ipPort = Mockito.mock(IpPort.class);
    Mockito.when(ipPort.getPort()).thenReturn(10);
    assertEquals(10, ipPort.getPort());
    Mockito.when(ipPort.getHostOrIp()).thenReturn("ever");
    assertEquals("ever", ipPort.getHostOrIp());
    RestOperationMeta operation = Mockito.mock(RestOperationMeta.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    HttpClientRequest obj = VertxGetMethod.INSTANCE.createRequest(client, invocation, ipPort, "good", operation, asyncResp);
    Assert.assertNull(obj);
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) Invocation(io.servicecomb.core.Invocation) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HttpClient(io.vertx.core.http.HttpClient) IpPort(io.servicecomb.foundation.common.net.IpPort) AsyncResponse(io.servicecomb.core.AsyncResponse) Test(org.junit.Test)

Example 19 with AsyncResponse

use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.

the class TestHighwayTransport method testSendException.

@Test
public void testSendException() throws Exception {
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    commonHighwayMock(invocation);
    Holder<Boolean> sended = new Holder<Boolean>(false);
    new MockUp<HighwayClient>() {

        @Mock
        public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
            sended.value = true;
        }
    };
    transport.send(invocation, asyncResp);
    Assert.assertTrue(sended.value);
}
Also used : Invocation(io.servicecomb.core.Invocation) Holder(javax.xml.ws.Holder) MockUp(mockit.MockUp) AsyncResponse(io.servicecomb.core.AsyncResponse) Test(org.junit.Test)

Example 20 with AsyncResponse

use of io.servicecomb.core.AsyncResponse in project java-chassis by ServiceComb.

the class TestVertxHttpMethod method testHandleResponse.

@Test
public void testHandleResponse() {
    boolean status = false;
    try {
        Invocation invocation = Mockito.mock(Invocation.class);
        AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
        HttpClientResponse httpResponse = Mockito.mock(HttpClientResponse.class);
        OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
        RestOperationMeta swaggerRestOperation = Mockito.mock(RestOperationMeta.class);
        Endpoint endpoint = Mockito.mock(Endpoint.class);
        Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
        URLPathBuilder urlPathBuilder = Mockito.mock(URLPathBuilder.class);
        Mockito.when(swaggerRestOperation.getPathBuilder()).thenReturn(urlPathBuilder);
        operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
        Mockito.when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
        Mockito.when(invocation.getEndpoint()).thenReturn(endpoint);
        String contentType = httpResponse.getHeader("Content-Type");
        ProduceProcessor produceProcessor = Mockito.mock(ProduceProcessor.class);
        Mockito.when(swaggerRestOperation.findProduceProcessor(contentType)).thenReturn(produceProcessor);
        this.handleResponse(invocation, httpResponse, swaggerRestOperation, asyncResp);
    } catch (Exception ex) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) Invocation(io.servicecomb.core.Invocation) Endpoint(io.servicecomb.core.Endpoint) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HttpClientResponse(io.vertx.core.http.HttpClientResponse) URLPathBuilder(io.servicecomb.common.rest.definition.path.URLPathBuilder) OperationMeta(io.servicecomb.core.definition.OperationMeta) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) AsyncResponse(io.servicecomb.core.AsyncResponse) Test(org.junit.Test)

Aggregations

AsyncResponse (io.servicecomb.core.AsyncResponse)22 Invocation (io.servicecomb.core.Invocation)18 Test (org.junit.Test)16 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)6 Response (io.servicecomb.core.Response)6 IpPort (io.servicecomb.foundation.common.net.IpPort)6 Endpoint (io.servicecomb.core.Endpoint)5 OperationMeta (io.servicecomb.core.definition.OperationMeta)5 HttpClientRequest (io.vertx.core.http.HttpClientRequest)5 MockUp (mockit.MockUp)5 HttpClient (io.vertx.core.http.HttpClient)4 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)3 HttpClientWithContext (io.servicecomb.foundation.vertx.client.http.HttpClientWithContext)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)2 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)2 URLPathBuilder (io.servicecomb.common.rest.definition.path.URLPathBuilder)2 InvocationException (io.servicecomb.core.exception.InvocationException)2 SyncResponseExecutor (io.servicecomb.core.provider.consumer.SyncResponseExecutor)2 URIEndpointObject (io.servicecomb.foundation.common.net.URIEndpointObject)2