]> source.dussan.org Git - sonarqube.git/blob
b0c62e92d3bbc1da9a3b9de2e46aa726d64462e5
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform.monitoring;
21
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;
40
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;
50
51 @RunWith(DataProviderRunner.class)
52 public class StandaloneSystemSectionTest {
53
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);
63
64   private final StandaloneSystemSection underTest = new StandaloneSystemSection(config, server, serverLogging,
65     officialDistribution, dockerSupport, statisticsSupport, sonarRuntime, commonSystemInformation);
66
67   @Before
68   public void setUp() {
69     when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
70     when(sonarRuntime.getEdition()).thenReturn(COMMUNITY);
71   }
72
73   @Test
74   public void name_is_not_empty() {
75     assertThat(underTest.name()).isNotEmpty();
76   }
77
78   @Test
79   public void test_getServerId() {
80     when(server.getId()).thenReturn("ABC");
81     assertThat(underTest.getServerId()).isEqualTo("ABC");
82   }
83
84   @Test
85   public void official_distribution() {
86     when(officialDistribution.check()).thenReturn(true);
87
88     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
89     assertThatAttributeIs(protobuf, "Official Distribution", true);
90   }
91
92   @Test
93   public void not_an_official_distribution() {
94     when(officialDistribution.check()).thenReturn(false);
95
96     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
97     assertThatAttributeIs(protobuf, "Official Distribution", false);
98   }
99
100   @Test
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");
105   }
106
107   @Test
108   public void toProtobuf_whenNoExternalUserAuthentication_shouldWriteNothing() {
109     when(commonSystemInformation.getExternalUserAuthentication()).thenReturn("");
110
111     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
112     assertThatAttributeIs(protobuf, "External User Authentication", "");
113   }
114
115   @Test
116   public void toProtobuf_whenEnabledIdentityProviders_shouldWriteThem() {
117     when(commonSystemInformation.getEnabledIdentityProviders()).thenReturn(List.of("Bitbucket, GitHub"));
118
119     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
120     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
121   }
122
123   @Test
124   public void toProtobuf_whenAllowsToSignUpEnabledIdentityProviders_shouldWriteThem() {
125     when(commonSystemInformation.getAllowsToSignUpEnabledIdentityProviders()).thenReturn(List.of("GitHub"));
126
127     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
128     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
129   }
130
131   @Test
132   public void return_nb_of_processors() {
133     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
134     assertThat(attribute(protobuf, "Processors").getLongValue()).isPositive();
135   }
136
137   @Test
138   public void toProtobuf_whenForceAuthentication_returnIt() {
139     when(commonSystemInformation.getForceAuthentication()).thenReturn(false);
140     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
141     assertThatAttributeIs(protobuf, "Force authentication", false);
142   }
143
144   @Test
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);
149   }
150
151   @Test
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);
157   }
158
159   @Test
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);
165   }
166
167   @Test
168   public void toProtobuf_whenInstanceIsManaged_shouldWriteItsProviderName() {
169     when(commonSystemInformation.getManagedProvider()).thenReturn("OKTA");
170
171     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
172     assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "OKTA");
173   }
174
175   @Test
176   public void toProtobuf_whenInstanceIsNotManaged_shouldWriteNothing() {
177     when(commonSystemInformation.getManagedProvider()).thenReturn("");
178     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
179
180     assertThatAttributeIs(protobuf, "External Users and Groups Provisioning", "");
181   }
182
183   @DataProvider
184   public static Object[][] trueOrFalse() {
185     return new Object[][] {
186       {true},
187       {false}
188     };
189   }
190
191   @DataProvider
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()},
198     };
199   }
200 }