From: Antoine Vigneau Date: Tue, 14 Mar 2023 14:09:53 +0000 (+0100) Subject: SONAR-18654 Make system/info return optional values X-Git-Tag: 10.0.0.68432~92 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6ad827619b064069105da433ea99e7a1a66b6eb6;p=sonarqube.git SONAR-18654 Make system/info return optional values --- diff --git a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java index 04474e44852..24bc246d11c 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java +++ b/server/sonar-process/src/main/java/org/sonar/process/systeminfo/SystemInfoUtils.java @@ -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 values) { + if (values != null && !values.isEmpty()) { + setAttribute(protobuf, key, COMMA_JOINER.join(values)); + } + } } diff --git a/server/sonar-server-common/src/testFixtures/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java b/server/sonar-server-common/src/testFixtures/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java index 5bc33fbe55e..89b1a83a131 100644 --- a/server/sonar-server-common/src/testFixtures/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java +++ b/server/sonar-server-common/src/testFixtures/java/org/sonar/server/platform/monitoring/SystemInfoTesting.java @@ -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(); + } } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java index f37d4bdaeb7..40629019e54 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/CommonSystemInformation.java @@ -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(); } } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java index 451c9865a4b..c238e175deb 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/StandaloneSystemSection.java @@ -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()); diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java index 4d9f45b4d71..31b3cbb7798 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSection.java @@ -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(); } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java index ebda4bb2c03..bc12e9fe82f 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/CommonSystemInformationTest.java @@ -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 diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java index b0c62e92d3b..1735090758b 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/StandaloneSystemSectionTest.java @@ -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 diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java index 578ae8605d7..86c42a4fb52 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/cluster/GlobalSystemSectionTest.java @@ -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);