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.

PersistCrossProjectDuplicationIndexStepTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.Collections;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.ce.task.projectanalysis.analysis.Analysis;
  30. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  31. import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
  32. import org.sonar.ce.task.projectanalysis.component.Component;
  33. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  34. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  35. import org.sonar.ce.task.projectanalysis.duplication.CrossProjectDuplicationStatusHolder;
  36. import org.sonar.ce.task.step.ComputationStep;
  37. import org.sonar.ce.task.step.TestComputationStepContext;
  38. import org.sonar.db.DbClient;
  39. import org.sonar.db.DbTester;
  40. import org.sonar.scanner.protocol.output.ScannerReport;
  41. import static java.util.Collections.singletonList;
  42. import static org.assertj.core.api.Assertions.assertThat;
  43. import static org.mockito.Mockito.mock;
  44. import static org.mockito.Mockito.when;
  45. public class PersistCrossProjectDuplicationIndexStepTest {
  46. private static final int FILE_1_REF = 2;
  47. private static final int FILE_2_REF = 3;
  48. private static final String FILE_2_UUID = "file2";
  49. private static final Component FILE_1 = ReportComponent.builder(Component.Type.FILE, FILE_1_REF).build();
  50. private static final Component FILE_2 = ReportComponent.builder(Component.Type.FILE, FILE_2_REF)
  51. .setStatus(Component.Status.SAME).setUuid(FILE_2_UUID).build();
  52. private static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1)
  53. .addChildren(FILE_1)
  54. .addChildren(FILE_2)
  55. .build();
  56. private static final ScannerReport.CpdTextBlock CPD_TEXT_BLOCK = ScannerReport.CpdTextBlock.newBuilder()
  57. .setHash("a8998353e96320ec")
  58. .setStartLine(30)
  59. .setEndLine(45)
  60. .build();
  61. private static final String ANALYSIS_UUID = "analysis uuid";
  62. private static final String BASE_ANALYSIS_UUID = "base analysis uuid";
  63. @Rule
  64. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  65. @Rule
  66. public BatchReportReaderRule reportReader = new BatchReportReaderRule();
  67. @Rule
  68. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule().setRoot(PROJECT);
  69. @Rule
  70. public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
  71. private CrossProjectDuplicationStatusHolder crossProjectDuplicationStatusHolder = mock(CrossProjectDuplicationStatusHolder.class);
  72. private Analysis baseAnalysis = mock(Analysis.class);
  73. private DbClient dbClient = dbTester.getDbClient();
  74. private ComputationStep underTest;
  75. @Before
  76. public void setUp() {
  77. when(baseAnalysis.getUuid()).thenReturn(BASE_ANALYSIS_UUID);
  78. analysisMetadataHolder.setUuid(ANALYSIS_UUID);
  79. analysisMetadataHolder.setBaseAnalysis(baseAnalysis);
  80. underTest = new PersistCrossProjectDuplicationIndexStep(crossProjectDuplicationStatusHolder, dbClient, treeRootHolder, analysisMetadataHolder, reportReader);
  81. }
  82. @Test
  83. public void persist_cpd_text_block() {
  84. when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
  85. reportReader.putDuplicationBlocks(FILE_1_REF, singletonList(CPD_TEXT_BLOCK));
  86. TestComputationStepContext context = new TestComputationStepContext();
  87. underTest.execute(context);
  88. Map<String, Object> dto = dbTester.selectFirst("select HASH, START_LINE, END_LINE, INDEX_IN_FILE, COMPONENT_UUID, ANALYSIS_UUID from duplications_index");
  89. assertThat(dto)
  90. .containsEntry("HASH", CPD_TEXT_BLOCK.getHash())
  91. .containsEntry("START_LINE", 30L)
  92. .containsEntry("END_LINE", 45L)
  93. .containsEntry("INDEX_IN_FILE", 0L)
  94. .containsEntry("COMPONENT_UUID", FILE_1.getUuid())
  95. .containsEntry("ANALYSIS_UUID", ANALYSIS_UUID);
  96. context.getStatistics().assertValue("inserts", 1);
  97. }
  98. @Test
  99. public void persist_many_cpd_text_blocks() {
  100. when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
  101. reportReader.putDuplicationBlocks(FILE_1_REF, Arrays.asList(
  102. CPD_TEXT_BLOCK,
  103. ScannerReport.CpdTextBlock.newBuilder()
  104. .setHash("b1234353e96320ff")
  105. .setStartLine(20)
  106. .setEndLine(15)
  107. .build()));
  108. TestComputationStepContext context = new TestComputationStepContext();
  109. underTest.execute(context);
  110. List<Map<String, Object>> dtos = dbTester.select("select HASH, START_LINE, END_LINE, INDEX_IN_FILE, COMPONENT_UUID, ANALYSIS_UUID from duplications_index");
  111. assertThat(dtos).extracting("HASH").containsOnly(CPD_TEXT_BLOCK.getHash(), "b1234353e96320ff");
  112. assertThat(dtos).extracting("START_LINE").containsOnly(30L, 20L);
  113. assertThat(dtos).extracting("END_LINE").containsOnly(45L, 15L);
  114. assertThat(dtos).extracting("INDEX_IN_FILE").containsOnly(0L, 1L);
  115. assertThat(dtos).extracting("COMPONENT_UUID").containsOnly(FILE_1.getUuid());
  116. assertThat(dtos).extracting("ANALYSIS_UUID").containsOnly(ANALYSIS_UUID);
  117. context.getStatistics().assertValue("inserts", 2);
  118. }
  119. @Test
  120. public void nothing_to_persist_when_no_cpd_text_blocks_in_report() {
  121. when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
  122. reportReader.putDuplicationBlocks(FILE_1_REF, Collections.emptyList());
  123. TestComputationStepContext context = new TestComputationStepContext();
  124. underTest.execute(context);
  125. assertThat(dbTester.countRowsOfTable("duplications_index")).isZero();
  126. context.getStatistics().assertValue("inserts", 0);
  127. }
  128. @Test
  129. public void nothing_to_do_when_cross_project_duplication_is_disabled() {
  130. when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(false);
  131. reportReader.putDuplicationBlocks(FILE_1_REF, singletonList(CPD_TEXT_BLOCK));
  132. TestComputationStepContext context = new TestComputationStepContext();
  133. underTest.execute(context);
  134. assertThat(dbTester.countRowsOfTable("duplications_index")).isZero();
  135. context.getStatistics().assertValue("inserts", null);
  136. }
  137. }