@RunWith(DataProviderRunner.class)
public class TelemetryDataJsonWriterTest {
- private static final TelemetryData.Builder SOME_TELEMETRY_DATA = TelemetryData.builder()
- .setServerId("foo")
- .setVersion("bar")
- .setPlugins(Collections.emptyMap())
- .setDatabase(new TelemetryData.Database("H2", "11"));
-
private final Random random = new Random();
private final TelemetryDataJsonWriter underTest = new TelemetryDataJsonWriter();
@Test
public void write_server_id_and_version() {
- TelemetryData data = SOME_TELEMETRY_DATA.build();
+ TelemetryData data = telemetryBuilder().build();
String json = writeTelemetryData(data);
@Test
public void does_not_write_edition_if_null() {
- TelemetryData data = SOME_TELEMETRY_DATA.build();
+ TelemetryData data = telemetryBuilder().build();
String json = writeTelemetryData(data);
@Test
@UseDataProvider("allEditions")
public void writes_edition_if_non_null(EditionProvider.Edition edition) {
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setEdition(edition)
.build();
@Test
public void does_not_write_license_type_if_null() {
- TelemetryData data = SOME_TELEMETRY_DATA.build();
+ TelemetryData data = telemetryBuilder().build();
String json = writeTelemetryData(data);
@Test
public void writes_licenseType_if_non_null() {
String expected = randomAlphabetic(12);
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setLicenseType(expected)
.build();
public void writes_database() {
String name = randomAlphabetic(12);
String version = randomAlphabetic(10);
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setDatabase(new TelemetryData.Database(name, version))
.build();
@Test
public void writes_no_plugins() {
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setPlugins(Collections.emptyMap())
.build();
Map<String, String> plugins = IntStream.range(0, 1 + random.nextInt(10))
.boxed()
.collect(MoreCollectors.uniqueIndex(i -> "P" + i, i -> "V" + i));
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setPlugins(plugins)
.build();
@Test
public void does_not_write_installation_date_if_null() {
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setInstallationDate(null)
.build();
@Test
public void write_installation_date_in_utc_format() {
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setInstallationDate(1_000L)
.build();
@Test
public void does_not_write_installation_version_if_null() {
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setInstallationVersion(null)
.build();
@Test
public void write_installation_version() {
String installationVersion = randomAlphabetic(5);
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setInstallationVersion(installationVersion)
.build();
@Test
public void write_docker_flag() {
boolean inDocker = random.nextBoolean();
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setInDocker(inDocker)
.build();
@Test
public void writes_security_custom_config() {
- TelemetryData data = SOME_TELEMETRY_DATA
+ TelemetryData data = telemetryBuilder()
.setCustomSecurityConfigs(Arrays.asList("php", "java"))
.build();
@Test
public void writes_all_users_with_anonymous_md5_uuids() {
- TelemetryData data = SOME_TELEMETRY_DATA
- .setUsers(getUsers())
+ TelemetryData data = telemetryBuilder()
+ .setUsers(attachUsers())
.build();
String json = writeTelemetryData(data);
@Test
public void writes_all_projects() {
- TelemetryData data = SOME_TELEMETRY_DATA
- .setProjects(getProjects())
+ TelemetryData data = telemetryBuilder()
+ .setProjects(attachProjects())
.build();
String json = writeTelemetryData(data);
" \"projects\": [" +
" {" +
" \"projectUuid\": \"uuid-0\"," +
- " \"language\": \"lang-0\"," +
" \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," +
+ " \"language\": \"lang-0\"," +
" \"loc\": 2" +
" }," +
" {" +
" \"projectUuid\": \"uuid-1\"," +
- " \"language\": \"lang-1\"," +
" \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," +
+ " \"language\": \"lang-1\"," +
" \"loc\": 4" +
" }," +
" {" +
" \"projectUuid\": \"uuid-2\"," +
- " \"language\": \"lang-2\"," +
" \"lastAnalysis\":\"1970-01-01T00:00:00+0000\"," +
+ " \"language\": \"lang-2\"," +
" \"loc\": 6" +
" }" +
" ]" +
@Test
public void writes_all_projects_stats_with_analyzed_languages() {
- TelemetryData data = SOME_TELEMETRY_DATA
- .setProjectStatistics(getProjectStats(true))
+ TelemetryData data = telemetryBuilder()
+ .setProjectStatistics(attachProjectStats(true))
.build();
String json = writeTelemetryData(data);
@Test
public void writes_all_projects_stats_with_unanalyzed_languages() {
- TelemetryData data = SOME_TELEMETRY_DATA
- .setProjectStatistics(getProjectStats(false))
+ TelemetryData data = telemetryBuilder()
+ .setProjectStatistics(attachProjectStats(false))
.build();
String json = writeTelemetryData(data);
assertThat(json).doesNotContain("hasUnanalyzedC", "hasUnanalyzedCpp");
}
+ private static TelemetryData.Builder telemetryBuilder() {
+ return TelemetryData.builder()
+ .setServerId("foo")
+ .setVersion("bar")
+ .setPlugins(Collections.emptyMap())
+ .setDatabase(new TelemetryData.Database("H2", "11"));
+ }
+
@NotNull
- private static List<UserTelemetryDto> getUsers() {
+ private static List<UserTelemetryDto> attachUsers() {
return IntStream.range(0, 3)
.mapToObj(
i -> new UserTelemetryDto().setUuid("uuid-" + i).setActive(i % 2 == 0).setLastConnectionDate(1L).setLastSonarlintConnectionDate(2L).setExternalIdentityProvider("gitlab"))
.collect(Collectors.toList());
}
- private static List<TelemetryData.Project> getProjects() {
+ private static List<TelemetryData.Project> attachProjects() {
return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.Project("uuid-" + i, 1L, "lang-" + i, (i + 1L) * 2L)).collect(Collectors.toList());
}
- private List<TelemetryData.ProjectStatistics> getProjectStats(boolean hasUnanalyzedLanguages) {
+ private List<TelemetryData.ProjectStatistics> attachProjectStats(boolean hasUnanalyzedLanguages) {
return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.ProjectStatistics("uuid-" + i, (i + 1L) * 2L, (i + 1L) * 2L, hasUnanalyzedLanguages ? i % 2 == 0 : null, hasUnanalyzedLanguages ? i % 2 != 0 : null, "scm-" + i, "ci-" + i, "devops-" + i))
.collect(Collectors.toList());
}
try (DbSession dbSession = dbClient.openSession(false)) {
data.setDatabase(loadDatabaseMetadata(dbSession));
- Map<String, String> scmByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDSCM);
- Map<String, String> ciByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDCI);
- Map<String, ProjectAlmKeyAndProject> almAndUrlByProject = getAlmAndUrlByProject(dbSession);
- List<String> projectUuids = dbClient.projectDao().selectAllProjectUuids(dbSession);
-
- Map<String, PrBranchAnalyzedLanguageCountByProjectDto> prAndBranchCountByProjects = dbClient.branchDao().countPrBranchAnalyzedLanguageByProjectUuid(dbSession)
- .stream().collect(Collectors.toMap(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, Function.identity()));
-
- boolean isCommunityEdition = editionProvider.get().filter(edition -> edition.equals(COMMUNITY)).isPresent();
- List<TelemetryData.ProjectStatistics> projectStatistics = new ArrayList<>();
- for (String projectUuid : projectUuids) {
- Optional<PrBranchAnalyzedLanguageCountByProjectDto> counts = ofNullable(prAndBranchCountByProjects.get(projectUuid));
-
- Long branchCount = counts.map(PrBranchAnalyzedLanguageCountByProjectDto::getBranch).orElse(0L);
- Long pullRequestCount = counts.map(PrBranchAnalyzedLanguageCountByProjectDto::getPullRequest).orElse(0L);
-
- Boolean hasUnanalyzedCMeasures = null;
- Boolean hasUnanalyzedCppMeasures = null;
- if (isCommunityEdition) {
- hasUnanalyzedCMeasures = counts.map(PrBranchAnalyzedLanguageCountByProjectDto::getUnanalyzedCCount).orElse(0L) > 0;
- hasUnanalyzedCppMeasures = counts.map(PrBranchAnalyzedLanguageCountByProjectDto::getUnanalyzedCppCount).orElse(0L) > 0;
- }
-
- String scm = Optional.ofNullable(scmByProject.get(projectUuid)).orElse(UNDETECTED);
- String ci = Optional.ofNullable(ciByProject.get(projectUuid)).orElse(UNDETECTED);
- String devopsPlatform = null;
- if (almAndUrlByProject.containsKey(projectUuid)) {
- ProjectAlmKeyAndProject projectAlmKeyAndProject = almAndUrlByProject.get(projectUuid);
- devopsPlatform = getAlmName(projectAlmKeyAndProject.getAlmId(), projectAlmKeyAndProject.getUrl());
- }
- devopsPlatform = Optional.ofNullable(devopsPlatform).orElse(UNDETECTED);
-
- projectStatistics.add(
- new TelemetryData.ProjectStatistics(projectUuid, branchCount, pullRequestCount, hasUnanalyzedCMeasures, hasUnanalyzedCppMeasures, scm, ci, devopsPlatform));
- }
- data.setProjectStatistics(projectStatistics);
-
- data.setUsers(dbClient.userDao().selectUsersForTelemetry(dbSession));
-
- List<ProjectMeasureDto> measures = dbClient.measureDao().selectLastMeasureForAllProjects(dbSession, NCLOC_LANGUAGE_DISTRIBUTION_KEY);
- List<TelemetryData.Project> projects = new ArrayList<>();
- for (ProjectMeasureDto measure : measures) {
- for (String measureTextValue : measure.getTextValue().split(";")) {
- String[] languageAndLoc = measureTextValue.split("=");
- String language = languageAndLoc[0];
- Long loc = Long.parseLong(languageAndLoc[1]);
- projects.add(new TelemetryData.Project(measure.getProjectUuid(), measure.getLastAnalysis(), language, loc));
- }
- }
- data.setProjects(projects);
+ resolveProjectStatistics(data, dbSession);
+ resolveProjects(data, dbSession);
+ resolveUsers(data, dbSession);
}
setSecurityCustomConfigIfPresent(data);
return data.build();
}
+ private void resolveProjectStatistics(TelemetryData.Builder data, DbSession dbSession) {
+ List<String> projectUuids = dbClient.projectDao().selectAllProjectUuids(dbSession);
+ Map<String, String> scmByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDSCM);
+ Map<String, String> ciByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDCI);
+ Map<String, ProjectAlmKeyAndProject> almAndUrlByProject = getAlmAndUrlByProject(dbSession);
+ Map<String, PrBranchAnalyzedLanguageCountByProjectDto> prAndBranchCountByProjects = dbClient.branchDao().countPrBranchAnalyzedLanguageByProjectUuid(dbSession)
+ .stream().collect(Collectors.toMap(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, Function.identity()));
+
+ boolean isCommunityEdition = editionProvider.get().filter(edition -> edition.equals(COMMUNITY)).isPresent();
+ List<TelemetryData.ProjectStatistics> projectStatistics = new ArrayList<>();
+ for (String projectUuid : projectUuids) {
+ Optional<PrBranchAnalyzedLanguageCountByProjectDto> counts = ofNullable(prAndBranchCountByProjects.get(projectUuid));
+
+ Long branchCount = counts.map(PrBranchAnalyzedLanguageCountByProjectDto::getBranch).orElse(0L);
+ Long pullRequestCount = counts.map(PrBranchAnalyzedLanguageCountByProjectDto::getPullRequest).orElse(0L);
+
+ Boolean hasUnanalyzedCMeasures = null;
+ Boolean hasUnanalyzedCppMeasures = null;
+ if (isCommunityEdition) {
+ hasUnanalyzedCMeasures = counts.map(PrBranchAnalyzedLanguageCountByProjectDto::getUnanalyzedCCount).orElse(0L) > 0;
+ hasUnanalyzedCppMeasures = counts.map(PrBranchAnalyzedLanguageCountByProjectDto::getUnanalyzedCppCount).orElse(0L) > 0;
+ }
+
+ String scm = Optional.ofNullable(scmByProject.get(projectUuid)).orElse(UNDETECTED);
+ String ci = Optional.ofNullable(ciByProject.get(projectUuid)).orElse(UNDETECTED);
+ String devopsPlatform = null;
+ if (almAndUrlByProject.containsKey(projectUuid)) {
+ ProjectAlmKeyAndProject projectAlmKeyAndProject = almAndUrlByProject.get(projectUuid);
+ devopsPlatform = getAlmName(projectAlmKeyAndProject.getAlmId(), projectAlmKeyAndProject.getUrl());
+ }
+ devopsPlatform = Optional.ofNullable(devopsPlatform).orElse(UNDETECTED);
+
+ projectStatistics.add(
+ new TelemetryData.ProjectStatistics(projectUuid, branchCount, pullRequestCount, hasUnanalyzedCMeasures, hasUnanalyzedCppMeasures, scm, ci, devopsPlatform));
+ }
+ data.setProjectStatistics(projectStatistics);
+ }
+
+ private void resolveProjects(TelemetryData.Builder data, DbSession dbSession) {
+ List<ProjectMeasureDto> measures = dbClient.measureDao().selectLastMeasureForAllProjects(dbSession, NCLOC_LANGUAGE_DISTRIBUTION_KEY);
+ List<TelemetryData.Project> projects = new ArrayList<>();
+ for (ProjectMeasureDto measure : measures) {
+ for (String measureTextValue : measure.getTextValue().split(";")) {
+ String[] languageAndLoc = measureTextValue.split("=");
+ String language = languageAndLoc[0];
+ Long loc = Long.parseLong(languageAndLoc[1]);
+ projects.add(new TelemetryData.Project(measure.getProjectUuid(), measure.getLastAnalysis(), language, loc));
+ }
+ }
+ data.setProjects(projects);
+ }
+
+ private void resolveUsers(TelemetryData.Builder data, DbSession dbSession) {
+ data.setUsers(dbClient.userDao().selectUsersForTelemetry(dbSession));
+ }
+
private void setSecurityCustomConfigIfPresent(TelemetryData.Builder data) {
editionProvider.get()
.filter(edition -> asList(ENTERPRISE, DATACENTER).contains(edition))