*/
package org.sonar.process.systeminfo;
+import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class SystemInfoUtils {
+ private static final Joiner COMMA_JOINER = Joiner.on(", ");
+
private SystemInfoUtils() {
// prevent instantiation
}
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));
+ }
+ }
}
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();
+ }
}
.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();
}
}
*/
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;
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;
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());
*/
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;
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;
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();
}
mockManagedInstance(false);
assertThat(commonSystemInformation.getManagedProvider())
- .isEmpty();
+ .isNull();
}
@Test
mockManagedInstance(true);
assertThat(commonSystemInformation.getManagedProvider())
- .isEmpty();
+ .isNull();
}
@Test
@Test
public void getExternalUserAuthentication_whenNotDefined_shouldReturnNull() {
assertThat(commonSystemInformation.getExternalUserAuthentication())
- .isEmpty();
+ .isNull();
}
@Test
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;
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)
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");
}
@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
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"));
}
@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
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)
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");
}
@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
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"));
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);