Search in sources :

Example 6 with Response

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

the class TestResponse method testAr.

@Test
public void testAr() {
    ar.success(Status.ACCEPTED, 1);
    Assert.assertEquals(true, response.isSuccessed());
    Assert.assertEquals(false, response.isFailed());
    Assert.assertEquals(1, (int) response.getResult());
    Assert.assertEquals(Status.ACCEPTED.getStatusCode(), response.getStatusCode());
    Assert.assertEquals(Status.ACCEPTED.getReasonPhrase(), response.getReasonPhrase());
    Assert.assertEquals(Status.ACCEPTED, response.getStatus());
    ar.success(2);
    Assert.assertEquals(2, (int) response.getResult());
    Assert.assertEquals(Status.OK.getStatusCode(), response.getStatusCode());
    Response r = Response.succResp(3);
    ar.complete(r);
    Assert.assertEquals(r, response);
    ar.consumerFail(new Error("abc"));
    CommonExceptionData data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Bad Request", data.getMessage());
    Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
    ar.fail(InvocationType.CONSUMER, new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Bad Request", data.getMessage());
    Assert.assertEquals(ExceptionFactory.CONSUMER_INNER_STATUS_CODE, response.getStatusCode());
    InvocationException consumerException = new InvocationException(300, "abc", "def");
    ar.consumerFail(consumerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(300, response.getStatusCode());
    ar.fail(InvocationType.CONSUMER, consumerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(300, response.getStatusCode());
    ar.producerFail(new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
    Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
    ar.fail(InvocationType.PRODUCER, new Error("abc"));
    data = (CommonExceptionData) ((InvocationException) response.getResult()).getErrorData();
    Assert.assertEquals("Cse Internal Server Error", data.getMessage());
    Assert.assertEquals(ExceptionFactory.PRODUCER_INNER_STATUS_CODE, response.getStatusCode());
    InvocationException producerException = new InvocationException(500, "abc", "def");
    ar.producerFail(producerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(500, response.getStatusCode());
    ar.fail(InvocationType.PRODUCER, producerException);
    Assert.assertEquals("def", ((InvocationException) response.getResult()).getErrorData());
    Assert.assertEquals(500, response.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) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) Test(org.junit.Test)

Example 7 with Response

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

the class TestSwaggerProducerOperation method processException_normal.

@Test
public void processException_normal(@Mocked SwaggerInvocation invocation) {
    Error error = new Error("abc");
    Response response = swaggerProducerOperation.processException(invocation, error);
    Assert.assertSame(Status.OK, response.getStatus());
    Assert.assertEquals("response from error: abc", response.getResult());
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Test(org.junit.Test)

Example 8 with Response

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

the class TestSwaggerProducerOperation method processException_InvocationTargetException.

@Test
public void processException_InvocationTargetException(@Mocked SwaggerInvocation invocation) {
    Error error = new Error("abc");
    InvocationTargetException targetException = new InvocationTargetException(error);
    Response response = swaggerProducerOperation.processException(invocation, targetException);
    Assert.assertSame(Status.OK, response.getStatus());
    Assert.assertEquals("response from error: abc", response.getResult());
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 9 with Response

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

the class TestDefaultExceptionToResponseConverter method convert.

@Test
public void convert(@Mocked SwaggerInvocation swaggerInvocation, @Mocked Error e) {
    Response response = converter.convert(swaggerInvocation, e);
    Assert.assertSame(e, ((InvocationException) response.getResult()).getCause());
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Test(org.junit.Test)

Example 10 with Response

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

the class TestInvocationExceptionToResponseConverter method convert.

@Test
public void convert(@Mocked SwaggerInvocation swaggerInvocation, @Mocked InvocationException e) {
    Response response = converter.convert(swaggerInvocation, e);
    Assert.assertSame(e, 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