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());
}
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);
});
}
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());
}
}
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());
}
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()));
}
Aggregations