From 85d04f60de0d293d4d2070db0725af2ea52f7b16 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 4 May 2015 10:07:04 +0200 Subject: [PATCH] SONAR-6365 consistent properties for plugin in JSON response --- .../platform/ws/UpgradesSystemWsAction.java | 33 ++++++++++--------- ...ins.json => example-upgrades_plugins.json} | 13 ++++++-- .../ws/UpgradesSystemWsActionTest.java | 7 ++-- 3 files changed, 33 insertions(+), 20 deletions(-) rename server/sonar-server/src/main/resources/org/sonar/server/platform/ws/{example-updates_plugins.json => example-upgrades_plugins.json} (65%) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/UpgradesSystemWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/UpgradesSystemWsAction.java index d3c9d4fea27..5e845559201 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/UpgradesSystemWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/UpgradesSystemWsAction.java @@ -26,6 +26,7 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; import org.sonar.server.plugins.UpdateCenterMatrixFactory; +import org.sonar.server.plugins.ws.PluginWSCommons; import org.sonar.updatecenter.common.Plugin; import org.sonar.updatecenter.common.Release; import org.sonar.updatecenter.common.SonarUpdate; @@ -39,8 +40,8 @@ import com.google.common.io.Resources; public class UpgradesSystemWsAction implements SystemWsAction { private static final boolean DO_NOT_FORCE_REFRESH = false; + private static final String ARRAY_UPGRADES = "upgrades"; - private static final String PROPERTY_NAME = "name"; private static final String PROPERTY_UPDATE_CENTER_REFRESH = "updateCenterRefresh"; private static final String PROPERTY_VERSION = "version"; private static final String PROPERTY_DESCRIPTION = "description"; @@ -49,13 +50,14 @@ public class UpgradesSystemWsAction implements SystemWsAction { private static final String PROPERTY_DOWNLOAD_URL = "downloadUrl"; private static final String OBJECT_PLUGINS = "plugins"; private static final String ARRAY_REQUIRE_UPDATE = "requireUpdate"; - private static final String PROPERTY_KEY = "key"; private static final String ARRAY_INCOMPATIBLE = "incompatible"; private final UpdateCenterMatrixFactory updateCenterFactory; + private final PluginWSCommons pluginWSCommons; - public UpgradesSystemWsAction(UpdateCenterMatrixFactory updateCenterFactory) { + public UpgradesSystemWsAction(UpdateCenterMatrixFactory updateCenterFactory, PluginWSCommons pluginWSCommons) { this.updateCenterFactory = updateCenterFactory; + this.pluginWSCommons = pluginWSCommons; } @Override @@ -67,20 +69,26 @@ public class UpgradesSystemWsAction implements SystemWsAction { "Plugin information is retrieved from Update Center. Date and time at which Update Center was last refreshed " + "is provided in the response.") .setHandler(this) - .setResponseExample(Resources.getResource(this.getClass(), "example-updates_plugins.json")); + .setResponseExample(Resources.getResource(this.getClass(), "example-upgrades_plugins.json")); } @Override public void handle(Request request, Response response) throws Exception { JsonWriter jsonWriter = response.newJsonWriter().setSerializeEmptys(false); + writeResponse(jsonWriter); + + jsonWriter.close(); + } + + private void writeResponse(JsonWriter jsonWriter) { jsonWriter.beginObject(); + UpdateCenter updateCenter = updateCenterFactory.getUpdateCenter(DO_NOT_FORCE_REFRESH); writeUpgrades(jsonWriter, updateCenter); jsonWriter.propDateTime(PROPERTY_UPDATE_CENTER_REFRESH, updateCenter.getDate()); - jsonWriter.endObject(); - jsonWriter.close(); + jsonWriter.endObject(); } private void writeUpgrades(JsonWriter jsonWriter, UpdateCenter updateCenter) { @@ -126,8 +134,7 @@ public class UpgradesSystemWsAction implements SystemWsAction { for (Release release : pluginsToUpgrade) { jsonWriter.beginObject(); - Plugin plugin = (Plugin) release.getArtifact(); - writePlugin(jsonWriter, plugin); + pluginWSCommons.writeMetadata(jsonWriter, (Plugin) release.getArtifact()); jsonWriter.prop(PROPERTY_VERSION, release.getVersion().toString()); jsonWriter.endObject(); @@ -136,18 +143,14 @@ public class UpgradesSystemWsAction implements SystemWsAction { jsonWriter.endArray(); } - private void writePlugin(JsonWriter jsonWriter, Plugin plugin) { - jsonWriter.prop(PROPERTY_KEY, plugin.getKey()); - jsonWriter.prop(PROPERTY_NAME, plugin.getName()); - jsonWriter.prop(PROPERTY_DESCRIPTION, plugin.getDescription()); - } - private void writeIncompatiblePlugins(JsonWriter jsonWriter, List incompatiblePlugins) { jsonWriter.name(ARRAY_INCOMPATIBLE).beginArray(); for (Plugin incompatiblePlugin : incompatiblePlugins) { jsonWriter.beginObject(); - writePlugin(jsonWriter, incompatiblePlugin); + + pluginWSCommons.writeMetadata(jsonWriter, incompatiblePlugin); + jsonWriter.endObject(); } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-updates_plugins.json b/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-upgrades_plugins.json similarity index 65% rename from server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-updates_plugins.json rename to server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-upgrades_plugins.json index d9438402d0c..83abaa3db82 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-updates_plugins.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-upgrades_plugins.json @@ -11,7 +11,12 @@ { "key": "views", "name": "Views", + "category": "Governance", "description": "Create aggregation trees to group projects. Projects can for instance be grouped by applications, applications by team, teams by department.", + "license": "Commercial", + "organizationName": "SonarSource", + "organizationUrl": "http://www.sonarsource.com", + "termsAndConditionsUrl": "http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf", "version": "2.8" } ], @@ -19,11 +24,15 @@ { "key": "branding", "name": "Branding", - "description": "Allows to add your own logo to the SonarQube UI." + "category": "Integration", + "description": "Allows to add your own logo to the SonarQube UI.", + "license": "GNU LGPL 3", + "organizationName": "SonarSource", + "organizationUrl": "http://www.sonarsource.com" } ] } } ], "updateCenterRefresh": "2015-04-24T16:08:36+0200" -} \ No newline at end of file +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesSystemWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesSystemWsActionTest.java index 73a2dd04b5e..f073f03d56d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesSystemWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesSystemWsActionTest.java @@ -25,6 +25,7 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.DateUtils; import org.sonar.server.plugins.UpdateCenterMatrixFactory; +import org.sonar.server.plugins.ws.PluginWSCommons; import org.sonar.server.ws.WsTester; import org.sonar.updatecenter.common.Plugin; import org.sonar.updatecenter.common.Release; @@ -49,7 +50,7 @@ public class UpgradesSystemWsActionTest { private UpdateCenterMatrixFactory updateCenterFactory = mock(UpdateCenterMatrixFactory.class); private UpdateCenter updateCenter = mock(UpdateCenter.class); - private UpgradesSystemWsAction underTest = new UpgradesSystemWsAction(updateCenterFactory); + private UpgradesSystemWsAction underTest = new UpgradesSystemWsAction(updateCenterFactory, new PluginWSCommons()); private Request request = mock(Request.class); private WsTester.TestResponse response = new WsTester.TestResponse(); @@ -94,7 +95,7 @@ public class UpgradesSystemWsActionTest { underTest.handle(request, response); assertJson(response.outputAsString()).setStrictArrayOrder(true) - .isSimilarTo(getClass().getResource("example-updates_plugins.json")); + .isSimilarTo(getClass().getResource("example-upgrades_plugins.json")); } private static SonarUpdate createSonar_51_update() { @@ -115,7 +116,7 @@ public class UpgradesSystemWsActionTest { .setHomepageUrl("http://redirect.sonarsource.com/plugins/views.html") .setLicense("Commercial") .setOrganization("SonarSource") - .setOrganizationUrl("http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf") + .setOrganizationUrl("http://www.sonarsource.com") .setTermsConditionsUrl("http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf") .setIssueTrackerUrl("http://jira.sonarsource.com/browse/VIEWS"); -- 2.39.5