Search in sources :

Example 1 with TcpData

use of org.apache.servicecomb.foundation.vertx.client.tcp.TcpData in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayCodec method testDecodeResponse.

@Test
public void testDecodeResponse() throws Exception {
    Invocation invocation = Mockito.mock(Invocation.class);
    Mockito.when(operationProtobuf.findResponseSchema(200)).thenReturn(Mockito.mock(WrapSchema.class));
    Map<String, String> context = new HashMap<>();
    Mockito.when(invocation.getContext()).thenReturn(context);
    TcpData tcpData = Mockito.mock(TcpData.class);
    Mockito.when(tcpData.getHeaderBuffer()).thenReturn(bodyBuffer);
    commonMock();
    ResponseHeader header = new ResponseHeader();
    header.setStatusCode(200);
    header.setContext(new HashMap<>());
    header.getContext().put("a", "10");
    Buffer responseBuf = HighwayCodec.encodeResponse(0, header, null, null, new ProtobufFeature());
    TcpData tcp = new TcpData(responseBuf.slice(23, responseBuf.length()), null);
    Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, tcp, null);
    Assert.assertEquals("10", invocation.getContext().get("a"));
    Assert.assertEquals(200, response.getStatusCode());
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(io.vertx.core.buffer.Buffer) Response(org.apache.servicecomb.swagger.invocation.Response) ResponseHeader(org.apache.servicecomb.transport.highway.message.ResponseHeader) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) NotWrapSchema(org.apache.servicecomb.codec.protobuf.utils.schema.NotWrapSchema) WrapSchema(org.apache.servicecomb.codec.protobuf.utils.WrapSchema) TcpData(org.apache.servicecomb.foundation.vertx.client.tcp.TcpData) ProtobufFeature(io.protostuff.runtime.ProtobufFeature) Test(org.junit.Test)

Example 2 with TcpData

use of org.apache.servicecomb.foundation.vertx.client.tcp.TcpData in project incubator-servicecomb-java-chassis by apache.

the class TestHighwayClient method doTestSend.

private Object doTestSend(Vertx vertx, HighwayClientConnectionPool pool, HighwayClientConnection tcpClient, Object decodedResponse) throws Exception {
    new MockUp<VertxUtils>() {

        @Mock
        <VERTICLE extends AbstractVerticle> boolean blockDeploy(Vertx vertx, Class<VERTICLE> cls, DeploymentOptions options) throws InterruptedException {
            return true;
        }
    };
    new MockUp<ClientPoolManager<HighwayClientConnectionPool>>() {

        @Mock
        public HighwayClientConnectionPool findClientPool(boolean sync) {
            return pool;
        }
    };
    new MockUp<ProtobufManager>() {

        @Mock
        public OperationProtobuf getOrCreateOperation(OperationMeta operationMeta) throws Exception {
            return operationProtobuf;
        }
    };
    new MockUp<HighwayClientConnectionPool>() {

        @Mock
        HighwayClientConnection findOrCreateClient(String endpoint) {
            return tcpClient;
        }
    };
    new MockUp<HighwayCodec>() {

        @Mock
        public Buffer encodeRequest(Invocation invocation, OperationProtobuf operationProtobuf, long msgId) throws Exception {
            return null;
        }

        @Mock
        Response decodeResponse(Invocation invocation, OperationProtobuf operationProtobuf, TcpData tcpData, ProtobufFeature protobufFeature) throws Throwable {
            if (Response.class.isInstance(decodedResponse)) {
                return (Response) decodedResponse;
            }
            throw (Throwable) decodedResponse;
        }
    };
    client.init(vertx);
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    Mockito.when(invocation.getEndpoint()).thenReturn(endpoint);
    Mockito.when(invocation.getEndpoint().getEndpoint()).thenReturn("endpoint");
    Mockito.when(invocation.getResponseExecutor()).thenReturn(new ReactiveExecutor());
    Holder<Object> result = new Holder<>();
    client.send(invocation, ar -> {
        result.value = ar.getResult();
    });
    return result.value;
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Holder(javax.xml.ws.Holder) ReactiveExecutor(org.apache.servicecomb.core.executor.ReactiveExecutor) MockUp(mockit.MockUp) Vertx(io.vertx.core.Vertx) AbstractVerticle(io.vertx.core.AbstractVerticle) TcpData(org.apache.servicecomb.foundation.vertx.client.tcp.TcpData) ProtobufFeature(io.protostuff.runtime.ProtobufFeature) Response(org.apache.servicecomb.swagger.invocation.Response) DeploymentOptions(io.vertx.core.DeploymentOptions) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf) BeforeClass(org.junit.BeforeClass) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta)

Aggregations

ProtobufFeature (io.protostuff.runtime.ProtobufFeature)2 Invocation (org.apache.servicecomb.core.Invocation)2 TcpData (org.apache.servicecomb.foundation.vertx.client.tcp.TcpData)2 Response (org.apache.servicecomb.swagger.invocation.Response)2 AbstractVerticle (io.vertx.core.AbstractVerticle)1 DeploymentOptions (io.vertx.core.DeploymentOptions)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Holder (javax.xml.ws.Holder)1 MockUp (mockit.MockUp)1 OperationProtobuf (org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)1 WrapSchema (org.apache.servicecomb.codec.protobuf.utils.WrapSchema)1 NotWrapSchema (org.apache.servicecomb.codec.protobuf.utils.schema.NotWrapSchema)1 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)1 ReactiveExecutor (org.apache.servicecomb.core.executor.ReactiveExecutor)1 ResponseHeader (org.apache.servicecomb.transport.highway.message.ResponseHeader)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1