use of org.apache.calcite.avatica.remote.Service.Response 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());
}
use of org.apache.calcite.avatica.remote.Service.Response in project calcite-avatica by apache.
the class ProtobufTranslationImplTest method parameters.
@Parameters
public static List<Object[]> parameters() {
List<Object[]> params = new ArrayList<>();
// The impl we're testing
ProtobufTranslationImpl translation = new ProtobufTranslationImpl();
// Identity transformation for Requests
RequestFunc requestFunc = new RequestFunc(translation);
// Identity transformation for Responses
ResponseFunc responseFunc = new ResponseFunc(translation);
List<Request> requests = getRequests();
List<Request> requestsWithNulls = getRequestsWithNulls();
List<Response> responses = getResponses();
// Requests
for (Request request : requests) {
params.add(new Object[] { request, requestFunc });
}
// Requests with nulls in parameters
for (Request request : requestsWithNulls) {
params.add(new Object[] { request, requestFunc });
}
// Responses
for (Response response : responses) {
params.add(new Object[] { response, responseFunc });
}
return params;
}
use of org.apache.calcite.avatica.remote.Service.Response in project calcite-avatica by apache.
the class AbstractHandlerTest method testExceptionUnwrappingWithoutContext.
@Test
public void testExceptionUnwrappingWithoutContext() {
@SuppressWarnings("unchecked") AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class);
Mockito.when(handler.unwrapException(Mockito.any(Exception.class))).thenCallRealMethod();
Exception e = new RuntimeException();
Response resp = handler.unwrapException(e);
assertTrue("Response should be ErrorResponse, but was " + resp.getClass(), resp instanceof ErrorResponse);
ErrorResponse errorResp = (ErrorResponse) resp;
assertEquals(ErrorResponse.UNKNOWN_ERROR_CODE, errorResp.errorCode);
assertEquals(AvaticaSeverity.UNKNOWN, errorResp.severity);
assertEquals(Arrays.asList(exceptionToString(e)), errorResp.exceptions);
e = new AvaticaRuntimeException();
resp = handler.unwrapException(e);
assertTrue("Response should be ErrorResponse, but was " + resp.getClass(), resp instanceof ErrorResponse);
errorResp = (ErrorResponse) resp;
assertEquals(ErrorResponse.UNKNOWN_ERROR_CODE, errorResp.errorCode);
assertEquals(AvaticaSeverity.UNKNOWN, errorResp.severity);
assertEquals(Arrays.asList(exceptionToString(e)), errorResp.exceptions);
}
use of org.apache.calcite.avatica.remote.Service.Response in project calcite-avatica by apache.
the class AbstractHandlerTest method testFailedErrorResponseSerialization.
@Test
public void testFailedErrorResponseSerialization() 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);
// 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.any(Service.class))).thenReturn(response);
// Throw an IOException when serializing the Response.
Mockito.when(handler.encode(response)).thenThrow(exception);
// Convert the IOException into an ErrorResponse
Mockito.when(handler.unwrapException(exception)).thenReturn(errorResponse);
// Fail to serialize the ErrorResponse
Mockito.when(handler.encode(errorResponse)).thenThrow(exception);
try {
handler.apply("this is mocked out");
} catch (RuntimeException e) {
assertEquals(exception, e.getCause());
}
}
Aggregations