3 * Copyright (C) 2009-2020 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.organization.DefaultOrganizationProviderImpl;
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.db.component.BranchType.BRANCH;
63 import static org.sonar.db.component.BranchType.PULL_REQUEST;
64 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_CPP_KEY;
65 import static org.sonar.server.metric.UnanalyzedLanguageMetrics.UNANALYZED_C_KEY;
67 public class TelemetryDataLoaderImplTest {
69 public DbTester db = DbTester.create();
71 public EsTester es = EsTester.create();
73 private final FakeServer server = new FakeServer();
74 private final PluginRepository pluginRepository = mock(PluginRepository.class);
75 private final TestSystem2 system2 = new TestSystem2().setNow(System.currentTimeMillis());
76 private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
77 private final DockerSupport dockerSupport = mock(DockerSupport.class);
78 private final InternalProperties internalProperties = spy(new MapInternalProperties());
79 private final ProjectMeasuresIndexer projectMeasuresIndexer = new ProjectMeasuresIndexer(db.getDbClient(), es.client());
80 private final UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client());
81 private final LicenseReader licenseReader = mock(LicenseReader.class);
83 private final TelemetryDataLoader communityUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2),
84 new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, new DefaultOrganizationProviderImpl(db.getDbClient()), internalProperties, dockerSupport, null);
85 private final TelemetryDataLoader commercialUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, new UserIndex(es.client(), system2),
86 new ProjectMeasuresIndex(es.client(), null, system2), editionProvider, new DefaultOrganizationProviderImpl(db.getDbClient()), internalProperties, dockerSupport, licenseReader);
89 public void send_telemetry_data() {
90 String serverId = "AU-TpxcB-iU5OvuD2FL7";
91 String version = "7.5.4";
92 server.setId(serverId);
93 server.setVersion(version);
94 List<PluginInfo> plugins = asList(newPlugin("java", "4.12.0.11033"), newPlugin("scmgit", "1.2"), new PluginInfo("other"));
95 when(pluginRepository.getPluginInfos()).thenReturn(plugins);
96 when(editionProvider.get()).thenReturn(Optional.of(DEVELOPER));
99 IntStream.range(0, userCount).forEach(i -> db.users().insertUser());
100 db.users().insertUser(u -> u.setActive(false));
101 userIndexer.indexAll();
103 MetricDto lines = db.measures().insertMetric(m -> m.setKey(LINES_KEY));
104 MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY));
105 MetricDto coverage = db.measures().insertMetric(m -> m.setKey(COVERAGE_KEY));
106 MetricDto nclocDistrib = db.measures().insertMetric(m -> m.setKey(NCLOC_LANGUAGE_DISTRIBUTION_KEY));
108 ComponentDto project1 = db.components().insertPublicProject(db.getDefaultOrganization());
109 ComponentDto project1Branch = db.components().insertProjectBranch(project1);
110 db.measures().insertLiveMeasure(project1, lines, m -> m.setValue(200d));
111 db.measures().insertLiveMeasure(project1, ncloc, m -> m.setValue(100d));
112 db.measures().insertLiveMeasure(project1, coverage, m -> m.setValue(80d));
113 db.measures().insertLiveMeasure(project1, nclocDistrib, m -> m.setValue(null).setData("java=200;js=50"));
115 ComponentDto project2 = db.components().insertPublicProject(db.getDefaultOrganization());
116 db.measures().insertLiveMeasure(project2, lines, m -> m.setValue(300d));
117 db.measures().insertLiveMeasure(project2, ncloc, m -> m.setValue(200d));
118 db.measures().insertLiveMeasure(project2, coverage, m -> m.setValue(80d));
119 db.measures().insertLiveMeasure(project2, nclocDistrib, m -> m.setValue(null).setData("java=300;kotlin=2500"));
120 projectMeasuresIndexer.indexAll();
122 TelemetryData data = communityUnderTest.load();
123 assertThat(data.getServerId()).isEqualTo(serverId);
124 assertThat(data.getVersion()).isEqualTo(version);
125 assertThat(data.getEdition()).contains(DEVELOPER);
126 assertDatabaseMetadata(data.getDatabase());
127 assertThat(data.getPlugins()).containsOnly(
128 entry("java", "4.12.0.11033"), entry("scmgit", "1.2"), entry("other", "undefined"));
129 assertThat(data.getUserCount()).isEqualTo(userCount);
130 assertThat(data.getProjectCount()).isEqualTo(2L);
131 assertThat(data.getNcloc()).isEqualTo(300L);
132 assertThat(data.getProjectCountByLanguage()).containsOnly(
133 entry("java", 2L), entry("kotlin", 1L), entry("js", 1L));
134 assertThat(data.getNclocByLanguage()).containsOnly(
135 entry("java", 500L), entry("kotlin", 2500L), entry("js", 50L));
136 assertThat(data.isInDocker()).isFalse();
139 private void assertDatabaseMetadata(TelemetryData.Database database) {
140 try (DbSession dbSession = db.getDbClient().openSession(false)) {
141 DatabaseMetaData metadata = dbSession.getConnection().getMetaData();
142 assertThat(database.getName()).isEqualTo("H2");
143 assertThat(database.getVersion()).isEqualTo(metadata.getDatabaseProductVersion());
144 } catch (SQLException e) {
145 throw new RuntimeException(e);
150 public void take_largest_branches() {
151 server.setId("AU-TpxcB-iU5OvuD2FL7").setVersion("7.5.4");
152 MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(NCLOC_KEY));
153 ComponentDto project = db.components().insertPublicProject(db.getDefaultOrganization());
154 ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH));
155 ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST));
156 db.measures().insertLiveMeasure(project, ncloc, m -> m.setValue(10d));
157 db.measures().insertLiveMeasure(branch1, ncloc, m -> m.setValue(20d));
158 db.measures().insertLiveMeasure(pr, ncloc, m -> m.setValue(30d));
159 projectMeasuresIndexer.indexAll();
161 TelemetryData data = communityUnderTest.load();
163 assertThat(data.getNcloc()).isEqualTo(20L);
167 public void data_contains_no_license_type_on_community_edition() {
168 TelemetryData data = communityUnderTest.load();
170 assertThat(data.getLicenseType()).isEmpty();
174 public void data_contains_no_license_type_on_commercial_edition_if_no_license() {
175 when(licenseReader.read()).thenReturn(Optional.empty());
177 TelemetryData data = commercialUnderTest.load();
179 assertThat(data.getLicenseType()).isEmpty();
183 public void data_has_license_type_on_commercial_edition_if_no_license() {
184 String licenseType = randomAlphabetic(12);
185 LicenseReader.License license = mock(LicenseReader.License.class);
186 when(license.getType()).thenReturn(licenseType);
187 when(licenseReader.read()).thenReturn(Optional.of(license));
189 TelemetryData data = commercialUnderTest.load();
191 assertThat(data.getLicenseType()).contains(licenseType);
195 public void send_server_id_and_version() {
196 String id = randomAlphanumeric(40);
197 String version = randomAlphanumeric(10);
199 server.setVersion(version);
201 TelemetryData data = communityUnderTest.load();
202 assertThat(data.getServerId()).isEqualTo(id);
203 assertThat(data.getVersion()).isEqualTo(version);
205 data = commercialUnderTest.load();
206 assertThat(data.getServerId()).isEqualTo(id);
207 assertThat(data.getVersion()).isEqualTo(version);
211 public void send_server_installation_date_and_installation_version() {
212 String installationVersion = "7.9.BEST.LTS.EVER";
213 Long installationDate = 1546300800000L; // 2019/01/01
214 internalProperties.write(InternalProperties.INSTALLATION_DATE, String.valueOf(installationDate));
215 internalProperties.write(InternalProperties.INSTALLATION_VERSION, installationVersion);
217 TelemetryData data = communityUnderTest.load();
219 assertThat(data.getInstallationDate()).isEqualTo(installationDate);
220 assertThat(data.getInstallationVersion()).isEqualTo(installationVersion);
224 public void do_not_send_server_installation_details_if_missing_property() {
225 TelemetryData data = communityUnderTest.load();
226 assertThat(data.getInstallationDate()).isNull();
227 assertThat(data.getInstallationVersion()).isNull();
229 data = commercialUnderTest.load();
230 assertThat(data.getInstallationDate()).isNull();
231 assertThat(data.getInstallationVersion()).isNull();
235 public void send_unanalyzed_languages_flags_when_edition_is_community() {
236 when(editionProvider.get()).thenReturn(Optional.of(COMMUNITY));
237 MetricDto unanalyzedC = db.measures().insertMetric(m -> m.setKey(UNANALYZED_C_KEY));
238 MetricDto unanalyzedCpp = db.measures().insertMetric(m -> m.setKey(UNANALYZED_CPP_KEY));
239 ComponentDto project1 = db.components().insertPublicProject();
240 ComponentDto project2 = db.components().insertPublicProject();
241 db.measures().insertLiveMeasure(project1, unanalyzedC);
242 db.measures().insertLiveMeasure(project2, unanalyzedC);
243 db.measures().insertLiveMeasure(project2, unanalyzedCpp);
245 TelemetryData data = communityUnderTest.load();
247 assertThat(data.hasUnanalyzedC().get()).isTrue();
248 assertThat(data.hasUnanalyzedCpp().get()).isTrue();
252 public void do_not_send_unanalyzed_languages_flags_when_edition_is_not_community() {
253 when(editionProvider.get()).thenReturn(Optional.of(DEVELOPER));
254 MetricDto unanalyzedC = db.measures().insertMetric(m -> m.setKey(UNANALYZED_C_KEY));
255 MetricDto unanalyzedCpp = db.measures().insertMetric(m -> m.setKey(UNANALYZED_CPP_KEY));
256 ComponentDto project1 = db.components().insertPublicProject();
257 ComponentDto project2 = db.components().insertPublicProject();
258 db.measures().insertLiveMeasure(project1, unanalyzedC);
259 db.measures().insertLiveMeasure(project2, unanalyzedCpp);
261 TelemetryData data = communityUnderTest.load();
263 assertThat(data.hasUnanalyzedC()).isEmpty();
264 assertThat(data.hasUnanalyzedCpp()).isEmpty();
268 public void unanalyzed_languages_flags_are_set_to_false_when_no_unanalyzed_languages_and_edition_is_community() {
269 when(editionProvider.get()).thenReturn(Optional.of(COMMUNITY));
271 TelemetryData data = communityUnderTest.load();
273 assertThat(data.hasUnanalyzedC().get()).isFalse();
274 assertThat(data.hasUnanalyzedCpp().get()).isFalse();
277 private PluginInfo newPlugin(String key, String version) {
278 return new PluginInfo(key)
279 .setVersion(Version.create(version));