]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6497 SONAR-6496 WS create and update permission template with better tests...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 27 Aug 2015 16:39:19 +0000 (18:39 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 27 Aug 2015 16:39:30 +0000 (18:39 +0200)
17 files changed:
server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddGroupToTemplateAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/AddUserToTemplateAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/CreateTemplateAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/DeleteTemplateAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/Parameters.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/PermissionRequestValidator.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserFromTemplateAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/UpdateTemplateAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddGroupToTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/AddUserToTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/CreateTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/DeleteTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionRequestValidatorTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveGroupFromTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/RemoveUserFromTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/UpdateTemplateActionTest.java

index c7cf594ad2489a6eb90a3aca3645f6956db16e15..e7f019177315d3f14a14a6ffad5ed34d9679b9c2 100644 (file)
@@ -37,7 +37,7 @@ import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobal
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateNotAnyoneAndAdminPermission;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
 import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
 import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter;
 import static org.sonar.server.permission.ws.Parameters.createGroupNameParameter;
 import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter;
@@ -75,7 +75,7 @@ public class AddGroupToTemplateAction implements PermissionsWsAction {
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     checkGlobalAdminUser(userSession);
 
-    String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY);
+    String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
     String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
     WsGroup group = WsGroup.fromRequest(wsRequest);
 
index 905648c075081dc7eb9faebfb653ca728981ed83..eb1e3197693ba92774e54ffc437fb815ccbf6ee7 100644 (file)
@@ -38,7 +38,7 @@ import static org.sonar.db.user.GroupMembershipQuery.IN;
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
 import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
 import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN;
 import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter;
 import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameterToAddRemoveUserOrGroup;
@@ -74,7 +74,7 @@ public class AddUserToTemplateAction implements PermissionsWsAction {
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     checkGlobalAdminUser(userSession);
 
-    String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY);
+    String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
     String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
     final String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN);
 
index c782fad40a5022cf45300f1aba61d2ffba4e374c..04141c3dad403c2a77d0f013b576c8c9c85cfe60 100644 (file)
@@ -32,14 +32,14 @@ import org.sonarqube.ws.Permissions;
 import org.sonarqube.ws.Permissions.PermissionTemplate;
 
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPattern;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_DESCRIPTION;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_NAME;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_PATTERN;
 import static org.sonar.server.permission.ws.Parameters.createTemplateDescriptionParameter;
 import static org.sonar.server.permission.ws.Parameters.createTemplateProjectKeyPatternParameter;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPattern;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.validateTemplateNameFormat;
 import static org.sonar.server.permission.ws.PermissionTemplateDtoBuilder.create;
 import static org.sonar.server.permission.ws.PermissionTemplateDtoToPermissionTemplateResponse.toPermissionTemplateResponse;
 import static org.sonar.server.ws.WsUtils.checkRequest;
@@ -96,12 +96,12 @@ public class CreateTemplateAction implements PermissionsWsAction {
     }
   }
 
-  private void validateTemplateNameForCreation(DbSession dbSession, String templateName) {
-    checkRequest(!templateName.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK);
+  private void validateTemplateNameForCreation(DbSession dbSession, String name) {
+    validateTemplateNameFormat(name);
 
-    PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, templateName);
+    PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, name);
     checkRequest(permissionTemplateWithSameName == null, String.format
-      (MSG_TEMPLATE_WITH_SAME_NAME, templateName));
+      (MSG_TEMPLATE_WITH_SAME_NAME, name));
   }
 
   private PermissionTemplateDto insertTemplate(DbSession dbSession, String name, String description, String projectPattern) {
index 0eaeb105d73ff613ca73d167df1961a85e7cb8b7..8f15173e3e3cb1a350a0f3650bbd5f479770979a 100644 (file)
@@ -28,7 +28,7 @@ import org.sonar.db.DbSession;
 import org.sonar.server.user.UserSession;
 
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
-import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID;
 import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameter;
 
 public class DeleteTemplateAction implements PermissionsWsAction {
@@ -55,7 +55,7 @@ public class DeleteTemplateAction implements PermissionsWsAction {
   @Override
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     checkGlobalAdminUser(userSession);
-    String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY);
+    String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID);
 
     DbSession dbSession = dbClient.openSession(false);
     try {
index d9a8fac4cfe0929d3413c46d5f174ecf6b2838e7..211b4f61d042c13b7af77375b7ea8784b1ae111b 100644 (file)
@@ -32,11 +32,12 @@ class Parameters {
   static final String PARAM_PROJECT_UUID = "projectId";
   static final String PARAM_PROJECT_KEY = "projectKey";
   static final String PARAM_USER_LOGIN = "login";
-  static final String PARAM_LONG_TEMPLATE_KEY = "templateKey";
-  static final String PARAM_TEMPLATE_KEY = "key";
+  static final String PARAM_LONG_TEMPLATE_ID = "templateId";
+
+  static final String PARAM_TEMPLATE_ID = "id";
   static final String PARAM_TEMPLATE_NAME = "name";
   static final String PARAM_TEMPLATE_DESCRIPTION = "description";
-  static final String PARAM_TEMPLATE_PATTERN = "projectPattern";
+  static final String PARAM_TEMPLATE_PATTERN = "projectKeyPattern";
 
   private static final String PERMISSION_PARAM_DESCRIPTION = String.format("Permission" +
     "<ul>" +
@@ -99,7 +100,7 @@ class Parameters {
   }
 
   static void createTemplateKeyParameterToAddRemoveUserOrGroup(NewAction action) {
-    action.createParam(PARAM_LONG_TEMPLATE_KEY)
+    action.createParam(PARAM_LONG_TEMPLATE_ID)
       .setRequired(true)
       .setDescription("Template key")
       .setExampleValue("developer_template_20150820_170218");
@@ -118,7 +119,7 @@ class Parameters {
   }
 
   static void createTemplateKeyParameter(NewAction action) {
-    action.createParam(PARAM_TEMPLATE_KEY)
+    action.createParam(PARAM_TEMPLATE_ID)
       .setRequired(true)
       .setDescription("Key")
       .setExampleValue("af8cb8cc-1e78-4c4e-8c00-ee8e814009a5");
index f2df1900f01aaf62c99a49556f46683c07395b30..d9e1c92a5eea9637543d7e8274b80df161a9a30d 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.sonar.server.permission.ws;
 
+import com.google.common.base.CharMatcher;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 import javax.annotation.Nullable;
@@ -57,6 +58,11 @@ public class PermissionRequestValidator {
       format("It is not possible to add the '%s' permission to the '%s' group.", permission, groupName));
   }
 
+  public static void validateTemplateNameFormat(String name) {
+    String nameWithoutWhitespaces = CharMatcher.WHITESPACE.removeFrom(name);
+    checkRequest(!nameWithoutWhitespaces.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK);
+  }
+
   public static void validateProjectPattern(@Nullable String projectPattern) {
     if (isNullOrEmpty(projectPattern)) {
       return;
index 2233447bb0f1479c20c2a5815fd47a0273f02d4a..cb80d6fbf22bf1c638a9df019d20b4cfd2b0b801 100644 (file)
@@ -32,7 +32,7 @@ import org.sonar.server.user.UserSession;
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
 import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
 import static org.sonar.server.permission.ws.Parameters.createGroupIdParameter;
 import static org.sonar.server.permission.ws.Parameters.createGroupNameParameter;
 import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter;
@@ -70,7 +70,7 @@ public class RemoveGroupFromTemplateAction implements PermissionsWsAction {
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     checkGlobalAdminUser(userSession);
 
-    String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY);
+    String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
     String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
     WsGroup group = WsGroup.fromRequest(wsRequest);
 
index 20596a556a894f6e106aba1d34fb5ba6bec79fea..fa58616c86473018446d5b8d46a07f248137668d 100644 (file)
@@ -32,7 +32,7 @@ import org.sonar.server.user.UserSession;
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
 import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission;
 import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
 import static org.sonar.server.permission.ws.Parameters.PARAM_USER_LOGIN;
 import static org.sonar.server.permission.ws.Parameters.createProjectPermissionParameter;
 import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameterToAddRemoveUserOrGroup;
@@ -68,7 +68,7 @@ public class RemoveUserFromTemplateAction implements PermissionsWsAction {
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     checkGlobalAdminUser(userSession);
 
-    String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_KEY);
+    String templateKey = wsRequest.mandatoryParam(PARAM_LONG_TEMPLATE_ID);
     String permission = wsRequest.mandatoryParam(PARAM_PERMISSION);
     String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN);
 
index de2f40ae6aa7907ea4378e41c27d22bf2f62d44c..dc05fda8de5c8e5a8689aaa6493d27e3a569d488 100644 (file)
@@ -36,16 +36,16 @@ import org.sonarqube.ws.Permissions.UpdatePermissionTemplateResponse;
 import static com.google.common.base.Objects.firstNonNull;
 import static java.lang.String.format;
 import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME;
-import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPattern;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_DESCRIPTION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_NAME;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_PATTERN;
 import static org.sonar.server.permission.ws.Parameters.createTemplateDescriptionParameter;
 import static org.sonar.server.permission.ws.Parameters.createTemplateKeyParameter;
 import static org.sonar.server.permission.ws.Parameters.createTemplateProjectKeyPatternParameter;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_WITH_SAME_NAME;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPattern;
+import static org.sonar.server.permission.ws.PermissionRequestValidator.validateTemplateNameFormat;
 import static org.sonar.server.permission.ws.PermissionTemplateDtoToPermissionTemplateResponse.toPermissionTemplateResponse;
 import static org.sonar.server.ws.WsUtils.checkRequest;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
@@ -87,7 +87,7 @@ public class UpdateTemplateAction implements PermissionsWsAction {
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     checkGlobalAdminUser(userSession);
 
-    String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_KEY);
+    String key = wsRequest.mandatoryParam(PARAM_TEMPLATE_ID);
     String nameParam = wsRequest.param(PARAM_TEMPLATE_NAME);
     String descriptionParam = wsRequest.param(PARAM_TEMPLATE_DESCRIPTION);
     String projectPatternParam = wsRequest.param(PARAM_TEMPLATE_PATTERN);
@@ -130,11 +130,11 @@ public class UpdateTemplateAction implements PermissionsWsAction {
     return UpdatePermissionTemplateResponse.newBuilder().setPermissionTemplate(permissionTemplateBuilder).build();
   }
 
-  private void validateTemplateNameForUpdate(DbSession dbSession, String templateName, long templateId) {
-    checkRequest(!templateName.isEmpty(), MSG_TEMPLATE_NAME_NOT_BLANK);
+  private void validateTemplateNameForUpdate(DbSession dbSession, String name, long id) {
+    validateTemplateNameFormat(name);
 
-    PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, templateName);
-    checkRequest(permissionTemplateWithSameName == null || permissionTemplateWithSameName.getId() == templateId,
-      format(MSG_TEMPLATE_WITH_SAME_NAME, templateName));
+    PermissionTemplateDto permissionTemplateWithSameName = dbClient.permissionTemplateDao().selectByName(dbSession, name);
+    checkRequest(permissionTemplateWithSameName == null || permissionTemplateWithSameName.getId() == id,
+      format(MSG_TEMPLATE_WITH_SAME_NAME, name));
   }
 }
index ea4781f09d5e11ac4a78101d2b7f4e57663e699b..559d46605727e5cb5a81d0f0a495fb5d6a6885ac 100644 (file)
@@ -59,7 +59,7 @@ import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTem
 import static org.sonar.db.user.GroupMembershipQuery.IN;
 import static org.sonar.db.user.GroupTesting.newGroupDto;
 import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
 import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
 
 @Category(DbTests.class)
@@ -103,7 +103,7 @@ public class AddGroupToTemplateActionTest {
   @Test
   public void add_with_group_id() {
     ws.newRequest()
-      .setParam(PARAM_LONG_TEMPLATE_KEY, permissionTemplate.getKee())
+      .setParam(PARAM_LONG_TEMPLATE_ID, permissionTemplate.getKee())
       .setParam(PARAM_PERMISSION, CODEVIEWER)
       .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
       .execute();
@@ -200,7 +200,7 @@ public class AddGroupToTemplateActionTest {
       request.setParam(Parameters.PARAM_GROUP_NAME, groupName);
     }
     if (templateKey != null) {
-      request.setParam(PARAM_LONG_TEMPLATE_KEY, templateKey);
+      request.setParam(PARAM_LONG_TEMPLATE_ID, templateKey);
     }
     if (permission != null) {
       request.setParam(Parameters.PARAM_PERMISSION, permission);
index 80dd4dc66f6ddb8169a5a5dfb4bd1fdbd84d432c..5f4afa4bb4dd4787e9f56e5b86754bbd9c9ea7c7 100644 (file)
@@ -168,7 +168,7 @@ public class AddUserToTemplateActionTest {
       request.setParam(Parameters.PARAM_USER_LOGIN, userLogin);
     }
     if (templateKey != null) {
-      request.setParam(Parameters.PARAM_LONG_TEMPLATE_KEY, templateKey);
+      request.setParam(Parameters.PARAM_LONG_TEMPLATE_ID, templateKey);
     }
     if (permission != null) {
       request.setParam(Parameters.PARAM_PERMISSION, permission);
index 230c615bb5c3095b2bf2ad0d8f69c82d38a96ee5..4f58e5ed4b6119c0a120be5407a1d45e55b545d9 100644 (file)
@@ -119,7 +119,7 @@ public class CreateTemplateActionTest {
   @Test
   public void fail_if_regexp_if_not_valid() {
     expectedException.expect(BadRequestException.class);
-    expectedException.expectMessage("The 'projectPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
+    expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
 
     newRequest("Finance", null, "[azerty");
   }
index e30c3f403ee5c0cba3d817a7ed0af35daf8eaa83..ce7bf055326584a362fa95a259915ee704475af6 100644 (file)
@@ -40,7 +40,7 @@ import org.sonar.server.ws.WsActionTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID;
 
 public class DeleteTemplateActionTest {
 
@@ -114,7 +114,7 @@ public class DeleteTemplateActionTest {
   private TestResponse newRequest(@Nullable String key) {
     TestRequest request = ws.newRequest();
     if (key != null) {
-      request.setParam(PARAM_TEMPLATE_KEY, key);
+      request.setParam(PARAM_TEMPLATE_ID, key);
     }
 
     TestResponse result = executeRequest(request);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionRequestValidatorTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/PermissionRequestValidatorTest.java
new file mode 100644 (file)
index 0000000..82a7597
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.permission.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.server.exceptions.BadRequestException;
+
+import static org.sonar.server.permission.ws.PermissionRequestValidator.MSG_TEMPLATE_NAME_NOT_BLANK;
+
+public class PermissionRequestValidatorTest {
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void validate_template_name() {
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage(MSG_TEMPLATE_NAME_NOT_BLANK);
+
+    PermissionRequestValidator.validateTemplateNameFormat("   \r\n");
+  }
+}
index ede5d8edca94455790904f7b08a82685fd761a27..28e64178f61f0f07e4fc15151594ab320fdb4c03 100644 (file)
@@ -57,7 +57,7 @@ import static org.sonar.db.user.GroupMembershipQuery.IN;
 import static org.sonar.db.user.GroupTesting.newGroupDto;
 import static org.sonar.server.permission.ws.Parameters.PARAM_GROUP_ID;
 import static org.sonar.server.permission.ws.Parameters.PARAM_PERMISSION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_LONG_TEMPLATE_ID;
 
 @Category(DbTests.class)
 public class RemoveGroupFromTemplateActionTest {
@@ -108,7 +108,7 @@ public class RemoveGroupFromTemplateActionTest {
     commit();
 
     ws.newRequest()
-      .setParam(PARAM_LONG_TEMPLATE_KEY, permissionTemplate.getKee())
+      .setParam(PARAM_LONG_TEMPLATE_ID, permissionTemplate.getKee())
       .setParam(PARAM_PERMISSION, PERMISSION)
       .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
       .execute();
@@ -200,7 +200,7 @@ public class RemoveGroupFromTemplateActionTest {
       request.setParam(Parameters.PARAM_GROUP_NAME, groupName);
     }
     if (templateKey != null) {
-      request.setParam(PARAM_LONG_TEMPLATE_KEY, templateKey);
+      request.setParam(PARAM_LONG_TEMPLATE_ID, templateKey);
     }
     if (permission != null) {
       request.setParam(Parameters.PARAM_PERMISSION, permission);
index 9cbc0361c4cb376e7172d22dbd68e6d07bfae6e9..7b42bab1fd17b948603f8ad3956f1ce10098d89e 100644 (file)
@@ -192,7 +192,7 @@ public class RemoveUserFromTemplateActionTest {
       request.setParam(Parameters.PARAM_USER_LOGIN, userLogin);
     }
     if (templateKey != null) {
-      request.setParam(Parameters.PARAM_LONG_TEMPLATE_KEY, templateKey);
+      request.setParam(Parameters.PARAM_LONG_TEMPLATE_ID, templateKey);
     }
     if (permission != null) {
       request.setParam(Parameters.PARAM_PERMISSION, permission);
index 45acf036bfdc5b0b3d7c4c60a9976e210ae2a967..e9c1afa6f6d36f5186c0a96c9b3809bba42a2410 100644 (file)
@@ -47,7 +47,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_DESCRIPTION;
-import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_KEY;
+import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_ID;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_NAME;
 import static org.sonar.server.permission.ws.Parameters.PARAM_TEMPLATE_PATTERN;
 import static org.sonar.test.JsonAssert.assertJson;
@@ -162,10 +162,18 @@ public class UpdateTemplateActionTest {
     newRequest(templateDto.getKee(), "", null, null);
   }
 
+  @Test
+  public void fail_if_name_has_just_whitespaces() {
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("The template name must not be blank");
+
+    newRequest(templateDto.getKee(), "  \r\n", null, null);
+  }
+
   @Test
   public void fail_if_regexp_if_not_valid() {
     expectedException.expect(BadRequestException.class);
-    expectedException.expectMessage("The 'projectPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
+    expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
 
     newRequest(templateDto.getKee(), "Finance", null, "[azerty");
   }
@@ -207,7 +215,7 @@ public class UpdateTemplateActionTest {
   private TestResponse newRequest(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) {
     TestRequest request = ws.newRequest();
     if (key != null) {
-      request.setParam(PARAM_TEMPLATE_KEY, key);
+      request.setParam(PARAM_TEMPLATE_ID, key);
     }
     if (name != null) {
       request.setParam(PARAM_TEMPLATE_NAME, name);