From: alain <108417558+alain-kermis-sonarsource@users.noreply.github.com> Date: Thu, 29 Sep 2022 13:04:04 +0000 (+0200) Subject: SONAR-17195 Removed potential test bug and cleanup X-Git-Tag: 9.7.0.61563~147 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=29868c3beba7895a71597532b727d213e6320ad0;p=sonarqube.git SONAR-17195 Removed potential test bug and cleanup --- diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java index 6bc9a0c35fe..b777f7f5cc8 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java @@ -48,19 +48,13 @@ import static org.sonar.test.JsonAssert.assertJson; @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); @@ -72,7 +66,7 @@ public class TelemetryDataJsonWriterTest { @Test public void does_not_write_edition_if_null() { - TelemetryData data = SOME_TELEMETRY_DATA.build(); + TelemetryData data = telemetryBuilder().build(); String json = writeTelemetryData(data); @@ -82,7 +76,7 @@ public class TelemetryDataJsonWriterTest { @Test @UseDataProvider("allEditions") public void writes_edition_if_non_null(EditionProvider.Edition edition) { - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setEdition(edition) .build(); @@ -95,7 +89,7 @@ public class TelemetryDataJsonWriterTest { @Test public void does_not_write_license_type_if_null() { - TelemetryData data = SOME_TELEMETRY_DATA.build(); + TelemetryData data = telemetryBuilder().build(); String json = writeTelemetryData(data); @@ -105,7 +99,7 @@ public class TelemetryDataJsonWriterTest { @Test public void writes_licenseType_if_non_null() { String expected = randomAlphabetic(12); - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setLicenseType(expected) .build(); @@ -120,7 +114,7 @@ public class TelemetryDataJsonWriterTest { 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(); @@ -136,7 +130,7 @@ public class TelemetryDataJsonWriterTest { @Test public void writes_no_plugins() { - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setPlugins(Collections.emptyMap()) .build(); @@ -152,7 +146,7 @@ public class TelemetryDataJsonWriterTest { Map 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(); @@ -168,7 +162,7 @@ public class TelemetryDataJsonWriterTest { @Test public void does_not_write_installation_date_if_null() { - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setInstallationDate(null) .build(); @@ -179,7 +173,7 @@ public class TelemetryDataJsonWriterTest { @Test public void write_installation_date_in_utc_format() { - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setInstallationDate(1_000L) .build(); @@ -192,7 +186,7 @@ public class TelemetryDataJsonWriterTest { @Test public void does_not_write_installation_version_if_null() { - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setInstallationVersion(null) .build(); @@ -204,7 +198,7 @@ public class TelemetryDataJsonWriterTest { @Test public void write_installation_version() { String installationVersion = randomAlphabetic(5); - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setInstallationVersion(installationVersion) .build(); @@ -218,7 +212,7 @@ public class TelemetryDataJsonWriterTest { @Test public void write_docker_flag() { boolean inDocker = random.nextBoolean(); - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setInDocker(inDocker) .build(); @@ -231,7 +225,7 @@ public class TelemetryDataJsonWriterTest { @Test public void writes_security_custom_config() { - TelemetryData data = SOME_TELEMETRY_DATA + TelemetryData data = telemetryBuilder() .setCustomSecurityConfigs(Arrays.asList("php", "java")) .build(); @@ -244,8 +238,8 @@ public class TelemetryDataJsonWriterTest { @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); @@ -279,8 +273,8 @@ public class TelemetryDataJsonWriterTest { @Test public void writes_all_projects() { - TelemetryData data = SOME_TELEMETRY_DATA - .setProjects(getProjects()) + TelemetryData data = telemetryBuilder() + .setProjects(attachProjects()) .build(); String json = writeTelemetryData(data); @@ -289,20 +283,20 @@ public class TelemetryDataJsonWriterTest { " \"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" + " }" + " ]" + @@ -311,8 +305,8 @@ public class TelemetryDataJsonWriterTest { @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); @@ -355,27 +349,35 @@ public class TelemetryDataJsonWriterTest { @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 getUsers() { + private static List 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 getProjects() { + private static List attachProjects() { return IntStream.range(0, 3).mapToObj(i -> new TelemetryData.Project("uuid-" + i, 1L, "lang-" + i, (i + 1L) * 2L)).collect(Collectors.toList()); } - private List getProjectStats(boolean hasUnanalyzedLanguages) { + private List 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()); } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java index 258e0128442..29b97fd2045 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java @@ -111,56 +111,9 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { try (DbSession dbSession = dbClient.openSession(false)) { data.setDatabase(loadDatabaseMetadata(dbSession)); - Map scmByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDSCM); - Map ciByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDCI); - Map almAndUrlByProject = getAlmAndUrlByProject(dbSession); - List projectUuids = dbClient.projectDao().selectAllProjectUuids(dbSession); - - Map prAndBranchCountByProjects = dbClient.branchDao().countPrBranchAnalyzedLanguageByProjectUuid(dbSession) - .stream().collect(Collectors.toMap(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, Function.identity())); - - boolean isCommunityEdition = editionProvider.get().filter(edition -> edition.equals(COMMUNITY)).isPresent(); - List projectStatistics = new ArrayList<>(); - for (String projectUuid : projectUuids) { - Optional 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 measures = dbClient.measureDao().selectLastMeasureForAllProjects(dbSession, NCLOC_LANGUAGE_DISTRIBUTION_KEY); - List 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); @@ -173,6 +126,62 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { return data.build(); } + private void resolveProjectStatistics(TelemetryData.Builder data, DbSession dbSession) { + List projectUuids = dbClient.projectDao().selectAllProjectUuids(dbSession); + Map scmByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDSCM); + Map ciByProject = getAnalysisPropertyByProject(dbSession, SONAR_ANALYSIS_DETECTEDCI); + Map almAndUrlByProject = getAlmAndUrlByProject(dbSession); + Map prAndBranchCountByProjects = dbClient.branchDao().countPrBranchAnalyzedLanguageByProjectUuid(dbSession) + .stream().collect(Collectors.toMap(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, Function.identity())); + + boolean isCommunityEdition = editionProvider.get().filter(edition -> edition.equals(COMMUNITY)).isPresent(); + List projectStatistics = new ArrayList<>(); + for (String projectUuid : projectUuids) { + Optional 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 measures = dbClient.measureDao().selectLastMeasureForAllProjects(dbSession, NCLOC_LANGUAGE_DISTRIBUTION_KEY); + List 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))