You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateQualityProfilesLastUsedDateStepIT.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.step;
  21. import java.util.Arrays;
  22. import java.util.Date;
  23. import java.util.stream.Collectors;
  24. import javax.annotation.CheckForNull;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  29. import org.sonar.ce.task.projectanalysis.component.Component;
  30. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  31. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  32. import org.sonar.ce.task.projectanalysis.measure.Measure;
  33. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  34. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  35. import org.sonar.ce.task.step.TestComputationStepContext;
  36. import org.sonar.db.DbClient;
  37. import org.sonar.db.DbSession;
  38. import org.sonar.db.DbTester;
  39. import org.sonar.db.RowNotFoundException;
  40. import org.sonar.db.qualityprofile.QProfileDto;
  41. import org.sonar.db.qualityprofile.QualityProfileDbTester;
  42. import org.sonar.server.qualityprofile.QPMeasureData;
  43. import org.sonar.server.qualityprofile.QualityProfile;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  46. import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES;
  47. import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES_KEY;
  48. import static org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto;
  49. public class UpdateQualityProfilesLastUsedDateStepIT {
  50. private static final long ANALYSIS_DATE = 1_123_456_789L;
  51. private static final Component PROJECT = ReportComponent.DUMB_PROJECT;
  52. private QProfileDto sonarWayJava = newProfile("sonar-way-java");
  53. private QProfileDto sonarWayPhp = newProfile("sonar-way-php");
  54. private QProfileDto myQualityProfile = newProfile("my-qp");
  55. @Rule
  56. public DbTester db = DbTester.create(System2.INSTANCE);
  57. @Rule
  58. public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule().setAnalysisDate(ANALYSIS_DATE);
  59. @Rule
  60. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
  61. @Rule
  62. public MetricRepositoryRule metricRepository = new MetricRepositoryRule().add(QUALITY_PROFILES);
  63. @Rule
  64. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  65. private DbClient dbClient = db.getDbClient();
  66. private DbSession dbSession = db.getSession();
  67. private QualityProfileDbTester qualityProfileDb = new QualityProfileDbTester(db);
  68. UpdateQualityProfilesLastUsedDateStep underTest = new UpdateQualityProfilesLastUsedDateStep(dbClient, analysisMetadataHolder, treeRootHolder, metricRepository,
  69. measureRepository);
  70. @Test
  71. public void doest_not_update_profiles_when_no_measure() {
  72. qualityProfileDb.insert(sonarWayJava, sonarWayPhp, myQualityProfile);
  73. underTest.execute(new TestComputationStepContext());
  74. assertQualityProfileIsTheSame(sonarWayJava);
  75. assertQualityProfileIsTheSame(sonarWayPhp);
  76. assertQualityProfileIsTheSame(myQualityProfile);
  77. }
  78. @Test
  79. public void update_profiles_defined_in_quality_profiles_measure() {
  80. qualityProfileDb.insert(sonarWayJava, sonarWayPhp, myQualityProfile);
  81. measureRepository.addRawMeasure(1, QUALITY_PROFILES_KEY, Measure.newMeasureBuilder().create(
  82. toJson(sonarWayJava.getKee(), myQualityProfile.getKee())));
  83. underTest.execute(new TestComputationStepContext());
  84. assertQualityProfileIsTheSame(sonarWayPhp);
  85. assertQualityProfileIsUpdated(sonarWayJava);
  86. assertQualityProfileIsUpdated(myQualityProfile);
  87. }
  88. @Test
  89. public void ancestor_profiles_are_updated() {
  90. // Parent profiles should be updated
  91. QProfileDto rootProfile = newProfile("root");
  92. QProfileDto parentProfile = newProfile("parent").setParentKee(rootProfile.getKee());
  93. // Current profile => should be updated
  94. QProfileDto currentProfile = newProfile("current").setParentKee(parentProfile.getKee());
  95. // Child of current profile => should not be updated
  96. QProfileDto childProfile = newProfile("child").setParentKee(currentProfile.getKee());
  97. qualityProfileDb.insert(rootProfile, parentProfile, currentProfile, childProfile);
  98. measureRepository.addRawMeasure(1, QUALITY_PROFILES_KEY, Measure.newMeasureBuilder().create(toJson(currentProfile.getKee())));
  99. underTest.execute(new TestComputationStepContext());
  100. assertQualityProfileIsUpdated(rootProfile);
  101. assertQualityProfileIsUpdated(parentProfile);
  102. assertQualityProfileIsUpdated(currentProfile);
  103. assertQualityProfileIsTheSame(childProfile);
  104. }
  105. @Test
  106. public void fail_when_profile_is_linked_to_unknown_parent() {
  107. QProfileDto currentProfile = newProfile("current").setParentKee("unknown");
  108. qualityProfileDb.insert(currentProfile);
  109. measureRepository.addRawMeasure(1, QUALITY_PROFILES_KEY, Measure.newMeasureBuilder().create(toJson(currentProfile.getKee())));
  110. assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
  111. .isInstanceOf(RowNotFoundException.class);
  112. }
  113. @Test
  114. public void test_description() {
  115. assertThat(underTest.getDescription()).isEqualTo("Update last usage date of quality profiles");
  116. }
  117. private static QProfileDto newProfile(String key) {
  118. return newQualityProfileDto().setKee(key)
  119. // profile has been used before the analysis
  120. .setLastUsed(ANALYSIS_DATE - 10_000);
  121. }
  122. private void assertQualityProfileIsUpdated(QProfileDto qp) {
  123. assertThat(selectLastUser(qp.getKee())).withFailMessage("Quality profile '%s' hasn't been updated. Value: %d", qp.getKee(), qp.getLastUsed()).isEqualTo(ANALYSIS_DATE);
  124. }
  125. private void assertQualityProfileIsTheSame(QProfileDto qp) {
  126. assertThat(selectLastUser(qp.getKee())).isEqualTo(qp.getLastUsed());
  127. }
  128. @CheckForNull
  129. private Long selectLastUser(String qualityProfileKey) {
  130. return dbClient.qualityProfileDao().selectByUuid(dbSession, qualityProfileKey).getLastUsed();
  131. }
  132. private static String toJson(String... keys) {
  133. return QPMeasureData.toJson(new QPMeasureData(
  134. Arrays.stream(keys)
  135. .map(key -> new QualityProfile(key, key, key, new Date()))
  136. .toList()));
  137. }
  138. }