Search in sources :

Example 61 with Response

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

the class TestCompletableFutureConsumerResponseMapperFactory method completableFuture.

@Test
public void completableFuture() {
    Method consumerMethod = ReflectUtils.findMethod(this.getClass(), "consumer");
    Method swaggerMethod = ReflectUtils.findMethod(this.getClass(), "swagger");
    ConsumerResponseMapper mapper = factory.createResponseMapper(factorys, swaggerMethod.getGenericReturnType(), consumerMethod.getGenericReturnType());
    Response response = Response.ok(Arrays.asList("a", "b"));
    String[] arr = (String[]) mapper.mapResponse(response);
    Assert.assertThat(arr, Matchers.arrayContaining("a", "b"));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 62 with Response

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

the class TestCseResponseConsumerResponseMapperFactory method createResponseMapper.

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

Example 63 with Response

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

the class SwaggerProducerOperation method doInvoke.

public Response doInvoke(SwaggerInvocation invocation) {
    Response response = null;
    try {
        Object[] args = argumentsMapper.toProducerArgs(invocation);
        Object result = producerMethod.invoke(producerInstance, args);
        response = responseMapper.mapResponse(invocation.getStatus(), result);
    } catch (Throwable e) {
        response = processException(invocation, e);
    }
    return response;
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response)

Example 64 with Response

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

the class TestResponse method test.

@Test
public void test() {
    Response r = Response.create(200, "200", 2);
    Assert.assertEquals(200, r.getStatusCode());
    Assert.assertEquals(2, (int) r.getResult());
    Response r1 = r.build();
    Assert.assertEquals(r, r1);
    r = Response.create(300, "300", 3);
    Assert.assertEquals(300, r.getStatusCode());
    Assert.assertEquals("300", r.getReasonPhrase());
    Assert.assertEquals(3, ((InvocationException) r.getResult()).getErrorData());
    r = Response.createSuccess(Status.OK, 2);
    Assert.assertEquals(200, r.getStatusCode());
    Assert.assertEquals(2, (int) r.getResult());
    r = Response.success(2, Status.OK);
    Assert.assertEquals(200, r.getStatusCode());
    Assert.assertEquals(2, (int) r.getResult());
    r = Response.createFail(InvocationType.CONSUMER, "abc");
    Assert.assertEquals("CommonExceptionData [message=abc]", ((InvocationException) r.getResult()).getErrorData().toString());
    Assert.assertEquals(490, r.getStatusCode());
    r = Response.createFail(InvocationType.PRODUCER, "def");
    Assert.assertEquals("CommonExceptionData [message=def]", ((InvocationException) r.getResult()).getErrorData().toString());
    Assert.assertEquals(590, r.getStatusCode());
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Test(org.junit.Test)

Example 65 with Response

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

the class TestExceptionFactory method convertExceptionToResponse.

@Test
public void convertExceptionToResponse() {
    Error error = new Error("test");
    Response response = ExceptionFactory.convertExceptionToResponse(null, error);
    Assert.assertSame(Status.OK, response.getStatus());
    Assert.assertEquals("response from error: test", response.getResult());
}
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