diff options
author | Antoine Vigneau <antoine.vigneau@sonarsource.com> | 2023-03-14 15:09:53 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-03-22 20:04:07 +0000 |
commit | 6ad827619b064069105da433ea99e7a1a66b6eb6 (patch) | |
tree | 8c69fe628b8c8991b85133afb7ff1e787b8f760b /server/sonar-webserver-core | |
parent | c96fc17ae41f274d56cd329af1373a94352d74bf (diff) | |
download | sonarqube-6ad827619b064069105da433ea99e7a1a66b6eb6.tar.gz sonarqube-6ad827619b064069105da433ea99e7a1a66b6eb6.zip |
SONAR-18654 Make system/info return optional values
Diffstat (limited to 'server/sonar-webserver-core')
6 files changed, 86 insertions, 33 deletions
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) @@ -98,6 +100,14 @@ public class StandaloneSystemSectionTest { } @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"); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); @@ -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 @@ -121,6 +131,14 @@ public class StandaloneSystemSectionTest { } @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) @@ -61,6 +63,14 @@ public class GlobalSystemSectionTest { } @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"); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); @@ -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 @@ -84,6 +94,14 @@ public class GlobalSystemSectionTest { } @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")); @@ -92,6 +110,22 @@ public class GlobalSystemSectionTest { } @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); ProtobufSystemInfo.Section protobuf = underTest.toProtobuf(); |