From b48ef5e2fc174a391a050a00ed03298e5cc688da Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 26 Jan 2017 13:16:07 +0100 Subject: [PATCH] SONAR-6506 Rewrite internal WS api/updatecenter/installed_plugins in Java --- .../updatecenter/UpdateCenterModule.java | 2 + .../ws/InstalledPluginsAction.java | 71 ++++++++++++ ...ns.json => installed_plugins-example.json} | 0 .../updatecenter/UpdateCenterModuleTest.java | 6 +- .../ws/InstalledPluginsActionTest.java | 102 ++++++++++++++++++ 5 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/updatecenter/ws/InstalledPluginsAction.java rename server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/{example-installed_plugins.json => installed_plugins-example.json} (100%) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/updatecenter/ws/InstalledPluginsActionTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/updatecenter/UpdateCenterModule.java b/server/sonar-server/src/main/java/org/sonar/server/updatecenter/UpdateCenterModule.java index 0164e05e1fd..2eeb02061cf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/updatecenter/UpdateCenterModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/updatecenter/UpdateCenterModule.java @@ -22,6 +22,7 @@ package org.sonar.server.updatecenter; import org.sonar.core.platform.Module; import org.sonar.server.plugins.UpdateCenterClient; import org.sonar.server.plugins.UpdateCenterMatrixFactory; +import org.sonar.server.updatecenter.ws.InstalledPluginsAction; import org.sonar.server.updatecenter.ws.UpdateCenterWs; import org.sonar.server.updatecenter.ws.UploadAction; @@ -32,6 +33,7 @@ public class UpdateCenterModule extends Module { UpdateCenterClient.class, UpdateCenterMatrixFactory.class, UploadAction.class, + InstalledPluginsAction.class, UpdateCenterWs.class); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/updatecenter/ws/InstalledPluginsAction.java b/server/sonar-server/src/main/java/org/sonar/server/updatecenter/ws/InstalledPluginsAction.java new file mode 100644 index 00000000000..84f603fdc72 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/updatecenter/ws/InstalledPluginsAction.java @@ -0,0 +1,71 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.updatecenter.ws; + +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; +import org.sonar.core.platform.PluginInfo; +import org.sonar.server.plugins.ServerPluginRepository; +import org.sonar.updatecenter.common.Version; + +/** + * This web service is used by SonarLint and SonarRunner. + */ +public class InstalledPluginsAction implements UpdateCenterWsAction { + + private final ServerPluginRepository pluginRepository; + + public InstalledPluginsAction(ServerPluginRepository pluginRepository) { + this.pluginRepository = pluginRepository; + } + + @Override + public void define(WebService.NewController context) { + WebService.NewAction action = context.createAction("installed_plugins") + .setDescription("Get the list of all the plugins installed on the SonarQube instance") + .setSince("2.10") + .setDeprecatedSince("6.3") + .setInternal(true) + .setResponseExample(getClass().getResource("installed_plugins-example.json")) + .setHandler(this); + action.createParam("format") + .setDescription("Only json response format is available") + .setPossibleValues("json"); + } + + @Override + public void handle(Request request, Response response) throws Exception { + JsonWriter json = response.newJsonWriter().beginArray(); + for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) { + Version version = pluginInfo.getVersion(); + json.beginObject() + .prop("key", pluginInfo.getKey()) + .prop("name", pluginInfo.getName()); + if (version != null) { + json.prop("version", version.getName()); + } + json.endObject(); + } + json.endArray().close(); + } +} diff --git a/server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/example-installed_plugins.json b/server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/installed_plugins-example.json similarity index 100% rename from server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/example-installed_plugins.json rename to server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/installed_plugins-example.json diff --git a/server/sonar-server/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java index 8bbca3d0dfd..55713a65ef8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java @@ -19,16 +19,16 @@ */ package org.sonar.server.updatecenter; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import org.sonar.core.platform.ComponentContainer; +import static org.assertj.core.api.Assertions.assertThat; + public class UpdateCenterModuleTest { @Test public void verify_count_of_added_components() { ComponentContainer container = new ComponentContainer(); new UpdateCenterModule().configure(container); - assertThat(container.size()).isEqualTo(2 + 4); + assertThat(container.size()).isEqualTo(2 + 5); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/updatecenter/ws/InstalledPluginsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/updatecenter/ws/InstalledPluginsActionTest.java new file mode 100644 index 00000000000..62052f8a514 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/updatecenter/ws/InstalledPluginsActionTest.java @@ -0,0 +1,102 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.updatecenter.ws; + +import org.junit.Test; +import org.sonar.api.server.ws.WebService; +import org.sonar.core.platform.PluginInfo; +import org.sonar.server.plugins.ServerPluginRepository; +import org.sonar.server.ws.WsActionTester; +import org.sonar.updatecenter.common.Version; + +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.test.JsonAssert.assertJson; + +public class InstalledPluginsActionTest { + + private ServerPluginRepository pluginRepository = mock(ServerPluginRepository.class); + + private WsActionTester ws = new WsActionTester(new InstalledPluginsAction(pluginRepository)); + + @Test + public void return_plugins() throws Exception { + when(pluginRepository.getPluginInfos()).thenReturn(asList( + new PluginInfo("java").setName("Java").setVersion(Version.create("3.14")), + new PluginInfo("xoo").setName("Xoo").setVersion(Version.create("1.0")))); + + String result = ws.newRequest().execute().getInput(); + + assertJson(result).isSimilarTo("[" + + " {" + + " \"key\": \"java\",\n" + + " \"name\": \"Java\",\n" + + " \"version\": \"3.14\"\n" + + " }," + + " {" + + " \"key\": \"xoo\",\n" + + " \"name\": \"Xoo\",\n" + + " \"version\": \"1.0\"\n" + + " }" + + "]"); + } + + @Test + public void return_plugins_with_plugin_having_no_version() throws Exception { + when(pluginRepository.getPluginInfos()).thenReturn(singletonList( + new PluginInfo("java").setName("Java"))); + + String result = ws.newRequest().execute().getInput(); + + assertJson(result).isSimilarTo("[" + + " {" + + " \"key\": \"java\",\n" + + " \"name\": \"Java\"\n" + + " }" + + "]"); + } + + @Test + public void test_example() { + when(pluginRepository.getPluginInfos()).thenReturn(asList( + new PluginInfo("findbugs").setName("Findbugs").setVersion(Version.create("2.1")), + new PluginInfo("l10nfr").setName("French Pack").setVersion(Version.create("1.10")), + new PluginInfo("jira").setName("JIRA").setVersion(Version.create("1.2")))); + + String result = ws.newRequest().execute().getInput(); + + assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString()); + } + + @Test + public void test_definition() { + WebService.Action action = ws.getDef(); + assertThat(action).isNotNull(); + assertThat(action.isInternal()).isTrue(); + assertThat(action.isPost()).isFalse(); + assertThat(action.responseExampleAsString()).isNotEmpty(); + assertThat(action.params()).hasSize(1); + } + +} -- 2.39.5