Search in sources :

Example 11 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class TestBizkeeperCommand method testGetCacheKeyProvider.

@Test
public void testGetCacheKeyProvider() {
    Invocation invocation = Mockito.mock(Invocation.class);
    Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
    Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");
    HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter().withRequestCacheEnabled(true).withRequestLogEnabled(false);
    BizkeeperCommand bizkeeperCommand = new ProviderBizkeeperCommand("groupname", invocation, HystrixObservableCommand.Setter.withGroupKey(CommandKey.toHystrixCommandGroupKey("groupname", invocation)).andCommandKey(CommandKey.toHystrixCommandKey("groupname", invocation)).andCommandPropertiesDefaults(setter));
    String str = bizkeeperCommand.getCacheKey();
    Assert.assertNull(str);
    Response resp = Mockito.mock(Response.class);
    Mockito.when(resp.isFailed()).thenReturn(false);
    Assert.assertEquals(false, bizkeeperCommand.isFailedResponse(resp));
    Mockito.when(resp.isFailed()).thenReturn(true);
    InvocationException excp = Mockito.mock(InvocationException.class);
    Mockito.when(resp.getResult()).thenReturn(excp);
    Mockito.when(excp.getStatusCode()).thenReturn(400);
    Assert.assertEquals(false, bizkeeperCommand.isFailedResponse(resp));
    Mockito.when(resp.getResult()).thenReturn(excp);
    Mockito.when(excp.getStatusCode()).thenReturn(590);
    Assert.assertEquals(true, bizkeeperCommand.isFailedResponse(resp));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 12 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class TestDefaultHttpClientFilter method testAfterReceiveResponseNullProduceProcessor.

@Test
public void testAfterReceiveResponseNullProduceProcessor(@Mocked Invocation invocation, @Mocked HttpServletResponseEx responseEx, @Mocked OperationMeta operationMeta, @Mocked RestOperationMeta swaggerRestOperation) throws Exception {
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
            result = swaggerRestOperation;
        }
    };
    Response response = filter.afterReceiveResponse(invocation, responseEx);
    InvocationException exception = response.getResult();
    CommonExceptionData data = (CommonExceptionData) exception.getErrorData();
    Assert.assertEquals("method null, path null, statusCode 0, reasonPhrase null, response content-type null is not supported", data.getMessage());
}
Also used : Expectations(mockit.Expectations) Response(org.apache.servicecomb.swagger.invocation.Response) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) Test(org.junit.Test)

Example 13 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayClient method testSend_success_decode_failed.

@Test
public void testSend_success_decode_failed(@Mocked Vertx vertx, @Mocked HighwayClientConnectionPool pool, @Mocked HighwayClientConnection tcpClient) throws Exception {
    new MockUp<HighwayClientConnection>() {

        @Mock
        void send(AbstractTcpClientPackage tcpClientPackage, TcpResponseCallback callback) {
            callback.success(null);
        }
    };
    Object result = doTestSend(vertx, pool, tcpClient, new InvocationException(Status.BAD_REQUEST, (Object) "failed"));
    Assert.assertEquals("failed", ((InvocationException) result).getErrorData());
}
Also used : TcpResponseCallback(org.apache.servicecomb.foundation.vertx.client.tcp.TcpResponseCallback) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MockUp(mockit.MockUp) AbstractTcpClientPackage(org.apache.servicecomb.foundation.vertx.client.tcp.AbstractTcpClientPackage) Test(org.junit.Test)

Example 14 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayClient method testSend_failed.

@Test
public void testSend_failed(@Mocked Vertx vertx, @Mocked HighwayClientConnectionPool pool, @Mocked HighwayClientConnection tcpClient) throws Exception {
    new MockUp<HighwayClientConnection>() {

        @Mock
        void send(AbstractTcpClientPackage tcpClientPackage, TcpResponseCallback callback) {
            callback.fail(new InvocationException(Status.BAD_REQUEST, (Object) "failed"));
        }
    };
    Object result = doTestSend(vertx, pool, tcpClient, null);
    Assert.assertEquals("failed", ((InvocationException) result).getErrorData());
}
Also used : TcpResponseCallback(org.apache.servicecomb.foundation.vertx.client.tcp.TcpResponseCallback) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MockUp(mockit.MockUp) AbstractTcpClientPackage(org.apache.servicecomb.foundation.vertx.client.tcp.AbstractTcpClientPackage) Test(org.junit.Test)

Example 15 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class HighwayServerInvoke method sendResponse.

private void sendResponse(Map<String, String> context, Response response) {
    ResponseHeader header = new ResponseHeader();
    header.setStatusCode(response.getStatusCode());
    header.setReasonPhrase(response.getReasonPhrase());
    header.setContext(context);
    header.setHeaders(response.getHeaders());
    WrapSchema bodySchema = operationProtobuf.findResponseSchema(response.getStatusCode());
    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }
    try {
        Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body, protobufFeature);
        connection.write(respBuffer.getByteBuf());
    } catch (Exception e) {
        // 没招了,直接打日志
        String msg = String.format("encode response failed, %s, msgId=%d", operationProtobuf.getOperationMeta().getMicroserviceQualifiedName(), msgId);
        LOGGER.error(msg, e);
    } finally {
        invocation.onFinish(response);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ResponseHeader(org.apache.servicecomb.transport.highway.message.ResponseHeader) WrapSchema(org.apache.servicecomb.codec.protobuf.utils.WrapSchema) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException)

Aggregations

InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)24 Test (org.junit.Test)16 Response (org.apache.servicecomb.swagger.invocation.Response)8 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)8 MockUp (mockit.MockUp)5 Invocation (org.apache.servicecomb.core.Invocation)5 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 Expectations (mockit.Expectations)3 RestParam (org.apache.servicecomb.common.rest.definition.RestParam)3 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)3 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)2 ArrayList (java.util.ArrayList)2 Holder (javax.xml.ws.Holder)2 AbstractTcpClientPackage (org.apache.servicecomb.foundation.vertx.client.tcp.AbstractTcpClientPackage)2 TcpResponseCallback (org.apache.servicecomb.foundation.vertx.client.tcp.TcpResponseCallback)2 Buffer (io.vertx.core.buffer.Buffer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Status (javax.ws.rs.core.Response.Status)1 Mock (mockit.Mock)1 WrapSchema (org.apache.servicecomb.codec.protobuf.utils.WrapSchema)1