]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17195 Removed potential test bug and cleanup
authoralain <108417558+alain-kermis-sonarsource@users.noreply.github.com>
Thu, 29 Sep 2022 13:04:04 +0000 (15:04 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 29 Sep 2022 20:03:15 +0000 (20:03 +0000)
server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java
server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java

index 6bc9a0c35fe0bfe1cf2c8eb46bb9515978cc996b..b777f7f5cc89a77f417c1f7b531a093f6e8eb098 100644 (file)
@@ -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<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();
 
@@ -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<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());
   }
index 258e0128442cdc860710bf9e537e02e22f91faa3..29b97fd2045021d7bb76d77a834cf977b7e9c585 100644 (file)
@@ -111,56 +111,9 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
     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);
@@ -173,6 +126,62 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
     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))