Search in sources :

Example 1 with HandlerResponse

use of org.apache.calcite.avatica.remote.Handler.HandlerResponse in project calcite-avatica by apache.

the class AbstractHandlerTest method testFailedResponseSerialization.

@Test
public void testFailedResponseSerialization() throws IOException {
    @SuppressWarnings("unchecked") final AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class);
    final Request request = Mockito.mock(Request.class);
    final Response response = Mockito.mock(Response.class);
    final IOException exception = new IOException();
    final ErrorResponse errorResponse = Mockito.mock(ErrorResponse.class);
    final String serializedErrorResponse = "An ErrorResponse";
    // Accept a serialized request
    Mockito.when(handler.apply(Mockito.anyString())).thenCallRealMethod();
    // Deserialize it back into a POJO
    Mockito.when(handler.decode(Mockito.anyString())).thenReturn(request);
    // Construct the Response for that Request
    Mockito.when(request.accept(Mockito.nullable(Service.class))).thenReturn(response);
    // Throw an IOException when serializing the Response.
    Mockito.when(handler.encode(response)).thenThrow(exception);
    Mockito.when(handler.convertToErrorResponse(exception)).thenCallRealMethod();
    // Convert the IOException into an ErrorResponse
    Mockito.when(handler.unwrapException(exception)).thenReturn(errorResponse);
    Mockito.when(handler.encode(errorResponse)).thenReturn(serializedErrorResponse);
    HandlerResponse<String> handlerResp = handler.apply("this is mocked out");
    assertEquals(500, handlerResp.getStatusCode());
    assertEquals(serializedErrorResponse, handlerResp.getResponse());
}
Also used : HandlerResponse(org.apache.calcite.avatica.remote.Handler.HandlerResponse) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) Response(org.apache.calcite.avatica.remote.Service.Response) Request(org.apache.calcite.avatica.remote.Service.Request) IOException(java.io.IOException) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 HandlerResponse (org.apache.calcite.avatica.remote.Handler.HandlerResponse)1 ErrorResponse (org.apache.calcite.avatica.remote.Service.ErrorResponse)1 Request (org.apache.calcite.avatica.remote.Service.Request)1 Response (org.apache.calcite.avatica.remote.Service.Response)1 Test (org.junit.Test)1