*/
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;
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;
*/
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;
@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
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();
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);
}
}
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;
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;
@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);
.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);
" }" +
" ]" +
"}"
- );
+ );
}
@Test