From 1e46ff31c0749103915596d5ebdd7d5080000d40 Mon Sep 17 00:00:00 2001 From: Pierre Guillot Date: Wed, 4 Dec 2019 16:42:46 +0100 Subject: [PATCH] SONAR-12512 SONAR-12514 SONAR-515 Add multipleAlmEnabled in the payload --- .../MultipleAlmFeatureProvider.java | 48 +++++++++++++++ .../MultipleAlmFeatureProviderTest.java | 59 +++++++++++++++++++ .../org/sonar/server/ui/ws/GlobalAction.java | 10 +++- .../sonar/server/ui/ws/GlobalActionTest.java | 15 ++++- .../platformlevel/PlatformLevel4.java | 3 + 5 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/almsettings/MultipleAlmFeatureProvider.java create mode 100644 server/sonar-server-common/src/test/java/org/sonar/server/almsettings/MultipleAlmFeatureProviderTest.java diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/almsettings/MultipleAlmFeatureProvider.java b/server/sonar-server-common/src/main/java/org/sonar/server/almsettings/MultipleAlmFeatureProvider.java new file mode 100644 index 00000000000..abbf1c25e7c --- /dev/null +++ b/server/sonar-server-common/src/main/java/org/sonar/server/almsettings/MultipleAlmFeatureProvider.java @@ -0,0 +1,48 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.almsettings; + +import java.util.Optional; +import org.sonar.core.platform.EditionProvider; +import org.sonar.core.platform.PlatformEditionProvider; + +public class MultipleAlmFeatureProvider { + + private PlatformEditionProvider editionProvider; + + public MultipleAlmFeatureProvider(PlatformEditionProvider editionProvider) { + this.editionProvider = editionProvider; + } + + public boolean enabled() { + Optional edition = editionProvider.get(); + if (!edition.isPresent()) { + return false; + } + switch (edition.get()) { + case ENTERPRISE: + case DATACENTER: + return true; + default: + return false; + } + } + +} diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/almsettings/MultipleAlmFeatureProviderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/almsettings/MultipleAlmFeatureProviderTest.java new file mode 100644 index 00000000000..b5d25054345 --- /dev/null +++ b/server/sonar-server-common/src/test/java/org/sonar/server/almsettings/MultipleAlmFeatureProviderTest.java @@ -0,0 +1,59 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.almsettings; + +import java.util.Optional; +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.sonar.core.platform.EditionProvider; +import org.sonar.core.platform.PlatformEditionProvider; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class MultipleAlmFeatureProviderTest { + + private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); + private final MultipleAlmFeatureProvider underTest = new MultipleAlmFeatureProvider(editionProvider); + + @Test + public void is_enabled_on_CE() { + when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY)); + Assertions.assertThat(underTest.enabled()).isFalse(); + } + + @Test + public void is_enabled_on_DE() { + when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER)); + Assertions.assertThat(underTest.enabled()).isFalse(); + } + + @Test + public void is_enabled_on_EE() { + when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.ENTERPRISE)); + Assertions.assertThat(underTest.enabled()).isTrue(); + } + + @Test + public void is_enabled_on_DCE() { + when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DATACENTER)); + Assertions.assertThat(underTest.enabled()).isTrue(); + } +} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/GlobalAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/GlobalAction.java index cd363fb12fd..2d00a78df9e 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/GlobalAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/GlobalAction.java @@ -38,6 +38,7 @@ import org.sonar.core.platform.PlatformEditionProvider; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.dialect.H2; +import org.sonar.server.almsettings.MultipleAlmFeatureProvider; import org.sonar.server.branch.BranchFeatureProxy; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.OrganizationFlags; @@ -80,12 +81,13 @@ public class GlobalAction implements NavigationWsAction, Startable { private final BranchFeatureProxy branchFeature; private final UserSession userSession; private final PlatformEditionProvider editionProvider; + private final MultipleAlmFeatureProvider multipleAlmFeatureProvider; private final WebAnalyticsLoader webAnalyticsLoader; public GlobalAction(PageRepository pageRepository, Configuration config, ResourceTypes resourceTypes, Server server, WebServer webServer, DbClient dbClient, OrganizationFlags organizationFlags, DefaultOrganizationProvider defaultOrganizationProvider, BranchFeatureProxy branchFeature, UserSession userSession, PlatformEditionProvider editionProvider, - WebAnalyticsLoader webAnalyticsLoader) { + MultipleAlmFeatureProvider multipleAlmFeatureProvider, WebAnalyticsLoader webAnalyticsLoader) { this.pageRepository = pageRepository; this.config = config; this.resourceTypes = resourceTypes; @@ -97,6 +99,7 @@ public class GlobalAction implements NavigationWsAction, Startable { this.branchFeature = branchFeature; this.userSession = userSession; this.editionProvider = editionProvider; + this.multipleAlmFeatureProvider = multipleAlmFeatureProvider; this.webAnalyticsLoader = webAnalyticsLoader; this.systemSettingValuesByKey = new HashMap<>(); } @@ -140,6 +143,7 @@ public class GlobalAction implements NavigationWsAction, Startable { writeDatabaseProduction(json); writeOrganizationSupport(json); writeBranchSupport(json); + writeMultipleAlmEnabled(json); editionProvider.get().ifPresent(e -> json.prop("edition", e.name().toLowerCase(Locale.ENGLISH))); json.prop("standalone", webServer.isStandalone()); writeWebAnalytics(json); @@ -202,6 +206,10 @@ public class GlobalAction implements NavigationWsAction, Startable { json.prop("branchesEnabled", branchFeature.isEnabled()); } + private void writeMultipleAlmEnabled(JsonWriter json) { + json.prop("multipleAlmEnabled", multipleAlmFeatureProvider.enabled()); + } + private void writeWebAnalytics(JsonWriter json) { webAnalyticsLoader.getUrlPathToJs().ifPresent(p -> json.prop("webAnalyticsJsPath", p)); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java index 0d4a2a29cd2..1bb8865d9d4 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java @@ -37,6 +37,7 @@ import org.sonar.core.platform.PluginRepository; import org.sonar.db.DbClient; import org.sonar.db.dialect.H2; import org.sonar.db.dialect.PostgreSql; +import org.sonar.server.almsettings.MultipleAlmFeatureProvider; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.organization.TestOrganizationFlags; @@ -68,6 +69,7 @@ public class GlobalActionTest { private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.fromUuid("foo"); private BranchFeatureRule branchFeature = new BranchFeatureRule(); private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class); + private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class); private WebAnalyticsLoader webAnalyticsLoader = mock(WebAnalyticsLoader.class); private WsActionTester ws; @@ -239,6 +241,17 @@ public class GlobalActionTest { assertJson(call()).isSimilarTo("{\"branchesEnabled\":false}"); } + @Test + public void multiple_alm_enabled() { + init(); + when(multipleAlmFeatureProvider.enabled()).thenReturn(true); + assertJson(call()).isSimilarTo("{\"multipleAlmEnabled\":true}"); + + when(multipleAlmFeatureProvider.enabled()).thenReturn(false); + assertJson(call()).isSimilarTo("{\"multipleAlmEnabled\":false}"); + + } + @Test public void can_admin_on_global_level() { init(); @@ -350,7 +363,7 @@ public class GlobalActionTest { }}); pageRepository.start(); GlobalAction wsAction = new GlobalAction(pageRepository, settings.asConfig(), new ResourceTypes(resourceTypeTrees), server, - webServer, dbClient, organizationFlags, defaultOrganizationProvider, branchFeature, userSession, editionProvider, webAnalyticsLoader); + webServer, dbClient, organizationFlags, defaultOrganizationProvider, branchFeature, userSession, editionProvider, multipleAlmFeatureProvider, webAnalyticsLoader); ws = new WsActionTester(wsAction); wsAction.start(); } diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 2c91bda2dd3..0c12f143a4c 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -38,6 +38,7 @@ import org.sonar.core.component.DefaultResourceTypes; import org.sonar.core.extension.CoreExtensionsInstaller; import org.sonar.core.platform.ComponentContainer; import org.sonar.core.platform.PlatformEditionProvider; +import org.sonar.server.almsettings.MultipleAlmFeatureProvider; import org.sonar.server.authentication.AuthenticationModule; import org.sonar.server.authentication.LogOAuthWarning; import org.sonar.server.authentication.ws.AuthenticationWsModule; @@ -503,6 +504,8 @@ public class PlatformLevel4 extends PlatformLevel { CoreExtensionBootstraper.class, CoreExtensionStopper.class, + MultipleAlmFeatureProvider.class, + // Compute engine (must be after Views and Developer Cockpit) ReportAnalysisFailureNotificationModule.class, CeModule.class, -- 2.39.5