QueryProcessorImpl implements QueryProcessor { @Override public List<QueryResult> process(Query query) { try { return queryCache.get(query); } catch (ExecutionException e) { return Collections.singletonList(getQueryResult(EVALUATION_ERROR, 0, empty(), e.getMessage())); } }  @Override List<QueryResult> process(Query query);  }
@Test public void testMissingParameters() { List<QueryResult> results = queryProcessor.process(createQuery( "select * from instances where id = :id:" )); assertThat(results, hasSize(1)); assertThat(results.get(0).getStatus(), equalTo(QueryStatus.MISSING_PARAMETERS)); }
@Test public void testSyntaxError() { String errorMessage = "Some syntax error"; mockQueryPlanner.setException(new SQLSyntaxErrorException(errorMessage)); List<QueryResult> results = queryProcessor.process(createQuery(QUERY)); QueryResult syntaxErrorResult = new QueryResult(); syntaxErrorResult.setStatus(QueryStatus.SYNTAX_ERROR); syntaxErrorResult.setData(DataContainer.empty().toData()); syntaxErrorResult.setMessage(errorMessage); assertThat(results, contains(syntaxErrorResult)); }
@Test public void testEvaluationError() { String errorMessage = "something failed"; mockQueryPlanner.setQueryPlan(createQueryPlan( previousTaskResult -> { throw new SQLException(errorMessage); }, QueryType.SELECT )); List<QueryResult> results = queryProcessor.process(createQuery(QUERY)); QueryResult evaluationErrorResult = new QueryResult(); evaluationErrorResult.setStatus(QueryStatus.EVALUATION_ERROR); evaluationErrorResult.setData(DataContainer.empty().toData()); evaluationErrorResult.setMessage(errorMessage); assertThat(results, contains(evaluationErrorResult)); }
@Test public void testSuccess() { DataContainer dataContainer = new DataContainer(Arrays.asList("col_one", "col_two")); dataContainer.addRow(Arrays.asList("one", "two")); mockQueryPlanner.setQueryPlan(createQueryPlan( previousTaskResult -> { ExecutionResult executionResult = new ExecutionResult(); executionResult.setCount(1); executionResult.setData(dataContainer); return executionResult; }, QueryType.SELECT )); List<QueryResult> results = queryProcessor.process(createQuery(QUERY)); QueryResult successResult = new QueryResult(); successResult.setStatus(QueryStatus.SUCCESS); successResult.setData(dataContainer.toData()); successResult.setCount(1); successResult.setMessage(""); assertThat(results, contains(successResult)); }
@Test public void testExplain() { final String TASK_DESCRIPTION = "task_description"; Task explainTask = new Task() { @Override public ExecutionResult execute(ExecutionResult previousTaskResult) throws SQLException { throw new UnsupportedOperationException("Does nothing!"); } @Override public String toString() { return TASK_DESCRIPTION; } }; mockQueryPlanner.setQueryPlan(createQueryPlan(explainTask, QueryType.EXPLAIN)); DataContainer explainDataContainer = new DataContainer(Collections.singleton("task")); explainDataContainer.addRow(Collections.singletonList(TASK_DESCRIPTION)); List<QueryResult> results = queryProcessor.process(createQuery(QUERY)); QueryResult explainResult = new QueryResult(); explainResult.setStatus(QueryStatus.SUCCESS); explainResult.setData(explainDataContainer.toData()); explainResult.setCount(1); explainResult.setMessage(""); assertThat(results, contains(explainResult)); }