use of org.apache.calcite.avatica.remote.Service.RpcMetadataResponse 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.RpcMetadataResponse in project calcite-avatica by apache.
the class AvaticaClientRuntimeExceptionTest method testGetters.
@Test
public void testGetters() {
final String errorMsg = "My error message";
final int errorCode = 10;
final String sqlState = "abc12";
final AvaticaSeverity severity = AvaticaSeverity.ERROR;
final List<String> stacktraces = Arrays.asList("my stack trace");
final RpcMetadataResponse metadata = new RpcMetadataResponse("localhost:8765");
AvaticaClientRuntimeException e = new AvaticaClientRuntimeException(errorMsg, errorCode, sqlState, severity, stacktraces, metadata);
assertEquals(errorMsg, e.getMessage());
assertEquals(errorCode, e.getErrorCode());
assertEquals(severity, e.getSeverity());
assertEquals(stacktraces, e.getServerExceptions());
assertEquals(metadata, e.getRpcMetadata());
}
use of org.apache.calcite.avatica.remote.Service.RpcMetadataResponse in project calcite-avatica by apache.
the class ErrorResponseTest method testToClientRTE.
@Test
public void testToClientRTE() {
final String message = "There was an error";
final int code = 23;
final String state = "a1b2c";
final AvaticaSeverity severity = AvaticaSeverity.ERROR;
final List<String> exceptions = Arrays.asList("Server Stacktrace 1", "Server Stacktace 2");
final RpcMetadataResponse metadata = new RpcMetadataResponse("localhost:8765");
final ErrorResponse resp = new ErrorResponse(message, code, state, severity, exceptions, metadata);
AvaticaClientRuntimeException exception = resp.toException();
assertTrue("Expected error message to end with '" + resp.errorMessage + "', but was '" + exception.getMessage() + "'", exception.getMessage().endsWith(resp.errorMessage));
assertEquals(resp.errorCode, exception.getErrorCode());
assertEquals(resp.severity, exception.getSeverity());
assertEquals(resp.sqlState, exception.getSqlState());
assertEquals(resp.exceptions, exception.getServerExceptions());
assertEquals(resp.rpcMetadata, exception.getRpcMetadata());
}
use of org.apache.calcite.avatica.remote.Service.RpcMetadataResponse in project calcite-avatica by apache.
the class HttpServer method createRpcServerMetadata.
private RpcMetadataResponse createRpcServerMetadata(ServerConnector connector) throws UnknownHostException {
String host = connector.getHost();
if (null == host) {
// "null" means binding to all interfaces, we need to pick one so the client gets a real
// address and not "0.0.0.0" or similar.
host = InetAddress.getLocalHost().getHostName();
}
final int port = connector.getLocalPort();
return new RpcMetadataResponse(String.format(Locale.ROOT, "%s:%d", host, port));
}
use of org.apache.calcite.avatica.remote.Service.RpcMetadataResponse in project calcite-avatica by apache.
the class ProtobufHandlerTest method testFetch.
@Test
public void testFetch() throws Exception {
final String connectionId = "cnxn1";
final int statementId = 30;
final long offset = 10;
final int fetchMaxRowCount = 100;
final List<Common.TypedValue> values = new ArrayList<>();
values.add(Common.TypedValue.newBuilder().setType(Common.Rep.BOOLEAN).setBoolValue(true).build());
values.add(Common.TypedValue.newBuilder().setType(Common.Rep.STRING).setStringValue("my_string").build());
Requests.FetchRequest protoRequest = Requests.FetchRequest.newBuilder().setConnectionId(connectionId).setStatementId(statementId).setOffset(offset).setFetchMaxRowCount(fetchMaxRowCount).build();
byte[] serializedRequest = protoRequest.toByteArray();
FetchRequest request = new FetchRequest().deserialize(protoRequest);
List<Object> frameRows = new ArrayList<>();
frameRows.add(new Object[] { true, "my_string" });
Meta.Frame frame = Frame.create(0, true, frameRows);
RpcMetadataResponse metadata = new RpcMetadataResponse("localhost:8765");
FetchResponse response = new FetchResponse(frame, false, false, metadata);
when(translation.parseRequest(serializedRequest)).thenReturn(request);
when(service.apply(request)).thenReturn(response);
when(translation.serializeResponse(response)).thenReturn(response.serialize().toByteArray());
HandlerResponse<byte[]> handlerResponse = handler.apply(serializedRequest);
byte[] serializedResponse = handlerResponse.getResponse();
assertEquals(200, handlerResponse.getStatusCode());
Responses.FetchResponse protoResponse = Responses.FetchResponse.parseFrom(serializedResponse);
Common.Frame protoFrame = protoResponse.getFrame();
assertEquals(frame.offset, protoFrame.getOffset());
assertEquals(frame.done, protoFrame.getDone());
List<Common.Row> rows = protoFrame.getRowsList();
assertEquals(1, rows.size());
Common.Row row = rows.get(0);
List<Common.ColumnValue> columnValues = row.getValueList();
assertEquals(2, columnValues.size());
Iterator<Common.ColumnValue> iter = columnValues.iterator();
assertTrue(iter.hasNext());
Common.ColumnValue column = iter.next();
assertTrue("The Column should have contained a scalar: " + column, column.hasField(ColumnValue.getDescriptor().findFieldByNumber(ColumnValue.SCALAR_VALUE_FIELD_NUMBER)));
Common.TypedValue value = column.getScalarValue();
assertEquals(Common.Rep.BOOLEAN, value.getType());
assertEquals(true, value.getBoolValue());
assertTrue(iter.hasNext());
column = iter.next();
assertTrue("The Column should have contained a scalar: " + column, column.hasField(ColumnValue.getDescriptor().findFieldByNumber(ColumnValue.SCALAR_VALUE_FIELD_NUMBER)));
value = column.getScalarValue();
assertEquals(Common.Rep.STRING, value.getType());
assertEquals("my_string", value.getStringValue());
}
Aggregations