]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7129 WS api/components/tree search query parameter must have at least 3 characters
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 4 Mar 2016 08:58:14 +0000 (09:58 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 4 Mar 2016 15:30:42 +0000 (16:30 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ws/TreeAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/TreeActionTest.java

index 09a7efdb496faee941a96d8414964994ee9a053e..292b77400e7b49b8c0eaa2d3c151ea24ecaa82f4 100644 (file)
@@ -73,6 +73,7 @@ import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_STR
 
 public class TreeAction implements ComponentsWsAction {
   private static final int MAX_SIZE = 500;
+  private static final int QUERY_MINIMUM_LENGTH = 3;
   private static final String ALL_STRATEGY = "all";
   private static final String CHILDREN_STRATEGY = "children";
   private static final String LEAVES_STRATEGY = "leaves";
@@ -125,10 +126,11 @@ public class TreeAction implements ComponentsWsAction {
       .setExampleValue(NAME_SORT + ", " + PATH_SORT);
 
     action.createParam(Param.TEXT_QUERY)
-      .setDescription("Limit search to: <ul>" +
+      .setDescription(format("Limit search to: <ul>" +
         "<li>component names that contain the supplied string</li>" +
         "<li>component keys that are exactly the same as the supplied string</li>" +
-        "</ul>")
+        "</ul>" +
+        "Must have at least %d characters", QUERY_MINIMUM_LENGTH))
       .setExampleValue("FILE_NAM");
 
     createQualifiersParameter(action, newQualifierParameterContext(userSession, i18n, resourceTypes));
@@ -292,6 +294,9 @@ public class TreeAction implements ComponentsWsAction {
       .setPage(request.mandatoryParamAsInt(Param.PAGE))
       .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE));
     checkRequest(treeWsRequest.getPageSize() <= MAX_SIZE, "The '%s' parameter must be less than %d", Param.PAGE_SIZE, MAX_SIZE);
+    String searchQuery = treeWsRequest.getQuery();
+    checkRequest(searchQuery == null || searchQuery.length() >= QUERY_MINIMUM_LENGTH,
+      "The '%s' parameter must have at least %d characters", Param.TEXT_QUERY, QUERY_MINIMUM_LENGTH);
 
     return treeWsRequest;
   }
index b2616105467caea506a91779e012759565c7ca19..fa5008f95d504d57586cdd8e395b153d56fd9fc9 100644 (file)
@@ -307,6 +307,19 @@ public class TreeActionTest {
       .execute();
   }
 
+  @Test
+  public void fail_when_search_query_has_less_than_3_characters() {
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("The 'q' parameter must have at least 3 characters");
+    componentDb.insertComponent(newProjectDto("project-uuid"));
+    db.commit();
+
+    ws.newRequest()
+      .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+      .setParam(Param.TEXT_QUERY, "fi")
+      .execute();
+  }
+
   @Test
   public void fail_when_sort_is_unknown() {
     expectedException.expect(IllegalArgumentException.class);