--- /dev/null
+/*
+ * 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.QProfileCopier;
+import org.sonar.server.user.UserSession;
+
+public class QProfileCopyAction implements BaseQProfileWsAction {
+
+ private static final String PARAM_PROFILE_NAME = "toName";
+ private static final String PARAM_PROFILE_KEY = "fromKey";
+
+ private final QProfileCopier profileCopier;
+
+ public QProfileCopyAction(QProfileCopier profileCopier) {
+ this.profileCopier = profileCopier;
+ }
+
+ @Override
+ public void define(WebService.NewController controller) {
+ NewAction setDefault = controller.createAction("copy")
+ .setSince("5.2")
+ .setDescription("Copy a quality profile.")
+ .setPost(true)
+ .setHandler(this);
+
+ setDefault.createParam(PARAM_PROFILE_NAME)
+ .setDescription("The name for the new 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);
+
+ profileCopier.copyToName(profileKey, newName);
+
+ response.noContent();
+ }
+}
--- /dev/null
+/*
+ * 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.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.qualityprofile.QProfileCopier;
+import org.sonar.server.user.MockUserSession;
+import org.sonar.server.ws.WsTester;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public class QProfileCopyActionTest {
+
+ private WsTester tester;
+
+ // TODO Replace with proper DbTester + EsTester medium test during removal of DaoV2
+ @Mock
+ private QProfileCopier qProfileCopier;
+
+ @Before
+ public void setUp() throws Exception {
+ tester = new WsTester(new QProfilesWs(
+ mock(RuleActivationActions.class),
+ mock(BulkRuleActivationActions.class),
+ mock(ProjectAssociationActions.class),
+ new QProfileCopyAction(qProfileCopier)));
+ }
+
+ @Test
+ public void copy_nominal() throws Exception {
+ MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+ String fromProfileKey = "sonar-way-xoo2-23456";
+ String toName = "Other Sonar Way";
+ tester.newPostRequest("api/qualityprofiles", "copy")
+ .setParam("fromKey", fromProfileKey)
+ .setParam("toName", toName)
+ .execute().assertNoContent();
+
+ verify(qProfileCopier).copyToName(fromProfileKey, toName);
+ }
+
+ @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", "copy")
+ .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", "copy")
+ .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", "copy")
+ .setParam("key", "sonar-way-xoo1-13245")
+ .setParam("name", "Hey look I am not quality profile admin!")
+ .execute();
+ }
+}