3 * Copyright (C) 2009-2022 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.CoreProperties;
30 import org.sonar.api.config.internal.MapSettings;
31 import org.sonar.api.platform.Server;
32 import org.sonar.api.security.SecurityRealm;
33 import org.sonar.api.utils.log.LoggerLevel;
34 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
35 import org.sonar.server.authentication.IdentityProviderRepositoryRule;
36 import org.sonar.server.authentication.TestIdentityProvider;
37 import org.sonar.server.log.ServerLogging;
38 import org.sonar.server.platform.DockerSupport;
39 import org.sonar.server.platform.OfficialDistribution;
40 import org.sonar.server.user.SecurityRealmFactory;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
46 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
48 @RunWith(DataProviderRunner.class)
49 public class StandaloneSystemSectionTest {
52 public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
54 private MapSettings settings = new MapSettings();
55 private Server server = mock(Server.class);
56 private ServerLogging serverLogging = mock(ServerLogging.class);
57 private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
58 private OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
59 private DockerSupport dockerSupport = mock(DockerSupport.class);
61 private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server,
62 serverLogging, officialDistribution, dockerSupport);
66 when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
70 public void name_is_not_empty() {
71 assertThat(underTest.name()).isNotEmpty();
75 public void test_getServerId() {
76 when(server.getId()).thenReturn("ABC");
77 assertThat(underTest.getServerId()).isEqualTo("ABC");
81 public void official_distribution() {
82 when(officialDistribution.check()).thenReturn(true);
84 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
85 assertThatAttributeIs(protobuf, "Official Distribution", true);
89 public void not_an_official_distribution() {
90 when(officialDistribution.check()).thenReturn(false);
92 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
93 assertThatAttributeIs(protobuf, "Official Distribution", false);
97 public void get_realm() {
98 SecurityRealm realm = mock(SecurityRealm.class);
99 when(realm.getName()).thenReturn("LDAP");
100 when(securityRealmFactory.getRealm()).thenReturn(realm);
102 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
103 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
107 public void no_realm() {
108 when(securityRealmFactory.getRealm()).thenReturn(null);
110 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
111 assertThat(attribute(protobuf, "External User Authentication")).isNull();
115 public void get_enabled_identity_providers() {
116 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
120 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
122 .setName("Bitbucket")
124 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
129 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
130 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
134 public void get_enabled_identity_providers_allowing_users_to_signup() {
135 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
139 .setAllowsUsersToSignUp(true));
140 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
142 .setName("Bitbucket")
144 .setAllowsUsersToSignUp(false));
145 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
149 .setAllowsUsersToSignUp(true));
151 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
152 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
156 public void return_nb_of_processors() {
157 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
158 assertThat(attribute(protobuf, "Processors").getLongValue()).isPositive();
162 public void get_force_authentication_defaults_to_true() {
163 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
164 assertThatAttributeIs(protobuf, "Force authentication", true);
168 public void get_force_authentication() {
169 settings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false);
170 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
171 assertThatAttributeIs(protobuf, "Force authentication", false);
175 @UseDataProvider("trueOrFalse")
176 public void return_docker_flag_from_DockerSupport(boolean flag) {
177 when(dockerSupport.isRunningInDocker()).thenReturn(flag);
178 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
179 assertThat(attribute(protobuf, "Docker").getBooleanValue()).isEqualTo(flag);
183 public static Object[][] trueOrFalse() {
184 return new Object[][] {