]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18654 Component for indicating if instance is externally managed.
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>
Mon, 6 Mar 2023 14:15:05 +0000 (15:15 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:06 +0000 (20:04 +0000)
server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedInstanceService.java [new file with mode: 0644]
server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedInstanceService.java [new file with mode: 0644]
server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedInstanceServiceTest.java [new file with mode: 0644]
server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java

diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedInstanceService.java b/server/sonar-server-common/src/main/java/org/sonar/server/management/DelegatingManagedInstanceService.java
new file mode 100644 (file)
index 0000000..6fa3c97
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.management;
+
+import java.util.Set;
+import javax.annotation.Priority;
+import org.sonar.api.server.ServerSide;
+
+@ServerSide
+@Priority(ManagedInstanceService.DELEGATING_INSTANCE_PRIORITY)
+public class DelegatingManagedInstanceService implements ManagedInstanceService {
+
+  private final Set<ManagedInstanceService> delegates;
+
+  public DelegatingManagedInstanceService(Set<ManagedInstanceService> delegates) {
+    this.delegates = delegates;
+  }
+
+  public final boolean isInstanceExternallyManaged() {
+    return delegates.stream().anyMatch(ManagedInstanceService::isInstanceExternallyManaged);
+  }
+}
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedInstanceService.java b/server/sonar-server-common/src/main/java/org/sonar/server/management/ManagedInstanceService.java
new file mode 100644 (file)
index 0000000..00add36
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.management;
+
+public interface ManagedInstanceService {
+
+  int DELEGATING_INSTANCE_PRIORITY = 1;
+
+  boolean isInstanceExternallyManaged();
+}
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedInstanceServiceTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/management/DelegatingManagedInstanceServiceTest.java
new file mode 100644 (file)
index 0000000..a772a26
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.management;
+
+import java.util.Collections;
+import java.util.Set;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DelegatingManagedInstanceServiceTest {
+
+  @Test
+  public void isInstanceExternallyManaged_whenNoChecksDefined_returnsFalse() {
+    DelegatingManagedInstanceService checker = new DelegatingManagedInstanceService(Collections.emptySet());
+    assertThat(checker.isInstanceExternallyManaged()).isFalse();
+  }
+
+  @Test
+  public void isInstanceExternallyManaged_whenAllChecksReturnFalse_returnsFalse() {
+
+    DelegatingManagedInstanceService checker = new DelegatingManagedInstanceService(Set.of(() -> false, () -> false));
+    assertThat(checker.isInstanceExternallyManaged()).isFalse();
+  }
+
+  @Test
+  public void isInstanceExternallyManaged_whenOneCheckReturnsTrue_returnsTrue() {
+    DelegatingManagedInstanceService checker = new DelegatingManagedInstanceService(Set.of(() -> false, () -> true, () -> false));
+    assertThat(checker.isInstanceExternallyManaged()).isTrue();
+  }
+
+}
index c57259da763669a3a395263d4b169a1f1599b5ed..10fe7bc75548a634ecfacc300c5dbe4504234a11 100644 (file)
@@ -123,6 +123,7 @@ import org.sonar.server.language.LanguageValidation;
 import org.sonar.server.language.ws.LanguageWs;
 import org.sonar.server.log.ServerLogging;
 import org.sonar.server.loginmessage.LoginMessageFeature;
+import org.sonar.server.management.DelegatingManagedInstanceService;
 import org.sonar.server.measure.index.ProjectsEsModule;
 import org.sonar.server.measure.live.LiveMeasureModule;
 import org.sonar.server.measure.ws.MeasuresWsModule;
@@ -302,6 +303,7 @@ public class PlatformLevel4 extends PlatformLevel {
       new MonitoringWsModule(),
       DefaultBranchNameResolver.class,
       DefaultDocumentationLinkGenerator.class,
+      DelegatingManagedInstanceService.class,
 
       // batch
       new BatchWsModule(),
@@ -634,8 +636,7 @@ public class PlatformLevel4 extends PlatformLevel {
 
       MainCollector.class,
 
-      PluginsRiskConsentFilter.class
-    );
+      PluginsRiskConsentFilter.class);
 
     // system info
     add(new SystemInfoWriterModule(getWebServer()));