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.

UpdateQualityProfilesLastUsedDateStepTest.java 7.0KB

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