Search in sources :

Example 16 with InvocationException

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"));
}
Also used : RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) InvocationException(io.servicecomb.core.exception.InvocationException) MicroserviceMeta(io.servicecomb.core.definition.MicroserviceMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) Test(org.junit.Test)

Example 17 with InvocationException

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());
}
Also used : Response(io.servicecomb.core.Response) Invocation(io.servicecomb.core.Invocation) Endpoint(io.servicecomb.core.Endpoint) ProducerOperation(io.servicecomb.core.provider.producer.ProducerOperation) InvocationException(io.servicecomb.core.exception.InvocationException) Holder(javax.xml.ws.Holder) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 18 with InvocationException

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());
    }
}
Also used : Invocation(io.servicecomb.core.Invocation) InvocationException(io.servicecomb.core.exception.InvocationException) MockUp(mockit.MockUp) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 19 with InvocationException

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));
}
Also used : Response(io.servicecomb.core.Response) Invocation(io.servicecomb.core.Invocation) InvocationException(io.servicecomb.core.exception.InvocationException) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 20 with InvocationException

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()));
}
Also used : InvocationException(io.servicecomb.core.exception.InvocationException) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Aggregations

InvocationException (io.servicecomb.core.exception.InvocationException)20 Test (org.junit.Test)13 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)8 Invocation (io.servicecomb.core.Invocation)6 OperationMeta (io.servicecomb.core.definition.OperationMeta)6 Response (io.servicecomb.core.Response)4 RestParam (io.servicecomb.common.rest.definition.RestParam)3 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)3 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)2 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)2 ArrayList (java.util.ArrayList)2 Holder (javax.xml.ws.Holder)2 Expectations (mockit.Expectations)2 MockUp (mockit.MockUp)2 WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)1 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)1 OperationLocator (io.servicecomb.common.rest.locator.OperationLocator)1 ServicePathManager (io.servicecomb.common.rest.locator.ServicePathManager)1 AsyncResponse (io.servicecomb.core.AsyncResponse)1 Endpoint (io.servicecomb.core.Endpoint)1