From: Jean-Baptiste Lievremont Date: Wed, 15 Apr 2015 12:19:47 +0000 (+0200) Subject: SONAR-6399 Internal WS to list supported export formats X-Git-Tag: 5.2-RC1~2213 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=997aec1cdffaf61400768c58044205492150fcb8;p=sonarqube.git SONAR-6399 Internal WS to list supported export formats --- 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 index 00000000000..74a567d96af --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileExportersAction.java @@ -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 index 00000000000..9e3c4d3190c --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileExportersActionTest.java @@ -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 index 00000000000..cc0788d870d --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileExportersActionTest/exporters.json @@ -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"]} + ] +}