*/
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";
.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)
.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),
.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);