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;
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 java.util.List;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.sonar.api.SonarEdition;
30 import org.sonar.api.SonarRuntime;
31 import org.sonar.api.config.Configuration;
32 import org.sonar.api.config.internal.MapSettings;
33 import org.sonar.api.platform.Server;
34 import org.sonar.api.utils.log.LoggerLevel;
35 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
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.platform.StatisticsSupport;
41 import static java.util.Collections.emptyList;
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.api.SonarEdition.COMMUNITY;
46 import static org.sonar.api.SonarEdition.DATACENTER;
47 import static org.sonar.api.SonarEdition.DEVELOPER;
48 import static org.sonar.api.SonarEdition.ENTERPRISE;
49 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
50 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeDoesNotExist;
51 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
53 @RunWith(DataProviderRunner.class)
54 public class StandaloneSystemSectionTest {
56 private final MapSettings settings = new MapSettings();
57 private final Configuration config = settings.asConfig();
58 private final Server server = mock(Server.class);
59 private final ServerLogging serverLogging = mock(ServerLogging.class);
60 private final OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
61 private final DockerSupport dockerSupport = mock(DockerSupport.class);
62 private final StatisticsSupport statisticsSupport = mock(StatisticsSupport.class);
63 private final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
64 private final CommonSystemInformation commonSystemInformation = mock(CommonSystemInformation.class);
66 private final StandaloneSystemSection underTest = new StandaloneSystemSection(config, server, serverLogging,
67 officialDistribution, dockerSupport, statisticsSupport, sonarRuntime, commonSystemInformation);
71 when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
72 when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
76 public void name_is_not_empty() {
77 assertThat(underTest.name()).isNotEmpty();
81 public void test_getServerId() {
82 when(server.getId()).thenReturn("ABC");
83 assertThat(underTest.getServerId()).isEqualTo("ABC");
87 public void official_distribution() {
88 when(officialDistribution.check()).thenReturn(true);
90 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
91 assertThatAttributeIs(protobuf, "Official Distribution", true);
95 public void not_an_official_distribution() {
96 when(officialDistribution.check()).thenReturn(false);
98 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
99 assertThatAttributeIs(protobuf, "Official Distribution", false);
103 public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() {
104 when(commonSystemInformation.getExternalUserAuthentication()).thenReturn(null);
106 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
107 assertThatAttributeDoesNotExist(protobuf, "External User Authentication");
111 public void toProtobuf_whenExternalUserAuthentication_shouldWriteIt() {
112 when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("LDAP");
113 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
114 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
118 public void toProtobuf_whenNoIdentityProviders_shouldWriteNothing() {
119 when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(emptyList());
121 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
122 assertThatAttributeDoesNotExist(protobuf, "Accepted external identity providers");
126 public void toProtobuf_whenEnabledIdentityProviders_shouldWriteThem() {
127 when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(List.of("Bitbucket, GitHub"));
129 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
130 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
134 public void toProtobuf_whenNoAllowsToSignUpEnabledIdentityProviders_shouldWriteNothing() {
135 when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(emptyList());
137 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
138 assertThatAttributeDoesNotExist(protobuf, "External identity providers whose users are allowed to sign themselves up");
142 public void toProtobuf_whenAllowsToSignUpEnabledIdentityProviders_shouldWriteThem() {
143 when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(List.of("GitHub"));
145 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
146 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
150 public void return_nb_of_processors() {
151 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
152 assertThat(attribute(protobuf, "Processors").getLongValue()).isPositive();
156 public void toProtobuf_whenForceAuthentication_returnIt() {
157 when(commonSystemInformation.getForceAuthentication()).thenReturn(false);
158 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
159 assertThatAttributeIs(protobuf, "Force authentication", false);
163 public void return_Lines_of_Codes_from_StatisticsSupport(){
164 when(statisticsSupport.getLinesOfCode()).thenReturn(17752L);
165 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
166 assertThatAttributeIs(protobuf,"Lines of Code", 17752L);
170 @UseDataProvider("trueOrFalse")
171 public void return_docker_flag_from_DockerSupport(boolean flag) {
172 when(dockerSupport.isRunningInDocker()).thenReturn(flag);
173 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
174 assertThat(attribute(protobuf, "Docker").getBooleanValue()).isEqualTo(flag);
178 @UseDataProvider("editions")
179 public void get_edition(SonarEdition sonarEdition, String editionLabel) {
180 when(sonarRuntime.getEdition()).thenReturn(sonarEdition);
181 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
182 assertThatAttributeIs(protobuf, "Edition", editionLabel);
186 public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
187 when(commonSystemInformation.getManagedInstanceProviderName()).thenReturn(null);
188 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
190 assertThatAttributeDoesNotExist(protobuf, "External Users and Groups Provisioning");
194 public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
195 when(commonSystemInformation.getManagedInstanceProviderName()).thenReturn("Okta");
197 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
198 assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "Okta");
202 public static Object[][] trueOrFalse() {
203 return new Object[][] {
210 public static Object[][] editions() {
211 return new Object[][] {
212 {COMMUNITY, COMMUNITY.getLabel()},
213 {DEVELOPER, DEVELOPER.getLabel()},
214 {ENTERPRISE, ENTERPRISE.getLabel()},
215 {DATACENTER, DATACENTER.getLabel()},