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.

ScmInfoDbLoaderTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.scm;
  21. import com.google.common.collect.ImmutableList;
  22. import java.util.Collections;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Optional;
  26. import javax.annotation.Nullable;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.api.utils.log.LogTester;
  31. import org.sonar.ce.task.projectanalysis.analysis.Analysis;
  32. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  33. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  34. import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
  35. import org.sonar.ce.task.projectanalysis.component.Component;
  36. import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
  37. import org.sonar.ce.task.projectanalysis.filemove.MutableMovedFilesRepositoryRule;
  38. import org.sonar.core.hash.SourceHashComputer;
  39. import org.sonar.core.util.Uuids;
  40. import org.sonar.db.DbTester;
  41. import org.sonar.db.component.BranchType;
  42. import org.sonar.db.protobuf.DbFileSources;
  43. import org.sonar.db.source.FileSourceDto;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.when;
  47. import static org.sonar.api.utils.log.LoggerLevel.TRACE;
  48. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  49. public class ScmInfoDbLoaderTest {
  50. static final int FILE_REF = 1;
  51. static final Component FILE = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").build();
  52. static final long DATE_1 = 123456789L;
  53. static Analysis baseProjectAnalysis = new Analysis.Builder()
  54. .setUuid("uuid_1")
  55. .setCreatedAt(123456789L)
  56. .build();
  57. @Rule
  58. public LogTester logTester = new LogTester();
  59. @Rule
  60. public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
  61. @Rule
  62. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  63. @Rule
  64. public BatchReportReaderRule reportReader = new BatchReportReaderRule();
  65. @Rule
  66. public MutableMovedFilesRepositoryRule movedFiles = new MutableMovedFilesRepositoryRule();
  67. private Branch branch = mock(Branch.class);
  68. private ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
  69. private ScmInfoDbLoader underTest = new ScmInfoDbLoader(analysisMetadataHolder, movedFiles, dbTester.getDbClient(), referenceBranchComponentUuids);
  70. @Test
  71. public void returns_ScmInfo_from_DB() {
  72. analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
  73. analysisMetadataHolder.setBranch(null);
  74. String hash = computeSourceHash(1);
  75. addFileSourceInDb("henry", DATE_1, "rev-1", hash);
  76. DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
  77. assertThat(scmInfo.getAllChangesets()).hasSize(1);
  78. assertThat(scmInfo.fileHash()).isEqualTo(hash);
  79. assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'FILE_UUID'");
  80. }
  81. @Test
  82. public void read_from_reference_branch_if_no_base() {
  83. analysisMetadataHolder.setBaseAnalysis(null);
  84. analysisMetadataHolder.setBranch(branch);
  85. String referenceFileUuid = "referenceFileUuid";
  86. String hash = computeSourceHash(1);
  87. when(referenceBranchComponentUuids.getComponentUuid(FILE.getDbKey())).thenReturn(referenceFileUuid);
  88. addFileSourceInDb("henry", DATE_1, "rev-1", hash, referenceFileUuid);
  89. DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
  90. assertThat(scmInfo.getAllChangesets()).hasSize(1);
  91. assertThat(scmInfo.fileHash()).isEqualTo(hash);
  92. assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'referenceFileUuid'");
  93. }
  94. @Test
  95. public void read_from_target_if_pullrequest() {
  96. Branch branch = mock(Branch.class);
  97. when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
  98. analysisMetadataHolder.setBaseAnalysis(null);
  99. analysisMetadataHolder.setBranch(branch);
  100. String targetBranchFileUuid = "targetBranchFileUuid";
  101. String hash = computeSourceHash(1);
  102. when(referenceBranchComponentUuids.getComponentUuid(FILE.getDbKey())).thenReturn(targetBranchFileUuid);
  103. addFileSourceInDb("henry", DATE_1, "rev-1", hash, targetBranchFileUuid);
  104. DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
  105. assertThat(scmInfo.getAllChangesets()).hasSize(1);
  106. assertThat(scmInfo.fileHash()).isEqualTo(hash);
  107. assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'targetBranchFileUuid'");
  108. }
  109. @Test
  110. public void return_empty_if_no_dto_available() {
  111. analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
  112. analysisMetadataHolder.setBranch(null);
  113. Optional<DbScmInfo> scmInfo = underTest.getScmInfo(FILE);
  114. assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'FILE_UUID'");
  115. assertThat(scmInfo).isEmpty();
  116. }
  117. @Test
  118. public void do_not_read_from_db_on_first_analysis_if_there_is_no_reference_branch() {
  119. Branch branch = mock(Branch.class);
  120. when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
  121. analysisMetadataHolder.setBaseAnalysis(null);
  122. analysisMetadataHolder.setBranch(branch);
  123. assertThat(underTest.getScmInfo(FILE)).isEmpty();
  124. assertThat(logTester.logs(TRACE)).isEmpty();
  125. }
  126. private static List<String> generateLines(int lineCount) {
  127. ImmutableList.Builder<String> builder = ImmutableList.builder();
  128. for (int i = 0; i < lineCount; i++) {
  129. builder.add("line " + i);
  130. }
  131. return builder.build();
  132. }
  133. private static String computeSourceHash(int lineCount) {
  134. SourceHashComputer sourceHashComputer = new SourceHashComputer();
  135. Iterator<String> lines = generateLines(lineCount).iterator();
  136. while (lines.hasNext()) {
  137. sourceHashComputer.addLine(lines.next(), lines.hasNext());
  138. }
  139. return sourceHashComputer.getHash();
  140. }
  141. private void addFileSourceInDb(@Nullable String author, @Nullable Long date, @Nullable String revision, String srcHash) {
  142. addFileSourceInDb(author, date, revision, srcHash, FILE.getUuid());
  143. }
  144. private void addFileSourceInDb(@Nullable String author, @Nullable Long date, @Nullable String revision, String srcHash, String fileUuid) {
  145. DbFileSources.Data.Builder fileDataBuilder = DbFileSources.Data.newBuilder();
  146. DbFileSources.Line.Builder builder = fileDataBuilder.addLinesBuilder()
  147. .setLine(1);
  148. if (author != null) {
  149. builder.setScmAuthor(author);
  150. }
  151. if (date != null) {
  152. builder.setScmDate(date);
  153. }
  154. if (revision != null) {
  155. builder.setScmRevision(revision);
  156. }
  157. dbTester.getDbClient().fileSourceDao().insert(dbTester.getSession(), new FileSourceDto()
  158. .setUuid(Uuids.createFast())
  159. .setLineHashes(Collections.singletonList("lineHash"))
  160. .setFileUuid(fileUuid)
  161. .setProjectUuid("PROJECT_UUID")
  162. .setSourceData(fileDataBuilder.build())
  163. .setSrcHash(srcHash));
  164. dbTester.commit();
  165. }
  166. }