Search in sources :

Example 41 with Response

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

the class TestSpringmvcConsumerResponseMapperFactory method createResponseMapper.

@Test
public void createResponseMapper() {
    Method responseEntityMethod = ReflectUtils.findMethod(this.getClass(), "responseEntity");
    Method listMethod = ReflectUtils.findMethod(this.getClass(), "list");
    ConsumerResponseMapper mapper = factory.createResponseMapper(factorys, listMethod.getGenericReturnType(), responseEntityMethod.getGenericReturnType());
    Assert.assertThat(mapper, Matchers.instanceOf(SpringmvcConsumerResponseMapper.class));
    Response response = Response.ok(Arrays.asList("a", "b"));
    @SuppressWarnings("unchecked") ResponseEntity<String[]> responseEntity = (ResponseEntity<String[]>) mapper.mapResponse(response);
    Assert.assertThat(responseEntity.getBody(), Matchers.arrayContaining("a", "b"));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ResponseEntity(org.springframework.http.ResponseEntity) ConsumerResponseMapper(org.apache.servicecomb.swagger.invocation.response.consumer.ConsumerResponseMapper) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 42 with Response

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

the class TestSpringmvcProducerResponseMapper method mapResponse_withoutHeader_sucess.

@SuppressWarnings("unchecked")
@Test
public void mapResponse_withoutHeader_sucess() {
    ResponseEntity<String[]> responseEntity = new ResponseEntity<>(arrResult, org.springframework.http.HttpStatus.OK);
    Response response = mapper.mapResponse(null, responseEntity);
    Assert.assertThat((List<String>) response.getResult(), Matchers.contains("a", "b"));
    Assert.assertEquals(Status.OK, response.getStatus());
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ResponseEntity(org.springframework.http.ResponseEntity) Test(org.junit.Test)

Example 43 with Response

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

the class TestSpringmvcProducerResponseMapper method mapResponse_withHeader.

@Test
public void mapResponse_withHeader() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("h", "v");
    ResponseEntity<String[]> responseEntity = new ResponseEntity<>(arrResult, headers, org.springframework.http.HttpStatus.OK);
    Response response = mapper.mapResponse(null, responseEntity);
    List<Object> hv = response.getHeaders().getHeader("h");
    Assert.assertThat(hv, Matchers.contains("v"));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) Test(org.junit.Test)

Example 44 with Response

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

the class TestSpringmvcProducerResponseMapperFactory method createResponseMapper.

@Test
public void createResponseMapper() {
    Method responseEntityMethod = ReflectUtils.findMethod(this.getClass(), "responseEntity");
    Method listMethod = ReflectUtils.findMethod(this.getClass(), "list");
    ProducerResponseMapper mapper = factory.createResponseMapper(factorys, listMethod.getGenericReturnType(), responseEntityMethod.getGenericReturnType());
    Assert.assertThat(mapper, Matchers.instanceOf(SpringmvcProducerResponseMapper.class));
    ResponseEntity<String[]> responseEntity = new ResponseEntity<String[]>(new String[] { "a", "b" }, HttpStatus.OK);
    Response response = mapper.mapResponse(null, responseEntity);
    Assert.assertThat(response.getResult(), Matchers.contains("a", "b"));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ProducerResponseMapper(org.apache.servicecomb.swagger.invocation.response.producer.ProducerResponseMapper) ResponseEntity(org.springframework.http.ResponseEntity) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 45 with Response

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

the class JaxrsProducerResponseMapper method mapResponse.

@Override
public Response mapResponse(StatusType status, Object response) {
    javax.ws.rs.core.Response jaxrsResponse = (javax.ws.rs.core.Response) response;
    Response cseResponse = Response.status(jaxrsResponse.getStatusInfo()).entity(jaxrsResponse.getEntity());
    MultivaluedMap<String, Object> headers = jaxrsResponse.getHeaders();
    for (Entry<String, List<Object>> entry : headers.entrySet()) {
        if (entry.getValue() == null || entry.getValue().isEmpty()) {
            continue;
        }
        cseResponse.getHeaders().addHeader(entry.getKey(), entry.getValue());
    }
    return cseResponse;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) List(java.util.List)

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