]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7135 WS api/measures/component_tree search query parameter has at least 3 chara...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 4 Mar 2016 08:51:03 +0000 (09:51 +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/measure/ws/ComponentTreeAction.java
server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java

index 7f903223247d3b21dc75263bc33423fa1cf98650..f7850a85908de3a30d62bbc6987d9949540d6883 100644 (file)
@@ -75,6 +75,7 @@ import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_STRATEG
  */
 public class ComponentTreeAction implements MeasuresWsAction {
   private static final int MAX_SIZE = 500;
+  private static final int QUERY_MINIMUM_LENGTH = 3;
   static final String ALL_STRATEGY = "all";
   static final String CHILDREN_STRATEGY = "children";
   static final String LEAVES_STRATEGY = "leaves";
@@ -120,10 +121,11 @@ public class ComponentTreeAction implements MeasuresWsAction {
       .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");
 
     action.createParam(PARAM_BASE_COMPONENT_ID)
@@ -243,6 +245,9 @@ public class ComponentTreeAction implements MeasuresWsAction {
       .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
       .setQuery(request.param(Param.TEXT_QUERY));
     checkRequest(componentTreeWsRequest.getPageSize() <= MAX_SIZE, "The '%s' parameter must be less than %d", Param.PAGE_SIZE, MAX_SIZE);
+    String searchQuery = componentTreeWsRequest.getQuery();
+    checkRequest(searchQuery == null || searchQuery.length() >= QUERY_MINIMUM_LENGTH,
+      "The '%s' parameter must have at least %d characters", Param.TEXT_QUERY, QUERY_MINIMUM_LENGTH);
     String metricSortValue = componentTreeWsRequest.getMetricSort();
     checkRequest(!componentTreeWsRequest.getMetricKeys().isEmpty(), "The '%s' parameter must contain at least one metric key", PARAM_METRIC_KEYS);
     checkRequest(metricSortValue == null ^ componentTreeWsRequest.getSort().contains(METRIC_SORT),
index 2f6af91b77029ee5bc4926a674c1711d927e527d..bb5cf2c44f9d0390ae3976f69a115f4b32c59f88 100644 (file)
@@ -318,6 +318,20 @@ public class ComponentTreeActionTest {
       .setParam(PARAM_METRIC_KEYS, "ncloc, new_violations, unknown-metric, another-unknown-metric"));
   }
 
+  @Test
+  public void fail_when_search_query_have_less_than_3_characters() {
+    componentDb.insertProjectAndSnapshot(newProjectDto("project-uuid"));
+    insertNclocMetric();
+    insertNewViolationsMetric();
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("The 'q' parameter must have at least 3 characters");
+
+    call(ws.newRequest()
+      .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
+      .setParam(PARAM_METRIC_KEYS, "ncloc, new_violations")
+      .setParam(Param.TEXT_QUERY, "fi"));
+  }
+
   @Test
   public void fail_when_insufficient_privileges() {
     userSession.anonymous().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);