Search in sources :

Example 1 with BufferImpl

use of io.vertx.core.buffer.impl.BufferImpl in project incubator-servicecomb-java-chassis by apache.

the class TestDefaultHttpClientFilter method extractResult_decodeError.

@Test
public void extractResult_decodeError(@Mocked Invocation invocation, @Mocked ReadStreamPart part, @Mocked OperationMeta operationMeta, @Mocked RestOperationMeta swaggerRestOperation, @Mocked HttpServletResponseEx responseEx) {
    Map<String, Object> handlerContext = new HashMap<>();
    new Expectations() {

        {
            invocation.getHandlerContext();
            result = handlerContext;
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.findResponseType(400);
            result = SimpleType.constructUnsafe(Date.class);
            operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
            result = swaggerRestOperation;
            responseEx.getStatus();
            result = 400;
            responseEx.getBodyBuffer();
            result = new BufferImpl().appendString("abc");
        }
    };
    new MockUp<DefaultHttpClientFilter>() {

        @Mock
        ProduceProcessor findProduceProcessor(RestOperationMeta restOperation, HttpServletResponseEx responseEx) {
            return new ProduceJsonProcessor();
        }
    };
    Response response = filter.extractResponse(invocation, responseEx);
    Assert.assertEquals(400, response.getStatusCode());
    Assert.assertEquals(InvocationException.class, response.<InvocationException>getResult().getClass());
    InvocationException invocationException = response.getResult();
    Assert.assertEquals("InvocationException: code=400;msg=CommonExceptionData [message=method null, path null, statusCode 400, reasonPhrase null, response content-type null is not supported]", invocationException.getMessage());
    Assert.assertEquals("Unrecognized token 'abc': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" + " at [Source: (org.apache.servicecomb.foundation.vertx.stream.BufferInputStream); line: 1, column: 4]", invocationException.getCause().getMessage());
    Assert.assertEquals(CommonExceptionData.class, invocationException.getErrorData().getClass());
    CommonExceptionData commonExceptionData = (CommonExceptionData) invocationException.getErrorData();
    Assert.assertEquals("method null, path null, statusCode 400, reasonPhrase null, response content-type null is not supported", commonExceptionData.getMessage());
}
Also used : Expectations(mockit.Expectations) ProduceJsonProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceJsonProcessor) HashMap(java.util.HashMap) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) MockUp(mockit.MockUp) BufferImpl(io.vertx.core.buffer.impl.BufferImpl) Date(java.util.Date) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) Test(org.junit.Test)

Example 2 with BufferImpl

use of io.vertx.core.buffer.impl.BufferImpl in project incubator-servicecomb-java-chassis by apache.

the class TestDefaultHttpClientFilter method extractResult_decodeError200.

@Test
public void extractResult_decodeError200(@Mocked Invocation invocation, @Mocked ReadStreamPart part, @Mocked OperationMeta operationMeta, @Mocked RestOperationMeta swaggerRestOperation, @Mocked HttpServletResponseEx responseEx) {
    Map<String, Object> handlerContext = new HashMap<>();
    new Expectations() {

        {
            invocation.getHandlerContext();
            result = handlerContext;
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.findResponseType(200);
            result = SimpleType.constructUnsafe(Date.class);
            operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
            result = swaggerRestOperation;
            responseEx.getStatus();
            result = 200;
            responseEx.getBodyBuffer();
            result = new BufferImpl().appendString("abc");
        }
    };
    new MockUp<DefaultHttpClientFilter>() {

        @Mock
        ProduceProcessor findProduceProcessor(RestOperationMeta restOperation, HttpServletResponseEx responseEx) {
            return new ProduceJsonProcessor();
        }
    };
    Response response = filter.extractResponse(invocation, responseEx);
    Assert.assertEquals(400, response.getStatusCode());
    Assert.assertEquals(InvocationException.class, response.<InvocationException>getResult().getClass());
    InvocationException invocationException = response.getResult();
    Assert.assertEquals("InvocationException: code=400;msg=CommonExceptionData [message=method null, path null, statusCode 200, reasonPhrase null, response content-type null is not supported]", invocationException.getMessage());
    Assert.assertEquals("Unrecognized token 'abc': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" + " at [Source: (org.apache.servicecomb.foundation.vertx.stream.BufferInputStream); line: 1, column: 4]", invocationException.getCause().getMessage());
    Assert.assertEquals(CommonExceptionData.class, invocationException.getErrorData().getClass());
    CommonExceptionData commonExceptionData = (CommonExceptionData) invocationException.getErrorData();
    Assert.assertEquals("method null, path null, statusCode 200, reasonPhrase null, response content-type null is not supported", commonExceptionData.getMessage());
}
Also used : Expectations(mockit.Expectations) ProduceJsonProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceJsonProcessor) HashMap(java.util.HashMap) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) MockUp(mockit.MockUp) BufferImpl(io.vertx.core.buffer.impl.BufferImpl) Date(java.util.Date) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) Test(org.junit.Test)

Example 3 with BufferImpl

use of io.vertx.core.buffer.impl.BufferImpl in project java-chassis by ServiceComb.

the class TestDefaultHttpClientFilter method extractResult_decodeError.

@Test
public void extractResult_decodeError(@Mocked Invocation invocation, @Mocked ReadStreamPart part, @Mocked OperationMeta operationMeta, @Mocked RestOperationMeta swaggerRestOperation, @Mocked HttpServletResponseEx responseEx) {
    Map<String, Object> handlerContext = new HashMap<>();
    new Expectations() {

        {
            invocation.getHandlerContext();
            result = handlerContext;
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.findResponseType(400);
            result = SimpleType.constructUnsafe(Date.class);
            operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
            result = swaggerRestOperation;
            responseEx.getStatus();
            result = 400;
            responseEx.getBodyBuffer();
            result = new BufferImpl().appendString("abc");
        }
    };
    new MockUp<DefaultHttpClientFilter>() {

        @Mock
        ProduceProcessor findProduceProcessor(RestOperationMeta restOperation, HttpServletResponseEx responseEx) {
            return new ProduceJsonProcessor();
        }
    };
    Response response = filter.extractResponse(invocation, responseEx);
    Assert.assertEquals(400, response.getStatusCode());
    Assert.assertEquals(InvocationException.class, response.<InvocationException>getResult().getClass());
    InvocationException invocationException = response.getResult();
    Assert.assertEquals("InvocationException: code=400;msg=CommonExceptionData [message=method null, path null, statusCode 400, reasonPhrase null, response content-type null is not supported]", invocationException.getMessage());
    Assert.assertEquals("Unrecognized token 'abc': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" + " at [Source: (org.apache.servicecomb.foundation.vertx.stream.BufferInputStream); line: 1, column: 4]", invocationException.getCause().getMessage());
    Assert.assertEquals(CommonExceptionData.class, invocationException.getErrorData().getClass());
    CommonExceptionData commonExceptionData = (CommonExceptionData) invocationException.getErrorData();
    Assert.assertEquals("method null, path null, statusCode 400, reasonPhrase null, response content-type null is not supported", commonExceptionData.getMessage());
}
Also used : Expectations(mockit.Expectations) ProduceJsonProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceJsonProcessor) HashMap(java.util.HashMap) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) MockUp(mockit.MockUp) BufferImpl(io.vertx.core.buffer.impl.BufferImpl) Date(java.util.Date) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) Test(org.junit.Test)

Example 4 with BufferImpl

use of io.vertx.core.buffer.impl.BufferImpl in project java-chassis by ServiceComb.

the class TestDefaultHttpClientFilter method extractResult_decodeError200.

@Test
public void extractResult_decodeError200(@Mocked Invocation invocation, @Mocked ReadStreamPart part, @Mocked OperationMeta operationMeta, @Mocked RestOperationMeta swaggerRestOperation, @Mocked HttpServletResponseEx responseEx) {
    Map<String, Object> handlerContext = new HashMap<>();
    new Expectations() {

        {
            invocation.getHandlerContext();
            result = handlerContext;
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.findResponseType(200);
            result = SimpleType.constructUnsafe(Date.class);
            operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
            result = swaggerRestOperation;
            responseEx.getStatus();
            result = 200;
            responseEx.getBodyBuffer();
            result = new BufferImpl().appendString("abc");
        }
    };
    new MockUp<DefaultHttpClientFilter>() {

        @Mock
        ProduceProcessor findProduceProcessor(RestOperationMeta restOperation, HttpServletResponseEx responseEx) {
            return new ProduceJsonProcessor();
        }
    };
    Response response = filter.extractResponse(invocation, responseEx);
    Assert.assertEquals(400, response.getStatusCode());
    Assert.assertEquals(InvocationException.class, response.<InvocationException>getResult().getClass());
    InvocationException invocationException = response.getResult();
    Assert.assertEquals("InvocationException: code=400;msg=CommonExceptionData [message=method null, path null, statusCode 200, reasonPhrase null, response content-type null is not supported]", invocationException.getMessage());
    Assert.assertEquals("Unrecognized token 'abc': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n" + " at [Source: (org.apache.servicecomb.foundation.vertx.stream.BufferInputStream); line: 1, column: 4]", invocationException.getCause().getMessage());
    Assert.assertEquals(CommonExceptionData.class, invocationException.getErrorData().getClass());
    CommonExceptionData commonExceptionData = (CommonExceptionData) invocationException.getErrorData();
    Assert.assertEquals("method null, path null, statusCode 200, reasonPhrase null, response content-type null is not supported", commonExceptionData.getMessage());
}
Also used : Expectations(mockit.Expectations) ProduceJsonProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceJsonProcessor) HashMap(java.util.HashMap) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) MockUp(mockit.MockUp) BufferImpl(io.vertx.core.buffer.impl.BufferImpl) Date(java.util.Date) Response(org.apache.servicecomb.swagger.invocation.Response) HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) Test(org.junit.Test)

Aggregations

BufferImpl (io.vertx.core.buffer.impl.BufferImpl)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Expectations (mockit.Expectations)4 MockUp (mockit.MockUp)4 ProduceJsonProcessor (org.apache.servicecomb.common.rest.codec.produce.ProduceJsonProcessor)4 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)4 HttpServletResponseEx (org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx)4 Response (org.apache.servicecomb.swagger.invocation.Response)4 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)4 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)4 Test (org.junit.Test)4