3 * Copyright (C) 2009-2017 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 java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.config.internal.MapSettings;
27 import org.sonar.api.platform.Server;
28 import org.sonar.api.security.SecurityRealm;
29 import org.sonar.api.utils.log.LoggerLevel;
30 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
31 import org.sonar.server.authentication.IdentityProviderRepositoryRule;
32 import org.sonar.server.authentication.TestIdentityProvider;
33 import org.sonar.server.health.Health;
34 import org.sonar.server.health.TestStandaloneHealthChecker;
35 import org.sonar.server.platform.ServerId;
36 import org.sonar.server.platform.ServerIdLoader;
37 import org.sonar.server.platform.ServerLogging;
38 import org.sonar.server.user.SecurityRealmFactory;
40 import static java.util.Arrays.asList;
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.process.systeminfo.SystemInfoUtils.attribute;
45 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
47 public class StandaloneSystemSectionTest {
49 private static final String SERVER_ID_PROPERTY = "Server ID";
50 private static final String SERVER_ID_VALIDATED_PROPERTY = "Server ID validated";
53 public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
55 private MapSettings settings = new MapSettings();
56 private Server server = mock(Server.class);
57 private ServerIdLoader serverIdLoader = mock(ServerIdLoader.class);
58 private ServerLogging serverLogging = mock(ServerLogging.class);
59 private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
60 private TestStandaloneHealthChecker healthChecker = new TestStandaloneHealthChecker();
61 private OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
63 private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server,
64 serverLogging, serverIdLoader, officialDistribution, healthChecker);
67 public void setUp() throws Exception {
68 when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
69 when(serverIdLoader.getRaw()).thenReturn(Optional.empty());
70 when(serverIdLoader.get()).thenReturn(Optional.empty());
74 public void name_is_not_empty() {
75 assertThat(underTest.name()).isNotEmpty();
79 public void test_getServerId() {
80 when(serverIdLoader.getRaw()).thenReturn(Optional.of("ABC"));
81 assertThat(underTest.getServerId()).isEqualTo("ABC");
83 when(serverIdLoader.getRaw()).thenReturn(Optional.empty());
84 assertThat(underTest.getServerId()).isNull();
88 public void attributes_contain_information_about_valid_server_id() {
89 when(serverIdLoader.get()).thenReturn(Optional.of(new ServerId("ABC", true)));
91 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
92 assertThatAttributeIs(protobuf, SERVER_ID_PROPERTY, "ABC");
93 assertThatAttributeIs(protobuf, SERVER_ID_VALIDATED_PROPERTY, true);
97 public void attributes_contain_information_about_non_valid_server_id() {
98 when(serverIdLoader.get()).thenReturn(Optional.of(new ServerId("ABC", false)));
100 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
101 assertThatAttributeIs(protobuf, SERVER_ID_PROPERTY, "ABC");
102 assertThatAttributeIs(protobuf, SERVER_ID_VALIDATED_PROPERTY, false);
106 public void attributes_do_not_contain_information_about_server_id_if_absent() {
107 when(serverIdLoader.get()).thenReturn(Optional.empty());
109 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
110 assertThat(attribute(protobuf, SERVER_ID_PROPERTY)).isNull();
111 assertThat(attribute(protobuf, SERVER_ID_VALIDATED_PROPERTY)).isNull();
115 public void official_distribution() throws Exception {
116 when(officialDistribution.check()).thenReturn(true);
118 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
119 assertThatAttributeIs(protobuf, "Official Distribution", true);
123 public void not_an_official_distribution() throws Exception {
124 when(officialDistribution.check()).thenReturn(false);
126 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
127 assertThatAttributeIs(protobuf, "Official Distribution", false);
131 public void get_log_level() throws Exception {
132 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
133 assertThatAttributeIs(protobuf, "Logs Level", "DEBUG");
137 public void get_realm() throws Exception {
138 SecurityRealm realm = mock(SecurityRealm.class);
139 when(realm.getName()).thenReturn("LDAP");
140 when(securityRealmFactory.getRealm()).thenReturn(realm);
142 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
143 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
147 public void no_realm() throws Exception {
148 when(securityRealmFactory.getRealm()).thenReturn(null);
150 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
151 assertThat(attribute(protobuf, "External User Authentication")).isNull();
155 public void get_enabled_identity_providers() throws Exception {
156 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
160 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
162 .setName("Bitbucket")
164 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
169 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
170 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
174 public void get_enabled_identity_providers_allowing_users_to_signup() throws Exception {
175 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
179 .setAllowsUsersToSignUp(true));
180 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
182 .setName("Bitbucket")
184 .setAllowsUsersToSignUp(false));
185 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
189 .setAllowsUsersToSignUp(true));
191 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
192 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
196 public void return_health() {
197 healthChecker.setHealth(Health.newHealthCheckBuilder()
198 .setStatus(Health.Status.YELLOW)
203 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
204 assertThatAttributeIs(protobuf, "Health", "YELLOW");
205 SystemInfoTesting.assertThatAttributeHasOnlyValues(protobuf, "Health Causes", asList("foo", "bar"));
209 public void return_nb_of_processors() {
210 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
211 assertThat(attribute(protobuf, "Processors").getLongValue()).isGreaterThan(0);