]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6304 WS to backup quality profile
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 26 Mar 2015 11:03:26 +0000 (12:03 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 9 Apr 2015 07:44:15 +0000 (09:44 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileBackuper.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileBackupActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java

index abb36161ba27a504fc61adc362ef415f82089622..720d51d5c6d360eb4451bb2346b301b0e0eb7bfb 100644 (file)
@@ -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);
index e8f29e64d73061e7f3b664b0126d5b875dc96cb0..1b9365c692c102f050c1ea1e49d45e9e50be1512 100644 (file)
@@ -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 (file)
index 0000000..8664ea2
--- /dev/null
@@ -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.");
+  }
+
+}
index 0893743dd836c2dd30dbd7e46070c750a895b140..0f2b50a6e7911d53a5d9f243b89ec311815d525a 100644 (file)
@@ -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 (file)
index 0000000..311ef59
--- /dev/null
@@ -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 = "<polop><palap/></polop>";
+    doAnswer(new Answer<Void>() {
+      @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();
+  }
+}
index e48291f8221240a08e4e7c2752828b86e13bc32b..c8900b7c00ccfbc74deeb4497367e63df2b4b816 100644 (file)
@@ -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);
   }