Search in sources :

Example 56 with Invocation

use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class TestGrpcCodec method testDecodeResponseEx.

@Test
public void testDecodeResponseEx() {
    boolean status = false;
    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
    OperationProtobuf operationProtobuf = Mockito.mock(OperationProtobuf.class);
    WrapSchema schema = ProtobufSchemaUtils.getOrCreateSchema(int.class);
    Mockito.when(operationProtobuf.getRequestSchema()).thenReturn(schema);
    Mockito.when(operationMeta.getExtData("protobuf")).thenReturn(operationProtobuf);
    HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
    SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
    Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
    Mockito.when(request.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"name\":\"test\"}");
    Invocation invocation = Mockito.mock(Invocation.class);
    Response response = Mockito.mock(Response.class);
    Mockito.when(response.getResult()).thenReturn("test");
    Mockito.when(operationProtobuf.getResponseSchema()).thenReturn(Mockito.mock(WrapSchema.class));
    HttpClientResponse httpResponse = Mockito.mock(HttpClientResponse.class);
    Buffer buffer = Mockito.mock(Buffer.class);
    Mockito.when(httpResponse.statusCode()).thenReturn(200);
    try {
        GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, buffer);
    } catch (Exception e) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : HttpClientResponse(io.vertx.core.http.HttpClientResponse) Response(io.servicecomb.core.Response) Buffer(io.vertx.core.buffer.Buffer) Invocation(io.servicecomb.core.Invocation) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) HttpServerRequest(io.vertx.core.http.HttpServerRequest) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OperationMeta(io.servicecomb.core.definition.OperationMeta) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) Test(org.junit.Test)

Example 57 with Invocation

use of io.servicecomb.core.Invocation 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 58 with Invocation

use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class TestShutdownHookHandler method testShutdownHookHandlerCount.

@Test
public void testShutdownHookHandlerCount(@Mocked Response response) throws Exception {
    Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", false);
    ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
    Assert.assertEquals(0, handler.getActiveCount());
    // no reply
    Invocation invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
        }
    }.getMockInstance();
    handler.handle(invocation, asyncResp -> {
    });
    Assert.assertEquals(1, requestCounter.get());
    Assert.assertEquals(1, handler.getActiveCount());
    // normal
    invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
            asyncResp.handle(response);
        }
    }.getMockInstance();
    handler.handle(invocation, asyncResp -> {
    });
    Assert.assertEquals(2, requestCounter.get());
    Assert.assertEquals(1, handler.getActiveCount());
    // next exception
    invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
            throw new Error();
        }
    }.getMockInstance();
    try {
        handler.handle(invocation, asyncResp -> {
        });
        Assert.assertFalse(true);
    } catch (Throwable e) {
        Assert.assertEquals(3, requestCounter.get());
        Assert.assertEquals(1, handler.getActiveCount());
    }
// reply exception
// TODO: should be fixed
//        try {
//            handler.handle(invocation, asyncResp -> {
//                throw new Error();
//            });
//
//            Assert.assertFalse(true);
//        } catch (Throwable e) {
//            Assert.assertEquals(3, requestCounter.get());
//            Assert.assertEquals(1, handler.getActiveCount());
//        }
}
Also used : Invocation(io.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(io.servicecomb.core.AsyncResponse) Mock(mockit.Mock) InvocationException(io.servicecomb.core.exception.InvocationException) Test(org.junit.Test)

Example 59 with Invocation

use of io.servicecomb.core.Invocation 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 60 with Invocation

use of io.servicecomb.core.Invocation in project java-chassis by ServiceComb.

the class TestInvokerUtils method testReactiveInvoke.

@Test
public void testReactiveInvoke() {
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    boolean validAssert;
    try {
        InvokerUtils.reactiveInvoke(invocation, asyncResp);
        validAssert = true;
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Invocation(io.servicecomb.core.Invocation) AsyncResponse(io.servicecomb.core.AsyncResponse) InvocationException(io.servicecomb.core.exception.InvocationException) Test(org.junit.Test)

Aggregations

Invocation (io.servicecomb.core.Invocation)70 Test (org.junit.Test)55 OperationMeta (io.servicecomb.core.definition.OperationMeta)29 Response (io.servicecomb.core.Response)20 AsyncResponse (io.servicecomb.core.AsyncResponse)19 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)10 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)9 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)9 WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)9 MockUp (mockit.MockUp)9 InvocationException (io.servicecomb.core.exception.InvocationException)8 Endpoint (io.servicecomb.core.Endpoint)7 IpPort (io.servicecomb.foundation.common.net.IpPort)6 HttpClientRequest (io.vertx.core.http.HttpClientRequest)6 HttpClientResponse (io.vertx.core.http.HttpClientResponse)6 URLPathBuilder (io.servicecomb.common.rest.definition.path.URLPathBuilder)5 Buffer (io.vertx.core.buffer.Buffer)5 HttpClient (io.vertx.core.http.HttpClient)4 ArrayList (java.util.ArrayList)4 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)3