]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8231 Return selected languages even when no project for this language
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 6 Mar 2017 15:47:25 +0000 (16:47 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 7 Mar 2017 11:19:51 +0000 (12:19 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java

index 81e5aaf8a48faa100eca0b7b6bed80cbd1aa09da..464d1e8aa44dfe0fdd92cb63beab0815d1064d3b 100644 (file)
@@ -227,7 +227,7 @@ public class SearchProjectsAction implements ComponentsWsAction {
     Ordering<ComponentDto> ordering = Ordering.explicit(projectUuids).onResultOf(ComponentDto::uuid);
     List<ComponentDto> projects = ordering.immutableSortedCopy(dbClient.componentDao().selectByUuids(dbSession, projectUuids));
     Map<String, SnapshotDto> analysisByProjectUuid = getSnapshots(dbSession, request, projectUuids);
-    return new SearchResults(projects, favoriteProjectUuids, esResults, analysisByProjectUuid);
+    return new SearchResults(projects, favoriteProjectUuids, esResults, analysisByProjectUuid, query);
   }
 
   private static boolean hasFavoriteFilter(List<Criterion> criteria) {
@@ -285,8 +285,7 @@ public class SearchProjectsAction implements ComponentsWsAction {
     return request.build();
   }
 
-  private SearchProjectsWsResponse buildResponse(SearchProjectsRequest request, SearchResults searchResults,
-    Map<String, OrganizationDto> organizationsByUuid) {
+  private SearchProjectsWsResponse buildResponse(SearchProjectsRequest request, SearchResults searchResults, Map<String, OrganizationDto> organizationsByUuid) {
     Function<ComponentDto, Component> dbToWsComponent = new DbToWsComponent(organizationsByUuid, searchResults.favoriteProjectUuids, searchResults.analysisByProjectUuid,
       userSession.isLoggedIn());
 
@@ -301,15 +300,17 @@ public class SearchProjectsAction implements ComponentsWsAction {
           .forEach(response::addComponents);
         return response;
       })
-      .map(response -> addFacets(searchResults.facets, response))
+      .map(response -> addFacets(searchResults, response))
       .map(SearchProjectsWsResponse.Builder::build)
       .findFirst()
       .orElseThrow(() -> new IllegalStateException("SearchProjectsWsResponse not built"));
   }
 
-  private static SearchProjectsWsResponse.Builder addFacets(Facets esFacets, SearchProjectsWsResponse.Builder wsResponse) {
+  private static SearchProjectsWsResponse.Builder addFacets(SearchResults searchResults, SearchProjectsWsResponse.Builder wsResponse) {
+    Facets esFacets = searchResults.facets;
     EsToWsFacet esToWsFacet = new EsToWsFacet();
 
+    searchResults.query.getLanguages().ifPresent(languages -> addMandatoryValuesToFacet(esFacets, FILTER_LANGUAGES, languages));
     Common.Facets wsFacets = esFacets.getAll().entrySet().stream()
       .map(esToWsFacet)
       .collect(Collector.of(
@@ -325,6 +326,18 @@ public class SearchProjectsAction implements ComponentsWsAction {
     return wsResponse;
   }
 
+  private static void addMandatoryValuesToFacet(Facets facets, String facetName, Iterable<String> mandatoryValues) {
+    Map<String, Long> buckets = facets.get(facetName);
+    if (buckets == null) {
+      return;
+    }
+    for (String mandatoryValue : mandatoryValues) {
+      if (!buckets.containsKey(mandatoryValue)) {
+        buckets.put(mandatoryValue, 0L);
+      }
+    }
+  }
+
   private static class EsToWsFacet implements Function<Entry<String, LinkedHashMap<String, Long>>, Common.Facet> {
     private final BucketToFacetValue bucketToFacetValue = new BucketToFacetValue();
     private final Common.Facet.Builder wsFacet = Common.Facet.newBuilder();
@@ -413,14 +426,17 @@ public class SearchProjectsAction implements ComponentsWsAction {
     private final Set<String> favoriteProjectUuids;
     private final Facets facets;
     private final Map<String, SnapshotDto> analysisByProjectUuid;
+    private final ProjectMeasuresQuery query;
     private final int total;
 
-    private SearchResults(List<ComponentDto> projects, Set<String> favoriteProjectUuids, SearchIdResult<String> searchResults, Map<String, SnapshotDto> analysisByProjectUuid) {
+    private SearchResults(List<ComponentDto> projects, Set<String> favoriteProjectUuids, SearchIdResult<String> searchResults, Map<String, SnapshotDto> analysisByProjectUuid,
+      ProjectMeasuresQuery query) {
       this.projects = projects;
       this.favoriteProjectUuids = favoriteProjectUuids;
       this.total = (int) searchResults.getTotal();
       this.facets = searchResults.getFacets();
       this.analysisByProjectUuid = analysisByProjectUuid;
+      this.query = query;
     }
   }
 }
index a6496d4f25d626a29f7f66c3607a078e7944739d..5ce96fec3f8f2cb18fde8985090b5f6d08743ac7 100644 (file)
@@ -484,6 +484,26 @@ public class SearchProjectsActionTest {
         tuple("<null>", 2L));
   }
 
+  @Test
+  public void return_languages_facet_with_language_having_no_project_if_language_is_in_filter() {
+    OrganizationDto organization = db.getDefaultOrganization();
+    insertProjectInDbAndEs(newProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81d)), null, asList("<null>", "java"));
+    insertProjectInDbAndEs(newProjectDto(organization).setName("Sonar Groovy"), newArrayList(newMeasure(COVERAGE, 81)), null, asList("java"));
+    insertMetrics(COVERAGE, NCLOC_LANGUAGE_DISTRIBUTION_KEY);
+
+    SearchProjectsWsResponse result = call(request.setFilter("languages = xoo").setFacets(singletonList(FILTER_LANGUAGES)));
+
+    Common.Facet facet = result.getFacets().getFacetsList().stream()
+      .filter(oneFacet -> FILTER_LANGUAGES.equals(oneFacet.getProperty()))
+      .findFirst().orElseThrow(IllegalStateException::new);
+    assertThat(facet.getValuesList())
+      .extracting(Common.FacetValue::getVal, Common.FacetValue::getCount)
+      .containsOnly(
+        tuple("xoo", 0L),
+        tuple("java", 2L),
+        tuple("<null>", 1L));
+  }
+
   @Test
   public void default_sort_is_by_ascending_name() throws Exception {
     OrganizationDto organization = db.getDefaultOrganization();