From: Jean-Baptiste Lievremont Date: Thu, 26 Mar 2015 11:03:26 +0000 (+0100) Subject: SONAR-6304 WS to backup quality profile X-Git-Tag: 5.2-RC1~2335 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9d5ef51098e460fe753e46dedbc109ad1be0d6d8;p=sonarqube.git SONAR-6304 WS to backup quality profile --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index abb36161ba2..720d51d5c6d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -385,6 +385,7 @@ class ServerComponents { pico.addSingleton(QProfileDeleteAction.class); pico.addSingleton(QProfileRenameAction.class); pico.addSingleton(QProfileCopyAction.class); + pico.addSingleton(QProfileBackupAction.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/QProfileBackuper.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java index e8f29e64d73..1b9365c692c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java @@ -57,7 +57,7 @@ public class QProfileBackuper implements ServerComponent { this.index = index; } - void backup(String key, Writer writer) { + public void backup(String key, Writer writer) { QualityProfileDto profile; DbSession dbSession = db.openSession(false); try { diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java new file mode 100644 index 00000000000..8664ea2a76e --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java @@ -0,0 +1,57 @@ +/* + * 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 com.google.common.base.Charsets; +import org.sonar.api.server.ws.Request; +import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.Response.Stream; +import org.sonar.api.server.ws.WebService; +import org.sonar.server.qualityprofile.QProfileBackuper; + +import java.io.OutputStreamWriter; + +public class QProfileBackupAction implements BaseQProfileWsAction { + + private static final String PARAM_KEY = "key"; + private final QProfileBackuper backuper; + + public QProfileBackupAction(QProfileBackuper backuper) { + this.backuper = backuper; + } + + @Override + public void handle(Request request, Response response) throws Exception { + Stream stream = response.stream(); + stream.setMediaType("text/xml"); + backuper.backup(request.mandatoryParam(PARAM_KEY), new OutputStreamWriter(stream.output(), Charsets.UTF_8)); + } + + @Override + public void define(WebService.NewController controller) { + controller.createAction("backup") + .setSince("5.2") + .setDescription("Backup a quality profile in XML form.") + .setHandler(this) + .createParam(PARAM_KEY) + .setDescription("Key of the profile to backup."); + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java index 0893743dd83..0f2b50a6e79 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java @@ -30,7 +30,6 @@ public class QProfilesWs implements WebService { private final ProjectAssociationActions projectAssociationActions; private final BaseQProfileWsAction[] actions; - public QProfilesWs(RuleActivationActions ruleActivationActions, BulkRuleActivationActions bulkRuleActivationActions, ProjectAssociationActions projectAssociationActions, diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileBackupActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileBackupActionTest.java new file mode 100644 index 00000000000..311ef591bed --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileBackupActionTest.java @@ -0,0 +1,83 @@ +/* + * 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.invocation.InvocationOnMock; +import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.sonar.server.qualityprofile.QProfileBackuper; +import org.sonar.server.ws.WsTester; +import org.sonar.server.ws.WsTester.Result; + +import java.io.PrintWriter; +import java.io.Writer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; + +@RunWith(MockitoJUnitRunner.class) +public class QProfileBackupActionTest { + + // TODO Replace with proper DbTester + EsTester medium test once DaoV2 is removed + @Mock + private QProfileBackuper backuper; + + private WsTester tester; + + @Before + public void setUp() throws Exception { + tester = new WsTester(new QProfilesWs( + mock(RuleActivationActions.class), + mock(BulkRuleActivationActions.class), + mock(ProjectAssociationActions.class), + new QProfileBackupAction(backuper))); + } + + @Test + public void backup_profile() throws Exception { + String profileKey = "polop-palap-xoo-12345"; + + final String response = ""; + doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + Writer w = (Writer) invocation.getArguments()[1]; + new PrintWriter(w).print(response); + w.close(); + return null; + } + }).when(backuper).backup(eq(profileKey), any(Writer.class)); + + Result result = tester.newGetRequest("api/qualityprofiles", "backup").setParam("key", profileKey).execute(); + assertThat(result.outputAsString()).isEqualTo(response); + } + + @Test(expected = IllegalArgumentException.class) + public void fail_on_missing_key() throws Exception { + tester.newGetRequest("api/qualityprofiles", "backup").execute(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java index e48291f8221..c8900b7c00c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java @@ -53,7 +53,8 @@ public class QProfilesWsTest { new ProjectAssociationActions(null, null, null, languages), new QProfileRestoreBuiltInAction(null), new QProfileSearchAction(languages, null, null), - new QProfileSetDefaultAction(languages, null, null) + new QProfileSetDefaultAction(languages, null, null), + new QProfileBackupAction(null) )).controller(QProfilesWs.API_ENDPOINT); }