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;
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 {
.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) {
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:
}
public enum Operator {
- LTE("<="), GT(">"), EQ("=");
+ LT("<"), LTE("<="), GT(">"), GTE(">="), EQ("=");
String value;
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(
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(
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 {
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");
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);