From c1a942976f2ba145f99c4256f7e36c8917554bcb Mon Sep 17 00:00:00 2001 From: Daniel Schwarz Date: Thu, 11 May 2017 11:40:37 +0200 Subject: [PATCH] =?utf8?q?SONAR-9186=20fix=20number=20of=20=E2=80=9Cmore?= =?utf8?q?=E2=80=9D=20in=20api/components/suggestions?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../component/ws/SuggestionsAction.java | 5 +- .../component/ws/SuggestionsActionTest.java | 102 +++++++++++++----- 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 hits = componentsPerQualifier.get(q) + List componentsOfThisQualifier = componentsPerQualifier.get(q); + List 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 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)); } } } -- 2.39.5