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.SonarEdition;
31 import org.sonar.api.SonarRuntime;
32 import org.sonar.api.config.internal.MapSettings;
33 import org.sonar.api.platform.Server;
34 import org.sonar.api.security.SecurityRealm;
35 import org.sonar.api.utils.log.LoggerLevel;
36 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
37 import org.sonar.server.authentication.IdentityProviderRepositoryRule;
38 import org.sonar.server.authentication.TestIdentityProvider;
39 import org.sonar.server.log.ServerLogging;
40 import org.sonar.server.platform.DockerSupport;
41 import org.sonar.server.platform.OfficialDistribution;
42 import org.sonar.server.user.SecurityRealmFactory;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.sonar.api.SonarEdition.COMMUNITY;
48 import static org.sonar.api.SonarEdition.DATACENTER;
49 import static org.sonar.api.SonarEdition.DEVELOPER;
50 import static org.sonar.api.SonarEdition.ENTERPRISE;
51 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
52 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
54 @RunWith(DataProviderRunner.class)
55 public class StandaloneSystemSectionTest {
58 public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
60 private MapSettings settings = new MapSettings();
61 private Server server = mock(Server.class);
62 private ServerLogging serverLogging = mock(ServerLogging.class);
63 private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
64 private OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
65 private DockerSupport dockerSupport = mock(DockerSupport.class);
67 private SonarRuntime sonarRuntime = mock(SonarRuntime.class);
69 private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server,
70 serverLogging, officialDistribution, dockerSupport, sonarRuntime);
74 when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
75 when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
79 public void name_is_not_empty() {
80 assertThat(underTest.name()).isNotEmpty();
84 public void test_getServerId() {
85 when(server.getId()).thenReturn("ABC");
86 assertThat(underTest.getServerId()).isEqualTo("ABC");
90 public void official_distribution() {
91 when(officialDistribution.check()).thenReturn(true);
93 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
94 assertThatAttributeIs(protobuf, "Official Distribution", true);
98 public void not_an_official_distribution() {
99 when(officialDistribution.check()).thenReturn(false);
101 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
102 assertThatAttributeIs(protobuf, "Official Distribution", false);
106 public void get_realm() {
107 SecurityRealm realm = mock(SecurityRealm.class);
108 when(realm.getName()).thenReturn("LDAP");
109 when(securityRealmFactory.getRealm()).thenReturn(realm);
111 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
112 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
116 public void no_realm() {
117 when(securityRealmFactory.getRealm()).thenReturn(null);
119 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
120 assertThat(attribute(protobuf, "External User Authentication")).isNull();
124 public void get_enabled_identity_providers() {
125 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
129 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
131 .setName("Bitbucket")
133 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
138 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
139 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
143 public void get_enabled_identity_providers_allowing_users_to_signup() {
144 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
148 .setAllowsUsersToSignUp(true));
149 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
151 .setName("Bitbucket")
153 .setAllowsUsersToSignUp(false));
154 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
158 .setAllowsUsersToSignUp(true));
160 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
161 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
165 public void return_nb_of_processors() {
166 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
167 assertThat(attribute(protobuf, "Processors").getLongValue()).isPositive();
171 public void get_force_authentication_defaults_to_true() {
172 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
173 assertThatAttributeIs(protobuf, "Force authentication", true);
177 public void get_force_authentication() {
178 settings.setProperty(CoreProperties.CORE_FORCE_AUTHENTICATION_PROPERTY, false);
179 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
180 assertThatAttributeIs(protobuf, "Force authentication", false);
184 @UseDataProvider("trueOrFalse")
185 public void return_docker_flag_from_DockerSupport(boolean flag) {
186 when(dockerSupport.isRunningInDocker()).thenReturn(flag);
187 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
188 assertThat(attribute(protobuf, "Docker").getBooleanValue()).isEqualTo(flag);
192 @UseDataProvider("editions")
193 public void get_edition(SonarEdition sonarEdition, String editionLabel) {
194 when(sonarRuntime.getEdition()).thenReturn(sonarEdition);
195 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
196 assertThatAttributeIs(protobuf, "Edition", editionLabel);
200 public static Object[][] trueOrFalse() {
201 return new Object[][] {
208 public static Object[][] editions() {
209 return new Object[][] {
210 {COMMUNITY, COMMUNITY.getLabel()},
211 {DEVELOPER, DEVELOPER.getLabel()},
212 {ENTERPRISE, ENTERPRISE.getLabel()},
213 {DATACENTER, DATACENTER.getLabel()},