aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-03-06 16:47:25 +0100
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-03-07 12:19:51 +0100
commit5e681224aa7196b766c743224c4ed13af206b798 (patch)
treeb9f32102f90e76b2e4a800f4fe4b6ed0613749eb
parent641f4c3342821f14c3782cfb05dddd23dadfbdd4 (diff)
downloadsonarqube-5e681224aa7196b766c743224c4ed13af206b798.tar.gz
sonarqube-5e681224aa7196b766c743224c4ed13af206b798.zip
SONAR-8231 Return selected languages even when no project for this language
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java28
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java20
2 files changed, 42 insertions, 6 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java
index 81e5aaf8a48..464d1e8aa44 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java
@@ -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;
}
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java
index a6496d4f25d..5ce96fec3f8 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java
@@ -485,6 +485,26 @@ public class SearchProjectsActionTest {
}
@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();
insertProjectInDbAndEs(newProjectDto(organization).setName("Sonar Java"), newArrayList(newMeasure(COVERAGE, 81), newMeasure(NCLOC, 5d)));