From 0e4a34396865850189a5bc103b6fb7c25386417d Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 20 Dec 2018 15:49:24 +0100 Subject: [PATCH] SONARCLOUD-326 api/system/change_log_level run as if standalone on SC --- .../platformlevel/PlatformLevel4.java | 12 +- .../ws/ChangeLogLevelActionModule.java | 44 +++++++ .../ws/ChangeLogLevelActionModuleTest.java | 107 ++++++++++++++++++ 3 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelActionModule.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionModuleTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 9818b6fa7b3..fd7b34b3e73 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -121,9 +121,7 @@ import org.sonar.server.platform.WebCoreExtensionsInstaller; import org.sonar.server.platform.monitoring.WebSystemInfoModule; import org.sonar.server.platform.web.WebPagesFilter; import org.sonar.server.platform.web.requestid.HttpRequestIdModule; -import org.sonar.server.platform.ws.ChangeLogLevelAction; -import org.sonar.server.platform.ws.ChangeLogLevelClusterService; -import org.sonar.server.platform.ws.ChangeLogLevelStandaloneService; +import org.sonar.server.platform.ws.ChangeLogLevelActionModule; import org.sonar.server.platform.ws.DbMigrationStatusAction; import org.sonar.server.platform.ws.HealthActionModule; import org.sonar.server.platform.ws.L10nWs; @@ -256,11 +254,7 @@ public class PlatformLevel4 extends PlatformLevel { MetadataIndex.class, EsDbCompatibilityImpl.class); - addIfCluster( - NodeHealthModule.class, - ChangeLogLevelClusterService.class); - addIfStandalone( - ChangeLogLevelStandaloneService.class); + addIfCluster(NodeHealthModule.class); add( ClusterVerification.class, @@ -513,7 +507,7 @@ public class PlatformLevel4 extends PlatformLevel { StatusAction.class, MigrateDbAction.class, LogsAction.class, - ChangeLogLevelAction.class, + ChangeLogLevelActionModule.class, DbMigrationStatusAction.class, HealthActionModule.class, SystemWs.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelActionModule.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelActionModule.java new file mode 100644 index 00000000000..efc901a5c82 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelActionModule.java @@ -0,0 +1,44 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.platform.ws; + +import org.sonar.api.config.Configuration; +import org.sonar.core.platform.Module; +import org.sonar.server.platform.WebServer; + +public class ChangeLogLevelActionModule extends Module { + private final WebServer webServer; + private final Configuration configuration; + + public ChangeLogLevelActionModule(WebServer webServer, Configuration configuration) { + this.webServer = webServer; + this.configuration = configuration; + } + + @Override + protected void configureModule() { + add(ChangeLogLevelAction.class); + if (configuration.getBoolean("sonar.sonarcloud.enabled").orElse(false) || webServer.isStandalone()) { + add(ChangeLogLevelStandaloneService.class); + } else { + add(ChangeLogLevelClusterService.class); + } + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionModuleTest.java new file mode 100644 index 00000000000..a244d6fa74b --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionModuleTest.java @@ -0,0 +1,107 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 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.platform.ws; + +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.util.Collection; +import javax.annotation.Nullable; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.picocontainer.ComponentAdapter; +import org.sonar.api.config.internal.MapSettings; +import org.sonar.core.platform.ComponentContainer; +import org.sonar.server.platform.WebServer; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER; + +@RunWith(DataProviderRunner.class) +public class ChangeLogLevelActionModuleTest { + private WebServer webServer = mock(WebServer.class); + private MapSettings settings = new MapSettings(); + private ChangeLogLevelActionModule underTest = new ChangeLogLevelActionModule(webServer, settings.asConfig()); + + @Test + @UseDataProvider("notOnSonarCloud") + public void provide_returns_ChangeLogLevelClusterService_if_cluster_not_on_SonarCloud(@Nullable Boolean sonarcloudOrNot) { + when(webServer.isStandalone()).thenReturn(false); + if (sonarcloudOrNot != null) { + settings.setProperty("sonar.sonarcloud.enabled", sonarcloudOrNot); + } + ComponentContainer container = new ComponentContainer(); + + underTest.configure(container); + + Collection> adapters = container.getPicoContainer().getComponentAdapters(); + assertThat(adapters) + .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2) + .extracting(ComponentAdapter::getComponentKey) + .contains(ChangeLogLevelClusterService.class, ChangeLogLevelAction.class) + .doesNotContain(ChangeLogLevelStandaloneService.class); + } + + @Test + public void provide_returns_ChangeLogLevelStandaloneService_on_SonarCloud() { + when(webServer.isStandalone()).thenReturn(false); + settings.setProperty("sonar.sonarcloud.enabled", true); + ComponentContainer container = new ComponentContainer(); + + underTest.configure(container); + + verifyInStandaloneSQ(container); + } + + @Test + @UseDataProvider("notOnSonarCloud") + public void provide_returns_ChangeLogLevelStandaloneService_if_SQ_standalone(@Nullable Boolean sonarcloudOrNot) { + when(webServer.isStandalone()).thenReturn(true); + if (sonarcloudOrNot != null) { + settings.setProperty("sonar.sonarcloud.enabled", sonarcloudOrNot); + } + ComponentContainer container = new ComponentContainer(); + + underTest.configure(container); + + verifyInStandaloneSQ(container); + } + + private void verifyInStandaloneSQ(ComponentContainer container) { + Collection> adapters = container.getPicoContainer().getComponentAdapters(); + assertThat(adapters) + .hasSize(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 2) + .extracting(ComponentAdapter::getComponentKey) + .contains(ChangeLogLevelStandaloneService.class, ChangeLogLevelAction.class) + .doesNotContain(ChangeLogLevelClusterService.class); + } + + @DataProvider + public static Object[][] notOnSonarCloud() { + return new Object[][] { + {null}, + {false} + }; + } + + +} -- 2.39.5