Search in sources :

Example 26 with Response

use of org.apache.servicecomb.swagger.invocation.Response 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 27 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class HighwayCodec method decodeResponse.

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

Example 28 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class TestResponseMapperFactorys method createResponseMapper_cseResponse.

@Test
public void createResponseMapper_cseResponse() {
    ConsumerResponseMapper mapper = consumerResponseMapperFactorys.createResponseMapper(String.class, Response.class);
    Response response = Response.ok(null);
    Object result = mapper.mapResponse(response);
    Assert.assertSame(response, result);
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) DefaultConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.DefaultConsumerResponseMapper) Test(org.junit.Test)

Example 29 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class TestCompletableFutureProducerResponseMapperFactory method completableFuture.

@Test
public void completableFuture() {
    Method producerMethod = ReflectUtils.findMethod(this.getClass(), "producer");
    Method swaggerMethod = ReflectUtils.findMethod(this.getClass(), "swagger");
    ProducerResponseMapper mapper = factory.createResponseMapper(factorys, swaggerMethod.getGenericReturnType(), producerMethod.getGenericReturnType());
    Response response = mapper.mapResponse(Status.OK, new String[] { "a", "b" });
    Assert.assertThat(response.getResult(), Matchers.contains("a", "b"));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 30 with Response

use of org.apache.servicecomb.swagger.invocation.Response in project incubator-servicecomb-java-chassis by apache.

the class TestCseResponseProducerResponseMapperFactory method createResponseMapper.

@Test
public void createResponseMapper() {
    ProducerResponseMapper mapper = factory.createResponseMapper(null, null, Response.class);
    Response response = Response.ok(null);
    Assert.assertSame(response, mapper.mapResponse(Status.OK, response));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Test(org.junit.Test)

Aggregations

Response (org.apache.servicecomb.swagger.invocation.Response)65 Test (org.junit.Test)44 Invocation (org.apache.servicecomb.core.Invocation)17 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)14 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)11 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)8 Expectations (mockit.Expectations)7 ResponseEntity (org.springframework.http.ResponseEntity)7 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)6 MockUp (mockit.MockUp)6 Method (java.lang.reflect.Method)5 Headers (org.apache.servicecomb.swagger.invocation.response.Headers)5 Holder (javax.xml.ws.Holder)4 User (org.apache.servicecomb.demo.server.User)4 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)4 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ApiResponse (io.swagger.annotations.ApiResponse)3 List (java.util.List)3 ResponseHeaders (org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders)3