From e1822330df3d46fcdd077a09dc75122b02d4a6ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 13 Oct 2017 17:04:12 +0200 Subject: [PATCH] SONAR-9976 add editionBundled flag to several webServices api/plugins/[available|updates] api/system/upgrades --- .../edition/EditionBundledPlugins.java | 38 ++++++++++ .../server/plugins/ws/PluginWSCommons.java | 53 +++++++------- .../platform/ws/example-upgrades_plugins.json | 4 +- .../plugins/ws/example-available_plugins.json | 4 +- .../plugins/ws/example-updates_plugins.json | 2 + .../platform/ws/UpgradesActionTest.java | 10 +-- .../plugins/ws/AvailableActionTest.java | 72 ++++++++++++++++++- .../server/plugins/ws/UpdatesActionTest.java | 4 +- .../properties_per_plugin.json | 35 --------- 9 files changed, 153 insertions(+), 69 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/plugins/edition/EditionBundledPlugins.java delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/plugins/ws/AvailableActionTest/properties_per_plugin.json diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/edition/EditionBundledPlugins.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/edition/EditionBundledPlugins.java new file mode 100644 index 00000000000..362f027005b --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/edition/EditionBundledPlugins.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.plugins.edition; + +import java.util.Arrays; +import org.sonar.updatecenter.common.Plugin; + +public final class EditionBundledPlugins { + + private static final String SONARSOURCE_ORGANIZATION = "SonarSource"; + private static final String[] SONARSOURCE_COMMERCIAL_LICENSES = {"SonarSource", "Commercial"}; + + private EditionBundledPlugins() { + // prevents instantiation + } + + public static boolean isEditionBundled(Plugin plugin) { + return SONARSOURCE_ORGANIZATION.equalsIgnoreCase(plugin.getOrganization()) + && Arrays.stream(SONARSOURCE_COMMERCIAL_LICENSES).anyMatch(s -> s.equalsIgnoreCase(plugin.getLicense())); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java index 44c362d6c45..82ee4b1a472 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ws/PluginWSCommons.java @@ -49,33 +49,35 @@ import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; import static java.lang.String.CASE_INSENSITIVE_ORDER; import static org.apache.commons.lang.StringUtils.isNotBlank; +import static org.sonar.server.plugins.edition.EditionBundledPlugins.isEditionBundled; public class PluginWSCommons { - static final String PROPERTY_KEY = "key"; - static final String PROPERTY_NAME = "name"; - static final String PROPERTY_HASH = "hash"; - static final String PROPERTY_FILENAME = "filename"; - static final String PROPERTY_SONARLINT_SUPPORTED = "sonarLintSupported"; - static final String PROPERTY_DESCRIPTION = "description"; - static final String PROPERTY_LICENSE = "license"; - static final String PROPERTY_VERSION = "version"; - static final String PROPERTY_CATEGORY = "category"; - static final String PROPERTY_ORGANIZATION_NAME = "organizationName"; - static final String PROPERTY_ORGANIZATION_URL = "organizationUrl"; - static final String PROPERTY_DATE = "date"; - static final String PROPERTY_UPDATED_AT = "updatedAt"; - static final String PROPERTY_STATUS = "status"; - static final String PROPERTY_HOMEPAGE_URL = "homepageUrl"; - static final String PROPERTY_ISSUE_TRACKER_URL = "issueTrackerUrl"; - static final String OBJECT_ARTIFACT = "artifact"; - static final String PROPERTY_URL = "url"; - static final String PROPERTY_TERMS_AND_CONDITIONS_URL = "termsAndConditionsUrl"; - static final String OBJECT_UPDATE = "update"; - static final String OBJECT_RELEASE = "release"; - static final String ARRAY_REQUIRES = "requires"; - static final String PROPERTY_UPDATE_CENTER_REFRESH = "updateCenterRefresh"; - static final String PROPERTY_IMPLEMENTATION_BUILD = "implementationBuild"; - static final String PROPERTY_CHANGE_LOG_URL = "changeLogUrl"; + private static final String PROPERTY_KEY = "key"; + private static final String PROPERTY_NAME = "name"; + private static final String PROPERTY_HASH = "hash"; + private static final String PROPERTY_FILENAME = "filename"; + private static final String PROPERTY_SONARLINT_SUPPORTED = "sonarLintSupported"; + private static final String PROPERTY_DESCRIPTION = "description"; + private static final String PROPERTY_LICENSE = "license"; + private static final String PROPERTY_VERSION = "version"; + private static final String PROPERTY_CATEGORY = "category"; + private static final String PROPERTY_ORGANIZATION_NAME = "organizationName"; + private static final String PROPERTY_ORGANIZATION_URL = "organizationUrl"; + private static final String PROPERTY_DATE = "date"; + private static final String PROPERTY_UPDATED_AT = "updatedAt"; + private static final String PROPERTY_STATUS = "status"; + private static final String PROPERTY_HOMEPAGE_URL = "homepageUrl"; + private static final String PROPERTY_ISSUE_TRACKER_URL = "issueTrackerUrl"; + private static final String PROPERTY_EDITION_BUNDLED = "editionBundled"; + private static final String OBJECT_ARTIFACT = "artifact"; + private static final String PROPERTY_URL = "url"; + private static final String PROPERTY_TERMS_AND_CONDITIONS_URL = "termsAndConditionsUrl"; + private static final String OBJECT_UPDATE = "update"; + private static final String OBJECT_RELEASE = "release"; + private static final String ARRAY_REQUIRES = "requires"; + private static final String PROPERTY_UPDATE_CENTER_REFRESH = "updateCenterRefresh"; + private static final String PROPERTY_IMPLEMENTATION_BUILD = "implementationBuild"; + private static final String PROPERTY_CHANGE_LOG_URL = "changeLogUrl"; public static final Ordering NAME_KEY_PLUGIN_METADATA_COMPARATOR = Ordering.natural() .onResultOf(PluginInfo::getName) @@ -142,6 +144,7 @@ public class PluginWSCommons { jsonWriter.prop(PROPERTY_ORGANIZATION_URL, plugin.getOrganizationUrl()); jsonWriter.prop(PROPERTY_HOMEPAGE_URL, plugin.getHomepageUrl()); jsonWriter.prop(PROPERTY_ISSUE_TRACKER_URL, plugin.getIssueTrackerUrl()); + jsonWriter.prop(PROPERTY_EDITION_BUNDLED, isEditionBundled(plugin)); } public void writePluginUpdate(JsonWriter json, PluginUpdate pluginUpdate) { diff --git a/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-upgrades_plugins.json b/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-upgrades_plugins.json index 9d0d7aa4ae8..7364af7a584 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-upgrades_plugins.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/example-upgrades_plugins.json @@ -16,6 +16,7 @@ "license": "Commercial", "organizationName": "SonarSource", "organizationUrl": "http://www.sonarsource.com", + "editionBundled": true, "termsAndConditionsUrl": "http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf", "version": "2.8 (build 5498)" } @@ -28,7 +29,8 @@ "description": "Allows to add your own logo to the SonarQube UI.", "license": "GNU LGPL 3", "organizationName": "SonarSource", - "organizationUrl": "http://www.sonarsource.com" + "organizationUrl": "http://www.sonarsource.com", + "editionBundled": false } ] } diff --git a/server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-available_plugins.json b/server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-available_plugins.json index 0d2077ab2e3..f6453bc3008 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-available_plugins.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-available_plugins.json @@ -9,6 +9,7 @@ "organizationName": "SonarSource", "organizationUrl": "http://www.sonarsource.com", "termsAndConditionsUrl": "http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf", + "editionBundled": true, "release": { "version": "3.2", "date": "2015-03-10" @@ -26,6 +27,7 @@ "license": "GNU LGPL 3", "organizationName": "SonarSource and Jerome Van Der Linden, Stephane Nicolas, Florian Roncari, Thomas Bores", "organizationUrl": "http://www.sonarsource.com", + "editionBundled": false, "release": { "version": "1.0", "date": "2014-03-31" @@ -43,4 +45,4 @@ } ], "updateCenterRefresh": "2015-04-24T16:08:36+0200" -} \ No newline at end of file +} diff --git a/server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-updates_plugins.json b/server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-updates_plugins.json index cee849909a3..8adb9a7226a 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-updates_plugins.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/plugins/ws/example-updates_plugins.json @@ -9,6 +9,7 @@ "organizationName": "SonarSource", "organizationUrl": "http://www.sonarsource.com", "termsAndConditionsUrl": "http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf", + "editionBundled": true, "updates": [ { "release": { @@ -40,6 +41,7 @@ "license": "GNU LGPL 3", "organizationName": "SonarSource and Jerome Van Der Linden, Stephane Nicolas, Florian Roncari, Thomas Bores", "organizationUrl": "http://www.sonarsource.com", + "editionBundled": false, "updates": [ { "release": { diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java index fe378375307..327d9af2d69 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/UpgradesActionTest.java @@ -27,6 +27,7 @@ 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.WsActionTester; import org.sonar.server.ws.WsTester; import org.sonar.updatecenter.common.Plugin; import org.sonar.updatecenter.common.Release; @@ -44,10 +45,9 @@ import static org.sonar.test.JsonAssert.assertJson; public class UpgradesActionTest { private static final String DUMMY_CONTROLLER_KEY = "dummy"; - private static final String JSON_EMPTY_UPGRADE_LIST = - "{" + - " \"upgrades\":" + "[]" + - "}"; + private static final String JSON_EMPTY_UPGRADE_LIST = "{" + + " \"upgrades\":" + "[]" + + "}"; private static Release release; private UpdateCenterMatrixFactory updateCenterFactory = mock(UpdateCenterMatrixFactory.class); @@ -141,6 +141,6 @@ public class UpgradesActionTest { underTest.handle(request, response); assertJson(response.outputAsString()).withStrictArrayOrder() - .isSimilarTo(getClass().getResource("example-upgrades_plugins.json")); + .isSimilarTo(new WsActionTester(underTest).getDef().responseExampleAsString()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java index 05034b1397c..96b8b710567 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/AvailableActionTest.java @@ -27,6 +27,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.DateUtils; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.WsActionTester; import org.sonar.server.ws.WsTester; import org.sonar.updatecenter.common.Plugin; import org.sonar.updatecenter.common.PluginUpdate; @@ -86,6 +87,39 @@ public class AvailableActionTest extends AbstractUpdateCenterBasedPluginsWsActio assertThat(action.responseExample()).isNotNull(); } + @Test + public void verify_example() throws Exception { + logInAsSystemAdministrator(); + when(updateCenter.findAvailablePlugins()).thenReturn(of( + pluginUpdate(release(Plugin.factory("abap") + .setName("ABAP") + .setCategory("Languages") + .setDescription("Enable analysis and reporting on ABAP projects") + .setLicense("Commercial") + .setOrganization("SonarSource") + .setOrganizationUrl("http://www.sonarsource.com") + .setTermsConditionsUrl("http://dist.sonarsource.com/SonarSource_Terms_And_Conditions.pdf"), + "3.2") + .setDate(DateUtils.parseDate("2015-03-10")), + COMPATIBLE), + pluginUpdate(release(Plugin.factory("android") + .setName("Android") + .setCategory("Languages") + .setDescription("Import Android Lint reports.") + .setLicense("GNU LGPL 3") + .setOrganization("SonarSource and Jerome Van Der Linden, Stephane Nicolas, Florian Roncari, Thomas Bores") + .setOrganizationUrl("http://www.sonarsource.com"), + "1.0") + .setDate(DateUtils.parseDate("2014-03-31")) + .addOutgoingDependency(release(Plugin.factory("java").setName("Java").setDescription("SonarQube rule engine."), "0.3.6")), + COMPATIBLE))); + + underTest.handle(request, response); + + WsActionTester actionTester = new WsActionTester(underTest); + assertJson(response.outputAsString()).isSimilarTo(actionTester.getDef().responseExampleAsString()); + } + @Test public void request_fails_with_ForbiddenException_when_user_is_not_logged_in() throws Exception { expectedException.expect(ForbiddenException.class); @@ -128,7 +162,43 @@ public class AvailableActionTest extends AbstractUpdateCenterBasedPluginsWsActio underTest.handle(request, response); - assertJson(response.outputAsString()).isSimilarTo(resource("properties_per_plugin.json")); + assertJson(response.outputAsString()) + .isSimilarTo( + "{" + + " \"plugins\": [" + + " {" + + " \"key\": \"pkey\"," + + " \"name\": \"p_name\"," + + " \"category\": \"p_category\"," + + " \"description\": \"p_description\"," + + " \"license\": \"p_license\"," + + " \"termsAndConditionsUrl\": \"p_t_and_c_url\"," + + " \"organizationName\": \"p_orga_name\"," + + " \"organizationUrl\": \"p_orga_url\"," + + " \"homepageUrl\": \"p_homepage_url\"," + + " \"issueTrackerUrl\": \"p_issue_url\"," + + " \"release\": {" + + " \"version\": \"1.12.1\"," + + " \"date\": \"2015-04-16\"" + + " }," + + " \"update\": {" + + " \"status\": \"COMPATIBLE\"," + + " \"requires\": [" + + " {" + + " \"key\": \"pkey1\"," + + " \"name\": \"p_name_1\"" + + " }," + + " {" + + " \"key\": \"pkey2\"," + + " \"name\": \"p_name_2\"," + + " \"description\": \"p_desc_2\"" + + " }" + + " ]" + + " }" + + " }" + + " ]," + + " \"updateCenterRefresh\": \"2015-04-24T16:08:36+0200\"" + + "}"); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java index 5dee3e9082d..c3d39304e4e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ws/UpdatesActionTest.java @@ -26,6 +26,7 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.DateUtils; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.ws.WsActionTester; import org.sonar.server.ws.WsTester; import org.sonar.updatecenter.common.Plugin; import org.sonar.updatecenter.common.Release; @@ -135,7 +136,8 @@ public class UpdatesActionTest extends AbstractUpdateCenterBasedPluginsWsActionT underTest.handle(request, response); - assertJson(response.outputAsString()).isSimilarTo(getClass().getResource("example-updates_plugins.json")); + assertJson(response.outputAsString()) + .isSimilarTo(new WsActionTester(underTest).getDef().responseExampleAsString()); } @Test diff --git a/server/sonar-server/src/test/resources/org/sonar/server/plugins/ws/AvailableActionTest/properties_per_plugin.json b/server/sonar-server/src/test/resources/org/sonar/server/plugins/ws/AvailableActionTest/properties_per_plugin.json deleted file mode 100644 index dc6002d5d77..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/plugins/ws/AvailableActionTest/properties_per_plugin.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "plugins": [ - { - "key": "pkey", - "name": "p_name", - "category": "p_category", - "description": "p_description", - "license": "p_license", - "termsAndConditionsUrl": "p_t_and_c_url", - "organizationName": "p_orga_name", - "organizationUrl": "p_orga_url", - "homepageUrl": "p_homepage_url", - "issueTrackerUrl": "p_issue_url", - "release": { - "version": "1.12.1", - "date": "2015-04-16" - }, - "update": { - "status": "COMPATIBLE", - "requires": [ - { - "key": "pkey1", - "name": "p_name_1" - }, - { - "key": "pkey2", - "name": "p_name_2", - "description": "p_desc_2" - } - ] - } - } - ], - "updateCenterRefresh": "2015-04-24T16:08:36+0200" -} -- 2.39.5