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.platform.ServerId;
34 import org.sonar.server.platform.ServerIdLoader;
35 import org.sonar.server.platform.ServerLogging;
36 import org.sonar.server.user.SecurityRealmFactory;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
42 import static org.sonar.server.platform.monitoring.SystemInfoTesting.assertThatAttributeIs;
44 public class StandaloneSystemSectionTest {
46 private static final String SERVER_ID_PROPERTY = "Server ID";
47 private static final String SERVER_ID_VALIDATED_PROPERTY = "Server ID validated";
50 public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
52 private MapSettings settings = new MapSettings();
53 private Server server = mock(Server.class);
54 private ServerIdLoader serverIdLoader = mock(ServerIdLoader.class);
55 private ServerLogging serverLogging = mock(ServerLogging.class);
56 private SecurityRealmFactory securityRealmFactory = mock(SecurityRealmFactory.class);
57 private OfficialDistribution officialDistribution = mock(OfficialDistribution.class);
59 private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server,
60 serverLogging, serverIdLoader, officialDistribution);
63 public void setUp() throws Exception {
64 when(serverLogging.getRootLoggerLevel()).thenReturn(LoggerLevel.DEBUG);
65 when(serverIdLoader.getRaw()).thenReturn(Optional.empty());
66 when(serverIdLoader.get()).thenReturn(Optional.empty());
70 public void name_is_not_empty() {
71 assertThat(underTest.name()).isNotEmpty();
75 public void test_getServerId() {
76 when(serverIdLoader.getRaw()).thenReturn(Optional.of("ABC"));
77 assertThat(underTest.getServerId()).isEqualTo("ABC");
79 when(serverIdLoader.getRaw()).thenReturn(Optional.empty());
80 assertThat(underTest.getServerId()).isNull();
84 public void attributes_contain_information_about_valid_server_id() {
85 when(serverIdLoader.get()).thenReturn(Optional.of(new ServerId("ABC", true)));
87 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
88 assertThatAttributeIs(protobuf, SERVER_ID_PROPERTY, "ABC");
89 assertThatAttributeIs(protobuf, SERVER_ID_VALIDATED_PROPERTY, true);
93 public void attributes_contain_information_about_non_valid_server_id() {
94 when(serverIdLoader.get()).thenReturn(Optional.of(new ServerId("ABC", false)));
96 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
97 assertThatAttributeIs(protobuf, SERVER_ID_PROPERTY, "ABC");
98 assertThatAttributeIs(protobuf, SERVER_ID_VALIDATED_PROPERTY, false);
102 public void attributes_do_not_contain_information_about_server_id_if_absent() {
103 when(serverIdLoader.get()).thenReturn(Optional.empty());
105 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
106 assertThat(attribute(protobuf, SERVER_ID_PROPERTY)).isNull();
107 assertThat(attribute(protobuf, SERVER_ID_VALIDATED_PROPERTY)).isNull();
111 public void official_distribution() throws Exception {
112 when(officialDistribution.check()).thenReturn(true);
114 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
115 assertThatAttributeIs(protobuf, "Official Distribution", true);
119 public void not_an_official_distribution() throws Exception {
120 when(officialDistribution.check()).thenReturn(false);
122 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
123 assertThatAttributeIs(protobuf, "Official Distribution", false);
127 public void get_log_level() throws Exception {
128 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
129 assertThatAttributeIs(protobuf, "Logs Level", "DEBUG");
133 public void get_realm() throws Exception {
134 SecurityRealm realm = mock(SecurityRealm.class);
135 when(realm.getName()).thenReturn("LDAP");
136 when(securityRealmFactory.getRealm()).thenReturn(realm);
138 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
139 assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
143 public void no_realm() throws Exception {
144 when(securityRealmFactory.getRealm()).thenReturn(null);
146 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
147 assertThat(attribute(protobuf, "External User Authentication")).isNull();
151 public void get_enabled_identity_providers() throws Exception {
152 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
156 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
158 .setName("Bitbucket")
160 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
165 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
166 assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
170 public void get_enabled_identity_providers_allowing_users_to_signup() throws Exception {
171 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
175 .setAllowsUsersToSignUp(true));
176 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
178 .setName("Bitbucket")
180 .setAllowsUsersToSignUp(false));
181 identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
185 .setAllowsUsersToSignUp(true));
187 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
188 assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
192 public void return_nb_of_processors() {
193 ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
194 assertThat(attribute(protobuf, "Processors").getLongValue()).isGreaterThan(0);