use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
the class TestLocator method testLocateDynamic.
@Test
public void testLocateDynamic() {
MicroserviceMeta msm = new MicroserviceMeta("app:ms");
ServicePathManager spm = new ServicePathManager(msm);
RestOperationMeta rom = createRestOperatonMeta("GET", "abc/{id}");
spm.addResource(rom);
try {
spm.locateOperation("abc/10", "PUT");
} catch (InvocationException e) {
Assert.assertEquals("Method Not Allowed", ((CommonExceptionData) e.getErrorData()).getMessage());
}
OperationLocator locator = spm.locateOperation("abc/10", "GET");
Assert.assertEquals("10", locator.getPathVarMap().get("id"));
}
use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
the class TestProducerSchemaFactory method testGetOrCreateProducer.
@Test
public void testGetOrCreateProducer() throws Exception {
OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
Assert.assertEquals("add", operationMeta.getOperationId());
ProducerOperation 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 });
Holder<Response> holder = new Holder<>();
producerOperation.invoke(invocation, resp -> {
holder.value = resp;
});
Assert.assertEquals(3, (int) holder.value.getResult());
invocation = new Invocation((Endpoint) null, operationMeta, 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 io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
the class TestInvokerUtils method testInvokeWithException.
@Test
public void testInvokeWithException() {
new MockUp<SyncResponseExecutor>() {
@Mock
public Response waitResponse() throws InterruptedException {
return Mockito.mock(Response.class);
}
};
Invocation invocation = Mockito.mock(Invocation.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
Mockito.when(operationMeta.isSync()).thenReturn(true);
try {
InvokerUtils.invoke(invocation);
} catch (InvocationException e) {
Assert.assertEquals(490, e.getStatusCode());
}
}
use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
the class TestBizkeeperCommand method testGetCacheKeyConsumer.
@Test
public void testGetCacheKeyConsumer() {
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 ConsumerBizkeeperCommand("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(490);
Assert.assertEquals(true, bizkeeperCommand.isFailedResponse(resp));
}
use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.
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