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 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.api.SonarEdition.DATACENTER;
46 import static org.sonar.api.SonarEdition.DEVELOPER;
47 import static org.sonar.api.SonarEdition.ENTERPRISE;
48 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
49 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
51 @RunWith(DataProviderRunner.class)
52 public class StandaloneSystemSectionTest {
54 private final MapSettings settings = new MapSettings();
55 private final Configuration config = settings.asConfig();
56 private final Server server = mock(Server.class);
57 private final ServerLogging serverLogging = mock(ServerLogging.class);
58 private final OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
59 private final DockerSupport dockerSupport = mock(DockerSupport.class);
60 private final StatisticsSupport statisticsSupport = mock(StatisticsSupport.class);
61 private final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
62 private final CommonSystemInformation commonSystemInformation = mock(CommonSystemInformation.class);
64 private final StandaloneSystemSection underTest = new StandaloneSystemSection(config, server, serverLogging,
65 officialDistribution, dockerSupport, statisticsSupport, sonarRuntime, commonSystemInformation);
69 when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
70 when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
74 public void name_is_not_empty() {
75 assertThat(underTest.name()).isNotEmpty();
79 public void test_getServerId() {
80 when(server.getId()).thenReturn("ABC");
81 assertThat(underTest.getServerId()).isEqualTo("ABC");
85 public void official_distribution() {
86 when(officialDistribution.check()).thenReturn(true);
88 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
89 assertThatAttributeIs(protobuf, "Official Distribution", true);
93 public void not_an_official_distribution() {
94 when(officialDistribution.check()).thenReturn(false);
96 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
97 assertThatAttributeIs(protobuf, "Official Distribution", false);
101 public void toProtobuf_whenExternalUserAuthentication_shouldWriteIt() {
102 when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("LDAP");
103 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
104 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
108 public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() {
109 when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("");
111 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
112 assertThatAttributeIs(protobuf, "External User Authentication", "");
116 public void toProtobuf_whenEnabledIdentityProviders_shouldWriteThem() {
117 when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(List.of("Bitbucket, GitHub"));
119 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
120 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
124 public void toProtobuf_whenAllowsToSignUpEnabledIdentityProviders_shouldWriteThem() {
125 when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(List.of("GitHub"));
127 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
128 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
132 public void return_nb_of_processors() {
133 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
134 assertThat(attribute(protobuf, "Processors").getLongValue()).isPositive();
138 public void toProtobuf_whenForceAuthentication_returnIt() {
139 when(commonSystemInformation.getForceAuthentication()).thenReturn(false);
140 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
141 assertThatAttributeIs(protobuf, "Force authentication", false);
145 public void return_Lines_of_Codes_from_StatisticsSupport(){
146 when(statisticsSupport.getLinesOfCode()).thenReturn(17752L);
147 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
148 assertThatAttributeIs(protobuf,"Lines of Code", 17752L);
152 @UseDataProvider("trueOrFalse")
153 public void return_docker_flag_from_DockerSupport(boolean flag) {
154 when(dockerSupport.isRunningInDocker()).thenReturn(flag);
155 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
156 assertThat(attribute(protobuf, "Docker").getBooleanValue()).isEqualTo(flag);
160 @UseDataProvider("editions")
161 public void get_edition(SonarEdition sonarEdition, String editionLabel) {
162 when(sonarRuntime.getEdition()).thenReturn(sonarEdition);
163 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
164 assertThatAttributeIs(protobuf, "Edition", editionLabel);
168 public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
169 when(commonSystemInformation.getManagedProvider()).thenReturn("OKTA");
171 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
172 assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "OKTA");
176 public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
177 when(commonSystemInformation.getManagedProvider()).thenReturn("");
178 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
180 assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "");
184 public static Object[][] trueOrFalse() {
185 return new Object[][] {
192 public static Object[][] editions() {
193 return new Object[][] {
194 {COMMUNITY, COMMUNITY.getLabel()},
195 {DEVELOPER, DEVELOPER.getLabel()},
196 {ENTERPRISE, ENTERPRISE.getLabel()},
197 {DATACENTER, DATACENTER.getLabel()},