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.

PersistDuplicationDataStepTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.Optional;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.measures.Metric;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolderRule;
  28. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  29. import org.sonar.ce.task.projectanalysis.duplication.DuplicationRepositoryRule;
  30. import org.sonar.ce.task.projectanalysis.duplication.TextBlock;
  31. import org.sonar.ce.task.projectanalysis.measure.MeasureToMeasureDto;
  32. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  33. import org.sonar.ce.task.step.ComputationStep;
  34. import org.sonar.ce.task.step.TestComputationStepContext;
  35. import org.sonar.db.DbTester;
  36. import org.sonar.db.component.ComponentDto;
  37. import org.sonar.db.measure.LiveMeasureDto;
  38. import org.sonar.db.metric.MetricDto;
  39. import static org.assertj.core.api.Assertions.assertThat;
  40. import static org.sonar.api.measures.CoreMetrics.DUPLICATIONS_DATA_KEY;
  41. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  42. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  43. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  44. public class PersistDuplicationDataStepTest extends BaseStepTest {
  45. private static final int ROOT_REF = 1;
  46. private static final String PROJECT_KEY = "PROJECT_KEY";
  47. private static final String PROJECT_UUID = "u1";
  48. private static final int FILE_1_REF = 2;
  49. private static final String FILE_1_KEY = "FILE_1_KEY";
  50. private static final String FILE_1_UUID = "u2";
  51. private static final int FILE_2_REF = 3;
  52. private static final String FILE_2_KEY = "FILE_2_KEY";
  53. private static final String FILE_2_UUID = "u3";
  54. @Rule
  55. public DbTester db = DbTester.create(System2.INSTANCE);
  56. @Rule
  57. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
  58. .setRoot(
  59. builder(PROJECT, ROOT_REF).setKey(PROJECT_KEY).setUuid(PROJECT_UUID)
  60. .addChildren(
  61. builder(FILE, FILE_1_REF).setKey(FILE_1_KEY).setUuid(FILE_1_UUID)
  62. .build(),
  63. builder(FILE, FILE_2_REF).setKey(FILE_2_KEY).setUuid(FILE_2_UUID)
  64. .build())
  65. .build());
  66. @Rule
  67. public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
  68. @Rule
  69. public DuplicationRepositoryRule duplicationRepository = DuplicationRepositoryRule.create(treeRootHolder);
  70. @Rule
  71. public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
  72. @Before
  73. public void setUp() throws Exception {
  74. MetricDto metric = db.measures().insertMetric(m -> m.setKey(DUPLICATIONS_DATA_KEY).setValueType(Metric.ValueType.STRING.name()));
  75. insertComponent(PROJECT_KEY, PROJECT_UUID);
  76. insertComponent(FILE_1_KEY, FILE_1_UUID);
  77. insertComponent(FILE_2_KEY, FILE_2_UUID);
  78. db.commit();
  79. metricRepository.add(metric.getId(), new Metric.Builder(DUPLICATIONS_DATA_KEY, DUPLICATIONS_DATA_KEY, Metric.ValueType.STRING).create());
  80. }
  81. @Override
  82. protected ComputationStep step() {
  83. return underTest();
  84. }
  85. @Test
  86. public void nothing_to_persist_when_no_duplication() {
  87. TestComputationStepContext context = new TestComputationStepContext();
  88. underTest().execute(context);
  89. assertThatNothingPersisted();
  90. verifyStatistics(context, 0);
  91. }
  92. @Test
  93. public void compute_duplications_on_same_file() {
  94. duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), new TextBlock(6, 10));
  95. TestComputationStepContext context = new TestComputationStepContext();
  96. underTest().execute(context);
  97. assertThat(selectMeasureData(FILE_1_UUID)).hasValue("<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
  98. + FILE_1_KEY + "\"/></g></duplications>");
  99. assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
  100. assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
  101. }
  102. @Test
  103. public void compute_duplications_on_different_files() {
  104. duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
  105. TestComputationStepContext context = new TestComputationStepContext();
  106. underTest().execute(context);
  107. assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
  108. "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
  109. + FILE_2_KEY + "\"/></g></duplications>");
  110. assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
  111. assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
  112. }
  113. @Test
  114. public void compute_duplications_on_unchanged_file() {
  115. duplicationRepository.addExtendedProjectDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
  116. TestComputationStepContext context = new TestComputationStepContext();
  117. underTest().execute(context);
  118. assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
  119. "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"true\" r=\""
  120. + FILE_2_KEY + "\"/></g></duplications>");
  121. assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
  122. assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
  123. }
  124. @Test
  125. public void compute_duplications_on_different_projects() {
  126. String fileKeyFromOtherProject = "PROJECT2_KEY:file2";
  127. duplicationRepository.addCrossProjectDuplication(FILE_1_REF, new TextBlock(1, 5), fileKeyFromOtherProject, new TextBlock(6, 10));
  128. TestComputationStepContext context = new TestComputationStepContext();
  129. underTest().execute(context);
  130. assertThat(selectMeasureData(FILE_1_UUID)).hasValue(
  131. "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"false\" r=\""
  132. + fileKeyFromOtherProject + "\"/></g></duplications>");
  133. assertThat(selectMeasureData(FILE_2_UUID)).isEmpty();
  134. assertThat(selectMeasureData(PROJECT_UUID)).isEmpty();
  135. }
  136. private PersistDuplicationDataStep underTest() {
  137. return new PersistDuplicationDataStep(db.getDbClient(), treeRootHolder, metricRepository, duplicationRepository,
  138. new MeasureToMeasureDto(analysisMetadataHolder, treeRootHolder));
  139. }
  140. private void assertThatNothingPersisted() {
  141. assertThat(db.countRowsOfTable(db.getSession(), "live_measures")).isEqualTo(0);
  142. }
  143. private Optional<String> selectMeasureData(String componentUuid) {
  144. return db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), componentUuid, "duplications_data")
  145. .map(LiveMeasureDto::getTextValue);
  146. }
  147. private ComponentDto insertComponent(String key, String uuid) {
  148. ComponentDto componentDto = new ComponentDto()
  149. .setOrganizationUuid("org1")
  150. .setDbKey(key)
  151. .setUuid(uuid)
  152. .setUuidPath(uuid + ".")
  153. .setRootUuid(uuid)
  154. .setProjectUuid(uuid);
  155. db.getDbClient().componentDao().insert(db.getSession(), componentDto);
  156. return componentDto;
  157. }
  158. private static void verifyStatistics(TestComputationStepContext context, int expectedInsertsOrUpdates) {
  159. context.getStatistics().assertValue("insertsOrUpdates", expectedInsertsOrUpdates);
  160. }
  161. }