Search in sources :

Example 1 with Request

use of org.apache.calcite.avatica.remote.Service.Request in project calcite-avatica by apache.

the class ProtobufTranslationImplTest method getRequestsWithNulls.

private static List<Request> getRequestsWithNulls() {
    LinkedList<Request> requests = new LinkedList<>();
    // We're pretty fast and loose on what can be null.
    requests.add(new SchemasRequest(null, null, null));
    // Repeated fields default to an empty list
    requests.add(new TablesRequest(null, null, null, null, Collections.<String>emptyList()));
    requests.add(new ColumnsRequest(null, null, null, null, null));
    requests.add(new PrepareAndExecuteRequest(null, 0, null, 0));
    requests.add(new PrepareRequest(null, null, 0));
    requests.add(new CreateStatementRequest(null));
    requests.add(new CloseStatementRequest(null, 0));
    requests.add(new OpenConnectionRequest(null, null));
    requests.add(new CloseConnectionRequest(null));
    requests.add(new ConnectionSyncRequest(null, null));
    return requests;
}
Also used : SchemasRequest(org.apache.calcite.avatica.remote.Service.SchemasRequest) ExecuteRequest(org.apache.calcite.avatica.remote.Service.ExecuteRequest) TablesRequest(org.apache.calcite.avatica.remote.Service.TablesRequest) CloseStatementRequest(org.apache.calcite.avatica.remote.Service.CloseStatementRequest) FetchRequest(org.apache.calcite.avatica.remote.Service.FetchRequest) PrepareRequest(org.apache.calcite.avatica.remote.Service.PrepareRequest) CreateStatementRequest(org.apache.calcite.avatica.remote.Service.CreateStatementRequest) CloseConnectionRequest(org.apache.calcite.avatica.remote.Service.CloseConnectionRequest) CatalogsRequest(org.apache.calcite.avatica.remote.Service.CatalogsRequest) SyncResultsRequest(org.apache.calcite.avatica.remote.Service.SyncResultsRequest) Request(org.apache.calcite.avatica.remote.Service.Request) ConnectionSyncRequest(org.apache.calcite.avatica.remote.Service.ConnectionSyncRequest) SchemasRequest(org.apache.calcite.avatica.remote.Service.SchemasRequest) TableTypesRequest(org.apache.calcite.avatica.remote.Service.TableTypesRequest) DatabasePropertyRequest(org.apache.calcite.avatica.remote.Service.DatabasePropertyRequest) PrepareAndExecuteBatchRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteBatchRequest) TypeInfoRequest(org.apache.calcite.avatica.remote.Service.TypeInfoRequest) CommitRequest(org.apache.calcite.avatica.remote.Service.CommitRequest) PrepareAndExecuteRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest) OpenConnectionRequest(org.apache.calcite.avatica.remote.Service.OpenConnectionRequest) ColumnsRequest(org.apache.calcite.avatica.remote.Service.ColumnsRequest) RollbackRequest(org.apache.calcite.avatica.remote.Service.RollbackRequest) OpenConnectionRequest(org.apache.calcite.avatica.remote.Service.OpenConnectionRequest) LinkedList(java.util.LinkedList) TablesRequest(org.apache.calcite.avatica.remote.Service.TablesRequest) CloseConnectionRequest(org.apache.calcite.avatica.remote.Service.CloseConnectionRequest) CreateStatementRequest(org.apache.calcite.avatica.remote.Service.CreateStatementRequest) ConnectionSyncRequest(org.apache.calcite.avatica.remote.Service.ConnectionSyncRequest) PrepareAndExecuteRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest) CloseStatementRequest(org.apache.calcite.avatica.remote.Service.CloseStatementRequest) ColumnsRequest(org.apache.calcite.avatica.remote.Service.ColumnsRequest) PrepareRequest(org.apache.calcite.avatica.remote.Service.PrepareRequest)

Example 2 with Request

use of org.apache.calcite.avatica.remote.Service.Request 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)

Example 3 with Request

use of org.apache.calcite.avatica.remote.Service.Request in project calcite-avatica by apache.

the class ProtobufSerializationTest method testExecuteSerialization.

@Test
public void testExecuteSerialization() throws Exception {
    Service.ExecuteRequest executeRequest = new Service.ExecuteRequest(new StatementHandle("connection", 12345, getSignature()), getTypedValues(), 0);
    Requests.ExecuteRequest pbExecuteRequest = executeRequest.serialize();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    pbExecuteRequest.writeTo(baos);
    byte[] serialized = baos.toByteArray();
    baos.reset();
    WireMessage wireMsg = WireMessage.newBuilder().setName(Requests.ExecuteRequest.class.getName()).setWrappedMessage(UnsafeByteOperations.unsafeWrap(serialized)).build();
    wireMsg.writeTo(baos);
    serialized = baos.toByteArray();
    ProtobufTranslation translator = new ProtobufTranslationImpl();
    Request newRequest = translator.parseRequest(serialized);
    Assert.assertEquals(executeRequest, newRequest);
}
Also used : StatementHandle(org.apache.calcite.avatica.Meta.StatementHandle) Request(org.apache.calcite.avatica.remote.Service.Request) WireMessage(org.apache.calcite.avatica.proto.Common.WireMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Requests(org.apache.calcite.avatica.proto.Requests) Test(org.junit.Test)

Example 4 with Request

use of org.apache.calcite.avatica.remote.Service.Request 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);
    }
}
Also used : Response(org.apache.calcite.avatica.remote.Service.Response) Request(org.apache.calcite.avatica.remote.Service.Request) IOException(java.io.IOException) NoSuchConnectionException(org.apache.calcite.avatica.NoSuchConnectionException)

Example 5 with Request

use of org.apache.calcite.avatica.remote.Service.Request in project calcite-avatica by apache.

the class ProtobufTranslationImplTest method getRequests.

/**
 * Generates a collection of Requests whose serialization will be tested.
 */
private static List<Request> getRequests() {
    LinkedList<Request> requests = new LinkedList<>();
    requests.add(new CatalogsRequest());
    requests.add(new DatabasePropertyRequest());
    requests.add(new SchemasRequest("connectionId", "catalog", "schemaPattern"));
    requests.add(new TablesRequest("connectionId", "catalog", "schemaPattern", "tableNamePattern", Arrays.asList("STRING", "BOOLEAN", "INT")));
    requests.add(new TableTypesRequest());
    requests.add(new ColumnsRequest("connectionId", "catalog", "schemaPattern", "tableNamePattern", "columnNamePattern"));
    requests.add(new TypeInfoRequest());
    requests.add(new PrepareAndExecuteRequest("connectionId", Integer.MAX_VALUE, "sql", Long.MAX_VALUE));
    requests.add(new PrepareRequest("connectionId", "sql", Long.MAX_VALUE));
    List<TypedValue> paramValues = Arrays.asList(TypedValue.create(Rep.BOOLEAN.name(), Boolean.TRUE), TypedValue.create(Rep.STRING.name(), "string"));
    FetchRequest fetchRequest = new FetchRequest("connectionId", Integer.MAX_VALUE, Long.MAX_VALUE, Integer.MAX_VALUE);
    requests.add(fetchRequest);
    requests.add(new CreateStatementRequest("connectionId"));
    requests.add(new CloseStatementRequest("connectionId", Integer.MAX_VALUE));
    Map<String, String> info = new HashMap<>();
    info.put("param1", "value1");
    info.put("param2", "value2");
    requests.add(new OpenConnectionRequest("connectionId", info));
    requests.add(new CloseConnectionRequest("connectionId"));
    requests.add(new ConnectionSyncRequest("connectionId", new ConnectionPropertiesImpl(Boolean.FALSE, Boolean.FALSE, Integer.MAX_VALUE, "catalog", "schema")));
    requests.add(new SyncResultsRequest("connectionId", 12345, getSqlQueryState(), 150));
    requests.add(new SyncResultsRequest("connectionId2", 54321, getMetadataQueryState1(), 0));
    requests.add(new SyncResultsRequest("connectionId3", 5, getMetadataQueryState2(), 10));
    requests.add(new CommitRequest("connectionId"));
    requests.add(new RollbackRequest("connectionId"));
    // ExecuteBatchRequest omitted because of the special protobuf conversion it does
    List<String> commands = Arrays.asList("command1", "command2", "command3");
    requests.add(new PrepareAndExecuteBatchRequest("connectionId", 12345, commands));
    List<ColumnMetaData> columns = Collections.emptyList();
    List<AvaticaParameter> params = Collections.emptyList();
    Meta.CursorFactory cursorFactory = Meta.CursorFactory.create(Style.LIST, Object.class, Collections.<String>emptyList());
    Signature signature = Signature.create(columns, "sql", params, cursorFactory, Meta.StatementType.SELECT);
    Meta.StatementHandle handle = new Meta.StatementHandle("1234", 1, signature);
    requests.add(new ExecuteRequest(handle, Arrays.<TypedValue>asList((TypedValue) null), 10));
    requests.add(new ExecuteRequest(handle, Arrays.asList(TypedValue.EXPLICIT_NULL), 10));
    return requests;
}
Also used : PrepareAndExecuteBatchRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteBatchRequest) Meta(org.apache.calcite.avatica.Meta) HashMap(java.util.HashMap) DatabasePropertyRequest(org.apache.calcite.avatica.remote.Service.DatabasePropertyRequest) SchemasRequest(org.apache.calcite.avatica.remote.Service.SchemasRequest) ConnectionPropertiesImpl(org.apache.calcite.avatica.ConnectionPropertiesImpl) OpenConnectionRequest(org.apache.calcite.avatica.remote.Service.OpenConnectionRequest) RollbackRequest(org.apache.calcite.avatica.remote.Service.RollbackRequest) SyncResultsRequest(org.apache.calcite.avatica.remote.Service.SyncResultsRequest) AvaticaParameter(org.apache.calcite.avatica.AvaticaParameter) TablesRequest(org.apache.calcite.avatica.remote.Service.TablesRequest) TableTypesRequest(org.apache.calcite.avatica.remote.Service.TableTypesRequest) ExecuteRequest(org.apache.calcite.avatica.remote.Service.ExecuteRequest) PrepareAndExecuteRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest) FetchRequest(org.apache.calcite.avatica.remote.Service.FetchRequest) PrepareAndExecuteRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest) CloseStatementRequest(org.apache.calcite.avatica.remote.Service.CloseStatementRequest) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) PrepareRequest(org.apache.calcite.avatica.remote.Service.PrepareRequest) CommitRequest(org.apache.calcite.avatica.remote.Service.CommitRequest) ExecuteRequest(org.apache.calcite.avatica.remote.Service.ExecuteRequest) TablesRequest(org.apache.calcite.avatica.remote.Service.TablesRequest) CloseStatementRequest(org.apache.calcite.avatica.remote.Service.CloseStatementRequest) FetchRequest(org.apache.calcite.avatica.remote.Service.FetchRequest) PrepareRequest(org.apache.calcite.avatica.remote.Service.PrepareRequest) CreateStatementRequest(org.apache.calcite.avatica.remote.Service.CreateStatementRequest) CloseConnectionRequest(org.apache.calcite.avatica.remote.Service.CloseConnectionRequest) CatalogsRequest(org.apache.calcite.avatica.remote.Service.CatalogsRequest) SyncResultsRequest(org.apache.calcite.avatica.remote.Service.SyncResultsRequest) Request(org.apache.calcite.avatica.remote.Service.Request) ConnectionSyncRequest(org.apache.calcite.avatica.remote.Service.ConnectionSyncRequest) SchemasRequest(org.apache.calcite.avatica.remote.Service.SchemasRequest) TableTypesRequest(org.apache.calcite.avatica.remote.Service.TableTypesRequest) DatabasePropertyRequest(org.apache.calcite.avatica.remote.Service.DatabasePropertyRequest) PrepareAndExecuteBatchRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteBatchRequest) TypeInfoRequest(org.apache.calcite.avatica.remote.Service.TypeInfoRequest) CommitRequest(org.apache.calcite.avatica.remote.Service.CommitRequest) PrepareAndExecuteRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest) OpenConnectionRequest(org.apache.calcite.avatica.remote.Service.OpenConnectionRequest) ColumnsRequest(org.apache.calcite.avatica.remote.Service.ColumnsRequest) RollbackRequest(org.apache.calcite.avatica.remote.Service.RollbackRequest) LinkedList(java.util.LinkedList) CloseConnectionRequest(org.apache.calcite.avatica.remote.Service.CloseConnectionRequest) TypeInfoRequest(org.apache.calcite.avatica.remote.Service.TypeInfoRequest) CreateStatementRequest(org.apache.calcite.avatica.remote.Service.CreateStatementRequest) Signature(org.apache.calcite.avatica.Meta.Signature) ConnectionSyncRequest(org.apache.calcite.avatica.remote.Service.ConnectionSyncRequest) CatalogsRequest(org.apache.calcite.avatica.remote.Service.CatalogsRequest) ColumnsRequest(org.apache.calcite.avatica.remote.Service.ColumnsRequest)

Aggregations

Request (org.apache.calcite.avatica.remote.Service.Request)7 Response (org.apache.calcite.avatica.remote.Service.Response)4 IOException (java.io.IOException)3 CatalogsRequest (org.apache.calcite.avatica.remote.Service.CatalogsRequest)3 CloseConnectionRequest (org.apache.calcite.avatica.remote.Service.CloseConnectionRequest)3 CloseStatementRequest (org.apache.calcite.avatica.remote.Service.CloseStatementRequest)3 ColumnsRequest (org.apache.calcite.avatica.remote.Service.ColumnsRequest)3 CommitRequest (org.apache.calcite.avatica.remote.Service.CommitRequest)3 ConnectionSyncRequest (org.apache.calcite.avatica.remote.Service.ConnectionSyncRequest)3 CreateStatementRequest (org.apache.calcite.avatica.remote.Service.CreateStatementRequest)3 DatabasePropertyRequest (org.apache.calcite.avatica.remote.Service.DatabasePropertyRequest)3 ErrorResponse (org.apache.calcite.avatica.remote.Service.ErrorResponse)3 ExecuteRequest (org.apache.calcite.avatica.remote.Service.ExecuteRequest)3 FetchRequest (org.apache.calcite.avatica.remote.Service.FetchRequest)3 OpenConnectionRequest (org.apache.calcite.avatica.remote.Service.OpenConnectionRequest)3 PrepareAndExecuteBatchRequest (org.apache.calcite.avatica.remote.Service.PrepareAndExecuteBatchRequest)3 PrepareAndExecuteRequest (org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest)3 PrepareRequest (org.apache.calcite.avatica.remote.Service.PrepareRequest)3 RollbackRequest (org.apache.calcite.avatica.remote.Service.RollbackRequest)3 SchemasRequest (org.apache.calcite.avatica.remote.Service.SchemasRequest)3