]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6399 Internal WS to list supported export formats
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 15 Apr 2015 12:19:47 +0000 (14:19 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 17 Apr 2015 15:23:28 +0000 (17:23 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileExportersAction.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileExportersActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileExportersActionTest/exporters.json [new file with mode: 0644]

diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileExportersAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileExportersAction.java
new file mode 100644 (file)
index 0000000..74a567d
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * 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.profiles.ProfileExporter;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService.NewController;
+import org.sonar.api.utils.text.JsonWriter;
+
+public class QProfileExportersAction implements BaseQProfileWsAction {
+
+  private ProfileExporter[] exporters;
+
+  public QProfileExportersAction(ProfileExporter[] exporters) {
+    this.exporters = exporters;
+  }
+
+  public QProfileExportersAction() {
+    this(new ProfileExporter[0]);
+  }
+
+  @Override
+  public void define(NewController context) {
+    context.createAction("exporters")
+      .setDescription("Lists available profile export formats.")
+      .setHandler(this)
+      .setInternal(true)
+      .setResponseExample(getClass().getResource("example-exporters.json"))
+      .setSince("5.2");
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    JsonWriter json = response.newJsonWriter().beginObject().name("exporters").beginArray();
+    for (ProfileExporter exporter : exporters) {
+      json.beginObject()
+        .prop("key", exporter.getKey())
+        .prop("name", exporter.getName());
+      json.name("languages").beginArray();
+      for (String language : exporter.getSupportedLanguages()) {
+        json.value(language);
+      }
+      json.endArray().endObject();
+    }
+    json.endArray().endObject().close();
+  }
+
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileExportersActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileExportersActionTest.java
new file mode 100644 (file)
index 0000000..9e3c4d3
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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.Test;
+import org.sonar.api.profiles.ProfileExporter;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.server.ws.WsTester;
+
+import java.io.Writer;
+
+import static org.mockito.Mockito.mock;
+
+public class QProfileExportersActionTest {
+
+  @Test
+  public void importers_nominal() throws Exception {
+    WsTester wsTester = new WsTester(new QProfilesWs(
+      mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class), mock(ProjectAssociationActions.class),
+      new QProfileExportersAction(createExporters())));
+
+    wsTester.newGetRequest("api/qualityprofiles", "exporters").execute().assertJson(getClass(), "exporters.json");
+  }
+
+  private ProfileExporter[] createExporters() {
+    class NoopImporter extends ProfileExporter {
+      private NoopImporter(String key, String name, String... languages) {
+        super(key, name);
+        setSupportedLanguages(languages);
+      }
+
+      @Override
+      public void exportProfile(RulesProfile profile, Writer writer) {
+        // Nothing
+      }
+
+    }
+    return new ProfileExporter[] {
+      new NoopImporter("findbugs", "FindBugs", "java"),
+      new NoopImporter("jslint", "JS Lint", "js"),
+      new NoopImporter("vaadin", "Vaadin", "java", "js")
+    };
+  }
+}
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileExportersActionTest/exporters.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileExportersActionTest/exporters.json
new file mode 100644 (file)
index 0000000..cc0788d
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "exporters": [
+    {"key": "findbugs", "name": "FindBugs", "languages": ["java"]},
+    {"key": "jslint", "name": "JS Lint", "languages": ["js"]},
+    {"key": "vaadin", "name": "Vaadin", "languages": ["java", "js"]}
+  ]
+}