From 5cd5847842c3204ff20e44dece00c6805b13da45 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 20 Oct 2016 16:10:32 +0200 Subject: [PATCH] SONAR-8287 Support operators '>=' and '<' --- .../component/es/ProjectMeasuresIndex.java | 16 ++++--- .../component/es/ProjectMeasuresQuery.java | 2 +- .../es/ProjectMeasuresIndexTest.java | 27 ++++++++++++ .../ws/ProjectMeasuresQueryFactoryTest.java | 44 +++++++++++++++---- 4 files changed, 75 insertions(+), 14 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresIndex.java b/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresIndex.java index 21d522208a2..3115942030e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresIndex.java @@ -21,14 +21,14 @@ package org.sonar.server.component.es; import com.google.common.collect.ImmutableList; import java.util.List; -import java.util.stream.IntStream; import java.util.Set; +import java.util.stream.IntStream; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; -import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.bucket.range.RangeBuilder; import org.elasticsearch.search.sort.SortOrder; import org.sonar.server.component.es.ProjectMeasuresQuery.MetricCriterion; @@ -51,13 +51,16 @@ import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY; import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY; import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY; +import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.FIELD_AUTHORIZATION_GROUPS; +import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.FIELD_AUTHORIZATION_USERS; import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.FIELD_MEASURES; import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.FIELD_MEASURES_KEY; import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.FIELD_MEASURES_VALUE; import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.FIELD_NAME; import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.FIELD_QUALITY_GATE; import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.INDEX_PROJECT_MEASURES; -import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.*; +import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.TYPE_AUTHORIZATION; +import static org.sonar.server.component.es.ProjectMeasuresIndexDefinition.TYPE_PROJECT_MEASURES; public class ProjectMeasuresIndex extends BaseIndex { @@ -126,8 +129,7 @@ public class ProjectMeasuresIndex extends BaseIndex { .filter("2", termQuery(FIELD_VALUE, 2d)) .filter("3", termQuery(FIELD_VALUE, 3d)) .filter("4", termQuery(FIELD_VALUE, 4d)) - .filter("5", termQuery(FIELD_VALUE, 5d)) - )); + .filter("5", termQuery(FIELD_VALUE, 5d)))); } private QueryBuilder createEsQuery(ProjectMeasuresQuery query) { @@ -150,6 +152,10 @@ public class ProjectMeasuresIndex extends BaseIndex { switch (criterion.getOperator()) { case GT: return rangeQuery(fieldName).gt(criterion.getValue()); + case GTE: + return rangeQuery(fieldName).gte(criterion.getValue()); + case LT: + return rangeQuery(fieldName).lt(criterion.getValue()); case LTE: return rangeQuery(fieldName).lte(criterion.getValue()); case EQ: diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresQuery.java b/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresQuery.java index 6725b1cf604..6301a3c6d6f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresQuery.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresQuery.java @@ -57,7 +57,7 @@ public class ProjectMeasuresQuery { } public enum Operator { - LTE("<="), GT(">"), EQ("="); + LT("<"), LTE("<="), GT(">"), GTE(">="), EQ("="); String value; diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/es/ProjectMeasuresIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/es/ProjectMeasuresIndexTest.java index 58874673cf1..7f08375a874 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/es/ProjectMeasuresIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/es/ProjectMeasuresIndexTest.java @@ -95,6 +95,20 @@ public class ProjectMeasuresIndexTest { assertThat(result.getTotal()).isEqualTo(9); } + @Test + public void filter_with_lower_than() { + addDocs( + newDoc("P1", "K1", "N1").setMeasures(newArrayList(newMeasure(COVERAGE, 79d), newMeasure(NCLOC, 10_000d))), + newDoc("P2", "K2", "N2").setMeasures(newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 10_000d))), + newDoc("P3", "K3", "N3").setMeasures(newArrayList(newMeasure(COVERAGE, 81d), newMeasure(NCLOC, 10_000d)))); + + ProjectMeasuresQuery esQuery = new ProjectMeasuresQuery() + .addMetricCriterion(new MetricCriterion(COVERAGE, Operator.LT, 80d)); + List result = underTest.search(esQuery, new SearchOptions()).getIds(); + + assertThat(result).containsExactly("P1"); + } + @Test public void filter_with_lower_than_or_equals() { addDocs( @@ -122,6 +136,19 @@ public class ProjectMeasuresIndexTest { new SearchOptions()).getIds()).isEmpty(); } + @Test + public void filter_with_greater_than_or_equals() { + addDocs( + newDoc("P1", "K1", "N1").setMeasures(newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 30_000d))), + newDoc("P2", "K2", "N2").setMeasures(newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 30_001d))), + newDoc("P3", "K3", "N3").setMeasures(newArrayList(newMeasure(COVERAGE, 80d), newMeasure(NCLOC, 30_001d)))); + + assertThat(underTest.search(new ProjectMeasuresQuery().addMetricCriterion(new MetricCriterion(NCLOC, Operator.GTE, 30_001d)), + new SearchOptions()).getIds()).containsExactly("P2", "P3"); + assertThat(underTest.search(new ProjectMeasuresQuery().addMetricCriterion(new MetricCriterion(NCLOC, Operator.GTE, 100_000d)), + new SearchOptions()).getIds()).isEmpty(); + } + @Test public void filter_with_equals() { addDocs( diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactoryTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactoryTest.java index 1550e03a071..a3626564428 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactoryTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactoryTest.java @@ -32,7 +32,6 @@ import static org.sonar.server.component.es.ProjectMeasuresQuery.MetricCriterion import static org.sonar.server.component.es.ProjectMeasuresQuery.Operator; import static org.sonar.server.component.ws.ProjectMeasuresQueryFactory.newProjectMeasuresQuery; import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.OK; -import static org.sonar.test.ExceptionCauseMatcher.hasType; public class ProjectMeasuresQueryFactoryTest { @@ -50,6 +49,42 @@ public class ProjectMeasuresQueryFactoryTest { tuple("coverage", Operator.LTE, 80d)); } + @Test + public void create_query_having_lesser_than_operation() throws Exception { + ProjectMeasuresQuery query = newProjectMeasuresQuery("ncloc < 10"); + + assertThat(query.getMetricCriteria()) + .extracting(MetricCriterion::getMetricKey, MetricCriterion::getOperator, MetricCriterion::getValue) + .containsOnly(tuple("ncloc", Operator.LT, 10d)); + } + + @Test + public void create_query_having_lesser_than_or_equals_operation() throws Exception { + ProjectMeasuresQuery query = newProjectMeasuresQuery("ncloc <= 10"); + + assertThat(query.getMetricCriteria()) + .extracting(MetricCriterion::getMetricKey, MetricCriterion::getOperator, MetricCriterion::getValue) + .containsOnly(tuple("ncloc", Operator.LTE, 10d)); + } + + @Test + public void create_query_having_greater_than_operation() throws Exception { + ProjectMeasuresQuery query = newProjectMeasuresQuery("ncloc > 10"); + + assertThat(query.getMetricCriteria()) + .extracting(MetricCriterion::getMetricKey, MetricCriterion::getOperator, MetricCriterion::getValue) + .containsOnly(tuple("ncloc", Operator.GT, 10d)); + } + + @Test + public void create_query_having_greater_than_or_equals_operation() throws Exception { + ProjectMeasuresQuery query = newProjectMeasuresQuery("ncloc >= 10"); + + assertThat(query.getMetricCriteria()) + .extracting(MetricCriterion::getMetricKey, MetricCriterion::getOperator, MetricCriterion::getValue) + .containsOnly(tuple("ncloc", Operator.GTE, 10d)); + } + @Test public void create_query_having_equal_operation() throws Exception { ProjectMeasuresQuery query = newProjectMeasuresQuery("ncloc = 10"); @@ -100,13 +135,6 @@ public class ProjectMeasuresQueryFactoryTest { assertThat(result.getMetricCriteria()).isEmpty(); } - @Test - public void fail_on_unknown_operator() throws Exception { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectCause(hasType(IllegalArgumentException.class).andMessage("Unknown operator '>='")); - newProjectMeasuresQuery("ncloc >= 10"); - } - @Test public void fail_on_invalid_criteria() throws Exception { expectedException.expect(IllegalArgumentException.class); -- 2.39.5