]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9245 facet new_lines in search_projects WS
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 22 May 2017 07:26:51 +0000 (09:26 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Fri, 9 Jun 2017 06:26:48 +0000 (08:26 +0200)
it/it-tests/src/test/java/it/projectSearch/SearchProjectsTest.java
server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java

index a4743dedc7b8042850880d2283c5741f72cfa2d7..46d30a139b654314cfa6f7f09f0f116273c59e23 100644 (file)
@@ -200,7 +200,7 @@ public class SearchProjectsTest {
     analyzeProject(projectKey, "shared/xoo-history-v2");
 
     SearchProjectsWsResponse response = searchProjects(SearchProjectsRequest.builder().setOrganization(organizationKey).setFacets(asList(
-      "new_reliability_rating", "new_security_rating", "new_maintainability_rating", "new_coverage", "new_duplicated_lines_density")).build());
+      "new_reliability_rating", "new_security_rating", "new_maintainability_rating", "new_coverage", "new_duplicated_lines_density", "new_lines")).build());
 
     checkFacet(response, "new_reliability_rating",
       tuple("1", 1L),
@@ -232,6 +232,12 @@ public class SearchProjectsTest {
       tuple("5.0-10.0", 0L),
       tuple("10.0-20.0", 0L),
       tuple("20.0-*", 0L));
+    checkFacet(response, "new_lines",
+      tuple("*-1000.0", 0L),
+      tuple("1000.0-10000.0", 0L),
+      tuple("10000.0-100000.0", 0L),
+      tuple("100000.0-500000.0", 0L),
+      tuple("500000.0-*", 0L));
   }
 
   private void checkFacet(SearchProjectsWsResponse response, String facetKey, Tuple... values) {
index a97448c0944ef977e0d7f536bdefaf4337387c2a..4fced4575796b5d3f631c5fa82b0b970b92e8a94 100644 (file)
@@ -67,6 +67,7 @@ import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY;
 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY;
+import static org.sonar.api.measures.CoreMetrics.NEW_LINES_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY;
@@ -91,6 +92,7 @@ public class ProjectMeasuresIndex {
 
   public static final List<String> SUPPORTED_FACETS = ImmutableList.of(
     NCLOC_KEY,
+    NEW_LINES_KEY,
     DUPLICATED_LINES_DENSITY_KEY,
     NEW_DUPLICATED_LINES_DENSITY_KEY,
     COVERAGE_KEY,
@@ -105,6 +107,7 @@ public class ProjectMeasuresIndex {
     FILTER_LANGUAGES,
     FILTER_TAGS);
 
+  private static final Double[] LINES_THRESHOLDS = new Double[]{1_000d, 10_000d, 100_000d, 500_000d};
   private static final Double[] COVERAGE_THRESHOLDS = new Double[]{30d, 50d, 70d, 80d};
   private static final Double[] DUPLICATIONS_THRESHOLDS = new Double[]{3d, 5d, 10d, 20d};
 
@@ -112,7 +115,8 @@ public class ProjectMeasuresIndex {
   private static final String FIELD_MEASURES_VALUE = FIELD_MEASURES + "." + ProjectMeasuresIndexDefinition.FIELD_MEASURES_VALUE;
 
   private static final Map<String, FacetSetter> FACET_FACTORIES = ImmutableMap.<String, FacetSetter>builder()
-    .put(NCLOC_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, NCLOC_KEY, facetBuilder, 1_000d, 10_000d, 100_000d, 500_000d))
+    .put(NCLOC_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, NCLOC_KEY, facetBuilder, LINES_THRESHOLDS))
+    .put(NEW_LINES_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, NEW_LINES_KEY, facetBuilder, LINES_THRESHOLDS))
     .put(DUPLICATED_LINES_DENSITY_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, DUPLICATED_LINES_DENSITY_KEY, facetBuilder, DUPLICATIONS_THRESHOLDS))
     .put(NEW_DUPLICATED_LINES_DENSITY_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, NEW_DUPLICATED_LINES_DENSITY_KEY, facetBuilder, DUPLICATIONS_THRESHOLDS))
     .put(COVERAGE_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, COVERAGE_KEY, facetBuilder, COVERAGE_THRESHOLDS))
index 8b5424ba831964fb15a986713dabeda0875e743b..1f06d4d4dff1b57b07e36f291fba053b735b7db2 100644 (file)
@@ -69,6 +69,7 @@ import static org.assertj.core.api.Assertions.tuple;
 import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY;
 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY;
+import static org.sonar.api.measures.CoreMetrics.NEW_LINES_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING_KEY;
 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY;
@@ -171,7 +172,7 @@ public class SearchProjectsActionTest {
     Param facets = def.param("facets");
     assertThat(facets.defaultValue()).isNull();
     assertThat(facets.possibleValues()).containsOnly("ncloc", "duplicated_lines_density", "coverage", "sqale_rating", "reliability_rating", "security_rating", "alert_status",
-      "languages", "tags", "new_reliability_rating", "new_security_rating", "new_maintainability_rating", "new_coverage", "new_duplicated_lines_density");
+      "languages", "tags", "new_reliability_rating", "new_security_rating", "new_maintainability_rating", "new_coverage", "new_duplicated_lines_density", "new_lines");
   }
 
   @Test
@@ -414,6 +415,34 @@ public class SearchProjectsActionTest {
     assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getKey(), project3.key());
   }
 
+  @Test
+  public void filter_projects_by_ncloc() {
+    userSession.logIn();
+    OrganizationDto organizationDto = db.organizations().insert();
+    MetricDto ncloc = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC).setValueType(INT.name()));
+    ComponentDto project1 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(80d)));
+    ComponentDto project2 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(85d)));
+    ComponentDto project3 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(10d)));
+
+    SearchProjectsWsResponse result = call(request.setFilter("ncloc <= 80"));
+
+    assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getKey(), project3.key());
+  }
+
+  @Test
+  public void filter_projects_by_new_lines() {
+    userSession.logIn();
+    OrganizationDto organizationDto = db.organizations().insert();
+    MetricDto newLines = db.measureDbTester().insertMetric(c -> c.setKey(NEW_LINES_KEY).setValueType(INT.name()));
+    ComponentDto project1 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(80d)));
+    ComponentDto project2 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(85d)));
+    ComponentDto project3 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(10d)));
+
+    SearchProjectsWsResponse result = call(request.setFilter("new_lines <= 80"));
+
+    assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getKey(), project3.key());
+  }
+
   @Test
   public void filter_projects_by_text_query() {
     userSession.logIn();
@@ -775,6 +804,54 @@ public class SearchProjectsActionTest {
         tuple("20.0-*", 0L));
   }
 
+  @Test
+  public void return_ncloc_facet() {
+    userSession.logIn();
+    OrganizationDto organizationDto = db.organizations().insert();
+    MetricDto coverage = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC).setValueType(INT.name()));
+    insertProject(organizationDto, new Measure(coverage, c -> c.setValue(100d)));
+    insertProject(organizationDto, new Measure(coverage, c -> c.setValue(15_000d)));
+    insertProject(organizationDto, new Measure(coverage, c -> c.setValue(50_000d)));
+
+    SearchProjectsWsResponse result = call(request.setFacets(singletonList(NCLOC)));
+
+    Common.Facet facet = result.getFacets().getFacetsList().stream()
+      .filter(oneFacet -> NCLOC.equals(oneFacet.getProperty()))
+      .findFirst().orElseThrow(IllegalStateException::new);
+    assertThat(facet.getValuesList())
+      .extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
+      .containsExactly(
+        tuple("*-1000.0", 1L),
+        tuple("1000.0-10000.0", 0L),
+        tuple("10000.0-100000.0", 2L),
+        tuple("100000.0-500000.0", 0L),
+        tuple("500000.0-*", 0L));
+  }
+
+  @Test
+  public void return_new_lines_facet() {
+    userSession.logIn();
+    OrganizationDto organizationDto = db.organizations().insert();
+    MetricDto coverage = db.measureDbTester().insertMetric(c -> c.setKey(NEW_LINES_KEY).setValueType(INT.name()));
+    insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(100d)));
+    insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(15_000d)));
+    insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(50_000d)));
+
+    SearchProjectsWsResponse result = call(request.setFacets(singletonList(NEW_LINES_KEY)));
+
+    Common.Facet facet = result.getFacets().getFacetsList().stream()
+      .filter(oneFacet -> NEW_LINES_KEY.equals(oneFacet.getProperty()))
+      .findFirst().orElseThrow(IllegalStateException::new);
+    assertThat(facet.getValuesList())
+      .extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
+      .containsExactly(
+        tuple("*-1000.0", 1L),
+        tuple("1000.0-10000.0", 0L),
+        tuple("10000.0-100000.0", 2L),
+        tuple("100000.0-500000.0", 0L),
+        tuple("500000.0-*", 0L));
+  }
+
   @Test
   public void default_sort_is_by_ascending_name() throws Exception {
     userSession.logIn();
index cd670d5d709dc36e3bac5082285700eb962bc0ec..0c8cc042d9d94113e29b193458eb1961f2ef86ce 100644 (file)
@@ -83,6 +83,7 @@ public class ProjectMeasuresIndexTest {
   private static final String DUPLICATION = "duplicated_lines_density";
   private static final String NEW_DUPLICATION = "new_duplicated_lines_density";
   private static final String NCLOC = "ncloc";
+  private static final String NEW_LINES = "new_lines";
   private static final String LANGUAGES = "languages";
 
   private static final OrganizationDto ORG = OrganizationTesting.newOrganizationDto();
@@ -530,6 +531,41 @@ public class ProjectMeasuresIndexTest {
       entry("500000.0-*", 0L));
   }
 
+  @Test
+  public void facet_new_lines() {
+    index(
+      // 3 docs with ncloc<1K
+      newDoc(NEW_LINES, 0d),
+      newDoc(NEW_LINES, 0d),
+      newDoc(NEW_LINES, 999d),
+      // 2 docs with ncloc>=1K and ncloc<10K
+      newDoc(NEW_LINES, 1_000d),
+      newDoc(NEW_LINES, 9_999d),
+      // 4 docs with ncloc>=10K and ncloc<100K
+      newDoc(NEW_LINES, 10_000d),
+      newDoc(NEW_LINES, 10_000d),
+      newDoc(NEW_LINES, 11_000d),
+      newDoc(NEW_LINES, 99_000d),
+      // 2 docs with ncloc>=100K and ncloc<500K
+      newDoc(NEW_LINES, 100_000d),
+      newDoc(NEW_LINES, 499_000d),
+      // 5 docs with ncloc>= 500K
+      newDoc(NEW_LINES, 500_000d),
+      newDoc(NEW_LINES, 100_000_000d),
+      newDoc(NEW_LINES, 500_000d),
+      newDoc(NEW_LINES, 1_000_000d),
+      newDoc(NEW_LINES, 100_000_000_000d));
+
+    Facets facets = underTest.search(new ProjectMeasuresQuery(), new SearchOptions().addFacets(NEW_LINES)).getFacets();
+
+    assertThat(facets.get(NEW_LINES)).containsExactly(
+      entry("*-1000.0", 3L),
+      entry("1000.0-10000.0", 2L),
+      entry("10000.0-100000.0", 4L),
+      entry("100000.0-500000.0", 2L),
+      entry("500000.0-*", 5L));
+  }
+
   @Test
   public void facet_coverage() {
     index(