From d190439f1a9ecf392f962cd3cd5e56e2a7c54993 Mon Sep 17 00:00:00 2001 From: Antonio Lucas Neres de Oliveira Barros Date: Fri, 13 Sep 2019 11:32:19 -0300 Subject: [PATCH] [#4] throw exception when client asks for wrong columns: - to ensure consistent behavior, the server should not respond when the client asks wrong columns --- .../breezil/queryfier/engine/QueryBuilder.java | 18 +++++++++++------- .../queryfier/engine/QueryBuilderTest.java | 8 ++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/breezil/queryfier/engine/QueryBuilder.java b/src/main/java/io/breezil/queryfier/engine/QueryBuilder.java index 7f0b613..9cc42bd 100644 --- a/src/main/java/io/breezil/queryfier/engine/QueryBuilder.java +++ b/src/main/java/io/breezil/queryfier/engine/QueryBuilder.java @@ -28,6 +28,13 @@ public class QueryBuilder { public QueryBuilder() { this.joinMaps = new HashMap<>(); } + + private void validateColumns(QBase toParse, QQuery q) { + if (toParse.getColumns().size() != q.getProjections().size()) { + throw new IllegalArgumentException( + "Some column(s): " + toParse.getColumns() + " do not belong to the entity you requested."); + } + } @SuppressWarnings("rawtypes") public QQuery parseQuery(QBase toParse) throws IllegalAccessException { @@ -203,25 +210,22 @@ public List mapToActualColumns(List sortedColumns) { private void configureProjections(QBase toParse, QQuery q, List projections) { if (toParse.getColumns().isEmpty()) { - projections.forEach(p -> q.addProjection(p)); + projections.forEach(q::addProjection); } else { addColumnsWithProjections(toParse, q, projections); addColumnsWithAggregationFunctions(toParse, q); + validateColumns(toParse, q); } } private void addColumnsWithAggregationFunctions(QBase toParse, QQuery q) { toParse.getColumns().stream() .filter(col -> col.contains(QProjection.SPLITTER)) - .forEach(col -> { - q.addProjection(new QProjection(col)); - }); + .forEach(col -> q.addProjection(new QProjection(col))); } private void addColumnsWithProjections(QBase toParse, QQuery q, List projections) { - projections.stream().filter(p -> { - return toParse.getColumns().contains(p.getAlias()); - }).forEach(p -> q.addProjection(p)); + projections.stream().filter(p -> toParse.getColumns().contains(p.getAlias())).forEach(q::addProjection); } private void configureSelectionAndParameter(QBase toParse, QQuery q, Field f) throws IllegalAccessException { diff --git a/src/test/java/io/breezil/queryfier/engine/QueryBuilderTest.java b/src/test/java/io/breezil/queryfier/engine/QueryBuilderTest.java index 2933a32..d9ff926 100644 --- a/src/test/java/io/breezil/queryfier/engine/QueryBuilderTest.java +++ b/src/test/java/io/breezil/queryfier/engine/QueryBuilderTest.java @@ -53,6 +53,14 @@ public void recuperaApenasColunasInformadas() { NUMBER_OF_QUERIED_PROJECTIONS, q.getProjections().size()); } + @Test(expected = IllegalArgumentException.class) + public void shoudFailWithWrongColumns() { + sf.addColumn("main"); + sf.addColumn("president"); + QQuery q = convertDTO2Query(sf); + Assert.fail("president does not belong to StateFilter. It should fail then"); + } + @Test public void deveOrdenarAscendentemente() { sf.addSortedColumns("governor");