]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18654 Make system/info return optional values
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Tue, 14 Mar 2023 14:09:53 +0000 (15:09 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:07 +0000 (20:04 +0000)
server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java
server/sonar-server-common/src/testFixtures/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java

index 04474e4485287ea34ca820627e13f0320180fb6b..24bc246d11c6a2ff96a21452e2edb79a5f4b990c 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.process.systeminfo;
 
+import com.google.common.base.Joiner;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -33,6 +34,8 @@ import static java.util.Arrays.stream;
 
 public class SystemInfoUtils {
 
+  private static final Joiner COMMA_JOINER = Joiner.on(", ");
+
   private SystemInfoUtils() {
     // prevent instantiation
   }
@@ -95,4 +98,10 @@ public class SystemInfoUtils {
     result.addAll(alphabeticalOrderedMap.values());
     return result;
   }
+
+  public static void addIfNotEmpty(ProtobufSystemInfo.Section.Builder protobuf, String key, @Nullable List<String> values) {
+    if (values != null && !values.isEmpty()) {
+      setAttribute(protobuf, key, COMMA_JOINER.join(values));
+    }
+  }
 }
index 5bc33fbe55ed7ca5596e5a7e67b50aaa0f90e2e9..89b1a83a13163308d180940db77e712ab53c7487 100644 (file)
@@ -47,4 +47,9 @@ public class SystemInfoTesting {
     assertThat(value).as(key).isNotNull();
     assertThat(value.getLongValue()).isEqualTo(expectedValue);
   }
+
+  public static void assertThatAttributeDoesNotExist(ProtobufSystemInfo.Section section, String key) {
+    ProtobufSystemInfo.Attribute value = attribute(section, key);
+    assertThat(value).as(key).isNull();
+  }
 }
index f37d4bdaeb73172b5e8375d8a75bf9640e8117c6..40629019e54963ea23c363c3c8842bd592291d83 100644 (file)
@@ -78,14 +78,14 @@ public class CommonSystemInformation {
         .filter(IdentityProvider::isEnabled)
         .findFirst()
         .map(IdentityProvider::getName)
-        .orElse("");
+        .orElse(null);
     }
-    return "";
+    return null;
   }
 
   @CheckForNull
   public String getExternalUserAuthentication() {
     SecurityRealm realm = securityRealmFactory.getRealm();
-    return realm == null ? "" : realm.getName();
+    return realm == null ? null : realm.getName();
   }
 }
index 451c9865a4ba2fc7f0aefe96a83dfd6f58ed9703..c238e175deb0fb6d000a07275963ffb038822996 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.platform.monitoring;
 
-import com.google.common.base.Joiner;
 import org.sonar.api.SonarRuntime;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.platform.Server;
@@ -34,12 +33,11 @@ import static org.sonar.api.measures.CoreMetrics.NCLOC;
 import static org.sonar.process.ProcessProperties.Property.PATH_DATA;
 import static org.sonar.process.ProcessProperties.Property.PATH_HOME;
 import static org.sonar.process.ProcessProperties.Property.PATH_TEMP;
+import static org.sonar.process.systeminfo.SystemInfoUtils.addIfNotEmpty;
 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
 
 public class StandaloneSystemSection extends BaseSectionMBean implements SystemSectionMBean {
 
-  private static final Joiner COMMA_JOINER = Joiner.on(", ");
-
   private final Configuration config;
   private final Server server;
   private final ServerLogging serverLogging;
@@ -95,9 +93,10 @@ public class StandaloneSystemSection extends BaseSectionMBean implements SystemS
     setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker());
     setAttribute(protobuf, "External Users and Groups Provisioning", commonSystemInformation.getManagedProvider());
     setAttribute(protobuf, "External User Authentication", commonSystemInformation.getExternalUserAuthentication());
-    setAttribute(protobuf, "Accepted external identity providers", COMMA_JOINER.join(commonSystemInformation.getEnabledIdentityProviders()));
-    setAttribute(protobuf, "External identity providers whose users are allowed to sign themselves up",
-      COMMA_JOINER.join(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()));
+    addIfNotEmpty(protobuf, "Accepted external identity providers",
+      commonSystemInformation.getEnabledIdentityProviders());
+    addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up",
+      commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders());
     setAttribute(protobuf, "High Availability", false);
     setAttribute(protobuf, "Official Distribution", officialDistribution.check());
     setAttribute(protobuf, "Force authentication", commonSystemInformation.getForceAuthentication());
index 4d9f45b4d71cc335c23c7e60eb3a91f2aa228d7b..31b3cbb77986df230ae51bca4f65db82b1b0d54b 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.platform.monitoring.cluster;
 
-import com.google.common.base.Joiner;
 import org.sonar.api.SonarRuntime;
 import org.sonar.api.platform.Server;
 import org.sonar.api.server.ServerSide;
@@ -31,13 +30,12 @@ import org.sonar.server.platform.StatisticsSupport;
 import org.sonar.server.platform.monitoring.CommonSystemInformation;
 
 import static org.sonar.api.measures.CoreMetrics.NCLOC;
+import static org.sonar.process.systeminfo.SystemInfoUtils.addIfNotEmpty;
 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
 
 @ServerSide
 public class GlobalSystemSection implements SystemInfoSection, Global {
 
-  private static final Joiner COMMA_JOINER = Joiner.on(", ");
-
   private final Server server;
   private final DockerSupport dockerSupport;
   private final StatisticsSupport statisticsSupport;
@@ -63,10 +61,14 @@ public class GlobalSystemSection implements SystemInfoSection, Global {
     setAttribute(protobuf, NCLOC.getName() ,statisticsSupport.getLinesOfCode());
     setAttribute(protobuf, "Docker", dockerSupport.isRunningInDocker());
     setAttribute(protobuf, "High Availability", true);
-    setAttribute(protobuf, "External Users and Groups Provisioning", commonSystemInformation.getManagedProvider());
-    setAttribute(protobuf, "External User Authentication", commonSystemInformation.getExternalUserAuthentication());
-    setAttribute(protobuf, "Accepted external identity providers", COMMA_JOINER.join(commonSystemInformation.getEnabledIdentityProviders()));
-    setAttribute(protobuf, "External identity providers whose users are allowed to sign themselves up", COMMA_JOINER.join(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()));
+    setAttribute(protobuf, "External Users and Groups Provisioning",
+      commonSystemInformation.getManagedProvider());
+    setAttribute(protobuf, "External User Authentication",
+      commonSystemInformation.getExternalUserAuthentication());
+    addIfNotEmpty(protobuf, "Accepted external identity providers",
+      commonSystemInformation.getEnabledIdentityProviders());
+    addIfNotEmpty(protobuf, "External identity providers whose users are allowed to sign themselves up",
+      commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders());
     setAttribute(protobuf, "Force authentication", commonSystemInformation.getForceAuthentication());
     return protobuf.build();
   }
index ebda4bb2c038fdf1be77fdff09e54fc3c6061965..bc12e9fe82f90cb29d60abdf5b7e9dd093ca5ec5 100644 (file)
@@ -126,7 +126,7 @@ public class CommonSystemInformationTest {
     mockManagedInstance(false);
 
     assertThat(commonSystemInformation.getManagedProvider())
-      .isEmpty();
+      .isNull();
   }
 
   @Test
@@ -135,7 +135,7 @@ public class CommonSystemInformationTest {
     mockManagedInstance(true);
 
     assertThat(commonSystemInformation.getManagedProvider())
-      .isEmpty();
+      .isNull();
   }
 
   @Test
@@ -153,7 +153,7 @@ public class CommonSystemInformationTest {
   @Test
   public void getExternalUserAuthentication_whenNotDefined_shouldReturnNull() {
     assertThat(commonSystemInformation.getExternalUserAuthentication())
-      .isEmpty();
+      .isNull();
   }
 
   @Test
index b0c62e92d3bbc1da9a3b9de2e46aa726d64462e5..1735090758b2f14354676d93e2a0966caa485c10 100644 (file)
@@ -38,6 +38,7 @@ import org.sonar.server.platform.DockerSupport;
 import org.sonar.server.platform.OfficialDistribution;
 import org.sonar.server.platform.StatisticsSupport;
 
+import static java.util.Collections.emptyList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -46,6 +47,7 @@ import static org.sonar.api.SonarEdition.DATACENTER;
 import static org.sonar.api.SonarEdition.DEVELOPER;
 import static org.sonar.api.SonarEdition.ENTERPRISE;
 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
+import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeDoesNotExist;
 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
 
 @RunWith(DataProviderRunner.class)
@@ -97,6 +99,14 @@ public class StandaloneSystemSectionTest {
     assertThatAttributeIs(protobuf, "Official Distribution", false);
   }
 
+  @Test
+  public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() {
+    when(commonSystemInformation.getExternalUserAuthentication()).thenReturn(null);
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeDoesNotExist(protobuf, "External User Authentication");
+  }
+
   @Test
   public void toProtobuf_whenExternalUserAuthentication_shouldWriteIt() {
     when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("LDAP");
@@ -105,11 +115,11 @@ public class StandaloneSystemSectionTest {
   }
 
   @Test
-  public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() {
-    when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("");
+  public void toProtobuf_whenNoIdentityProviders_shouldWriteNothing() {
+    when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(emptyList());
 
     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
-    assertThatAttributeIs(protobuf, "External User Authentication", "");
+    assertThatAttributeDoesNotExist(protobuf, "Accepted external identity providers");
   }
 
   @Test
@@ -120,6 +130,14 @@ public class StandaloneSystemSectionTest {
     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
   }
 
+  @Test
+  public void toProtobuf_whenNoAllowsToSignUpEnabledIdentityProviders_shouldWriteNothing() {
+    when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(emptyList());
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeDoesNotExist(protobuf, "External identity providers whose users are allowed to sign themselves up");
+  }
+
   @Test
   public void toProtobuf_whenAllowsToSignUpEnabledIdentityProviders_shouldWriteThem() {
     when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(List.of("GitHub"));
@@ -165,19 +183,19 @@ public class StandaloneSystemSectionTest {
   }
 
   @Test
-  public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
-    when(commonSystemInformation.getManagedProvider()).thenReturn("OKTA");
-
+  public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
+    when(commonSystemInformation.getManagedProvider()).thenReturn(null);
     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
-    assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "OKTA");
+
+    assertThatAttributeDoesNotExist(protobuf, "External Users and Groups Provisioning");
   }
 
   @Test
-  public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
-    when(commonSystemInformation.getManagedProvider()).thenReturn("");
-    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+  public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
+    when(commonSystemInformation.getManagedProvider()).thenReturn("Okta");
 
-    assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "");
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "Okta");
   }
 
   @DataProvider
index 578ae8605d77968b54cc1809a270f9658aabfc85..86c42a4fb52af183dac5ed6be4ca9cfd7c1653f3 100644 (file)
@@ -33,10 +33,12 @@ import org.sonar.server.platform.DockerSupport;
 import org.sonar.server.platform.StatisticsSupport;
 import org.sonar.server.platform.monitoring.CommonSystemInformation;
 
+import static java.util.Collections.emptyList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.sonar.api.SonarEdition.COMMUNITY;
+import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeDoesNotExist;
 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
 
 @RunWith(DataProviderRunner.class)
@@ -60,6 +62,14 @@ public class GlobalSystemSectionTest {
     assertThat(underTest.toProtobuf().getName()).isEqualTo("System");
   }
 
+  @Test
+  public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() {
+    when(commonSystemInformation.getExternalUserAuthentication()).thenReturn(null);
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeDoesNotExist(protobuf, "External User Authentication");
+  }
+
   @Test
   public void toProtobuf_whenExternalUserAuthentication_shouldWriteIt() {
     when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("LDAP");
@@ -68,11 +78,11 @@ public class GlobalSystemSectionTest {
   }
 
   @Test
-  public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() {
-    when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("");
+  public void toProtobuf_whenNoIdentityProviders_shouldWriteNothing() {
+    when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(emptyList());
 
     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
-    assertThatAttributeIs(protobuf, "External User Authentication", "");
+    assertThatAttributeDoesNotExist(protobuf, "Accepted external identity providers");
   }
 
   @Test
@@ -83,6 +93,14 @@ public class GlobalSystemSectionTest {
     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
   }
 
+  @Test
+  public void toProtobuf_whenNoAllowsToSignUpEnabledIdentityProviders_shouldWriteNothing() {
+    when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(emptyList());
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeDoesNotExist(protobuf, "External identity providers whose users are allowed to sign themselves up");
+  }
+
   @Test
   public void toProtobuf_whenAllowsToSignUpEnabledIdentityProviders_shouldWriteThem() {
     when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(List.of("GitHub"));
@@ -91,6 +109,22 @@ public class GlobalSystemSectionTest {
     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
   }
 
+  @Test
+  public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
+    when(commonSystemInformation.getManagedProvider()).thenReturn(null);
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeDoesNotExist(protobuf, "External Users and Groups Provisioning");
+  }
+
+  @Test
+  public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
+    when(commonSystemInformation.getManagedProvider()).thenReturn("Okta");
+
+    ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+    assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "Okta");
+  }
+
   @Test
   public void toProtobuf_whenForceAuthentication_returnIt() {
     when(commonSystemInformation.getForceAuthentication()).thenReturn(false);