From b511b6e0e9f61407080612d48874db295f24072b Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Tue, 7 Apr 2015 15:59:36 +0200 Subject: [PATCH] SONAR-6298 Add internal WS to list supported profile import file formats --- .../server/platform/ServerComponents.java | 1 + .../ws/QProfileImportersAction.java | 65 +++++++++++++++++++ .../qualityprofile/ws/example-importers.json | 8 +++ .../ws/QProfileImportersActionTest.java | 62 ++++++++++++++++++ .../qualityprofile/ws/QProfilesWsTest.java | 14 +++- .../importers.json | 7 ++ 6 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileImportersAction.java create mode 100644 server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-importers.json create mode 100644 server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest.java create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest/importers.json 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 f929ea2f92b..eb874b84664 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 @@ -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 index 00000000000..90ba1f4459e --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileImportersAction.java @@ -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 index 00000000000..d5faa647cfd --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-importers.json @@ -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 index 00000000000..dfd16c7d012 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest.java @@ -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") + }; + } +} 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 c32f67baeca..7aaea756dec 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 @@ -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 index 00000000000..2c180eec988 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileImportersActionTest/importers.json @@ -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"]} + ] +} -- 2.39.5