@Test public void maximum_length_ok() { String parameter = "maximum_length_param"; defineParameterTestAction(newParam -> newParam.setMaximumLength(10), parameter); String value = repeat("X", 10); String param = underTest.setParam(parameter, value).param(parameter); assertThat(param).isEqualTo(value); }
@Override public void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction(ACTION_CREATE) .setPost(true) .setDescription("Create a Quality Gate.<br>" + "Requires the 'Administer Quality Gates' permission.") .setSince("4.3") .setResponseExample(getClass().getResource("create-example.json")) .setHandler(this); action.createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("The name of the quality gate to create") .setExampleValue("My Quality Gate"); wsSupport.createOrganizationParam(action); }
@Override public void define(NewController controller) { NewAction action = controller.createAction("create") .setDescription("Create a group.<br>" + "Requires the following permission: 'Administer System'.") .setHandler(this) .setPost(true) .setResponseExample(getClass().getResource("create-example.json")) .setSince("5.2"); action.createParam(PARAM_ORGANIZATION_KEY) .setDescription("Key of organization. If unset then default organization is used.") .setExampleValue("my-org") .setSince("6.2") .setInternal(true); action.createParam(PARAM_GROUP_NAME) .setRequired(true) .setMaximumLength(GROUP_NAME_MAX_LENGTH) .setDescription(format("Name for the new group. A group name cannot be larger than %d characters and must be unique. " + "The value 'anyone' (whatever the case) is reserved and cannot be used.", GROUP_NAME_MAX_LENGTH)) .setExampleValue("sonar-users"); action.createParam(PARAM_GROUP_DESCRIPTION) .setMaximumLength(DESCRIPTION_MAX_LENGTH) .setDescription(format("Description for the new group. A group description cannot be larger than %d characters.", DESCRIPTION_MAX_LENGTH)) .setExampleValue("Default group for new users"); }
static void addConditionParams(NewAction action) { action .createParam(PARAM_METRIC) .setDescription("Condition metric.<br/>" + " Only metric of the following types are allowed:" + "<ul>" + "<li>INT</li>" + "<li>MILLISEC</li>" + "<li>RATING</li>" + "<li>WORK_DUR</li>" + "<li>FLOAT</li>" + "<li>PERCENT</li>" + "<li>LEVEL</li>" + "") .setRequired(true) .setExampleValue("blocker_violations"); action.createParam(PARAM_OPERATOR) .setDescription("Condition operator:<br/>" + "<ul>" + "<li>LT = is lower than</li>" + "<li>GT = is greater than</li>" + "</ui>") .setExampleValue(Condition.Operator.GREATER_THAN.getDbValue()) .setPossibleValues(getPossibleOperators()); action.createParam(PARAM_ERROR) .setMaximumLength(CONDITION_MAX_LENGTH) .setDescription("Condition error threshold") .setRequired(true) .setExampleValue("10"); }
.setMaximumLength(MAX_KEY_LENGTH) .setDescription("Key") .setExampleValue("team_size"); .setMaximumLength(MAX_NAME_LENGTH) .setDescription("Name") .setExampleValue("Team Size"); .setMaximumLength(MAX_DESCRIPTION_LENGTH) .setExampleValue("Size of the team"); .setMaximumLength(MAX_DOMAIN_LENGTH) .setDescription("Domain") .setExampleValue("Tests");
@Override public void define(WebService.NewController controller) { NewAction create = controller.createAction(ACTION_CREATE) .setPost(true) .setDescription("Create a quality profile.<br>" + "Requires to be logged in and the 'Administer Quality Profiles' permission.") .setResponseExample(getClass().getResource("create-example.json")) .setSince("5.2") .setHandler(this); createOrganizationParam(create) .setSince("6.4"); create.createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("Quality profile name") .setExampleValue("My Sonar way") .setDeprecatedKey("profileName", "6.6"); create.createParam(PARAM_LANGUAGE) .setRequired(true) .setDescription("Quality profile language") .setExampleValue("js") .setPossibleValues(getOrderedLanguageKeys(languages)); for (ProfileImporter importer : importers) { create.createParam(getBackupParamName(importer.getKey())) .setDescription(String.format("A configuration file for %s.", importer.getName())); } }
@Override public void define(WebService.NewController context) { WebService.NewAction action = context.createAction(ACTION_CREATE) .setDescription("Create a new project link.<br>" + "Requires 'Administer' permission on the specified project, " + "or global 'Administer' permission.") .setHandler(this) .setPost(true) .setResponseExample(getClass().getResource("create-example.json")) .setSince("6.1"); action.createParam(PARAM_PROJECT_ID) .setDescription("Project id") .setExampleValue(UUID_EXAMPLE_01); action.createParam(PARAM_PROJECT_KEY) .setDescription("Project key") .setExampleValue(KEY_PROJECT_EXAMPLE_001); action.createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(LINK_NAME_MAX_LENGTH) .setDescription("Link name") .setExampleValue("Custom"); action.createParam(PARAM_URL) .setRequired(true) .setMaximumLength(LINK_URL_MAX_LENGTH) .setDescription("Link url") .setExampleValue("http://example.com"); }
@Override public void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction(UPDATE_ACTION) .setPost(true) .setDescription("Update a Webhook.<br>" + "Requires 'Administer' permission on the specified project, or global 'Administer' permission.") .setSince("7.1") .setHandler(this); action.createParam(KEY_PARAM) .setRequired(true) .setMaximumLength(KEY_PARAM_MAXIMUN_LENGTH) .setDescription("The key of the webhook to be updated, "+ "auto-generated value can be obtained through api/webhooks/create or api/webhooks/list") .setExampleValue(KEY_PROJECT_EXAMPLE_001); action.createParam(NAME_PARAM) .setRequired(true) .setMaximumLength(NAME_PARAM_MAXIMUM_LENGTH) .setDescription("new name of the webhook") .setExampleValue(NAME_WEBHOOK_EXAMPLE_001); action.createParam(URL_PARAM) .setRequired(true) .setMaximumLength(URL_PARAM_MAXIMUM_LENGTH) .setDescription("new url to be called by the webhook") .setExampleValue(URL_WEBHOOK_EXAMPLE_001); }
@Override public void define(NewController context) { NewAction action = context.createAction("update") .setDescription("Update a group.<br>" + "Requires the following permission: 'Administer System'.") .setHandler(this) .setPost(true) .setResponseExample(getClass().getResource("update.example.json")) .setSince("5.2") .setChangelog(new Change("6.4", "The default group is no longer editable")); action.createParam(PARAM_GROUP_ID) .setDescription("Identifier of the group.") .setExampleValue("42") .setRequired(true); action.createParam(PARAM_GROUP_NAME) .setMaximumLength(GROUP_NAME_MAX_LENGTH) .setDescription(format("New optional name for the group. A group name cannot be larger than %d characters and must be unique. " + "Value 'anyone' (whatever the case) is reserved and cannot be used. If value is empty or not defined, then name is not changed.", GROUP_NAME_MAX_LENGTH)) .setExampleValue("my-group"); action.createParam(PARAM_GROUP_DESCRIPTION) .setMaximumLength(DESCRIPTION_MAX_LENGTH) .setDescription(format("New optional description for the group. A group description cannot be larger than %d characters. " + "If value is not defined, then description is not changed.", DESCRIPTION_MAX_LENGTH)) .setExampleValue("Default group for new users"); }
@Override public void define(WebService.NewController context) { WebService.NewAction action = context.createAction("update_event") .setDescription("Update a project analysis event.<br>" + "Only events of category '%s' and '%s' can be updated.<br>" + "Requires one of the following permissions:" + "<ul>" + " <li>'Administer System'</li>" + " <li>'Administer' rights on the specified project</li>" + "</ul>", EventCategory.VERSION.name(), EventCategory.OTHER.name()) .setSince("6.3") .setPost(true) .setResponseExample(getClass().getResource("update_event-example.json")) .setHandler(this); action.createParam(PARAM_EVENT) .setDescription("Event key") .setExampleValue(Uuids.UUID_EXAMPLE_08) .setRequired(true); action.createParam(PARAM_NAME) .setMaximumLength(org.sonar.db.event.EventValidator.MAX_NAME_LENGTH) .setDescription("New name") .setExampleValue("5.6") .setRequired(true); }
void addOrganizationDetailsParams(WebService.NewAction action, boolean isNameRequired) { action.createParam(PARAM_NAME) .setRequired(isNameRequired) .setMinimumLength(NAME_MIN_LENGTH) .setMaximumLength(NAME_MAX_LENGTH) .setDescription("Name of the organization") .setExampleValue("Foo Company"); action.createParam(PARAM_DESCRIPTION) .setRequired(false) .setMaximumLength(DESCRIPTION_MAX_LENGTH) .setDescription("Description of the organization.<br/> It must be less than 256 chars long.") .setExampleValue("The Foo company produces quality software for Bar."); action.createParam(PARAM_URL) .setRequired(false) .setMaximumLength(URL_MAX_LENGTH) .setDescription("URL of the organization.<br/> It must be less than 256 chars long.") .setExampleValue("https://www.foo.com"); action.createParam(PARAM_AVATAR_URL) .setRequired(false) .setMaximumLength(URL_MAX_LENGTH) .setDescription("URL of the organization avatar.<br/> It must be less than 256 chars long.") .setExampleValue("https://www.foo.com/foo.png"); }
@Override public void define(WebService.NewController controller) { NewAction setDefault = controller.createAction("rename") .setPost(true) .setDescription("Rename a quality profile.<br> " + "Requires one of the following permissions:" + "<ul>" + " <li>'Administer Quality Profiles'</li>" + " <li>Edit right on the specified quality profile</li>" + "</ul>") .setSince("5.2") .setHandler(this); setDefault.createParam(PARAM_KEY) .setRequired(true) .setDescription("Quality profile key") .setExampleValue(UUID_EXAMPLE_01); setDefault.createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("New quality profile name") .setExampleValue("My Sonar way"); }
@Override public void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction("set_setting") .setDescription("Update a setting value.<br>" + "Requires user to be authenticated") .setSince("7.6") .setInternal(true) .setPost(true) .setHandler(this); action.createParam(PARAM_KEY) .setRequired(true) .setMaximumLength(100) .setDescription("Setting key") .setPossibleValues("notifications.optOut", UserUpdater.NOTIFICATIONS_READ_DATE); action.createParam(PARAM_VALUE) .setRequired(true) .setMaximumLength(4000) .setDescription("Setting value") .setExampleValue("true"); }
@Override public void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction("update_login") .setDescription("Update a user login. A login can be updated many times,<br/>" + "Updating the login of a user will invalidate his browser session, he will be required to login again.<br/>" + "Requires Administer System permission") .setSince("7.6") .setPost(true) .setHandler(this); action.createParam(PARAM_LOGIN) .setRequired(true) .setDescription("The current login (case-sensitive)") .setExampleValue("mylogin"); action.createParam(PARAM_NEW_LOGIN) .setRequired(true) .setMaximumLength(LOGIN_MAX_LENGTH) .setMinimumLength(LOGIN_MIN_LENGTH) .setDescription("The new login. It must not already exist.") .setExampleValue("mynewlogin"); }
@Override public void define(WebService.NewController context) { WebService.NewAction action = context.createAction(ACTION_GENERATE) .setSince("5.3") .setPost(true) .setDescription("Generate a user access token. <br />" + "Please keep your tokens secret. They enable to authenticate and analyze projects.<br />" + "It requires administration permissions to specify a 'login' and generate a token for another user. Otherwise, a token is generated for the current user.") .setResponseExample(getClass().getResource("generate-example.json")) .setHandler(this); action.createParam(PARAM_LOGIN) .setDescription("User login. If not set, the token is generated for the authenticated user.") .setExampleValue("g.hopper"); action.createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(MAX_TOKEN_NAME_LENGTH) .setDescription("Token name") .setExampleValue("Project scan on Travis"); }
@Override public void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction("rename") .setPost(true) .setDescription("Rename a Quality Gate.<br>" + "Requires the 'Administer Quality Gates' permission.") .setSince("4.3") .setHandler(this); action.createParam(PARAM_ID) .setRequired(true) .setDescription("ID of the quality gate to rename") .setExampleValue("1"); action.createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(NAME_MAXIMUM_LENGTH) .setDescription("New name of the quality gate") .setExampleValue("My Quality Gate"); wsSupport.createOrganizationParam(action); }
@Override public void define(NewController context) { WebService.NewAction action = context.createAction(ACTION_RENAME) .setSince("6.6") .setDescription("Rename the main branch of a project.<br/>" + "Requires 'Administer' permission on the specified project.") .setPost(true) .setHandler(this); addProjectParam(action); action .createParam(PARAM_NAME) .setRequired(true) .setMaximumLength(255) .setDescription("New name of the main branch") .setExampleValue("branch1"); }
@Override public void define(WebService.NewController controller) { WebService.NewAction action = controller.createAction(DELETE_ACTION) .setPost(true) .setDescription("Delete a Webhook.<br>" + "Requires 'Administer' permission on the specified project, or global 'Administer' permission.") .setSince("7.1") .setHandler(this); action.createParam(KEY_PARAM) .setRequired(true) .setMaximumLength(KEY_PARAM_MAXIMUN_LENGTH) .setDescription("The key of the webhook to be deleted, "+ "auto-generated value can be obtained through api/webhooks/create or api/webhooks/list") .setExampleValue(KEY_PROJECT_EXAMPLE_001); }
@Test public void maximum_length_not_ok() { String parameter = "maximum_length_param"; defineParameterTestAction(newParam -> newParam.setMaximumLength(10), parameter); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(format("'%s' length (11) is longer than the maximum authorized (10)", parameter)); underTest.setParam(parameter, repeat("X", 11)).param(parameter); }
@Test public void param_with_maximum_length() { ((WebService) context -> { NewController newController = context.createController("api/custom_measures"); NewAction create = newDefaultAction(newController, "create"); create.createParam("string_value") .setMaximumLength(24); newController.done(); }).define(context); WebService.Action action = context.controller("api/custom_measures").action("create"); assertThat(action.param("string_value").maximumLength()).isEqualTo(24); }