Search in sources :

Example 1 with InvocationException

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

the class TestProducerSchemaFactory method testGetOrCreateProducer.

@Test
public void testGetOrCreateProducer() throws Exception {
    OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
    Assert.assertEquals("add", operationMeta.getOperationId());
    SwaggerProducerOperation producerOperation = operationMeta.getExtData(Const.PRODUCER_OPERATION);
    Object addBody = Class.forName("cse.gen.app.ms.schema.addBody").newInstance();
    ReflectUtils.setField(addBody, "x", 1);
    ReflectUtils.setField(addBody, "y", 2);
    Invocation invocation = new Invocation((Endpoint) null, operationMeta, new Object[] { addBody }) {

        @Override
        public String getInvocationQualifiedName() {
            return "";
        }
    };
    Holder<Response> holder = new Holder<>();
    producerOperation.invoke(invocation, resp -> {
        holder.value = resp;
    });
    Assert.assertEquals(3, (int) holder.value.getResult());
    invocation.setSwaggerArguments(new Object[] { 1, 2 });
    producerOperation.invoke(invocation, resp -> {
        holder.value = resp;
    });
    Assert.assertEquals(true, holder.value.isFailed());
    InvocationException exception = (InvocationException) holder.value.getResult();
    CommonExceptionData data = (CommonExceptionData) exception.getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Holder(javax.xml.ws.Holder) SwaggerProducerOperation(org.apache.servicecomb.swagger.engine.SwaggerProducerOperation) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 2 with InvocationException

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

the class TestShutdownHookHandler method testShutdownHookHandlerReject.

@Test
public void testShutdownHookHandlerReject() throws Exception {
    Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", true);
    Holder<InvocationType> typeHolder = new Holder<>(InvocationType.PRODUCER);
    Invocation invocation = new MockUp<Invocation>() {

        @Mock
        public InvocationType getInvocationType() {
            return typeHolder.value;
        }
    }.getMockInstance();
    ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
    handler.handle(invocation, asyncResp -> {
        InvocationException e = asyncResp.getResult();
        Assert.assertEquals(((CommonExceptionData) e.getErrorData()).getMessage(), "shutting down in progress");
        Assert.assertEquals(e.getStatusCode(), 590);
    });
    typeHolder.value = InvocationType.CONSUMER;
    handler.handle(invocation, asyncResp -> {
        InvocationException e = asyncResp.getResult();
        Assert.assertEquals(((CommonExceptionData) e.getErrorData()).getMessage(), "shutting down in progress");
        Assert.assertEquals(e.getStatusCode(), 490);
    });
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Holder(javax.xml.ws.Holder) InvocationType(org.apache.servicecomb.swagger.invocation.InvocationType) Mock(mockit.Mock) Test(org.junit.Test)

Example 3 with InvocationException

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

the class TestInvokerUtils method testSyncInvokeInvocationWithException.

@Test
public void testSyncInvokeInvocationWithException() throws InterruptedException {
    Invocation invocation = Mockito.mock(Invocation.class);
    Response response = Mockito.mock(Response.class);
    new MockUp<SyncResponseExecutor>() {

        @Mock
        public Response waitResponse() throws InterruptedException {
            return Mockito.mock(Response.class);
        }
    };
    Mockito.when(response.isSuccessed()).thenReturn(true);
    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    Mockito.when(operationMeta.getMicroserviceQualifiedName()).thenReturn("test");
    try {
        InvokerUtils.syncInvoke(invocation);
    } catch (InvocationException e) {
        Assert.assertEquals(490, e.getStatusCode());
    }
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MockUp(mockit.MockUp) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 4 with InvocationException

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

the class TestResponse method testAr.

@Test
public void testAr() {
    ar.success(Status.ACCEPTED, 1);
    Assert.assertEquals(true, response.isSuccessed());
    Assert.assertEquals(false, response.isFailed());
    Assert.assertEquals(1, (int) response.getResult());
    Assert.assertEquals(Status.ACCEPTED.getStatusCode(), response.getStatusCode());
    Assert.assertEquals(Status.ACCEPTED.getReasonPhrase(), response.getReasonPhrase());
    Assert.assertEquals(Status.ACCEPTED, response.getStatus());
    ar.success(2);
    Assert.assertEquals(2, (int) response.getResult());
    Assert.assertEquals(Status.OK.getStatusCode(), response.getStatusCode());
    Response r = Response.succResp(3);
    ar.complete(r);
    Assert.assertEquals(r, response);
    ar.consumerFail(new Error("abc"));
    CommonExceptionData data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Bad Request", data.getMessage());
    Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
    ar.fail(InvocationType.CONSUMER, new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Bad Request", data.getMessage());
    Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
    InvocationException consumerException = new InvocationException(300, "abc", "def");
    ar.consumerFail(consumerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(300, response.getStatusCode());
    ar.fail(InvocationType.CONSUMER, consumerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(300, response.getStatusCode());
    ar.producerFail(new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
    Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
    ar.fail(InvocationType.PRODUCER, new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
    Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
    InvocationException producerException = new InvocationException(500, "abc", "def");
    ar.producerFail(producerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(500, response.getStatusCode());
    ar.fail(InvocationType.PRODUCER, producerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(500, response.getStatusCode());
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) 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 5 with InvocationException

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

the class TestException method testInvocationException.

@Test
public void testInvocationException() {
    InvocationException oExceptionIn = new InvocationException(Status.OK, "I am gone now");
    oExceptionIn = ExceptionFactory.convertConsumerException(new Throwable());
    Assert.assertEquals(490, oExceptionIn.getStatusCode());
    oExceptionIn = ExceptionFactory.convertConsumerException(new Throwable(), "abc");
    Assert.assertEquals(490, oExceptionIn.getStatusCode());
    Assert.assertEquals("abc", ((CommonExceptionData) oExceptionIn.getErrorData()).getMessage());
    oExceptionIn = ExceptionFactory.convertProducerException(new Throwable());
    Assert.assertEquals(590, oExceptionIn.getStatusCode());
    oExceptionIn = ExceptionFactory.convertProducerException(new Throwable(), "abcd");
    Assert.assertEquals(590, oExceptionIn.getStatusCode());
    Assert.assertEquals("abcd", ((CommonExceptionData) oExceptionIn.getErrorData()).getMessage());
    oExceptionIn = ExceptionFactory.convertConsumerException(new InvocationException(Status.OK, new String("fake-object")));
    Assert.assertEquals(200, oExceptionIn.getStatusCode());
    oExceptionIn = ExceptionFactory.convertConsumerException(new InvocationTargetException(new Throwable()));
    Assert.assertNotEquals("java.lang.Throwable", oExceptionIn.getMessage());
    InvocationException oTemp = new InvocationException(Status.OK, new CommonExceptionData("testObject"));
    Assert.assertEquals("OK", oTemp.getReasonPhrase());
    Assert.assertEquals("CommonExceptionData [message=testObject]", (oTemp.getErrorData().toString()));
}
Also used : InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

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