Search in sources :

Example 1 with WrapSchema

use of io.servicecomb.codec.protobuf.utils.WrapSchema in project java-chassis by ServiceComb.

the class TestHighwayCodec method testEncodeResponse.

@Test
public void testEncodeResponse() {
    boolean status = true;
    WrapSchema bodySchema = Mockito.mock(WrapSchema.class);
    try {
        commonMock();
        HighwayCodec.encodeResponse(23432142, null, bodySchema, new Object());
    } catch (Exception e) {
        status = false;
    }
    Assert.assertTrue(status);
}
Also used : WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) NotWrapSchema(io.servicecomb.codec.protobuf.utils.schema.NotWrapSchema) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with WrapSchema

use of io.servicecomb.codec.protobuf.utils.WrapSchema in project java-chassis by ServiceComb.

the class HighwayCodec method decodeResponse.

public static Response decodeResponse(Invocation invocation, OperationProtobuf operationProtobuf, TcpData tcpData) throws Exception {
    ResponseHeader header = ResponseHeader.readObject(tcpData.getHeaderBuffer());
    invocation.getContext().putAll(header.getContext());
    WrapSchema bodySchema = operationProtobuf.findResponseSchema(header.getStatusCode());
    Object body = bodySchema.readObject(tcpData.getBodyBuffer());
    Response response = Response.create(header.getStatusCode(), header.getReasonPhrase(), body);
    response.setHeaders(header.getHeaders());
    return response;
}
Also used : Response(io.servicecomb.core.Response) ResponseHeader(io.servicecomb.transport.highway.message.ResponseHeader) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema)

Example 3 with WrapSchema

use of io.servicecomb.codec.protobuf.utils.WrapSchema in project java-chassis by ServiceComb.

the class TestGrpcCodec method testDecodeResponse.

@Test
public void testDecodeResponse() {
    boolean status = false;
    try {
        RoutingContext routingContext = Mockito.mock(RoutingContext.class);
        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);
        Buffer bodyBuffer = Mockito.mock(Buffer.class);
        Mockito.when(routingContext.getBody()).thenReturn(bodyBuffer);
        HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
        Mockito.when(routingContext.request()).thenReturn(request);
        Mockito.when(request.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"name\":\"test\"}");
        Invocation invocation = Mockito.mock(Invocation.class);
        Mockito.when(operationProtobuf.getResponseSchema()).thenReturn(Mockito.mock(WrapSchema.class));
        HttpClientResponse httpResponse = Mockito.mock(HttpClientResponse.class);
        Buffer buffer = Mockito.mock(Buffer.class);
        GrpcCodec.decodeResponse(invocation, operationProtobuf, httpResponse, buffer);
    } catch (Exception e) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) Invocation(io.servicecomb.core.Invocation) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) OperationMeta(io.servicecomb.core.definition.OperationMeta) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) Test(org.junit.Test)

Example 4 with WrapSchema

use of io.servicecomb.codec.protobuf.utils.WrapSchema in project java-chassis by ServiceComb.

the class TestGrpcCodec method testDecodeRequest.

@Test
public void testDecodeRequest() {
    boolean status = false;
    try {
        RoutingContext routingContext = Mockito.mock(RoutingContext.class);
        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);
        Buffer bodyBuffer = Mockito.mock(Buffer.class);
        Mockito.when(routingContext.getBody()).thenReturn(bodyBuffer);
        HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
        Mockito.when(routingContext.request()).thenReturn(request);
        SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
        Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
        Mockito.when(request.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"name\":\"test\"}");
        GrpcCodec.setGrpcTransport(new GrpcTransport());
        GrpcCodec.decodeRequest(routingContext, operationMeta);
    } catch (Exception e) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) OperationProtobuf(io.servicecomb.codec.protobuf.definition.OperationProtobuf) HttpServerRequest(io.vertx.core.http.HttpServerRequest) SchemaMeta(io.servicecomb.core.definition.SchemaMeta) OperationMeta(io.servicecomb.core.definition.OperationMeta) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) Test(org.junit.Test)

Example 5 with WrapSchema

use of io.servicecomb.codec.protobuf.utils.WrapSchema in project java-chassis by ServiceComb.

the class HighwayServerInvoke method sendResponse.

private void sendResponse(Map<String, String> context, Response response) {
    ResponseHeader header = new ResponseHeader();
    header.setStatusCode(response.getStatusCode());
    header.setReasonPhrase(response.getReasonPhrase());
    header.setContext(context);
    header.setHeaders(response.getHeaders());
    WrapSchema bodySchema = operationProtobuf.findResponseSchema(response.getStatusCode());
    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }
    try {
        Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body);
        netSocket.write(respBuffer);
    } catch (Exception e) {
        // 没招了,直接打日志
        String msg = String.format("encode response failed, %s, msgId=%d", operationProtobuf.getOperationMeta().getMicroserviceQualifiedName(), msgId);
        LOGGER.error(msg, e);
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ResponseHeader(io.servicecomb.transport.highway.message.ResponseHeader) WrapSchema(io.servicecomb.codec.protobuf.utils.WrapSchema) InvocationException(io.servicecomb.core.exception.InvocationException)

Aggregations

WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)11 Test (org.junit.Test)7 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)6 OperationMeta (io.servicecomb.core.definition.OperationMeta)6 Invocation (io.servicecomb.core.Invocation)5 Buffer (io.vertx.core.buffer.Buffer)4 HttpServerRequest (io.vertx.core.http.HttpServerRequest)4 Response (io.servicecomb.core.Response)3 SchemaMeta (io.servicecomb.core.definition.SchemaMeta)3 HttpClientResponse (io.vertx.core.http.HttpClientResponse)3 Endpoint (io.servicecomb.core.Endpoint)2 ResponseHeader (io.servicecomb.transport.highway.message.ResponseHeader)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 ArgsNotWrapSchema (io.servicecomb.codec.protobuf.utils.schema.ArgsNotWrapSchema)1 NormalWrapSchema (io.servicecomb.codec.protobuf.utils.schema.NormalWrapSchema)1 NotWrapSchema (io.servicecomb.codec.protobuf.utils.schema.NotWrapSchema)1 AsyncResponse (io.servicecomb.core.AsyncResponse)1 InvocationException (io.servicecomb.core.exception.InvocationException)1 UnitTestMeta (io.servicecomb.core.unittest.UnitTestMeta)1 IpPort (io.servicecomb.foundation.common.net.IpPort)1