]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7591 Make category an optional field in WS api/plugins/installed 950/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 10 May 2016 17:09:47 +0000 (19:09 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 11 May 2016 09:05:39 +0000 (11:05 +0200)
server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstalledAction.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java
sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java

index a888d7e35a25d179177be50ca5ba11eea16923a6..4c1034713b6dd7fd55f4e7da76db00238dc07ecb 100644 (file)
  */
 package org.sonar.server.plugins.ws;
 
-import com.google.common.collect.ImmutableMap;
 import com.google.common.io.Resources;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
 import java.util.SortedSet;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
@@ -33,6 +35,8 @@ import org.sonar.server.plugins.UpdateCenterMatrixFactory;
 import org.sonar.updatecenter.common.Plugin;
 
 import static com.google.common.collect.ImmutableSortedSet.copyOf;
+import static java.lang.String.format;
+import static java.util.Collections.singleton;
 import static org.sonar.server.plugins.ws.PluginWSCommons.NAME_KEY_PLUGIN_METADATA_COMPARATOR;
 import static org.sonar.server.plugins.ws.PluginWSCommons.compatiblePluginsByKey;
 
@@ -41,6 +45,7 @@ import static org.sonar.server.plugins.ws.PluginWSCommons.compatiblePluginsByKey
  */
 public class InstalledAction implements PluginsWsAction {
   private static final String ARRAY_PLUGINS = "plugins";
+  private static final String FIELD_CATEGORY = "category";
 
   private final ServerPluginRepository pluginRepository;
   private final PluginWSCommons pluginWSCommons;
@@ -54,11 +59,18 @@ public class InstalledAction implements PluginsWsAction {
 
   @Override
   public void define(WebService.NewController controller) {
-    controller.createAction("installed")
+    WebService.NewAction action = controller.createAction("installed")
       .setDescription("Get the list of all the plugins installed on the SonarQube instance, sorted by plugin name")
       .setSince("5.2")
       .setHandler(this)
       .setResponseExample(Resources.getResource(this.getClass(), "example-installed_plugins.json"));
+
+    action.createFieldsParam(singleton("category"))
+      .setDescription(format("Comma-separated list of the additional fields to be returned in response. No additional field is returned by default. Possible values are:" +
+        "<ul>" +
+        "<li>%s - category as defined in the Update Center. A connection to the Update Center is needed</li>" +
+        "</lu>", FIELD_CATEGORY))
+      .setSince("5.6");
   }
 
   @Override
@@ -69,7 +81,8 @@ public class InstalledAction implements PluginsWsAction {
     jsonWriter.setSerializeEmptys(false);
     jsonWriter.beginObject();
 
-    writePluginInfoList(jsonWriter, pluginInfoList);
+    List<String> additionalFields = request.paramAsStrings(WebService.Param.FIELDS);
+    writePluginInfoList(jsonWriter, pluginInfoList, additionalFields == null ? Collections.<String>emptyList() : additionalFields);
 
     jsonWriter.endObject();
     jsonWriter.close();
@@ -79,8 +92,10 @@ public class InstalledAction implements PluginsWsAction {
     return copyOf(NAME_KEY_PLUGIN_METADATA_COMPARATOR, pluginRepository.getPluginInfos());
   }
 
-  private void writePluginInfoList(JsonWriter jsonWriter, Collection<PluginInfo> pluginInfoList) {
-    ImmutableMap<String, Plugin> compatiblesPluginsByKeys = compatiblePluginsByKey(updateCenterMatrixFactory);
-    pluginWSCommons.writePluginInfoList(jsonWriter, pluginInfoList, compatiblesPluginsByKeys, ARRAY_PLUGINS);
+  private void writePluginInfoList(JsonWriter jsonWriter, Collection<PluginInfo> pluginInfoList, List<String> additionalFields) {
+    Map<String, Plugin> compatiblesPluginsFromUpdateCenter = additionalFields.isEmpty()
+      ? Collections.<String, Plugin>emptyMap()
+      : compatiblePluginsByKey(updateCenterMatrixFactory);
+    pluginWSCommons.writePluginInfoList(jsonWriter, pluginInfoList, compatiblesPluginsFromUpdateCenter, ARRAY_PLUGINS);
   }
 }
index 44911c9898873bac4edf350d64bcb681a28b3688..2ecc8132f0cdf11b64bc9d92d6ea05207dfad40b 100644 (file)
@@ -27,6 +27,7 @@ import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.core.platform.PluginInfo;
 import org.sonar.server.plugins.ServerPluginRepository;
 import org.sonar.server.plugins.UpdateCenterMatrixFactory;
@@ -36,9 +37,11 @@ import org.sonar.updatecenter.common.UpdateCenter;
 import org.sonar.updatecenter.common.Version;
 
 import static com.google.common.collect.ImmutableList.of;
+import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 import static org.sonar.test.JsonAssert.assertJson;
 
@@ -96,10 +99,7 @@ public class InstalledActionTest {
   @Test
   public void empty_fields_are_not_serialized_to_json() throws Exception {
     when(pluginRepository.getPluginInfos()).thenReturn(
-      of(
-      new PluginInfo("").setName("")
-      )
-      );
+      of(new PluginInfo("").setName("")));
 
     underTest.handle(request, response);
 
@@ -123,14 +123,57 @@ public class InstalledActionTest {
         .setJarFile(new File(getClass().getResource(jarFilename).toURI()))
       )
       );
+
+    underTest.handle(request, response);
+
+    verifyZeroInteractions(updateCenterMatrixFactory);
+    assertJson(response.outputAsString()).isSimilarTo(
+      "{" +
+        "  \"plugins\":" +
+        "  [" +
+        "    {" +
+        "      \"key\": \"plugKey\"," +
+        "      \"name\": \"plugName\"," +
+        "      \"description\": \"desc_it\"," +
+        "      \"version\": \"1.0\"," +
+        "      \"license\": \"license_hey\"," +
+        "      \"organizationName\": \"org_name\"," +
+        "      \"organizationUrl\": \"org_url\"," +
+        "      \"homepageUrl\": \"homepage_url\"," +
+        "      \"issueTrackerUrl\": \"issueTracker_url\"," +
+        "      \"implementationBuild\": \"sou_rev_sha1\"" +
+        "    }" +
+        "  ]" +
+        "}"
+      );
+  }
+
+  @Test
+  public void category_is_returned_when_in_additional_fields() throws Exception {
+    String jarFilename = getClass().getSimpleName() + "/" + "some.jar";
+    when(pluginRepository.getPluginInfos()).thenReturn(of(
+      new PluginInfo("plugKey")
+        .setName("plugName")
+        .setDescription("desc_it")
+        .setVersion(Version.create("1.0"))
+        .setLicense("license_hey")
+        .setOrganizationName("org_name")
+        .setOrganizationUrl("org_url")
+        .setHomepageUrl("homepage_url")
+        .setIssueTrackerUrl("issueTracker_url")
+        .setImplementationBuild("sou_rev_sha1")
+        .setJarFile(new File(getClass().getResource(jarFilename).toURI()))
+      )
+    );
     UpdateCenter updateCenter = mock(UpdateCenter.class);
     when(updateCenterMatrixFactory.getUpdateCenter(false)).thenReturn(Optional.of(updateCenter));
     when(updateCenter.findAllCompatiblePlugins()).thenReturn(
       Arrays.asList(
         new Plugin("plugKey")
           .setCategory("cat_1")
-        )
-      );
+      )
+    );
+    when(request.paramAsStrings(Param.FIELDS)).thenReturn(singletonList("category"));
 
     underTest.handle(request, response);
 
@@ -153,7 +196,7 @@ public class InstalledActionTest {
         "    }" +
         "  ]" +
         "}"
-      );
+    );
   }
 
   @Test
index d4bed5bb27a81ce3d33f27e757691dba04a41478..e42473d03377e9d5595e3c168ff5ec5a6e7720bb 100644 (file)
@@ -387,7 +387,7 @@ public interface WebService extends Definable<WebService.Context> {
         .setPossibleValues(possibleValues);
     }
 
-    /**$
+    /**
      *
      * Creates the parameter {@link org.sonar.api.server.ws.WebService.Param#TEXT_QUERY}, which is
      * used to search for a subset of fields containing the supplied string.