diff options
author | Daniel Schwarz <daniel.schwarz@sonarsource.com> | 2017-05-11 11:40:37 +0200 |
---|---|---|
committer | Daniel Schwarz <bartfastiel@users.noreply.github.com> | 2017-05-11 14:46:37 +0200 |
commit | c1a942976f2ba145f99c4256f7e36c8917554bcb (patch) | |
tree | 65e26cb3e488ce889aa139671f0ac3bee595315f /server | |
parent | 996eaef682bd9e8b630e50e2f899a61e6ac64e68 (diff) | |
download | sonarqube-c1a942976f2ba145f99c4256f7e36c8917554bcb.tar.gz sonarqube-c1a942976f2ba145f99c4256f7e36c8917554bcb.zip |
SONAR-9186 fix number of “more” in api/components/suggestions
Diffstat (limited to 'server')
2 files changed, 81 insertions, 26 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java index e792752cba4..8b71294a811 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java @@ -183,7 +183,8 @@ public class SuggestionsAction implements ComponentsWsAction { ComponentIndexResults componentsPerQualifiers = ComponentIndexResults.newBuilder().setQualifiers( qualifiers.stream().map(q -> { - List<ComponentHit> hits = componentsPerQualifier.get(q) + List<ComponentDto> componentsOfThisQualifier = componentsPerQualifier.get(q); + List<ComponentHit> hits = componentsOfThisQualifier .stream() .sorted(comparator) .skip(skip) @@ -191,7 +192,7 @@ public class SuggestionsAction implements ComponentsWsAction { .map(ComponentDto::uuid) .map(ComponentHit::new) .collect(MoreCollectors.toList(limit)); - int totalHits = componentsPerQualifier.size(); + int totalHits = componentsOfThisQualifier.size(); return new ComponentHitsPerQualifier(q, hits, totalHits); })).build(); return buildResponse(recentlyBrowsedKeys, favoriteUuids, componentsPerQualifiers, dbSession, authorizedComponents, skip + limit).build(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java index 9247f5cb0ef..c8331873834 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java @@ -63,6 +63,7 @@ import static org.mockito.Mockito.mock; import static org.sonar.api.web.UserRole.USER; import static org.sonar.db.component.ComponentTesting.newModuleDto; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; +import static org.sonar.db.component.ComponentTesting.newPublicProjectDto; import static org.sonar.server.component.ws.SuggestionsAction.PARAM_MORE; import static org.sonar.server.component.ws.SuggestionsAction.PARAM_QUERY; import static org.sonar.server.component.ws.SuggestionsAction.PARAM_RECENTLY_BROWSED; @@ -472,64 +473,113 @@ public class SuggestionsActionTest { } @Test - public void should_not_propose_to_show_more_results_if_0_projects_are_found() throws Exception { - check_proposal_to_show_more_results(0, 0, 0L, null); + public void should_not_propose_to_show_more_results_if_0_projects_are_found() { + check_proposal_to_show_more_results(0, 0, 0L, null, true); } @Test - public void should_not_propose_to_show_more_results_if_5_projects_are_found() throws Exception { - check_proposal_to_show_more_results(5, 5, 0L, null); + public void should_not_propose_to_show_more_results_if_0_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(0, 0, 0L, null, false); } @Test - public void should_not_propose_to_show_more_results_if_6_projects_are_found() throws Exception { - check_proposal_to_show_more_results(6, 6, 0L, null); + public void should_not_propose_to_show_more_results_if_5_projects_are_found() { + check_proposal_to_show_more_results(5, 5, 0L, null, true); } @Test - public void should_propose_to_show_more_results_if_7_projects_are_found() throws Exception { - check_proposal_to_show_more_results(7, 6, 1L, null); + public void should_not_propose_to_show_more_results_if_5_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(5, 5, 0L, null, false); } @Test - public void show_more_results_if_requested_and_5_projects_are_found() throws Exception { - check_proposal_to_show_more_results(5, 0, 0L, SuggestionCategory.PROJECT); + public void should_not_propose_to_show_more_results_if_6_projects_are_found() { + check_proposal_to_show_more_results(6, 6, 0L, null, true); } @Test - public void show_more_results_if_requested_and_6_projects_are_found() throws Exception { - check_proposal_to_show_more_results(6, 0, 0L, SuggestionCategory.PROJECT); + public void should_not_propose_to_show_more_results_if_6_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(6, 6, 0L, null, false); } @Test - public void show_more_results_if_requested_and_7_projects_are_found() throws Exception { - check_proposal_to_show_more_results(7, 1, 0L, SuggestionCategory.PROJECT); + public void should_propose_to_show_more_results_if_7_projects_are_found() { + check_proposal_to_show_more_results(7, 6, 1L, null, true); } @Test - public void show_more_results_if_requested_and_26_projects_are_found() throws Exception { - check_proposal_to_show_more_results(26, 20, 0L, SuggestionCategory.PROJECT); + public void should_propose_to_show_more_results_if_7_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(7, 6, 1L, null, false); } @Test - public void show_more_results_if_requested_and_27_projects_are_found() throws Exception { - check_proposal_to_show_more_results(27, 20, 1L, SuggestionCategory.PROJECT); + public void show_more_results_if_requested_and_5_projects_are_found() { + check_proposal_to_show_more_results(5, 0, 0L, SuggestionCategory.PROJECT, true); } - private void check_proposal_to_show_more_results(int numberOfProjects, int expectedNumberOfResults, long expectedNumberOfMoreResults, @Nullable SuggestionCategory more) - throws Exception { + @Test + public void show_more_results_if_requested_and_5_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(5, 0, 0L, SuggestionCategory.PROJECT, false); + } + + @Test + public void show_more_results_if_requested_and_6_projects_are_found() { + check_proposal_to_show_more_results(6, 0, 0L, SuggestionCategory.PROJECT, true); + } + + @Test + public void show_more_results_if_requested_and_6_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(6, 0, 0L, SuggestionCategory.PROJECT, false); + } + + @Test + public void show_more_results_if_requested_and_7_projects_are_found() { + check_proposal_to_show_more_results(7, 1, 0L, SuggestionCategory.PROJECT, true); + } + + @Test + public void show_more_results_if_requested_and_7_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(7, 1, 0L, SuggestionCategory.PROJECT, false); + } + + @Test + public void show_more_results_if_requested_and_26_projects_are_found() { + check_proposal_to_show_more_results(26, 20, 0L, SuggestionCategory.PROJECT, true); + } + + @Test + public void show_more_results_if_requested_and_26_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(26, 20, 0L, SuggestionCategory.PROJECT, false); + } + + @Test + public void show_more_results_if_requested_and_27_projects_are_found() { + check_proposal_to_show_more_results(27, 20, 1L, SuggestionCategory.PROJECT, true); + } + + @Test + public void show_more_results_if_requested_and_27_projects_are_found_and_no_search_query_is_provided() { + check_proposal_to_show_more_results(27, 20, 1L, SuggestionCategory.PROJECT, false); + } + + private void check_proposal_to_show_more_results(int numberOfProjects, int expectedNumberOfResults, long expectedNumberOfMoreResults, @Nullable SuggestionCategory more, + boolean useQuery) { String namePrefix = "MyProject"; List<ComponentDto> projects = range(0, numberOfProjects) - .mapToObj(i -> db.components().insertComponent(newPrivateProjectDto(organization).setName(namePrefix + i))) + .mapToObj(i -> db.components().insertComponent(newPublicProjectDto(organization).setName(namePrefix + i))) .collect(Collectors.toList()); componentIndexer.indexOnStartup(null); projects.forEach(authorizationIndexerTester::allowOnlyAnyone); TestRequest request = ws.newRequest() - .setMethod("POST") - .setParam(PARAM_QUERY, namePrefix); + .setMethod("POST"); + if (useQuery) { + request.setParam(PARAM_QUERY, namePrefix); + } else { + doReturn(projects).when(favoriteFinder).list(); + } ofNullable(more).ifPresent(c -> request.setParam(PARAM_MORE, c.getName())); SuggestionsWsResponse response = request .executeProtobuf(SuggestionsWsResponse.class); @@ -546,9 +596,13 @@ public class SuggestionsActionTest { .isEmpty(); } else { assertThat(response.getResultsList()) - .filteredOn(q -> q.getItemsCount() > 0) + .filteredOn(c -> "TRK".equals(c.getQ())) .extracting(Category::getMore) .containsExactly(expectedNumberOfMoreResults); + response.getResultsList().stream() + .filter(c -> !"TRK".equals(c.getQ())) + .map(Category::getMore) + .forEach(m -> assertThat(m).isEqualTo(0L)); } } } |