SQLService { public void execute(SQLQueryRequest request, ResponseListener<QueryResponse> listener) { try { executionEngine.execute( plan( analyze( parse(request.getQuery()))), listener); } catch (Exception e) { listener.onFailure(e); } } SQLService(SQLSyntaxParser parser, Analyzer analyzer,
                    StorageEngine storageEngine, ExecutionEngine executionEngine); void execute(SQLQueryRequest request, ResponseListener<QueryResponse> listener); void execute(PhysicalPlan plan, ResponseListener<QueryResponse> listener); UnresolvedPlan parse(String query); LogicalPlan analyze(UnresolvedPlan ast); PhysicalPlan plan(LogicalPlan logicalPlan);  }
@Test public void canExecuteSqlQuery() { doAnswer(invocation -> { ResponseListener<QueryResponse> listener = invocation.getArgument(1); listener.onResponse(new QueryResponse(schema, Collections.emptyList())); return null; }).when(executionEngine).execute(any(), any()); sqlService.execute( new SQLQueryRequest(new JSONObject(), "SELECT 123", "_opendistro/_sql", "jdbc"), new ResponseListener<QueryResponse>() { @Override public void onResponse(QueryResponse response) { assertNotNull(response); } @Override public void onFailure(Exception e) { fail(e); } }); }
@Test public void canExecuteFromPhysicalPlan() { doAnswer(invocation -> { ResponseListener<QueryResponse> listener = invocation.getArgument(1); listener.onResponse(new QueryResponse(schema, Collections.emptyList())); return null; }).when(executionEngine).execute(any(), any()); sqlService.execute(mock(PhysicalPlan.class), new ResponseListener<QueryResponse>() { @Override public void onResponse(QueryResponse response) { assertNotNull(response); } @Override public void onFailure(Exception e) { fail(e); } }); }
@Test public void canCaptureErrorDuringExecution() { sqlService.execute( new SQLQueryRequest(new JSONObject(), "SELECT", "_opendistro/_sql", ""), new ResponseListener<QueryResponse>() { @Override public void onResponse(QueryResponse response) { fail(); } @Override public void onFailure(Exception e) { assertNotNull(e); } }); }
@Test public void canCaptureErrorDuringExecutionFromPhysicalPlan() { doThrow(new RuntimeException()).when(executionEngine).execute(any(), any()); sqlService.execute(mock(PhysicalPlan.class), new ResponseListener<QueryResponse>() { @Override public void onResponse(QueryResponse response) { fail(); } @Override public void onFailure(Exception e) { assertNotNull(e); } }); }