]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6302 WS to rename a quality profile
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 2 Apr 2015 15:18:36 +0000 (17:18 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 3 Apr 2015 14:46:17 +0000 (16:46 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileRenameAction.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRenameActionTest.java [new file with mode: 0644]

index 7762405242228dff0138e2985d37c9a7849c52ae..9981c9fc4337b9d4b59b1007eec94dfe4808dae4 100644 (file)
@@ -383,6 +383,7 @@ class ServerComponents {
     pico.addSingleton(QProfileSetDefaultAction.class);
     pico.addSingleton(QProfileProjectsAction.class);
     pico.addSingleton(QProfileDeleteAction.class);
+    pico.addSingleton(QProfileRenameAction.class);
     pico.addSingleton(QProfilesWs.class);
     pico.addSingleton(ProfilesWs.class);
     pico.addSingleton(RuleActivationActions.class);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileRenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileRenameAction.java
new file mode 100644 (file)
index 0000000..10ceb36
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * 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.qualityprofile.ws;
+
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.server.ws.WebService.NewAction;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.qualityprofile.QProfileFactory;
+import org.sonar.server.user.UserSession;
+
+public class QProfileRenameAction implements BaseQProfileWsAction {
+
+  private static final String PARAM_PROFILE_NAME = "name";
+  private static final String PARAM_PROFILE_KEY = "key";
+
+  private final QProfileFactory profileFactory;
+
+  public QProfileRenameAction(QProfileFactory profileFactory) {
+    this.profileFactory = profileFactory;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    NewAction setDefault = controller.createAction("rename")
+      .setSince("5.2")
+      .setDescription("Rename a quality profile.")
+      .setPost(true)
+      .setHandler(this);
+
+    setDefault.createParam(PARAM_PROFILE_NAME)
+      .setDescription("The new name for the quality profile.")
+      .setExampleValue("My Sonar way")
+      .setRequired(true);
+
+    setDefault.createParam(PARAM_PROFILE_KEY)
+      .setDescription("The key of a quality profile.")
+      .setExampleValue("sonar-way-js-12345")
+      .setRequired(true);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    UserSession.get().checkLoggedIn().checkGlobalPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+    String newName = request.mandatoryParam(PARAM_PROFILE_NAME);
+    String profileKey = request.mandatoryParam(PARAM_PROFILE_KEY);
+
+    profileFactory.rename(profileKey, newName);
+
+    response.noContent();
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRenameActionTest.java
new file mode 100644 (file)
index 0000000..ba784e0
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * 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.qualityprofile.ws;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.core.qualityprofile.db.QualityProfileDao;
+import org.sonar.core.qualityprofile.db.QualityProfileDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.qualityprofile.QProfileFactory;
+import org.sonar.server.user.MockUserSession;
+import org.sonar.server.ws.WsTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class QProfileRenameActionTest {
+
+  @ClassRule
+  public static DbTester dbTester = new DbTester();
+
+  private DbClient dbClient;
+
+  private QualityProfileDao qualityProfileDao;
+
+  private String xoo1Key = "xoo1", xoo2Key = "xoo2";
+
+  private WsTester tester;
+
+  private DbSession session;
+
+
+  @Before
+  public void setUp() throws Exception {
+    dbTester.truncateTables();
+    qualityProfileDao = new QualityProfileDao(dbTester.myBatis(), mock(System2.class));
+    dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), qualityProfileDao);
+    session = dbClient.openSession(false);
+
+    createProfiles();
+
+    tester = new WsTester(new QProfilesWs(
+      mock(RuleActivationActions.class),
+      mock(BulkRuleActivationActions.class),
+      mock(ProjectAssociationActions.class),
+      new QProfileRenameAction(new QProfileFactory(dbClient))));
+  }
+
+  @After
+  public void tearDown() {
+    session.close();
+  }
+
+  @Test
+  public void rename_nominal() throws Exception {
+    MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+    tester.newPostRequest("api/qualityprofiles", "rename")
+      .setParam("key", "sonar-way-xoo2-23456")
+      .setParam("name", "Other Sonar Way")
+      .execute().assertNoContent();
+
+    assertThat(qualityProfileDao.getNonNullByKey(session, "sonar-way-xoo2-23456").getName()).isEqualTo("Other Sonar Way");
+  }
+
+  @Test(expected = BadRequestException.class)
+  public void do_nothing_on_conflict() throws Exception {
+    MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+    tester.newPostRequest("api/qualityprofiles", "rename")
+      .setParam("key", "sonar-way-xoo2-23456")
+      .setParam("name", "My Sonar way")
+      .execute();
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void fail_on_missing_key() throws Exception {
+    MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+    tester.newPostRequest("api/qualityprofiles", "rename")
+      .setParam("name", "Other Sonar Way")
+      .execute();
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void fail_on_missing_name() throws Exception {
+    MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+    tester.newPostRequest("api/qualityprofiles", "rename")
+      .setParam("key", "sonar-way-xoo1-13245")
+      .execute();
+  }
+
+  @Test(expected = ForbiddenException.class)
+  public void fail_on_missing_permission() throws Exception {
+    MockUserSession.set().setLogin("obiwan");
+
+    tester.newPostRequest("api/qualityprofiles", "rename")
+      .setParam("key", "sonar-way-xoo1-13245")
+      .setParam("name", "Hey look I am not quality profile admin!")
+      .execute();
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void fail_on_unknown_profile() throws Exception {
+    MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+    tester.newPostRequest("api/qualityprofiles", "rename")
+      .setParam("key", "polop")
+      .setParam("name", "Uh oh, I don't know this profile")
+      .execute();
+  }
+
+  private void createProfiles() {
+    qualityProfileDao.insert(session,
+      QualityProfileDto.createFor("sonar-way-xoo1-12345").setLanguage(xoo1Key).setName("Sonar way").setDefault(true),
+      QualityProfileDto.createFor("sonar-way-xoo2-23456").setLanguage(xoo2Key).setName("Sonar way"),
+      QualityProfileDto.createFor("my-sonar-way-xoo2-34567").setLanguage(xoo2Key).setName("My Sonar way").setParentKee("sonar-way-xoo2-23456").setDefault(true)
+      );
+    session.commit();
+  }
+}