]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8287 Support operators '>=' and '<'
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 20 Oct 2016 14:10:32 +0000 (16:10 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 21 Oct 2016 06:12:21 +0000 (08:12 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresIndex.java
server/sonar-server/src/main/java/org/sonar/server/component/es/ProjectMeasuresQuery.java
server/sonar-server/src/test/java/org/sonar/server/component/es/ProjectMeasuresIndexTest.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/ProjectMeasuresQueryFactoryTest.java

index 21d522208a2da2d16cbc3f29f9341ef27fb12069..3115942030ef0327e5954272179b306a52ff5492 100644 (file)
@@ -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:
index 6725b1cf60483aadb452b5529c9d376e58743c98..6301a3c6d6fa3864c5982ef721936334ae5b04aa 100644 (file)
@@ -57,7 +57,7 @@ public class ProjectMeasuresQuery {
   }
 
   public enum Operator {
-    LTE("<="), GT(">"), EQ("=");
+    LT("<"), LTE("<="), GT(">"), GTE(">="), EQ("=");
 
     String value;
 
index 58874673cf171bd6425560b684e0799744526a77..7f08375a87401069d34966f5f1d88d9c1cc23cb9 100644 (file)
@@ -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<String> 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(
index 1550e03a0713d4b71f309b9108a47dda25bf3782..a36265644283875884919e7a010e78a5a4d72a07 100644 (file)
@@ -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);