diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2019-01-29 11:24:02 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-02-11 09:11:48 +0100 |
commit | db18af0468a0c79a104afcc5cd9bcca8e30704c9 (patch) | |
tree | ab6d33b8d4de1726d1b8598714ed8eb8bbb82acf | |
parent | bc7e1d7ab9188e9892fc216ec13af622bab17e00 (diff) | |
download | sonarqube-db18af0468a0c79a104afcc5cd9bcca8e30704c9.tar.gz sonarqube-db18af0468a0c79a104afcc5cd9bcca8e30704c9.zip |
fix Request.mandatoryParam not failing on blank parameter value
25 files changed, 134 insertions, 131 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RequestValidator.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RequestValidator.java index 941628cadc3..dab4e60be70 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RequestValidator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RequestValidator.java @@ -35,7 +35,6 @@ import org.sonar.server.ws.WsUtils; import static com.google.common.base.Strings.isNullOrEmpty; import static java.lang.String.format; -import static org.apache.commons.lang.StringUtils.isBlank; import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN; @@ -43,7 +42,6 @@ import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_Q public class RequestValidator { public static final String MSG_TEMPLATE_WITH_SAME_NAME = "A template with the name '%s' already exists (case insensitive)."; - private static final String MSG_TEMPLATE_NAME_NOT_BLANK = "The template name must not be blank"; private final PermissionService permissionService; private final String allProjectsPermissionsOnOneLine; @@ -69,10 +67,6 @@ public class RequestValidator { format("It is not possible to add the '%s' permission to group 'Anyone'.", permission)); } - public static void validateTemplateNameFormat(String name) { - checkRequest(!isBlank(name), MSG_TEMPLATE_NAME_NOT_BLANK); - } - public static void validateQualifier(@Nullable String qualifier, ResourceTypes resourceTypes) { if (qualifier == null) { return; diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/CreateTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/CreateTemplateAction.java index 9252bfd99a0..9c8e5458f85 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/CreateTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/CreateTemplateAction.java @@ -118,8 +118,6 @@ public class CreateTemplateAction implements PermissionsWsAction { } private void validateTemplateNameForCreation(DbSession dbSession, OrganizationDto org, String name) { - RequestValidator.validateTemplateNameFormat(name); - PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao() .selectByName(dbSession, org.getUuid(), name); checkRequest(permissionTemplateWithSameName == null, format(MSG_TEMPLATE_WITH_SAME_NAME, name)); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java index 4f47894966c..bf40cc593c9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/UpdateTemplateAction.java @@ -34,12 +34,14 @@ import org.sonar.server.permission.ws.PermissionsWsAction; import org.sonar.server.permission.ws.RequestValidator; import org.sonar.server.permission.ws.WsParameters; import org.sonar.server.user.UserSession; +import org.sonar.server.ws.WsUtils; import org.sonarqube.ws.Permissions.PermissionTemplate; import org.sonarqube.ws.Permissions.UpdateTemplateWsResponse; import static com.google.common.base.MoreObjects.firstNonNull; import static java.lang.String.format; import static java.util.Objects.requireNonNull; +import static org.apache.commons.lang.StringUtils.isBlank; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin; import static org.sonar.server.permission.ws.RequestValidator.MSG_TEMPLATE_WITH_SAME_NAME; import static org.sonar.server.permission.ws.template.PermissionTemplateDtoToPermissionTemplateResponse.toPermissionTemplateResponse; @@ -141,7 +143,7 @@ public class UpdateTemplateAction implements PermissionsWsAction { } private void validateTemplateNameForUpdate(DbSession dbSession, String organizationUuid, String name, long id) { - RequestValidator.validateTemplateNameFormat(name); + WsUtils.checkRequest(!isBlank(name), "The template name must not be blank"); PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, organizationUuid, name); checkRequest(permissionTemplateWithSameName == null || permissionTemplateWithSameName.getId() == id, diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java index 989df2eb478..0b15bb9970a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java @@ -47,7 +47,6 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkState; import static java.lang.String.format; import static java.util.Optional.ofNullable; -import static org.apache.commons.lang.StringUtils.isNotBlank; import static org.sonar.db.event.EventValidator.MAX_NAME_LENGTH; import static org.sonar.server.projectanalysis.ws.EventCategory.OTHER; import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION; @@ -163,7 +162,6 @@ public class CreateEventAction implements ProjectAnalysesWsAction { } private EventDto toDbEvent(CreateEventRequest request, SnapshotDto analysis) { - checkArgument(isNotBlank(request.getName()), "A non empty name is required"); return new EventDto() .setUuid(uuidFactory.create()) .setAnalysisUuid(analysis.getUuid()) diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/SetBaselineAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/SetBaselineAction.java index 7694ae9b9a1..981cf8659f0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/SetBaselineAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/SetBaselineAction.java @@ -38,6 +38,7 @@ import org.sonar.server.user.UserSession; import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; +import static org.apache.commons.lang.StringUtils.trimToNull; import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY; import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS; import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_BRANCH; @@ -90,10 +91,9 @@ public class SetBaselineAction implements ProjectAnalysesWsAction { } private void doHandle(Request request) { - String projectKey = mandatoryNonEmptyParam(request, PARAM_PROJECT); - String branchKey = request.param(PARAM_BRANCH); - checkArgument(branchKey == null || !branchKey.isEmpty(), "The '%s' parameter must not be empty", PARAM_BRANCH); - String analysisUuid = mandatoryNonEmptyParam(request, PARAM_ANALYSIS); + String projectKey = request.mandatoryParam(PARAM_PROJECT); + String branchKey = trimToNull(request.param(PARAM_BRANCH)); + String analysisUuid = request.mandatoryParam(PARAM_ANALYSIS); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto projectBranch = getProjectBranch(dbSession, projectKey, branchKey); @@ -104,12 +104,6 @@ public class SetBaselineAction implements ProjectAnalysesWsAction { } } - private static String mandatoryNonEmptyParam(Request request, String param) { - String value = request.mandatoryParam(param); - checkArgument(!value.isEmpty(), "The '%s' parameter must not be empty", param); - return value; - } - private ComponentDto getProjectBranch(DbSession dbSession, String projectKey, @Nullable String branchKey) { if (branchKey == null) { return componentFinder.getByUuidOrKey(dbSession, null, projectKey, PROJECT_ID_AND_KEY); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java index f3a020cd57c..d95f64fd6c4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java @@ -29,7 +29,6 @@ import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.user.UserSession; -import static com.google.common.base.Preconditions.checkArgument; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.server.qualitygate.ws.QualityGatesWs.parseId; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; @@ -79,8 +78,6 @@ public class CopyAction implements QualityGatesWsAction { Long id = parseId(request, PARAM_ID); String destinationName = request.mandatoryParam(PARAM_NAME); - checkArgument(!destinationName.isEmpty(), "The 'name' parameter is empty"); - try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = wsSupport.getOrganization(dbSession, request); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java index 7ee5c627579..b9d9892f924 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java @@ -31,7 +31,6 @@ import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.user.UserSession; import org.sonarqube.ws.Qualitygates.CreateResponse; -import static com.google.common.base.Preconditions.checkArgument; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.ACTION_CREATE; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; import static org.sonar.server.ws.WsUtils.writeProtobuf; @@ -80,7 +79,6 @@ public class CreateAction implements QualityGatesWsAction { userSession.checkPermission(OrganizationPermission.ADMINISTER_QUALITY_GATES, organizationDto.getUuid()); String name = request.mandatoryParam(PARAM_NAME); - checkArgument(!name.isEmpty(), "The 'name' parameter is empty"); QualityGateDto newQualityGate = qualityGateUpdater.create(dbSession, organizationDto, name); CreateResponse.Builder createResponse = CreateResponse.newBuilder() diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java index 6e450e70e55..ea6faab78fc 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java @@ -30,11 +30,9 @@ import org.sonar.db.qualitygate.QualityGateDto; import org.sonarqube.ws.Qualitygates.QualityGate; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Strings.isNullOrEmpty; import static org.sonar.server.qualitygate.ws.CreateAction.NAME_MAXIMUM_LENGTH; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; -import static org.sonar.server.util.Validation.CANT_BE_EMPTY_MESSAGE; import static org.sonar.server.ws.WsUtils.writeProtobuf; public class RenameAction implements QualityGatesWsAction { @@ -86,7 +84,6 @@ public class RenameAction implements QualityGatesWsAction { private QualityGateDto rename(DbSession dbSession, OrganizationDto organization, long id, String name) { QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, id); wsSupport.checkCanEdit(qualityGate); - checkArgument(!isNullOrEmpty(name), CANT_BE_EMPTY_MESSAGE, "Name"); checkNotAlreadyExists(dbSession, organization, qualityGate, name); qualityGate.setName(name); dbClient.qualityGateDao().update(qualityGate, dbSession); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RenameAction.java index 688b2a35dd1..05e8497628e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RenameAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RenameAction.java @@ -19,7 +19,6 @@ */ package org.sonar.server.qualityprofile.ws; -import org.apache.commons.lang.StringUtils; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -88,7 +87,6 @@ public class RenameAction implements QProfileWsAction { } private void doHandle(String newName, String profileKey) { - checkRequest(StringUtils.isNotBlank(newName), "Name must be set"); checkRequest(newName.length() <= MAXIMUM_NAME_LENGTH, "Name is too long (>%d characters)", MAXIMUM_NAME_LENGTH); userSession.checkLoggedIn(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java index 0e2b51c0576..314fff12448 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/EncryptAction.java @@ -61,7 +61,6 @@ public class EncryptAction implements SettingsWsAction { userSession.checkIsSystemAdministrator(); String value = request.mandatoryParam(PARAM_VALUE); - checkRequest(!value.isEmpty(), "Parameter '%s' must not be empty", PARAM_VALUE); Encryption encryption = settings.getEncryption(); checkRequest(encryption.hasSecretKey(), "No secret key available"); diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java index 38991fd6c27..8a1e6df9b1b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/SetAction.java @@ -59,7 +59,6 @@ import org.sonar.server.user.UserSession; import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; -import static org.apache.commons.lang.StringUtils.isNotEmpty; import static org.sonar.server.setting.ws.SettingsWsParameters.PARAM_BRANCH; import static org.sonar.server.setting.ws.SettingsWsParameters.PARAM_COMPONENT; import static org.sonar.server.setting.ws.SettingsWsParameters.PARAM_FIELD_VALUES; @@ -301,7 +300,6 @@ public class SetAction implements SettingsWsAction { .setComponent(request.param(PARAM_COMPONENT)) .setBranch(request.param(PARAM_BRANCH)) .setPullRequest(request.param(PARAM_PULL_REQUEST)); - checkArgument(isNotEmpty(set.getKey()), "Setting key is mandatory and must not be empty"); checkArgument(set.getValues() != null, "Setting values must not be null"); checkArgument(set.getFieldValues() != null, "Setting fields values must not be null"); return set; diff --git a/server/sonar-server/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java b/server/sonar-server/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java index ee8d21af7ca..9e405d6696c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/usertoken/ws/GenerateAction.java @@ -86,7 +86,7 @@ public class GenerateAction implements UserTokensWsAction { private UserTokens.GenerateWsResponse doHandle(Request request) { try (DbSession dbSession = dbClient.openSession(false)) { - String name = getName(request); + String name = request.mandatoryParam(PARAM_NAME).trim(); UserDto user = userTokenSupport.getUser(dbSession, request); checkTokenDoesNotAlreadyExists(dbSession, user, name); @@ -111,12 +111,6 @@ public class GenerateAction implements UserTokensWsAction { checkRequest(userTokenDto == null, "A user token for login '%s' and name '%s' already exists", user.getLogin(), name); } - private static String getName(Request request) { - String name = request.mandatoryParam(PARAM_NAME).trim(); - checkRequest(!name.isEmpty(), "The '%s' parameter must not be blank", PARAM_NAME); - return name; - } - private UserTokenDto insertTokenInDb(DbSession dbSession, UserDto user, String name, String tokenHash) { UserTokenDto userTokenDto = new UserTokenDto() .setUserUuid(user.getUuid()) diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/CreateTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/CreateTemplateActionTest.java index 01a2a9e062b..80843e037bb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/CreateTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/CreateTemplateActionTest.java @@ -48,7 +48,7 @@ public class CreateTemplateActionTest extends BasePermissionWsTest<CreateTemplat } @Test - public void create_full_permission_template() throws Exception { + public void create_full_permission_template() { loginAsAdmin(db.getDefaultOrganization()); TestResponse result = newRequest("Finance", "Permissions for financially related projects", ".*\\.finance\\..*"); @@ -66,7 +66,7 @@ public class CreateTemplateActionTest extends BasePermissionWsTest<CreateTemplat } @Test - public void create_minimalist_permission_template() throws Exception { + public void create_minimalist_permission_template() { loginAsAdmin(db.getDefaultOrganization()); newRequest("Finance", null, null); @@ -81,7 +81,7 @@ public class CreateTemplateActionTest extends BasePermissionWsTest<CreateTemplat } @Test - public void fail_if_name_not_provided() throws Exception { + public void fail_if_name_not_provided() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(IllegalArgumentException.class); @@ -90,17 +90,17 @@ public class CreateTemplateActionTest extends BasePermissionWsTest<CreateTemplat } @Test - public void fail_if_name_empty() throws Exception { + public void fail_if_name_empty() { loginAsAdmin(db.getDefaultOrganization()); - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("The template name must not be blank"); + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'name' parameter is missing"); newRequest("", null, null); } @Test - public void fail_if_regexp_if_not_valid() throws Exception { + public void fail_if_regexp_if_not_valid() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(BadRequestException.class); @@ -110,7 +110,7 @@ public class CreateTemplateActionTest extends BasePermissionWsTest<CreateTemplat } @Test - public void fail_if_name_already_exists_in_database_case_insensitive() throws Exception { + public void fail_if_name_already_exists_in_database_case_insensitive() { loginAsAdmin(db.getDefaultOrganization()); PermissionTemplateDto template = db.permissionTemplates().insertTemplate(db.getDefaultOrganization()); @@ -121,7 +121,7 @@ public class CreateTemplateActionTest extends BasePermissionWsTest<CreateTemplat } @Test - public void fail_if_not_admin() throws Exception { + public void fail_if_not_admin() { userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES, db.getDefaultOrganization()); expectedException.expect(ForbiddenException.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java index 396b19f9076..1904cfa36d7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/UpdateTemplateActionTest.java @@ -67,7 +67,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void update_all_permission_template_fields() throws Exception { + public void update_all_permission_template_fields() { loginAsAdmin(db.getDefaultOrganization()); String result = call(template.getUuid(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*"); @@ -85,7 +85,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void update_with_the_same_values() throws Exception { + public void update_with_the_same_values() { loginAsAdmin(db.getDefaultOrganization()); call(template.getUuid(), template.getName(), template.getDescription(), template.getKeyPattern()); @@ -97,7 +97,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void update_name_only() throws Exception { + public void update_name_only() { loginAsAdmin(db.getDefaultOrganization()); call(template.getUuid(), "Finance", null, null); @@ -109,7 +109,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_key_is_not_found() throws Exception { + public void fail_if_key_is_not_found() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(NotFoundException.class); @@ -119,7 +119,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_name_already_exists_in_another_template() throws Exception { + public void fail_if_name_already_exists_in_another_template() { loginAsAdmin(db.getDefaultOrganization()); PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization(); @@ -130,7 +130,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_key_is_not_provided() throws Exception { + public void fail_if_key_is_not_provided() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(IllegalArgumentException.class); @@ -139,7 +139,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_name_empty() throws Exception { + public void fail_if_name_empty() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(BadRequestException.class); @@ -149,7 +149,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_name_has_just_whitespaces() throws Exception { + public void fail_if_name_has_just_whitespaces() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(BadRequestException.class); @@ -159,7 +159,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_regexp_if_not_valid() throws Exception { + public void fail_if_regexp_if_not_valid() { loginAsAdmin(db.getDefaultOrganization()); expectedException.expect(BadRequestException.class); @@ -169,7 +169,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_name_already_exists_in_database_case_insensitive() throws Exception { + public void fail_if_name_already_exists_in_database_case_insensitive() { loginAsAdmin(db.getDefaultOrganization()); PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization(); @@ -181,7 +181,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_not_logged_in() throws Exception { + public void fail_if_not_logged_in() { expectedException.expect(UnauthorizedException.class); userSession.anonymous(); @@ -189,7 +189,7 @@ public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplat } @Test - public void fail_if_not_admin() throws Exception { + public void fail_if_not_admin() { userSession.logIn().addPermission(SCAN, db.getDefaultOrganization()); expectedException.expect(ForbiddenException.class); diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java index e818d1b4bf9..6f25952f6bf 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/CreateEventActionTest.java @@ -212,7 +212,7 @@ public class CreateEventActionTest { logInAsProjectAdministrator(project); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("A non empty name is required"); + expectedException.expectMessage("The 'name' parameter is missing"); call(OTHER.name(), " ", analysis.getUuid()); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/SetBaselineActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/SetBaselineActionTest.java index 6ea8758481c..418d3121172 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/SetBaselineActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/SetBaselineActionTest.java @@ -73,7 +73,8 @@ public class SetBaselineActionTest { private WsActionTester ws = new WsActionTester(new SetBaselineAction(dbClient, userSession, TestComponentFinder.from(db))); @Test - public void set_baseline_on_main_branch() { + @UseDataProvider("nullOrEmpty") + public void set_baseline_on_main_branch(@Nullable String branchName) { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); BranchDto branch = new BranchDto() .setBranchType(BranchType.LONG) @@ -85,12 +86,21 @@ public class SetBaselineActionTest { SnapshotDto analysis = db.components().insertSnapshot(project); logInAsProjectAdministrator(project); - call(ImmutableMap.of(PARAM_PROJECT, project.getKey(), PARAM_ANALYSIS, analysis.getUuid())); + call(project.getKey(), branchName, analysis.getUuid()); BranchDto loaded = dbClient.branchDao().selectByUuid(dbSession, branch.getUuid()).get(); assertThat(loaded.getManualBaseline()).isEqualTo(analysis.getUuid()); } + @DataProvider + public static Object[][] nullOrEmpty() { + return new Object[][] { + {null}, + {""}, + {" "}, + }; + } + @Test public void set_baseline_on_long_living_branch() { ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert()); @@ -138,10 +148,9 @@ public class SetBaselineActionTest { return new Object[][] { {builder.put(PARAM_PROJECT, null).map, "The 'project' parameter is missing"}, - {builder.put(PARAM_PROJECT, "").map, "The 'project' parameter must not be empty"}, - {builder.put(PARAM_BRANCH, "").map, "The 'branch' parameter must not be empty"}, + {builder.put(PARAM_PROJECT, "").map, "The 'project' parameter is missing"}, {builder.put(PARAM_ANALYSIS, null).map, "The 'analysis' parameter is missing"}, - {builder.put(PARAM_ANALYSIS, "").map, "The 'analysis' parameter must not be empty"}, + {builder.put(PARAM_ANALYSIS, "").map, "The 'analysis' parameter is missing"}, }; } @@ -276,11 +285,17 @@ public class SetBaselineActionTest { httpRequest.execute(); } - private void call(String projectKey, String branchKey, String analysisUuid) { - call(ImmutableMap.of( - PARAM_PROJECT, projectKey, - PARAM_BRANCH, branchKey, - PARAM_ANALYSIS, analysisUuid)); + private void call(String projectKey, @Nullable String branchKey, String analysisUuid) { + if (branchKey == null) { + call(ImmutableMap.of( + PARAM_PROJECT, projectKey, + PARAM_ANALYSIS, analysisUuid)); + } else { + call(ImmutableMap.of( + PARAM_PROJECT, projectKey, + PARAM_BRANCH, branchKey, + PARAM_ANALYSIS, analysisUuid)); + } } private static class MapBuilder { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java index ed37d41f6f6..e1aaae72a28 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java @@ -19,9 +19,14 @@ */ package org.sonar.server.qualitygate.ws; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; +import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.core.util.UuidFactoryFast; @@ -38,10 +43,12 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Qualitygates.QualityGate; import static java.lang.String.format; +import static java.util.Optional.ofNullable; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.tuple; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; @@ -50,6 +57,7 @@ import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION; +@RunWith(DataProviderRunner.class) public class CopyActionTest { @Rule @@ -248,34 +256,31 @@ public class CopyActionTest { } @Test - public void fail_when_name_parameter_is_missing() { + @UseDataProvider("nullOrEmpty") + public void fail_when_name_parameter_is_missing(@Nullable String nameParameter) { OrganizationDto organization = db.organizations().insert(); userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("The 'name' parameter is missing"); - ws.newRequest() + TestRequest request = ws.newRequest() .setParam(PARAM_ID, qualityGate.getId().toString()) - .setParam(PARAM_ORGANIZATION, organization.getKey()) - .execute(); - } - - @Test - public void fail_when_name_parameter_is_empty() { - OrganizationDto organization = db.organizations().insert(); - userSession.addPermission(ADMINISTER_QUALITY_GATES, organization); - QualityGateDto qualityGate = db.qualityGates().insertQualityGate(organization); + .setParam(PARAM_ORGANIZATION, organization.getKey()); + ofNullable(nameParameter).ifPresent(t -> request.setParam(PARAM_NAME, t)); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("The 'name' parameter is empty"); + expectedException.expectMessage("The 'name' parameter is missing"); - ws.newRequest() - .setParam(PARAM_ID, qualityGate.getId().toString()) - .setParam(PARAM_NAME, "") - .setParam(PARAM_ORGANIZATION, organization.getKey()) - .execute(); + request.execute(); + } + + @DataProvider + public static Object[][] nullOrEmpty() { + return new Object[][] { + {null}, + {""}, + {" "} + }; } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java index 3815f19fb1d..646970436f3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java @@ -19,16 +19,15 @@ */ package org.sonar.server.qualitygate.ws; -import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; -import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; -import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION; - +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.Optional; - +import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.core.util.UuidFactoryFast; @@ -44,9 +43,16 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.Qualitygates.CreateResponse; +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; +import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; +import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ORGANIZATION; + +@RunWith(DataProviderRunner.class) public class CreateActionTest { @Rule @@ -184,30 +190,28 @@ public class CreateActionTest { } @Test - public void fail_when_name_parameter_is_empty() { + @UseDataProvider("nullOrEmpty") + public void fail_when_name_parameter_is_empty(@Nullable String nameParameter) { OrganizationDto org = db.organizations().insert(); userSession.addPermission(ADMINISTER_QUALITY_GATES, org); - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("The 'name' parameter is empty"); - - ws.newRequest() - .setParam(PARAM_NAME, "") - .setParam(PARAM_ORGANIZATION, org.getKey()) - .execute(); - } - - @Test - public void fail_when_name_parameter_is_missing() { - OrganizationDto org = db.organizations().insert(); - userSession.addPermission(ADMINISTER_QUALITY_GATES, org); + TestRequest request = ws.newRequest() + .setParam(PARAM_ORGANIZATION, org.getKey()); + Optional.ofNullable(nameParameter).ifPresent(t -> request.setParam(PARAM_NAME, "")); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The 'name' parameter is missing"); - ws.newRequest() - .setParam(PARAM_ORGANIZATION, org.getKey()) - .execute(); + request.execute(); + } + + @DataProvider + public static Object[][] nullOrEmpty() { + return new Object[][] { + {null}, + {""}, + {" "} + }; } private CreateResponse executeRequest(Optional<OrganizationDto> organization, String qualitGateName) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java index 17e5865c0f8..d5ae9f933ac 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java @@ -154,7 +154,7 @@ public class RenameActionTest { QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Name can't be empty"); + expectedException.expectMessage("The 'name' parameter is missing"); ws.newRequest() .setParam("id", qualityGate.getId().toString()) diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java index 53a734d2ef9..0f2f3f87e72 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RenameActionTest.java @@ -219,8 +219,8 @@ public class RenameActionTest { public void fail_if_blank_renaming() { String qualityProfileKey = createNewValidQualityProfileKey(); - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Name must be set"); + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'name' parameter is missing"); call(qualityProfileKey, " "); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java index 5565a3fc1f0..59acdbde22f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/EncryptActionTest.java @@ -116,8 +116,8 @@ public class EncryptActionTest { public void fail_if_value_is_empty() { logInAsSystemAdministrator(); - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("Parameter 'value' must not be empty"); + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'value' parameter is missing"); call(" "); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java index d93879a59dd..107cd5acc55 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetActionTest.java @@ -434,7 +434,7 @@ public class SetActionTest { @Test public void fail_when_empty_key_value() { expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Setting key is mandatory and must not be empty"); + expectedException.expectMessage("The 'key' parameter is missing"); callForGlobalSetting(" ", "my value"); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java index 3d491e61dfe..73e4b958061 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/DeactivateActionTest.java @@ -20,6 +20,7 @@ package org.sonar.server.user.ws; import java.util.Optional; +import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -47,6 +48,7 @@ import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.index.UserIndexDefinition; import org.sonar.server.user.index.UserIndexer; +import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; @@ -284,13 +286,23 @@ public class DeactivateActionTest { public void fail_if_login_is_blank() { logInAsSystemAdministrator(); - expectedException.expect(NotFoundException.class); - expectedException.expectMessage("User '' doesn't exist"); + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'login' parameter is missing"); deactivate(""); } @Test + public void fail_if_login_is_missing() { + logInAsSystemAdministrator(); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'login' parameter is missing"); + + deactivate(null); + } + + @Test public void fail_to_deactivate_last_administrator_of_default_organization() { UserDto admin = db.users().insertUser(); db.users().insertPermissionOnUser(admin, ADMINISTER); @@ -364,11 +376,11 @@ public class DeactivateActionTest { userSession.logIn().setSystemAdministrator(); } - private TestResponse deactivate(String login) { - return ws.newRequest() - .setMethod("POST") - .setParam("login", login) - .execute(); + private TestResponse deactivate(@Nullable String login) { + TestRequest request = ws.newRequest() + .setMethod("POST"); + Optional.ofNullable(login).ifPresent(t -> request.setParam("login", login)); + return request.execute(); } private void verifyThatUserExists(String login) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java index 1aeefcf7f96..0cf79cbca06 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/usertoken/ws/GenerateActionTest.java @@ -110,8 +110,8 @@ public class GenerateActionTest { UserDto user = db.users().insertUser(); logInAsSystemAdministrator(); - expectedException.expect(BadRequestException.class); - expectedException.expectMessage("The 'name' parameter must not be blank"); + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'name' parameter is missing"); newRequest(user.getLogin(), " "); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java index 40c4cd40ce9..34c844ab6b1 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java @@ -77,7 +77,7 @@ public abstract class Request { */ public String mandatoryParam(String key) { String value = param(key); - checkArgument(value != null, format(MSG_PARAMETER_MISSING, key)); + checkArgument(value != null && !value.isEmpty(), format(MSG_PARAMETER_MISSING, key)); return value; } |