3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.monitoring;
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.sonar.api.config.internal.MapSettings;
30 import org.sonar.api.platform.Server;
31 import org.sonar.api.security.SecurityRealm;
32 import org.sonar.api.utils.log.LoggerLevel;
33 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
34 import org.sonar.server.authentication.IdentityProviderRepositoryRule;
35 import org.sonar.server.authentication.TestIdentityProvider;
36 import org.sonar.server.log.ServerLogging;
37 import org.sonar.server.platform.DockerSupport;
38 import org.sonar.server.platform.OfficialDistribution;
39 import org.sonar.server.user.SecurityRealmFactory;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
45 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
47 @RunWith(DataProviderRunner.class)
48 public class StandaloneSystemSectionTest {
51 public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
53 private MapSettings settings = new MapSettings();
54 private Server server = mock(Server.class);
55 private ServerLogging serverLogging = mock(ServerLogging.class);
56 private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
57 private OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
58 private DockerSupport dockerSupport = mock(DockerSupport.class);
60 private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server,
61 serverLogging, officialDistribution, dockerSupport);
65 when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
69 public void name_is_not_empty() {
70 assertThat(underTest.name()).isNotEmpty();
74 public void test_getServerId() {
75 when(server.getId()).thenReturn("ABC");
76 assertThat(underTest.getServerId()).isEqualTo("ABC");
80 public void official_distribution() {
81 when(officialDistribution.check()).thenReturn(true);
83 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
84 assertThatAttributeIs(protobuf, "Official Distribution", true);
88 public void not_an_official_distribution() {
89 when(officialDistribution.check()).thenReturn(false);
91 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
92 assertThatAttributeIs(protobuf, "Official Distribution", false);
96 public void get_realm() {
97 SecurityRealm realm = mock(SecurityRealm.class);
98 when(realm.getName()).thenReturn("LDAP");
99 when(securityRealmFactory.getRealm()).thenReturn(realm);
101 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
102 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
106 public void no_realm() {
107 when(securityRealmFactory.getRealm()).thenReturn(null);
109 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
110 assertThat(attribute(protobuf, "External User Authentication")).isNull();
114 public void get_enabled_identity_providers() {
115 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
119 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
121 .setName("Bitbucket")
123 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
128 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
129 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
133 public void get_enabled_identity_providers_allowing_users_to_signup() {
134 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
138 .setAllowsUsersToSignUp(true));
139 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
141 .setName("Bitbucket")
143 .setAllowsUsersToSignUp(false));
144 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
148 .setAllowsUsersToSignUp(true));
150 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
151 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
155 public void return_nb_of_processors() {
156 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
157 assertThat(attribute(protobuf, "Processors").getLongValue()).isGreaterThan(0);
161 @UseDataProvider("trueOrFalse")
162 public void return_docker_flag_from_DockerSupport(boolean flag) {
163 when(dockerSupport.isRunningInDocker()).thenReturn(flag);
164 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
165 assertThat(attribute(protobuf, "Docker").getBooleanValue()).isEqualTo(flag);
169 public static Object[][] trueOrFalse() {
170 return new Object[][] {