]> source.dussan.org Git - sonarqube.git/commitdiff
SONARCLOUD-326 api/system/change_log_level run as if standalone on SC
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 20 Dec 2018 14:49:24 +0000 (15:49 +0100)
committerSonarTech <sonartech@sonarsource.com>
Fri, 21 Dec 2018 19:21:03 +0000 (20:21 +0100)
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/platform/ws/ChangeLogLevelActionModule.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/ws/ChangeLogLevelActionModuleTest.java [new file with mode: 0644]

index 9818b6fa7b33a3430210d978cf092b21601ec422..fd7b34b3e73dd1a88f57d58431b8ec049c48bf04 100644 (file)
@@ -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 (file)
index 0000000..efc901a
--- /dev/null
@@ -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 (file)
index 0000000..a244d6f
--- /dev/null
@@ -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<ComponentAdapter<?>> 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<ComponentAdapter<?>> 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}
+    };
+  }
+
+
+}