use of org.apache.calcite.avatica.remote.Service.Response in project calcite-avatica by apache.
the class ProtobufTranslationImplTest method getResponses.
/**
* Generates a collection of Responses whose serialization will be tested.
*/
private static List<Response> getResponses() {
final RpcMetadataResponse rpcMetadata = new RpcMetadataResponse("localhost:8765");
LinkedList<Response> responses = new LinkedList<>();
// Nested classes (Signature, ColumnMetaData, CursorFactory, etc) are implicitly getting tested)
// Stub out the metadata for a row
ScalarType arrayComponentType = ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER);
ColumnMetaData arrayColumnMetaData = getArrayColumnMetaData(arrayComponentType, 2, "counts");
List<ColumnMetaData> columns = Arrays.asList(MetaImpl.columnMetaData("str", 0, String.class, true), MetaImpl.columnMetaData("count", 1, Integer.class, true), arrayColumnMetaData);
List<AvaticaParameter> params = Arrays.asList(new AvaticaParameter(false, 10, 0, Types.VARCHAR, "VARCHAR", String.class.getName(), "str"));
Meta.CursorFactory cursorFactory = Meta.CursorFactory.create(Style.LIST, Object.class, Arrays.asList("str", "count", "counts"));
// The row values
List<Object> rows = new ArrayList<>();
rows.add(new Object[] { "str_value1", 50, Arrays.asList(1, 2, 3) });
rows.add(new Object[] { "str_value2", 100, Arrays.asList(1) });
// Create the signature and frame using the metadata and values
Signature signature = Signature.create(columns, "sql", params, cursorFactory, Meta.StatementType.SELECT);
Frame frame = Frame.create(Integer.MAX_VALUE, true, rows);
// And then create a ResultSetResponse
ResultSetResponse results1 = new ResultSetResponse("connectionId", Integer.MAX_VALUE, true, signature, frame, Long.MAX_VALUE, rpcMetadata);
responses.add(results1);
responses.add(new CloseStatementResponse(rpcMetadata));
ConnectionPropertiesImpl connProps = new ConnectionPropertiesImpl(false, true, Integer.MAX_VALUE, "catalog", "schema");
responses.add(new ConnectionSyncResponse(connProps, rpcMetadata));
responses.add(new OpenConnectionResponse(rpcMetadata));
responses.add(new CloseConnectionResponse(rpcMetadata));
responses.add(new CreateStatementResponse("connectionId", Integer.MAX_VALUE, rpcMetadata));
Map<Meta.DatabaseProperty, Object> propertyMap = new HashMap<>();
for (Meta.DatabaseProperty prop : Meta.DatabaseProperty.values()) {
propertyMap.put(prop, prop.defaultValue);
}
responses.add(new DatabasePropertyResponse(propertyMap, rpcMetadata));
responses.add(new ExecuteResponse(Arrays.asList(results1, results1, results1), false, rpcMetadata));
responses.add(new FetchResponse(frame, false, false, rpcMetadata));
responses.add(new FetchResponse(frame, true, true, rpcMetadata));
responses.add(new FetchResponse(frame, false, true, rpcMetadata));
responses.add(new PrepareResponse(new Meta.StatementHandle("connectionId", Integer.MAX_VALUE, signature), rpcMetadata));
StringWriter sw = new StringWriter();
new Exception().printStackTrace(new PrintWriter(sw));
responses.add(new ErrorResponse(Collections.singletonList(sw.toString()), "Test Error Message", ErrorResponse.UNKNOWN_ERROR_CODE, ErrorResponse.UNKNOWN_SQL_STATE, AvaticaSeverity.WARNING, rpcMetadata));
// No more results, statement not missing
responses.add(new SyncResultsResponse(false, false, rpcMetadata));
// Missing statement, no results
responses.add(new SyncResultsResponse(false, true, rpcMetadata));
// More results, no missing statement
responses.add(new SyncResultsResponse(true, false, rpcMetadata));
// Some tests to make sure ErrorResponse doesn't fail.
responses.add(new ErrorResponse((List<String>) null, null, 0, null, null, null));
responses.add(new ErrorResponse(Arrays.asList("stacktrace1", "stacktrace2"), null, 0, null, null, null));
responses.add(new CommitResponse());
responses.add(new RollbackResponse());
long[] updateCounts = new long[] { 1, 0, 1, 1 };
responses.add(new ExecuteBatchResponse("connectionId", 12345, updateCounts, false, rpcMetadata));
return responses;
}
use of org.apache.calcite.avatica.remote.Service.Response in project calcite-avatica by apache.
the class AbstractHandlerTest method testExceptionUnwrappingWithContext.
@Test
public void testExceptionUnwrappingWithContext() {
@SuppressWarnings("unchecked") AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class);
Mockito.when(handler.unwrapException(Mockito.any(Exception.class))).thenCallRealMethod();
final String msg = "Something failed!";
AvaticaRuntimeException e = new AvaticaRuntimeException(msg, ErrorResponse.UNKNOWN_ERROR_CODE, ErrorResponse.UNKNOWN_SQL_STATE, AvaticaSeverity.FATAL);
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.FATAL, errorResp.severity);
assertEquals(Arrays.asList(exceptionToString(e)), errorResp.exceptions);
assertEquals(msg, errorResp.errorMessage);
}
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 AbstractHandler method apply.
/**
* Compute a response for the given request, handling errors generated by that computation.
*
* @param serializedRequest The caller's request.
* @return A {@link Response} with additional context about that response.
*/
public HandlerResponse<T> apply(T serializedRequest) {
try {
final Service.Request request = decode(serializedRequest);
final Service.Response response = request.accept(service);
return new HandlerResponse<>(encode(response), HTTP_OK);
} catch (Exception e) {
return convertToErrorResponse(e);
}
}
use of org.apache.calcite.avatica.remote.Service.Response in project calcite-avatica by apache.
the class RemoteMetaTest method testMalformedRequest.
@Test
public void testMalformedRequest() throws Exception {
URL url = new URL("http://localhost:" + this.port);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
// Write some garbage data
wr.write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 });
wr.flush();
wr.close();
}
final int responseCode = conn.getResponseCode();
assertEquals(500, responseCode);
final InputStream inputStream = conn.getErrorStream();
byte[] responseBytes = AvaticaUtils.readFullyToBytes(inputStream);
ErrorResponse response;
switch(this.serialization) {
case JSON:
response = JsonService.MAPPER.readValue(responseBytes, ErrorResponse.class);
assertTrue("Unexpected error message: " + response.errorMessage, response.errorMessage.contains("Illegal character"));
break;
case PROTOBUF:
ProtobufTranslation pbTranslation = new ProtobufTranslationImpl();
Response genericResp = pbTranslation.parseResponse(responseBytes);
assertTrue("Response was not an ErrorResponse, but was " + genericResp.getClass(), genericResp instanceof ErrorResponse);
response = (ErrorResponse) genericResp;
assertTrue("Unexpected error message: " + response.errorMessage, response.errorMessage.contains("contained an invalid tag"));
break;
default:
fail("Unhandled serialization " + this.serialization);
throw new RuntimeException();
}
}
Aggregations