]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6506 Rewrite internal WS api/updatecenter/installed_plugins in Java 1569/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 26 Jan 2017 12:16:07 +0000 (13:16 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 26 Jan 2017 13:24:55 +0000 (14:24 +0100)
server/sonar-server/src/main/java/org/sonar/server/updatecenter/UpdateCenterModule.java
server/sonar-server/src/main/java/org/sonar/server/updatecenter/ws/InstalledPluginsAction.java [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/example-installed_plugins.json [deleted file]
server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/installed_plugins-example.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/updatecenter/UpdateCenterModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/updatecenter/ws/InstalledPluginsActionTest.java [new file with mode: 0644]

index 0164e05e1fd7bf945623f9e93225a58b249e48e4..2eeb02061cf4f226ffd77036d372acc8f2a63884 100644 (file)
@@ -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 (file)
index 0000000..84f603f
--- /dev/null
@@ -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/example-installed_plugins.json
deleted file mode 100644 (file)
index ca65c90..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-[
-  {
-    "key": "findbugs",
-    "name": "Findbugs",
-    "version": "2.1"
-  },
-  {
-    "key": "l10nfr",
-    "name": "French Pack",
-    "version": "1.10"
-  },
-  {
-    "key": "jira",
-    "name": "JIRA",
-    "version": "1.2"
-  }
-]
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/installed_plugins-example.json b/server/sonar-server/src/main/resources/org/sonar/server/updatecenter/ws/installed_plugins-example.json
new file mode 100644 (file)
index 0000000..ca65c90
--- /dev/null
@@ -0,0 +1,17 @@
+[
+  {
+    "key": "findbugs",
+    "name": "Findbugs",
+    "version": "2.1"
+  },
+  {
+    "key": "l10nfr",
+    "name": "French Pack",
+    "version": "1.10"
+  },
+  {
+    "key": "jira",
+    "name": "JIRA",
+    "version": "1.2"
+  }
+]
index 8bbca3d0dfd4f9432337d9122c7e6ae9ef8a8760..55713a65ef87ca1aec19119305bc1f6f91344225 100644 (file)
  */
 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 (file)
index 0000000..62052f8
--- /dev/null
@@ -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);
+  }
+
+}