3 * Copyright (C) 2009-2023 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.cluster;
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.SonarRuntime;
31 import org.sonar.api.config.internal.MapSettings;
32 import org.sonar.api.platform.Server;
33 import org.sonar.api.security.SecurityRealm;
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.platform.DockerSupport;
38 import org.sonar.server.platform.StatisticsSupport;
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.api.SonarEdition.COMMUNITY;
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 GlobalSystemSectionTest {
52 public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
54 private MapSettings settings = new MapSettings();
55 private Server server = mock(Server.class);
56 private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
58 private DockerSupport dockerSupport = mock(DockerSupport.class);
59 private StatisticsSupport statisticsSupport = mock(StatisticsSupport.class);
61 private SonarRuntime sonarRuntime = mock(SonarRuntime.class);
63 private GlobalSystemSection underTest = new GlobalSystemSection(settings.asConfig(),
64 server, securityRealmFactory, identityProviderRepository, dockerSupport, statisticsSupport, sonarRuntime);
68 when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
72 public void name_is_not_empty() {
73 assertThat(underTest.toProtobuf().getName()).isEqualTo("System");
77 public void get_realm() {
78 SecurityRealm realm = mock(SecurityRealm.class);
79 when(realm.getName()).thenReturn("LDAP");
80 when(securityRealmFactory.getRealm()).thenReturn(realm);
82 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
83 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
87 public void no_realm() {
88 when(securityRealmFactory.getRealm()).thenReturn(null);
90 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
91 assertThat(attribute(protobuf, "External User Authentication")).isNull();
95 public void get_enabled_identity_providers() {
96 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
100 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
102 .setName("Bitbucket")
104 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
109 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
110 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
114 public void get_enabled_identity_providers_allowing_users_to_signup() {
115 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
119 .setAllowsUsersToSignUp(true));
120 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
122 .setName("Bitbucket")
124 .setAllowsUsersToSignUp(false));
125 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
129 .setAllowsUsersToSignUp(true));
131 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
132 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
136 public void get_force_authentication_defaults_to_true() {
137 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
138 assertThatAttributeIs(protobuf, "Force authentication", true);
142 public void get_force_authentication() {
143 settings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false);
144 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
145 assertThatAttributeIs(protobuf, "Force authentication", false);
149 public void return_Lines_of_Codes_from_StatisticsSupport(){
150 when(statisticsSupport.getLinesOfCode()).thenReturn(17752L);
151 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
152 assertThatAttributeIs(protobuf,"Lines of Code", 17752L);
156 @UseDataProvider("trueOrFalse")
157 public void get_docker_flag(boolean flag) {
158 when(dockerSupport.isRunningInDocker()).thenReturn(flag);
160 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
161 assertThatAttributeIs(protobuf, "Docker", flag);
165 public void get_edition() {
166 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
167 assertThatAttributeIs(protobuf, "Edition", COMMUNITY.getLabel());
171 public static Object[][] trueOrFalse() {
172 return new Object[][] {