]> source.dussan.org Git - sonarqube.git/blob
ea5f52d10548c40abe70e6e7c34cef056ff83d13
[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.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;
39
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;
46
47 public class StandaloneSystemSectionTest {
48
49   private static final String SERVER_ID_PROPERTY = "Server ID";
50   private static final String SERVER_ID_VALIDATED_PROPERTY = "Server ID validated";
51
52   @Rule
53   public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
54
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);
62
63   private StandaloneSystemSection underTest = new StandaloneSystemSection(settings.asConfig(), securityRealmFactory, identityProviderRepository, server,
64     serverLogging, serverIdLoader, officialDistribution, healthChecker);
65
66   @Before
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());
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(serverIdLoader.getRaw()).thenReturn(Optional.of("ABC"));
81     assertThat(underTest.getServerId()).isEqualTo("ABC");
82
83     when(serverIdLoader.getRaw()).thenReturn(Optional.empty());
84     assertThat(underTest.getServerId()).isNull();
85   }
86
87   @Test
88   public void attributes_contain_information_about_valid_server_id() {
89     when(serverIdLoader.get()).thenReturn(Optional.of(new ServerId("ABC", true)));
90
91     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
92     assertThatAttributeIs(protobuf, SERVER_ID_PROPERTY, "ABC");
93     assertThatAttributeIs(protobuf, SERVER_ID_VALIDATED_PROPERTY, true);
94   }
95
96   @Test
97   public void attributes_contain_information_about_non_valid_server_id() {
98     when(serverIdLoader.get()).thenReturn(Optional.of(new ServerId("ABC", false)));
99
100     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
101     assertThatAttributeIs(protobuf, SERVER_ID_PROPERTY, "ABC");
102     assertThatAttributeIs(protobuf, SERVER_ID_VALIDATED_PROPERTY, false);
103   }
104
105   @Test
106   public void attributes_do_not_contain_information_about_server_id_if_absent() {
107     when(serverIdLoader.get()).thenReturn(Optional.empty());
108
109     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
110     assertThat(attribute(protobuf, SERVER_ID_PROPERTY)).isNull();
111     assertThat(attribute(protobuf, SERVER_ID_VALIDATED_PROPERTY)).isNull();
112   }
113
114   @Test
115   public void official_distribution() throws Exception {
116     when(officialDistribution.check()).thenReturn(true);
117
118     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
119     assertThatAttributeIs(protobuf, "Official Distribution", true);
120   }
121
122   @Test
123   public void not_an_official_distribution() throws Exception {
124     when(officialDistribution.check()).thenReturn(false);
125
126     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
127     assertThatAttributeIs(protobuf, "Official Distribution", false);
128   }
129
130   @Test
131   public void get_log_level() throws Exception {
132     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
133     assertThatAttributeIs(protobuf, "Logs Level", "DEBUG");
134   }
135
136   @Test
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);
141
142     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
143     assertThatAttributeIs(protobuf, "External User Authentication", "LDAP");
144   }
145
146   @Test
147   public void no_realm() throws Exception {
148     when(securityRealmFactory.getRealm()).thenReturn(null);
149
150     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
151     assertThat(attribute(protobuf, "External User Authentication")).isNull();
152   }
153
154   @Test
155   public void get_enabled_identity_providers() throws Exception {
156     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
157       .setKey("github")
158       .setName("GitHub")
159       .setEnabled(true));
160     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
161       .setKey("bitbucket")
162       .setName("Bitbucket")
163       .setEnabled(true));
164     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
165       .setKey("disabled")
166       .setName("Disabled")
167       .setEnabled(false));
168
169     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
170     assertThatAttributeIs(protobuf, "Accepted external identity providers", "Bitbucket, GitHub");
171   }
172
173   @Test
174   public void get_enabled_identity_providers_allowing_users_to_signup() throws Exception {
175     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
176       .setKey("github")
177       .setName("GitHub")
178       .setEnabled(true)
179       .setAllowsUsersToSignUp(true));
180     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
181       .setKey("bitbucket")
182       .setName("Bitbucket")
183       .setEnabled(true)
184       .setAllowsUsersToSignUp(false));
185     identityProviderRepository.addIdentityProvider(new TestIdentityProvider()
186       .setKey("disabled")
187       .setName("Disabled")
188       .setEnabled(false)
189       .setAllowsUsersToSignUp(true));
190
191     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
192     assertThatAttributeIs(protobuf, "External identity providers whose users are allowed to sign themselves up", "GitHub");
193   }
194
195   @Test
196   public void return_health() {
197     healthChecker.setHealth(Health.newHealthCheckBuilder()
198       .setStatus(Health.Status.YELLOW)
199       .addCause("foo")
200       .addCause("bar")
201       .build());
202
203     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
204     assertThatAttributeIs(protobuf, "Health", "YELLOW");
205     SystemInfoTesting.assertThatAttributeHasOnlyValues(protobuf, "Health Causes", asList("foo", "bar"));
206   }
207
208   @Test
209   public void return_nb_of_processors() {
210     ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
211     assertThat(attribute(protobuf, "Processors").getLongValue()).isGreaterThan(0);
212   }
213 }