]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9071 add number of more results to api/components/suggestions
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Fri, 7 Apr 2017 09:39:00 +0000 (11:39 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Thu, 20 Apr 2017 07:48:52 +0000 (09:48 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentsPerQualifier.java
server/sonar-server/src/main/java/org/sonar/server/component/ws/SuggestionsAction.java
server/sonar-server/src/main/resources/org/sonar/server/component/ws/components-example-suggestions.json
server/sonar-server/src/test/java/org/sonar/server/component/ws/SuggestionsActionTest.java
sonar-ws/src/main/protobuf/ws-components.proto

index 6d52e53f304008b19219cbb32f1167e51338aa5b..03ade3582002b845ec565bd0d3bbd66e9a14334f 100644 (file)
@@ -44,4 +44,8 @@ public class ComponentsPerQualifier {
   public long getTotalHits() {
     return totalHits;
   }
+
+  public long getNumberOfFurtherResults() {
+    return Math.max(getTotalHits() - componentUuids.size(), 0L);
+  }
 }
index abf3e5703b535d3169c3e7e9d2828eea69c6bfe2..0b2b452f983a5af5984d642b35de556a48751366 100644 (file)
@@ -128,6 +128,7 @@ public class SuggestionsAction implements ComponentsWsAction {
 
         return Qualifier.newBuilder()
           .setQ(qualifier.getQualifier())
+          .setMore(qualifier.getNumberOfFurtherResults())
           .addAllItems(results)
           .build();
       }).collect(MoreCollectors.toList());
index 4d18e9909ef960500784cd0e082655176da10993..865dad728c66bbce4bc91341291a0eee91aaabf8 100644 (file)
@@ -13,7 +13,8 @@
           "key": "org.sonarsource:sonarlint",
           "name": "SonarSource :: SonarLint"
         }
-      ]
+      ],
+      "more": 74
     },
     {
       "q": "FIL",
index 65aafbb04fe8b64b4440bc5ab3c4ddb55bc134c9..69f8c433d53b4bb84e2f8bb1e30a7c20ca908af9 100644 (file)
@@ -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);
+  }
 }
index 2524346cd366486658a5fdc65d9eeb880fe23869..3d289f86b73767ef113a1cd5452a5c02e8cd1b4d 100644 (file)
@@ -52,7 +52,8 @@ message SuggestionsWsResponse {
   
   message Qualifier {
        optional string q = 1;
-       repeated Component items = 4;
+       repeated Component items = 2;
+    optional int64 more = 3;
   }
 }