]> source.dussan.org Git - sonarqube.git/blob
33fdb132a4c1da9af39e900fa4979b0625a981f1
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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 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;
37
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;
43
44 public class StandaloneSystemSectionTest {
45
46   private static final String SERVER_ID_PROPERTY = "Server ID";
47   private static final String SERVER_ID_VALIDATED_PROPERTY = "Server ID validated";
48
49   @Rule
50   public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
51
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);
58
59   private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server,
60     serverLogging, serverIdLoader, officialDistribution);
61
62   @Before
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());
67   }
68
69   @Test
70   public void name_is_not_empty() {
71     assertThat(underTest.name()).isNotEmpty();
72   }
73
74   @Test
75   public void test_getServerId() {
76     when(serverIdLoader.getRaw()).thenReturn(Optional.of("ABC"));
77     assertThat(underTest.getServerId()).isEqualTo("ABC");
78
79     when(serverIdLoader.getRaw()).thenReturn(Optional.empty());
80     assertThat(underTest.getServerId()).isNull();
81   }
82
83   @Test
84   public void attributes_contain_information_about_valid_server_id() {
85     when(serverIdLoader.get()).thenReturn(Optional.of(new ServerId("ABC", true)));
86
87     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
88     assertThatAttributeIs(protobuf, SERVER_ID_PROPERTY, "ABC");
89     assertThatAttributeIs(protobuf, SERVER_ID_VALIDATED_PROPERTY, true);
90   }
91
92   @Test
93   public void attributes_contain_information_about_non_valid_server_id() {
94     when(serverIdLoader.get()).thenReturn(Optional.of(new ServerId("ABC", false)));
95
96     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
97     assertThatAttributeIs(protobuf, SERVER_ID_PROPERTY, "ABC");
98     assertThatAttributeIs(protobuf, SERVER_ID_VALIDATED_PROPERTY, false);
99   }
100
101   @Test
102   public void attributes_do_not_contain_information_about_server_id_if_absent() {
103     when(serverIdLoader.get()).thenReturn(Optional.empty());
104
105     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
106     assertThat(attribute(protobuf, SERVER_ID_PROPERTY)).isNull();
107     assertThat(attribute(protobuf, SERVER_ID_VALIDATED_PROPERTY)).isNull();
108   }
109
110   @Test
111   public void official_distribution() throws Exception {
112     when(officialDistribution.check()).thenReturn(true);
113
114     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
115     assertThatAttributeIs(protobuf, "Official Distribution", true);
116   }
117
118   @Test
119   public void not_an_official_distribution() throws Exception {
120     when(officialDistribution.check()).thenReturn(false);
121
122     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
123     assertThatAttributeIs(protobuf, "Official Distribution", false);
124   }
125
126   @Test
127   public void get_log_level() throws Exception {
128     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
129     assertThatAttributeIs(protobuf, "Logs Level", "DEBUG");
130   }
131
132   @Test
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);
137
138     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
139     assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
140   }
141
142   @Test
143   public void no_realm() throws Exception {
144     when(securityRealmFactory.getRealm()).thenReturn(null);
145
146     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
147     assertThat(attribute(protobuf, "External User Authentication")).isNull();
148   }
149
150   @Test
151   public void get_enabled_identity_providers() throws Exception {
152     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
153       .setKey("github")
154       .setName("GitHub")
155       .setEnabled(true));
156     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
157       .setKey("bitbucket")
158       .setName("Bitbucket")
159       .setEnabled(true));
160     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
161       .setKey("disabled")
162       .setName("Disabled")
163       .setEnabled(false));
164
165     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
166     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
167   }
168
169   @Test
170   public void get_enabled_identity_providers_allowing_users_to_signup() throws Exception {
171     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
172       .setKey("github")
173       .setName("GitHub")
174       .setEnabled(true)
175       .setAllowsUsersToSignUp(true));
176     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
177       .setKey("bitbucket")
178       .setName("Bitbucket")
179       .setEnabled(true)
180       .setAllowsUsersToSignUp(false));
181     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
182       .setKey("disabled")
183       .setName("Disabled")
184       .setEnabled(false)
185       .setAllowsUsersToSignUp(true));
186
187     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
188     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
189   }
190
191   @Test
192   public void return_nb_of_processors() {
193     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
194     assertThat(attribute(protobuf, "Processors").getLongValue()).isGreaterThan(0);
195   }
196 }