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.impl.utils.TestSystem2;
30 import org.sonar.core.platform.PlatformEditionProvider;
31 import org.sonar.core.platform.PluginInfo;
32 import org.sonar.core.platform.PluginRepository;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.db.metric.MetricDto;
37 import org.sonar.server.es.EsTester;
38 import org.sonar.server.measure.index.ProjectMeasuresIndex;
39 import org.sonar.server.measure.index.ProjectMeasuresIndexer;
40 import org.sonar.server.platform.DockerSupport;
41 import org.sonar.server.property.InternalProperties;
42 import org.sonar.server.property.MapInternalProperties;
43 import org.sonar.server.user.index.UserIndex;
44 import org.sonar.server.user.index.UserIndexer;
45 import org.sonar.updatecenter.common.Version;
47 import static java.util.Arrays.asList;
48 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
49 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
50 import static org.assertj.core.api.Assertions.assertThat;
51 import static org.assertj.core.api.Assertions.entry;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.spy;
54 import static org.mockito.Mockito.when;
55 import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY;
56 import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
57 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
58 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
59 import static org.sonar.core.platform.EditionProvider.Edition.COMMUNITY;
60 import static org.sonar.core.platform.EditionProvider.Edition.DEVELOPER;
61 import static org.sonar.db.component.BranchType.BRANCH;
62 import static org.sonar.db.component.BranchType.PULL_REQUEST;
63 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP_KEY;
64 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C_KEY;
66 public class TelemetryDataLoaderImplTest {
68 public DbTester db = DbTester.create();
70 public EsTester es = EsTester.create();
72 private final FakeServer server = new FakeServer();
73 private final PluginRepository pluginRepository = mock(PluginRepository.class);
74 private final TestSystem2 system2 = new TestSystem2().setNow(System.currentTimeMillis());
75 private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
76 private final DockerSupport dockerSupport = mock(DockerSupport.class);
77 private final InternalProperties internalProperties = spy(new MapInternalProperties());
78 private final ProjectMeasuresIndexer projectMeasuresIndexer = new ProjectMeasuresIndexer(db.getDbClient(), es.client());
79 private final UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client());
80 private final LicenseReader licenseReader = mock(LicenseReader.class);
82 private final TelemetryDataLoader communityUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2),
83 new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, dockerSupport, null);
84 private final TelemetryDataLoader commercialUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2),
85 new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, internalProperties, dockerSupport, licenseReader);
88 public void send_telemetry_data() {
89 String serverId = "AU-TpxcB-iU5OvuD2FL7";
90 String version = "7.5.4";
91 server.setId(serverId);
92 server.setVersion(version);
93 List<PluginInfo> plugins = asList(newPlugin("java", "4.12.0.11033"), newPlugin("scmgit", "1.2"), new PluginInfo("other"));
94 when(pluginRepository.getPluginInfos()).thenReturn(plugins);
95 when(editionProvider.get()).thenReturn(Optional.of(DEVELOPER));
98 IntStream.range(0, userCount).forEach(i -> db.users().insertUser());
99 db.users().insertUser(u -> u.setActive(false));
100 userIndexer.indexAll();
102 MetricDto lines = db.measures().insertMetric(m -> m.setKey(LINES_KEY));
103 MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY));
104 MetricDto coverage = db.measures().insertMetric(m -> m.setKey(COVERAGE_KEY));
105 MetricDto nclocDistrib = db.measures().insertMetric(m -> m.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY));
107 ComponentDto project1 = db.components().insertPublicProject();
108 ComponentDto project1Branch = db.components().insertProjectBranch(project1);
109 db.measures().insertLiveMeasure(project1, lines, m -> m.setValue(200d));
110 db.measures().insertLiveMeasure(project1, ncloc, m -> m.setValue(100d));
111 db.measures().insertLiveMeasure(project1, coverage, m -> m.setValue(80d));
112 db.measures().insertLiveMeasure(project1, nclocDistrib, m -> m.setValue(null).setData("java=200;js=50"));
114 ComponentDto project2 = db.components().insertPublicProject();
115 db.measures().insertLiveMeasure(project2, lines, m -> m.setValue(300d));
116 db.measures().insertLiveMeasure(project2, ncloc, m -> m.setValue(200d));
117 db.measures().insertLiveMeasure(project2, coverage, m -> m.setValue(80d));
118 db.measures().insertLiveMeasure(project2, nclocDistrib, m -> m.setValue(null).setData("java=300;kotlin=2500"));
119 projectMeasuresIndexer.indexAll();
122 db.almSettings().insertAzureAlmSetting();
123 db.almSettings().insertAzureAlmSetting(a -> a.setUrl("https://dev.azure.com"));
124 db.almSettings().insertBitbucketAlmSetting();
125 db.almSettings().insertBitbucketCloudAlmSetting();
126 db.almSettings().insertGitHubAlmSetting();
127 db.almSettings().insertGitHubAlmSetting(a -> a.setUrl("https://api.github.com"));
128 db.almSettings().insertGitlabAlmSetting();
129 db.almSettings().insertGitlabAlmSetting(a -> a.setUrl("https://gitlab.com/api/v4"));
131 TelemetryData data = communityUnderTest.load();
132 assertThat(data.getServerId()).isEqualTo(serverId);
133 assertThat(data.getVersion()).isEqualTo(version);
134 assertThat(data.getEdition()).contains(DEVELOPER);
135 assertDatabaseMetadata(data.getDatabase());
136 assertThat(data.getPlugins()).containsOnly(
137 entry("java", "4.12.0.11033"), entry("scmgit", "1.2"), entry("other", "undefined"));
138 assertThat(data.getUserCount()).isEqualTo(userCount);
139 assertThat(data.getProjectCount()).isEqualTo(2L);
140 assertThat(data.getNcloc()).isEqualTo(300L);
141 assertThat(data.getProjectCountByLanguage()).containsOnly(
142 entry("java", 2L), entry("kotlin", 1L), entry("js", 1L));
143 assertThat(data.getNclocByLanguage()).containsOnly(
144 entry("java", 500L), entry("kotlin", 2500L), entry("js", 50L));
145 assertThat(data.isInDocker()).isFalse();
146 assertThat(data.getAlmIntegrationCountByAlm())
147 .containsEntry("azure_devops_server", 1L)
148 .containsEntry("azure_devops_cloud", 1L)
149 .containsEntry("bitbucket_server", 1L)
150 .containsEntry("bitbucket_cloud", 1L)
151 .containsEntry("gitlab_server", 1L)
152 .containsEntry("gitlab_cloud", 1L)
153 .containsEntry("github_cloud", 1L)
154 .containsEntry("github_server", 1L);
157 private void assertDatabaseMetadata(TelemetryData.Database database) {
158 try (DbSession dbSession = db.getDbClient().openSession(false)) {
159 DatabaseMetaData metadata = dbSession.getConnection().getMetaData();
160 assertThat(database.getName()).isEqualTo("H2");
161 assertThat(database.getVersion()).isEqualTo(metadata.getDatabaseProductVersion());
162 } catch (SQLException e) {
163 throw new RuntimeException(e);
168 public void take_largest_branches() {
169 server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4");
170 MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY));
171 ComponentDto project = db.components().insertPublicProject();
172 ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH));
173 ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST));
174 db.measures().insertLiveMeasure(project, ncloc, m -> m.setValue(10d));
175 db.measures().insertLiveMeasure(branch1, ncloc, m -> m.setValue(20d));
176 db.measures().insertLiveMeasure(pr, ncloc, m -> m.setValue(30d));
177 projectMeasuresIndexer.indexAll();
179 TelemetryData data = communityUnderTest.load();
181 assertThat(data.getNcloc()).isEqualTo(20L);
185 public void data_contains_no_license_type_on_community_edition() {
186 TelemetryData data = communityUnderTest.load();
188 assertThat(data.getLicenseType()).isEmpty();
192 public void data_contains_no_license_type_on_commercial_edition_if_no_license() {
193 when(licenseReader.read()).thenReturn(Optional.empty());
195 TelemetryData data = commercialUnderTest.load();
197 assertThat(data.getLicenseType()).isEmpty();
201 public void data_has_license_type_on_commercial_edition_if_no_license() {
202 String licenseType = randomAlphabetic(12);
203 LicenseReader.License license = mock(LicenseReader.License.class);
204 when(license.getType()).thenReturn(licenseType);
205 when(licenseReader.read()).thenReturn(Optional.of(license));
207 TelemetryData data = commercialUnderTest.load();
209 assertThat(data.getLicenseType()).contains(licenseType);
213 public void send_server_id_and_version() {
214 String id = randomAlphanumeric(40);
215 String version = randomAlphanumeric(10);
217 server.setVersion(version);
219 TelemetryData data = communityUnderTest.load();
220 assertThat(data.getServerId()).isEqualTo(id);
221 assertThat(data.getVersion()).isEqualTo(version);
223 data = commercialUnderTest.load();
224 assertThat(data.getServerId()).isEqualTo(id);
225 assertThat(data.getVersion()).isEqualTo(version);
229 public void send_server_installation_date_and_installation_version() {
230 String installationVersion = "7.9.BEST.LTS.EVER";
231 Long installationDate = 1546300800000L; // 2019/01/01
232 internalProperties.write(InternalProperties.INSTALLATION_DATE, String.valueOf(installationDate));
233 internalProperties.write(InternalProperties.INSTALLATION_VERSION, installationVersion);
235 TelemetryData data = communityUnderTest.load();
237 assertThat(data.getInstallationDate()).isEqualTo(installationDate);
238 assertThat(data.getInstallationVersion()).isEqualTo(installationVersion);
242 public void do_not_send_server_installation_details_if_missing_property() {
243 TelemetryData data = communityUnderTest.load();
244 assertThat(data.getInstallationDate()).isNull();
245 assertThat(data.getInstallationVersion()).isNull();
247 data = commercialUnderTest.load();
248 assertThat(data.getInstallationDate()).isNull();
249 assertThat(data.getInstallationVersion()).isNull();
253 public void send_unanalyzed_languages_flags_when_edition_is_community() {
254 when(editionProvider.get()).thenReturn(Optional.of(COMMUNITY));
255 MetricDto unanalyzedC = db.measures().insertMetric(m -> m.setKey(UNANALYZED_C_KEY));
256 MetricDto unanalyzedCpp = db.measures().insertMetric(m -> m.setKey(UNANALYZED_CPP_KEY));
257 ComponentDto project1 = db.components().insertPublicProject();
258 ComponentDto project2 = db.components().insertPublicProject();
259 db.measures().insertLiveMeasure(project1, unanalyzedC);
260 db.measures().insertLiveMeasure(project2, unanalyzedC);
261 db.measures().insertLiveMeasure(project2, unanalyzedCpp);
263 TelemetryData data = communityUnderTest.load();
265 assertThat(data.hasUnanalyzedC().get()).isTrue();
266 assertThat(data.hasUnanalyzedCpp().get()).isTrue();
270 public void do_not_send_unanalyzed_languages_flags_when_edition_is_not_community() {
271 when(editionProvider.get()).thenReturn(Optional.of(DEVELOPER));
272 MetricDto unanalyzedC = db.measures().insertMetric(m -> m.setKey(UNANALYZED_C_KEY));
273 MetricDto unanalyzedCpp = db.measures().insertMetric(m -> m.setKey(UNANALYZED_CPP_KEY));
274 ComponentDto project1 = db.components().insertPublicProject();
275 ComponentDto project2 = db.components().insertPublicProject();
276 db.measures().insertLiveMeasure(project1, unanalyzedC);
277 db.measures().insertLiveMeasure(project2, unanalyzedCpp);
279 TelemetryData data = communityUnderTest.load();
281 assertThat(data.hasUnanalyzedC()).isEmpty();
282 assertThat(data.hasUnanalyzedCpp()).isEmpty();
286 public void unanalyzed_languages_flags_are_set_to_false_when_no_unanalyzed_languages_and_edition_is_community() {
287 when(editionProvider.get()).thenReturn(Optional.of(COMMUNITY));
289 TelemetryData data = communityUnderTest.load();
291 assertThat(data.hasUnanalyzedC().get()).isFalse();
292 assertThat(data.hasUnanalyzedCpp().get()).isFalse();
295 private PluginInfo newPlugin(String key, String version) {
296 return new PluginInfo(key)
297 .setVersion(Version.create(version));