]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6298 Add internal WS to list supported profile import file formats
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 7 Apr 2015 13:59:36 +0000 (15:59 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Thu, 9 Apr 2015 08:14:47 +0000 (10:14 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileImportersAction.java [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-importers.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java
server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest/importers.json [new file with mode: 0644]

index f929ea2f92bbb2742fad9b2f931b1e2078d4b56d..eb874b84664c358ab68c726fe94b49833c76d5d8 100644 (file)
@@ -388,6 +388,7 @@ class ServerComponents {
     pico.addSingleton(QProfileBackupAction.class);
     pico.addSingleton(QProfileRestoreAction.class);
     pico.addSingleton(QProfileCreateAction.class);
+    pico.addSingleton(QProfileImportersAction.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/QProfileImportersAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileImportersAction.java
new file mode 100644 (file)
index 0000000..90ba1f4
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * 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.ProfileImporter;
+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.utils.text.JsonWriter;
+
+public class QProfileImportersAction implements BaseQProfileWsAction {
+
+  private final ProfileImporter[] importers;
+
+  public QProfileImportersAction(ProfileImporter[] importers) {
+    this.importers = importers;
+  }
+
+  public QProfileImportersAction() {
+    this(new ProfileImporter[0]);
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    controller.createAction("importers")
+      .setSince("5.2")
+      .setDescription("List supported importers.")
+      .setInternal(true)
+      .setResponseExample(getClass().getResource("example-importers.json"))
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    JsonWriter json = response.newJsonWriter().beginObject().name("importers").beginArray();
+    for (ProfileImporter importer : importers) {
+      json.beginObject()
+        .prop("key", importer.getKey())
+        .prop("name", importer.getName())
+        .name("languages").beginArray();
+      for (String languageKey : importer.getSupportedLanguages()) {
+        json.value(languageKey);
+      }
+      json.endArray().endObject();
+    }
+    json.endArray().endObject().close();
+  }
+}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-importers.json b/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-importers.json
new file mode 100644 (file)
index 0000000..d5faa64
--- /dev/null
@@ -0,0 +1,8 @@
+{
+  "importers": [
+    {"key": "pmd", "name": "PMD", "languages": ["java"]},
+    {"key": "checkstyle", "name": "Checkstyle", "languages": ["java"]},
+    {"key": "js-lint", "name": "JS Lint", "languages": ["js"]},
+    {"key": "android-lint", "name": "Android Lint", "languages": ["xml", "java"]}
+  ]
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest.java
new file mode 100644 (file)
index 0000000..dfd16c7
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.ProfileImporter;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.utils.ValidationMessages;
+import org.sonar.server.ws.WsTester;
+
+import java.io.Reader;
+
+import static org.mockito.Mockito.mock;
+
+public class QProfileImportersActionTest {
+
+  @Test
+  public void importers_nominal() throws Exception {
+    WsTester wsTester = new WsTester(new QProfilesWs(
+      mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class), mock(ProjectAssociationActions.class),
+      new QProfileImportersAction(createImporters())));
+
+    wsTester.newGetRequest("api/qualityprofiles", "importers").execute().assertJson(getClass(), "importers.json");
+  }
+
+  private ProfileImporter[] createImporters() {
+    class NoopImporter extends ProfileImporter {
+      private NoopImporter(String key, String name, String... languages) {
+        super(key, name);
+        setSupportedLanguages(languages);
+      }
+
+      @Override
+      public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
+        return RulesProfile.create();
+      }
+
+    }
+    return new ProfileImporter[] {
+      new NoopImporter("findbugs", "FindBugs", "java"),
+      new NoopImporter("jslint", "JS Lint", "js"),
+      new NoopImporter("vaadin", "Vaadin", "java", "js")
+    };
+  }
+}
index c32f67baeca85b7a082681660ac27c587e9f23d9..7aaea756dec096b5ea8898fbdc8f7c9c44b0072b 100644 (file)
@@ -60,6 +60,7 @@ public class QProfilesWsTest {
       new BulkRuleActivationActions(profileService, ruleService, i18n),
       new ProjectAssociationActions(null, null, null, languages),
       new QProfileCreateAction(null, null, null, languages, importers),
+      new QProfileImportersAction(importers),
       new QProfileRestoreBuiltInAction(null),
       new QProfileSearchAction(languages, null, null),
       new QProfileSetDefaultAction(languages, null, null),
@@ -91,8 +92,7 @@ public class QProfilesWsTest {
     assertThat(controller).isNotNull();
     assertThat(controller.path()).isEqualTo(QProfilesWs.API_ENDPOINT);
     assertThat(controller.description()).isNotEmpty();
-    assertThat(controller.actions()).hasSize(13);
-    assertThat(controller.actions()).hasSize(10);
+    assertThat(controller.actions()).hasSize(14);
   }
 
   @Test
@@ -209,4 +209,14 @@ public class QProfilesWsTest {
     assertThat(create.param("backup_" + xoo2Key)).isNotNull();
     assertThat(create.param("backup_" + xoo2Key).isRequired()).isFalse();
   }
+
+  @Test
+  public void define_importers_action() throws Exception {
+    WebService.Action importers = controller.action("importers");
+    assertThat(importers).isNotNull();
+    assertThat(importers.isPost()).isFalse();
+    assertThat(importers.isInternal()).isTrue();
+    assertThat(importers.params()).isEmpty();
+    assertThat(importers.responseExampleAsString()).isNotEmpty();
+  }
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest/importers.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest/importers.json
new file mode 100644 (file)
index 0000000..2c180ee
--- /dev/null
@@ -0,0 +1,7 @@
+{
+  "importers": [
+    {"key": "findbugs", "name": "FindBugs", "languages": ["java"]},
+    {"key": "jslint", "name": "JS Lint", "languages": ["js"]},
+    {"key": "vaadin", "name": "Vaadin", "languages": ["java", "js"]}
+  ]
+}