3 * Copyright (C) 2009-2022 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 com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.io.StringWriter;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Locale;
31 import java.util.Random;
32 import java.util.stream.Collectors;
33 import java.util.stream.IntStream;
34 import org.apache.commons.codec.digest.DigestUtils;
35 import org.jetbrains.annotations.NotNull;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.sonar.api.utils.text.JsonWriter;
39 import org.sonar.core.platform.EditionProvider;
40 import org.sonar.core.util.stream.MoreCollectors;
41 import org.sonar.db.user.UserTelemetryDto;
43 import static java.util.stream.Collectors.joining;
44 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.sonar.test.JsonAssert.assertJson;
48 @RunWith(DataProviderRunner.class)
49 public class TelemetryDataJsonWriterTest {
51 private static final TelemetryData.Builder SOME_TELEMETRY_DATA = TelemetryData.builder()
54 .setPlugins(Collections.emptyMap())
55 .setDatabase(new TelemetryData.Database("H2", "11"));
57 private final Random random = new Random();
59 private final TelemetryDataJsonWriter underTest = new TelemetryDataJsonWriter();
62 public void write_server_id_and_version() {
63 TelemetryData data = SOME_TELEMETRY_DATA.build();
65 String json = writeTelemetryData(data);
67 assertJson(json).isSimilarTo("{" +
68 " \"id\": \"" + data.getServerId() + "\"," +
69 " \"version\": \"" + data.getVersion() + "\"" +
74 public void does_not_write_edition_if_null() {
75 TelemetryData data = SOME_TELEMETRY_DATA.build();
77 String json = writeTelemetryData(data);
79 assertThat(json).doesNotContain("edition");
83 @UseDataProvider("allEditions")
84 public void writes_edition_if_non_null(EditionProvider.Edition edition) {
85 TelemetryData data = SOME_TELEMETRY_DATA
89 String json = writeTelemetryData(data);
91 assertJson(json).isSimilarTo("{" +
92 " \"edition\": \"" + edition.name().toLowerCase(Locale.ENGLISH) + "\"" +
97 public void does_not_write_license_type_if_null() {
98 TelemetryData data = SOME_TELEMETRY_DATA.build();
100 String json = writeTelemetryData(data);
102 assertThat(json).doesNotContain("licenseType");
106 public void writes_licenseType_if_non_null() {
107 String expected = randomAlphabetic(12);
108 TelemetryData data = SOME_TELEMETRY_DATA
109 .setLicenseType(expected)
112 String json = writeTelemetryData(data);
114 assertJson(json).isSimilarTo("{" +
115 " \"licenseType\": \"" + expected + "\"" +
120 public void writes_database() {
121 String name = randomAlphabetic(12);
122 String version = randomAlphabetic(10);
123 TelemetryData data = SOME_TELEMETRY_DATA
124 .setDatabase(new TelemetryData.Database(name, version))
127 String json = writeTelemetryData(data);
129 assertJson(json).isSimilarTo("{" +
131 " \"name\": \"" + name + "\"," +
132 " \"version\": \"" + version + "\"" +
138 public void writes_no_plugins() {
139 TelemetryData data = SOME_TELEMETRY_DATA
140 .setPlugins(Collections.emptyMap())
143 String json = writeTelemetryData(data);
145 assertJson(json).isSimilarTo("{" +
151 public void writes_all_plugins() {
152 Map<String, String> plugins = IntStream.range(0, 1 + random.nextInt(10))
154 .collect(MoreCollectors.uniqueIndex(i -> "P" + i, i -> "V" + i));
155 TelemetryData data = SOME_TELEMETRY_DATA
159 String json = writeTelemetryData(data);
161 assertJson(json).isSimilarTo("{" +
164 plugins.entrySet().stream().map(e -> "{\"name\":\"" + e.getKey() + "\",\"version\":\"" + e.getValue() + "\"}").collect(joining(",")) +
170 public void does_not_write_installation_date_if_null() {
171 TelemetryData data = SOME_TELEMETRY_DATA
172 .setInstallationDate(null)
175 String json = writeTelemetryData(data);
177 assertThat(json).doesNotContain("installationDate");
181 public void write_installation_date_in_utc_format() {
182 TelemetryData data = SOME_TELEMETRY_DATA
183 .setInstallationDate(1_000L)
186 String json = writeTelemetryData(data);
188 assertJson(json).isSimilarTo("{" +
189 " \"installationDate\":\"1970-01-01T00:00:01+0000\"," +
194 public void does_not_write_installation_version_if_null() {
195 TelemetryData data = SOME_TELEMETRY_DATA
196 .setInstallationVersion(null)
199 String json = writeTelemetryData(data);
201 assertThat(json).doesNotContain("installationVersion");
205 public void write_installation_version() {
206 String installationVersion = randomAlphabetic(5);
207 TelemetryData data = SOME_TELEMETRY_DATA
208 .setInstallationVersion(installationVersion)
211 String json = writeTelemetryData(data);
213 assertJson(json).isSimilarTo("{" +
214 " \"installationVersion\":\"" + installationVersion + "\"" +
219 public void write_docker_flag() {
220 boolean inDocker = random.nextBoolean();
221 TelemetryData data = SOME_TELEMETRY_DATA
222 .setInDocker(inDocker)
225 String json = writeTelemetryData(data);
227 assertJson(json).isSimilarTo("{" +
228 " \"docker\":" + inDocker +
233 public void writes_has_unanalyzed_languages() {
234 TelemetryData data = SOME_TELEMETRY_DATA
235 .setHasUnanalyzedC(true)
236 .setHasUnanalyzedCpp(false)
239 String json = writeTelemetryData(data);
241 assertJson(json).isSimilarTo("{" +
242 " \"hasUnanalyzedC\":true," +
243 " \"hasUnanalyzedCpp\":false," +
248 public void writes_security_custom_config() {
249 TelemetryData data = SOME_TELEMETRY_DATA
250 .setCustomSecurityConfigs(Arrays.asList("php", "java"))
253 String json = writeTelemetryData(data);
255 assertJson(json).isSimilarTo("{" +
256 " \"customSecurityConfig\": [\"php\", \"java\"]" +
261 public void writes_all_users_with_anonymous_md5_uuids() {
262 TelemetryData data = SOME_TELEMETRY_DATA
263 .setUsers(getUsers())
266 String json = writeTelemetryData(data);
268 assertJson(json).isSimilarTo("{" +
271 " \"userUuid\":\"" + DigestUtils.sha3_224Hex("uuid-0") + "\"," +
272 " \"lastActivity\":\"1970-01-01T00:00:00+0000\"," +
273 " \"identityProvider\":\"gitlab\"," +
274 " \"lastSonarlintActivity\":\"1970-01-01T00:00:00+0000\"," +
275 " \"status\":\"active\"" +
278 " \"userUuid\":\"" + DigestUtils.sha3_224Hex("uuid-1") + "\"," +
279 " \"lastActivity\":\"1970-01-01T00:00:00+0000\"," +
280 " \"identityProvider\":\"gitlab\"," +
281 " \"lastSonarlintActivity\":\"1970-01-01T00:00:00+0000\"," +
282 " \"status\":\"inactive\"" +
285 " \"userUuid\":\"" + DigestUtils.sha3_224Hex("uuid-2") + "\"," +
286 " \"lastActivity\":\"1970-01-01T00:00:00+0000\"," +
287 " \"identityProvider\":\"gitlab\"," +
288 " \"lastSonarlintActivity\":\"1970-01-01T00:00:00+0000\"," +
289 " \"status\":\"active\"" +
296 public void writes_all_projects() {
297 TelemetryData data = SOME_TELEMETRY_DATA
298 .setProjects(getProjects())
301 String json = writeTelemetryData(data);
303 assertJson(json).isSimilarTo("{" +
306 " \"projectUuid\": \"uuid-0\"," +
307 " \"language\": \"lang-0\"," +
308 " \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," +
312 " \"projectUuid\": \"uuid-1\"," +
313 " \"language\": \"lang-1\"," +
314 " \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," +
318 " \"projectUuid\": \"uuid-2\"," +
319 " \"language\": \"lang-2\"," +
320 " \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," +
328 public void writes_all_projects_stats() {
329 TelemetryData data = SOME_TELEMETRY_DATA
330 .setProjectStatistics(getProjectStats())
333 String json = writeTelemetryData(data);
335 assertJson(json).isSimilarTo("{" +
336 " \"projects-general-stats\": [" +
338 " \"projectUuid\": \"uuid-0\"," +
339 " \"branchCount\": 2," +
340 " \"pullRequestCount\": 2," +
341 " \"scm\": \"scm-0\"," +
342 " \"ci\": \"ci-0\"," +
343 " \"devopsPlatform\": \"devops-0\"" +
346 " \"projectUuid\": \"uuid-1\"," +
347 " \"branchCount\": 4," +
348 " \"pullRequestCount\": 4," +
349 " \"scm\": \"scm-1\"," +
350 " \"ci\": \"ci-1\"," +
351 " \"devopsPlatform\": \"devops-1\"" +
354 " \"projectUuid\": \"uuid-2\"," +
355 " \"branchCount\": 6," +
356 " \"pullRequestCount\": 6," +
357 " \"scm\": \"scm-2\"," +
358 " \"ci\": \"ci-2\"," +
359 " \"devopsPlatform\": \"devops-2\"" +
366 private static List<UserTelemetryDto> getUsers() {
367 return IntStream.range(0, 3)
369 i -> new UserTelemetryDto().setUuid("uuid-" + i).setActive(i % 2 == 0).setLastConnectionDate(1L).setLastSonarlintConnectionDate(2L).setExternalIdentityProvider("gitlab"))
370 .collect(Collectors.toList());
373 private static List<TelemetryData.Project> getProjects() {
374 return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.Project("uuid-" + i, 1L, "lang-" + i, (i + 1L) * 2L)).collect(Collectors.toList());
377 private List<TelemetryData.ProjectStatistics> getProjectStats() {
378 return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.ProjectStatistics("uuid-" + i, (i + 1L) * 2L, (i + 1L) * 2L, "scm-" + i, "ci-" + i, "devops-" + i))
379 .collect(Collectors.toList());
383 public static Object[][] allEditions() {
384 return Arrays.stream(EditionProvider.Edition.values())
385 .map(t -> new Object[] {t})
386 .toArray(Object[][]::new);
389 private String writeTelemetryData(TelemetryData data) {
390 StringWriter jsonString = new StringWriter();
391 try (JsonWriter json = JsonWriter.of(jsonString)) {
392 underTest.writeTelemetryData(json, data);
394 return jsonString.toString();