aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>2017-04-07 11:39:00 +0200
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>2017-04-20 09:48:52 +0200
commitab0f95c9f298008323c7a4451c1c687762085d64 (patch)
treecdf028ef8c50924014845c6293b30690a658d126 /server
parent49b8e41fd5ff06a68bcaa9d1c4e4438037902298 (diff)
downloadsonarqube-ab0f95c9f298008323c7a4451c1c687762085d64.tar.gz
sonarqube-ab0f95c9f298008323c7a4451c1c687762085d64.zip
SONAR-9071 add number of more results to api/components/suggestions
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java4
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java1
-rw-r--r--server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-suggestions.json3
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java50
4 files changed, 57 insertions, 1 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java
index 6d52e53f304..03ade358200 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java
@@ -44,4 +44,8 @@ public class ComponentsPerQualifier {
public long getTotalHits() {
return totalHits;
}
+
+ public long getNumberOfFurtherResults() {
+ return Math.max(getTotalHits() - componentUuids.size(), 0L);
+ }
}
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 abf3e5703b5..0b2b452f983 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
@@ -128,6 +128,7 @@ public class SuggestionsAction implements ComponentsWsAction {
return Qualifier.newBuilder()
.setQ(qualifier.getQualifier())
+ .setMore(qualifier.getNumberOfFurtherResults())
.addAllItems(results)
.build();
}).collect(MoreCollectors.toList());
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-suggestions.json b/server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-suggestions.json
index 4d18e9909ef..865dad728c6 100644
--- a/server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-suggestions.json
+++ b/server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-suggestions.json
@@ -13,7 +13,8 @@
"key": "org.sonarsource:sonarlint",
"name": "SonarSource :: SonarLint"
}
- ]
+ ],
+ "more": 74
},
{
"q": "FIL",
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 65aafbb04fe..69f8c433d53 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
@@ -20,6 +20,8 @@
package org.sonar.server.component.ws;
import java.io.IOException;
+import java.util.List;
+import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -40,6 +42,7 @@ import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.WsComponents;
import org.sonarqube.ws.WsComponents.SuggestionsWsResponse;
+import static java.util.stream.IntStream.range;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
@@ -48,6 +51,7 @@ import static org.sonarqube.ws.MediaTypes.PROTOBUF;
public class SuggestionsActionTest {
+ public static final int NUMBER_OF_RESULTS_PER_QUALIFIER = 6;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
@Rule
@@ -92,4 +96,50 @@ public class SuggestionsActionTest {
.containsExactly(tuple(project.getKey(), organization.getKey()));
}
+ @Test
+ public void should_propose_to_show_more_results_if_7_projects_are_found() throws Exception {
+ check_proposal_to_show_more_results(7, 1L);
+ }
+
+ @Test
+ public void should_not_propose_to_show_more_results_if_6_projects_are_found() throws Exception {
+ check_proposal_to_show_more_results(6, 0L);
+ }
+ @Test
+ public void should_not_propose_to_show_more_results_if_5_projects_are_found() throws Exception {
+ check_proposal_to_show_more_results(5, 0L);
+ }
+
+ private void check_proposal_to_show_more_results(int numberOfProjects, long numberOfMoreResults) throws Exception {
+ String namePrefix = "MyProject";
+
+ List<ComponentDto> projects = range(0, numberOfProjects)
+ .mapToObj(i -> db.components().insertComponent(newProjectDto(organization).setName(namePrefix + i)))
+ .collect(Collectors.toList());
+
+ componentIndexer.indexOnStartup(null);
+ projects.forEach(authorizationIndexerTester::allowOnlyAnyone);
+
+ SuggestionsWsResponse response = actionTester.newRequest()
+ .setMethod("POST")
+ .setParam(URL_PARAM_QUERY, namePrefix)
+ .executeProtobuf(SuggestionsWsResponse.class);
+
+ // assert match in qualifier "TRK"
+ assertThat(response.getResultsList())
+ .filteredOn(q -> q.getItemsCount() > 0)
+ .extracting(SuggestionsWsResponse.Qualifier::getQ)
+ .containsExactly(Qualifiers.PROJECT);
+
+ // include limited number of results in the response
+ assertThat(response.getResultsList())
+ .flatExtracting(SuggestionsWsResponse.Qualifier::getItemsList)
+ .hasSize(Math.min(NUMBER_OF_RESULTS_PER_QUALIFIER, numberOfProjects));
+
+ // indicate, that there are more results
+ assertThat(response.getResultsList())
+ .filteredOn(q -> q.getItemsCount() > 0)
+ .extracting(SuggestionsWsResponse.Qualifier::getMore)
+ .containsExactly(numberOfMoreResults);
+ }
}