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";
.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));
.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;
}
.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);