3 * Copyright (C) 2009-2021 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.telemetry;
22 import java.sql.DatabaseMetaData;
23 import java.sql.SQLException;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.stream.IntStream;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.api.config.Configuration;
30 import org.sonar.api.impl.utils.TestSystem2;
31 import org.sonar.core.platform.PlatformEditionProvider;
32 import org.sonar.core.platform.PluginInfo;
33 import org.sonar.core.platform.PluginRepository;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.component.ComponentDto;
37 import org.sonar.db.metric.MetricDto;
38 import org.sonar.server.es.EsTester;
39 import org.sonar.server.measure.index.ProjectMeasuresIndex;
40 import org.sonar.server.measure.index.ProjectMeasuresIndexer;
41 import org.sonar.server.platform.DockerSupport;
42 import org.sonar.server.property.InternalProperties;
43 import org.sonar.server.property.MapInternalProperties;
44 import org.sonar.server.user.index.UserIndex;
45 import org.sonar.server.user.index.UserIndexer;
46 import org.sonar.updatecenter.common.Version;
48 import static java.util.Arrays.asList;
49 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
50 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.entry;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.spy;
55 import static org.mockito.Mockito.when;
56 import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY;
57 import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
58 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
59 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
60 import static org.sonar.core.platform.EditionProvider.Edition.COMMUNITY;
61 import static org.sonar.core.platform.EditionProvider.Edition.DEVELOPER;
62 import static org.sonar.core.platform.EditionProvider.Edition.ENTERPRISE;
63 import static org.sonar.db.component.BranchType.BRANCH;
64 import static org.sonar.db.component.BranchType.PULL_REQUEST;
65 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP_KEY;
66 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C_KEY;
68 public class TelemetryDataLoaderImplTest {
69 private final static Long NOW = 100_000_000L;
70 private final TestSystem2 system2 = new TestSystem2().setNow(NOW);
73 public DbTester db = DbTester.create(system2);
75 public EsTester es = EsTester.create();
77 private final FakeServer server = new FakeServer();
78 private final PluginRepository pluginRepository = mock(PluginRepository.class);
79 private final Configuration configuration = mock(Configuration.class);
80 private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
81 private final DockerSupport dockerSupport = mock(DockerSupport.class);
82 private final InternalProperties internalProperties = spy(new MapInternalProperties());
83 private final ProjectMeasuresIndexer projectMeasuresIndexer = new ProjectMeasuresIndexer(db.getDbClient(), es.client());
84 private final UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client());
85 private final LicenseReader licenseReader = mock(LicenseReader.class);
87 private final TelemetryDataLoader communityUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2),
88 new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, configuration, dockerSupport, null);
89 private final TelemetryDataLoader commercialUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2),
90 new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, configuration, dockerSupport, licenseReader);
93 public void send_telemetry_data() {
94 String serverId = "AU-TpxcB-iU5OvuD2FL7";
95 String version = "7.5.4";
96 server.setId(serverId);
97 server.setVersion(version);
98 List<PluginInfo> plugins = asList(newPlugin("java", "4.12.0.11033"), newPlugin("scmgit", "1.2"), new PluginInfo("other"));
99 when(pluginRepository.getPluginInfos()).thenReturn(plugins);
100 when(editionProvider.get()).thenReturn(Optional.of(DEVELOPER));
103 IntStream.range(0, userCount).forEach(i -> db.users().insertUser(u -> u.setExternalIdentityProvider("provider" + i)));
104 db.users().insertUser(u -> u.setActive(false).setExternalIdentityProvider("provider0"));
105 userIndexer.indexAll();
107 MetricDto lines = db.measures().insertMetric(m -> m.setKey(LINES_KEY));
108 MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY));
109 MetricDto coverage = db.measures().insertMetric(m -> m.setKey(COVERAGE_KEY));
110 MetricDto nclocDistrib = db.measures().insertMetric(m -> m.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY));
112 ComponentDto project1 = db.components().insertPublicProject();
113 ComponentDto project1Branch = db.components().insertProjectBranch(project1);
114 db.measures().insertLiveMeasure(project1, lines, m -> m.setValue(200d));
115 db.measures().insertLiveMeasure(project1, ncloc, m -> m.setValue(100d));
116 db.measures().insertLiveMeasure(project1, coverage, m -> m.setValue(80d));
117 db.measures().insertLiveMeasure(project1, nclocDistrib, m -> m.setValue(null).setData("java=200;js=50"));
119 ComponentDto project2 = db.components().insertPublicProject();
120 db.measures().insertLiveMeasure(project2, lines, m -> m.setValue(300d));
121 db.measures().insertLiveMeasure(project2, ncloc, m -> m.setValue(200d));
122 db.measures().insertLiveMeasure(project2, coverage, m -> m.setValue(80d));
123 db.measures().insertLiveMeasure(project2, nclocDistrib, m -> m.setValue(null).setData("java=300;kotlin=2500"));
124 projectMeasuresIndexer.indexAll();
127 db.almSettings().insertAzureAlmSetting();
128 db.almSettings().insertAzureAlmSetting(a -> a.setUrl("https://dev.azure.com"));
129 db.almSettings().insertBitbucketAlmSetting();
130 db.almSettings().insertBitbucketCloudAlmSetting();
131 db.almSettings().insertGitHubAlmSetting();
132 db.almSettings().insertGitHubAlmSetting(a -> a.setUrl("https://api.github.com"));
133 db.almSettings().insertGitlabAlmSetting();
134 db.almSettings().insertGitlabAlmSetting(a -> a.setUrl("https://gitlab.com/api/v4"));
136 TelemetryData data = communityUnderTest.load();
137 assertThat(data.getServerId()).isEqualTo(serverId);
138 assertThat(data.getVersion()).isEqualTo(version);
139 assertThat(data.getEdition()).contains(DEVELOPER);
140 assertDatabaseMetadata(data.getDatabase());
141 assertThat(data.getPlugins()).containsOnly(
142 entry("java", "4.12.0.11033"), entry("scmgit", "1.2"), entry("other", "undefined"));
143 assertThat(data.getUserCount()).isEqualTo(userCount);
144 assertThat(data.getProjectCount()).isEqualTo(2L);
145 assertThat(data.getNcloc()).isEqualTo(300L);
146 assertThat(data.getProjectCountByLanguage()).containsOnly(
147 entry("java", 2L), entry("kotlin", 1L), entry("js", 1L));
148 assertThat(data.getNclocByLanguage()).containsOnly(
149 entry("java", 500L), entry("kotlin", 2500L), entry("js", 50L));
150 assertThat(data.isInDocker()).isFalse();
151 assertThat(data.getAlmIntegrationCountByAlm())
152 .containsEntry("azure_devops_server", 1L)
153 .containsEntry("azure_devops_cloud", 1L)
154 .containsEntry("bitbucket_server", 1L)
155 .containsEntry("bitbucket_cloud", 1L)
156 .containsEntry("gitlab_server", 1L)
157 .containsEntry("gitlab_cloud", 1L)
158 .containsEntry("github_cloud", 1L)
159 .containsEntry("github_server", 1L);
160 assertThat(data.getExternalAuthenticationProviders()).containsExactlyInAnyOrder("provider0", "provider1", "provider2");
163 private void assertDatabaseMetadata(TelemetryData.Database database) {
164 try (DbSession dbSession = db.getDbClient().openSession(false)) {
165 DatabaseMetaData metadata = dbSession.getConnection().getMetaData();
166 assertThat(database.getName()).isEqualTo("H2");
167 assertThat(database.getVersion()).isEqualTo(metadata.getDatabaseProductVersion());
168 } catch (SQLException e) {
169 throw new RuntimeException(e);
174 public void take_largest_branches() {
175 server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4");
176 MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY));
177 ComponentDto project = db.components().insertPublicProject();
178 ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH));
179 ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST));
180 db.measures().insertLiveMeasure(project, ncloc, m -> m.setValue(10d));
181 db.measures().insertLiveMeasure(branch1, ncloc, m -> m.setValue(20d));
182 db.measures().insertLiveMeasure(pr, ncloc, m -> m.setValue(30d));
183 projectMeasuresIndexer.indexAll();
185 TelemetryData data = communityUnderTest.load();
187 assertThat(data.getNcloc()).isEqualTo(20L);
191 public void data_contains_no_license_type_on_community_edition() {
192 TelemetryData data = communityUnderTest.load();
194 assertThat(data.getLicenseType()).isEmpty();
198 public void data_contains_no_license_type_on_commercial_edition_if_no_license() {
199 when(licenseReader.read()).thenReturn(Optional.empty());
201 TelemetryData data = commercialUnderTest.load();
203 assertThat(data.getLicenseType()).isEmpty();
207 public void data_contains_weekly_count_sonarlint_users() {
208 db.users().insertUser(c -> c.setLastSonarlintConnectionDate(NOW - 100_000L));
209 db.users().insertUser(c -> c.setLastSonarlintConnectionDate(NOW));
211 db.users().insertUser(c -> c.setLastSonarlintConnectionDate(NOW - 1_000_000_000L));
212 db.users().insertUser();
214 TelemetryData data = communityUnderTest.load();
215 assertThat(data.sonarlintWeeklyUsers()).isEqualTo(2L);
219 public void data_has_license_type_on_commercial_edition_if_no_license() {
220 String licenseType = randomAlphabetic(12);
221 LicenseReader.License license = mock(LicenseReader.License.class);
222 when(license.getType()).thenReturn(licenseType);
223 when(licenseReader.read()).thenReturn(Optional.of(license));
225 TelemetryData data = commercialUnderTest.load();
227 assertThat(data.getLicenseType()).contains(licenseType);
231 public void send_server_id_and_version() {
232 String id = randomAlphanumeric(40);
233 String version = randomAlphanumeric(10);
235 server.setVersion(version);
237 TelemetryData data = communityUnderTest.load();
238 assertThat(data.getServerId()).isEqualTo(id);
239 assertThat(data.getVersion()).isEqualTo(version);
241 data = commercialUnderTest.load();
242 assertThat(data.getServerId()).isEqualTo(id);
243 assertThat(data.getVersion()).isEqualTo(version);
247 public void send_server_installation_date_and_installation_version() {
248 String installationVersion = "7.9.BEST.LTS.EVER";
249 Long installationDate = 1546300800000L; // 2019/01/01
250 internalProperties.write(InternalProperties.INSTALLATION_DATE, String.valueOf(installationDate));
251 internalProperties.write(InternalProperties.INSTALLATION_VERSION, installationVersion);
253 TelemetryData data = communityUnderTest.load();
255 assertThat(data.getInstallationDate()).isEqualTo(installationDate);
256 assertThat(data.getInstallationVersion()).isEqualTo(installationVersion);
260 public void do_not_send_server_installation_details_if_missing_property() {
261 TelemetryData data = communityUnderTest.load();
262 assertThat(data.getInstallationDate()).isNull();
263 assertThat(data.getInstallationVersion()).isNull();
265 data = commercialUnderTest.load();
266 assertThat(data.getInstallationDate()).isNull();
267 assertThat(data.getInstallationVersion()).isNull();
271 public void send_unanalyzed_languages_flags_when_edition_is_community() {
272 when(editionProvider.get()).thenReturn(Optional.of(COMMUNITY));
273 MetricDto unanalyzedC = db.measures().insertMetric(m -> m.setKey(UNANALYZED_C_KEY));
274 MetricDto unanalyzedCpp = db.measures().insertMetric(m -> m.setKey(UNANALYZED_CPP_KEY));
275 ComponentDto project1 = db.components().insertPublicProject();
276 ComponentDto project2 = db.components().insertPublicProject();
277 db.measures().insertLiveMeasure(project1, unanalyzedC);
278 db.measures().insertLiveMeasure(project2, unanalyzedC);
279 db.measures().insertLiveMeasure(project2, unanalyzedCpp);
281 TelemetryData data = communityUnderTest.load();
283 assertThat(data.hasUnanalyzedC().get()).isTrue();
284 assertThat(data.hasUnanalyzedCpp().get()).isTrue();
288 public void do_not_send_unanalyzed_languages_flags_when_edition_is_not_community() {
289 when(editionProvider.get()).thenReturn(Optional.of(DEVELOPER));
290 MetricDto unanalyzedC = db.measures().insertMetric(m -> m.setKey(UNANALYZED_C_KEY));
291 MetricDto unanalyzedCpp = db.measures().insertMetric(m -> m.setKey(UNANALYZED_CPP_KEY));
292 ComponentDto project1 = db.components().insertPublicProject();
293 ComponentDto project2 = db.components().insertPublicProject();
294 db.measures().insertLiveMeasure(project1, unanalyzedC);
295 db.measures().insertLiveMeasure(project2, unanalyzedCpp);
297 TelemetryData data = communityUnderTest.load();
299 assertThat(data.hasUnanalyzedC()).isEmpty();
300 assertThat(data.hasUnanalyzedCpp()).isEmpty();
304 public void unanalyzed_languages_flags_are_set_to_false_when_no_unanalyzed_languages_and_edition_is_community() {
305 when(editionProvider.get()).thenReturn(Optional.of(COMMUNITY));
307 TelemetryData data = communityUnderTest.load();
309 assertThat(data.hasUnanalyzedC().get()).isFalse();
310 assertThat(data.hasUnanalyzedCpp().get()).isFalse();
314 public void populate_security_custom_config_for_languages_on_enterprise() {
315 when(editionProvider.get()).thenReturn(Optional.of(ENTERPRISE));
317 when(configuration.get("sonar.security.config.javasecurity")).thenReturn(Optional.of("{}"));
318 when(configuration.get("sonar.security.config.phpsecurity")).thenReturn(Optional.of("{}"));
319 when(configuration.get("sonar.security.config.pythonsecurity")).thenReturn(Optional.of("{}"));
320 when(configuration.get("sonar.security.config.roslyn.sonaranalyzer.security.cs")).thenReturn(Optional.of("{}"));
322 TelemetryData data = commercialUnderTest.load();
324 assertThat(data.getCustomSecurityConfigs())
325 .containsExactlyInAnyOrder("java", "php", "python", "csharp");
329 public void skip_security_custom_config_on_community() {
330 when(editionProvider.get()).thenReturn(Optional.of(COMMUNITY));
332 TelemetryData data = communityUnderTest.load();
334 assertThat(data.getCustomSecurityConfigs()).isEmpty();
337 private PluginInfo newPlugin(String key, String version) {
338 return new PluginInfo(key)
339 .setVersion(Version.create(version));