diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-05-10 19:09:47 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2016-05-11 11:05:39 +0200 |
commit | cb0937144b627e3167a363270db86b5950e1c2fc (patch) | |
tree | 108b9be9085d27e71626b8370ecbc7c2cf196eca | |
parent | 1c5c7dcbf3d07caa64827e333f0f79f51a6bb19f (diff) | |
download | sonarqube-cb0937144b627e3167a363270db86b5950e1c2fc.tar.gz sonarqube-cb0937144b627e3167a363270db86b5950e1c2fc.zip |
SONAR-7591 Make category an optional field in WS api/plugins/installed
3 files changed, 72 insertions, 14 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstalledAction.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstalledAction.java index a888d7e35a2..4c1034713b6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstalledAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/InstalledAction.java @@ -19,9 +19,11 @@ */ 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); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java index 44911c98988..2ecc8132f0c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/InstalledActionTest.java @@ -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 diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java index d4bed5bb27a..e42473d0337 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java @@ -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. |