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.

ScmInfoRepositoryImplTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 com.tngtech.java.junit.dataprovider.DataProvider;
  23. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  24. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  25. import java.util.Collections;
  26. import java.util.Date;
  27. import java.util.EnumSet;
  28. import java.util.List;
  29. import java.util.Optional;
  30. import org.junit.Before;
  31. import org.junit.Rule;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.sonar.api.utils.log.LogTester;
  35. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  36. import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
  37. import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
  38. import org.sonar.ce.task.projectanalysis.component.Component;
  39. import org.sonar.ce.task.projectanalysis.component.Component.Status;
  40. import org.sonar.ce.task.projectanalysis.component.Component.Type;
  41. import org.sonar.ce.task.projectanalysis.component.FileAttributes;
  42. import org.sonar.ce.task.projectanalysis.component.FileStatuses;
  43. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  44. import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
  45. import org.sonar.ce.task.projectanalysis.source.SourceLinesDiff;
  46. import org.sonar.db.protobuf.DbFileSources.Line;
  47. import org.sonar.scanner.protocol.output.ScannerReport;
  48. import org.sonar.scanner.protocol.output.ScannerReport.Changesets;
  49. import static org.assertj.core.api.Assertions.assertThat;
  50. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  51. import static org.mockito.Mockito.mock;
  52. import static org.mockito.Mockito.verify;
  53. import static org.mockito.Mockito.verifyNoInteractions;
  54. import static org.mockito.Mockito.verifyNoMoreInteractions;
  55. import static org.mockito.Mockito.when;
  56. import static org.sonar.api.utils.log.LoggerLevel.TRACE;
  57. import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
  58. @RunWith(DataProviderRunner.class)
  59. public class ScmInfoRepositoryImplTest {
  60. static final int FILE_REF = 1;
  61. static final FileAttributes attributes = new FileAttributes(false, "java", 3);
  62. static final Component FILE = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").setFileAttributes(attributes).build();
  63. static final Component FILE_SAME = builder(Component.Type.FILE, FILE_REF).setStatus(Status.SAME).setKey("FILE_KEY").setUuid("FILE_UUID").setFileAttributes(attributes).build();
  64. static final long DATE_1 = 123456789L;
  65. static final long DATE_2 = 1234567810L;
  66. @Rule
  67. public LogTester logTester = new LogTester();
  68. @Rule
  69. public BatchReportReaderRule reportReader = new BatchReportReaderRule();
  70. @Rule
  71. public AnalysisMetadataHolderRule analysisMetadata = new AnalysisMetadataHolderRule();
  72. private final FileStatuses fileStatuses = mock(FileStatuses.class);
  73. private final SourceLinesDiff diff = mock(SourceLinesDiff.class);
  74. private final ScmInfoDbLoader dbLoader = mock(ScmInfoDbLoader.class);
  75. private final Date analysisDate = new Date();
  76. private final ScmInfoRepositoryImpl underTest = new ScmInfoRepositoryImpl(reportReader, analysisMetadata, dbLoader, diff, fileStatuses);
  77. @Before
  78. public void setUp() {
  79. analysisMetadata.setAnalysisDate(analysisDate);
  80. }
  81. @Test
  82. public void return_empty_if_component_is_not_file() {
  83. Component c = mock(Component.class);
  84. when(c.getType()).thenReturn(Type.DIRECTORY);
  85. assertThat(underTest.getScmInfo(c)).isEmpty();
  86. }
  87. @Test
  88. public void load_scm_info_from_cache_when_already_loaded() {
  89. addChangesetInReport("john", DATE_1, "rev-1");
  90. ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
  91. assertThat(scmInfo.getAllChangesets()).hasSize(1);
  92. assertThat(logTester.logs(TRACE)).hasSize(1);
  93. logTester.clear();
  94. underTest.getScmInfo(FILE);
  95. assertThat(logTester.logs(TRACE)).isEmpty();
  96. verifyNoInteractions(dbLoader);
  97. verifyNoInteractions(fileStatuses);
  98. verifyNoInteractions(diff);
  99. }
  100. @Test
  101. public void read_from_report() {
  102. addChangesetInReport("john", DATE_1, "rev-1");
  103. ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
  104. assertThat(scmInfo.getAllChangesets()).hasSize(1);
  105. Changeset changeset = scmInfo.getChangesetForLine(1);
  106. assertThat(changeset.getAuthor()).isEqualTo("john");
  107. assertThat(changeset.getDate()).isEqualTo(DATE_1);
  108. assertThat(changeset.getRevision()).isEqualTo("rev-1");
  109. assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from report for file 'FILE_KEY'");
  110. verifyNoInteractions(dbLoader);
  111. verifyNoInteractions(fileStatuses);
  112. verifyNoInteractions(diff);
  113. }
  114. @Test
  115. public void read_from_DB_if_no_report_and_file_unchanged() {
  116. createDbScmInfoWithOneLine();
  117. when(fileStatuses.isUnchanged(FILE_SAME)).thenReturn(true);
  118. // should clear revision and author
  119. ScmInfo scmInfo = underTest.getScmInfo(FILE_SAME).get();
  120. assertThat(scmInfo.getAllChangesets()).hasSize(1);
  121. assertChangeset(scmInfo.getChangesetForLine(1), null, null, 10L);
  122. verify(fileStatuses).isUnchanged(FILE_SAME);
  123. verify(dbLoader).getScmInfo(FILE_SAME);
  124. verifyNoMoreInteractions(dbLoader);
  125. verifyNoMoreInteractions(fileStatuses);
  126. verifyNoInteractions(diff);
  127. }
  128. @Test
  129. public void read_from_DB_if_no_report_and_file_unchanged_and_copyFromPrevious_is_true() {
  130. createDbScmInfoWithOneLine();
  131. when(fileStatuses.isUnchanged(FILE_SAME)).thenReturn(true);
  132. addFileSourceInReport(1);
  133. addCopyFromPrevious();
  134. ScmInfo scmInfo = underTest.getScmInfo(FILE_SAME).get();
  135. assertThat(scmInfo.getAllChangesets()).hasSize(1);
  136. assertChangeset(scmInfo.getChangesetForLine(1), "rev1", "author1", 10L);
  137. verify(fileStatuses).isUnchanged(FILE_SAME);
  138. verify(dbLoader).getScmInfo(FILE_SAME);
  139. verifyNoMoreInteractions(dbLoader);
  140. verifyNoMoreInteractions(fileStatuses);
  141. verifyNoInteractions(diff);
  142. }
  143. @Test
  144. public void generate_scm_info_when_nothing_in_report_nor_db() {
  145. when(dbLoader.getScmInfo(FILE)).thenReturn(Optional.empty());
  146. ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
  147. assertThat(scmInfo.getAllChangesets()).hasSize(3);
  148. for (int i = 1; i <= 3; i++) {
  149. assertChangeset(scmInfo.getChangesetForLine(i), null, null, analysisDate.getTime());
  150. }
  151. verify(dbLoader).getScmInfo(FILE);
  152. verifyNoMoreInteractions(dbLoader);
  153. verifyNoInteractions(fileStatuses);
  154. verifyNoInteractions(diff);
  155. }
  156. @Test
  157. public void generate_scm_info_when_nothing_in_db_and_report_is_has_no_changesets() {
  158. when(dbLoader.getScmInfo(FILE)).thenReturn(Optional.empty());
  159. addFileSourceInReport(3);
  160. ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
  161. assertThat(scmInfo.getAllChangesets()).hasSize(3);
  162. for (int i = 1; i <= 3; i++) {
  163. assertChangeset(scmInfo.getChangesetForLine(i), null, null, analysisDate.getTime());
  164. }
  165. verify(dbLoader).getScmInfo(FILE);
  166. verifyNoMoreInteractions(dbLoader);
  167. verifyNoInteractions(fileStatuses);
  168. verifyNoInteractions(diff);
  169. }
  170. @Test
  171. public void generate_scm_info_for_new_and_changed_lines_when_report_is_empty() {
  172. createDbScmInfoWithOneLine();
  173. when(diff.computeMatchingLines(FILE)).thenReturn(new int[] {1, 0, 0});
  174. addFileSourceInReport(3);
  175. ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
  176. assertThat(scmInfo.getAllChangesets()).hasSize(3);
  177. assertChangeset(scmInfo.getChangesetForLine(1), null, null, 10L);
  178. assertChangeset(scmInfo.getChangesetForLine(2), null, null, analysisDate.getTime());
  179. assertChangeset(scmInfo.getChangesetForLine(3), null, null, analysisDate.getTime());
  180. verify(dbLoader).getScmInfo(FILE);
  181. verify(diff).computeMatchingLines(FILE);
  182. verifyNoMoreInteractions(dbLoader);
  183. verifyNoMoreInteractions(diff);
  184. }
  185. @Test
  186. public void fail_with_NPE_when_component_is_null() {
  187. assertThatThrownBy(() -> underTest.getScmInfo(null))
  188. .isInstanceOf(NullPointerException.class)
  189. .hasMessage("Component cannot be null");
  190. }
  191. @Test
  192. @UseDataProvider("allTypeComponentButFile")
  193. public void do_not_query_db_nor_report_if_component_type_is_not_FILE(Component component) {
  194. BatchReportReader batchReportReader = mock(BatchReportReader.class);
  195. ScmInfoRepositoryImpl underTest = new ScmInfoRepositoryImpl(batchReportReader, analysisMetadata, dbLoader, diff, fileStatuses);
  196. assertThat(underTest.getScmInfo(component)).isEmpty();
  197. verifyNoInteractions(batchReportReader, dbLoader);
  198. }
  199. @DataProvider
  200. public static Object[][] allTypeComponentButFile() {
  201. Object[][] res = new Object[Component.Type.values().length - 1][1];
  202. int i = 0;
  203. for (Component.Type type : EnumSet.complementOf(EnumSet.of(Component.Type.FILE))) {
  204. if (type.isReportType()) {
  205. res[i][0] = ReportComponent.builder(type, i).build();
  206. } else {
  207. res[i][0] = ViewsComponent.builder(type, i).build();
  208. }
  209. i++;
  210. }
  211. return res;
  212. }
  213. private void assertChangeset(Changeset changeset, String revision, String author, long date) {
  214. assertThat(changeset.getAuthor()).isEqualTo(author);
  215. assertThat(changeset.getRevision()).isEqualTo(revision);
  216. assertThat(changeset.getDate()).isEqualTo(date);
  217. }
  218. private void addChangesetInReport(String author, Long date, String revision) {
  219. addChangesetInReport(author, date, revision, false);
  220. }
  221. private void addChangesetInReport(String author, Long date, String revision, boolean copyFromPrevious) {
  222. reportReader.putChangesets(ScannerReport.Changesets.newBuilder()
  223. .setComponentRef(FILE_REF)
  224. .setCopyFromPrevious(copyFromPrevious)
  225. .addChangeset(ScannerReport.Changesets.Changeset.newBuilder()
  226. .setAuthor(author)
  227. .setDate(date)
  228. .setRevision(revision)
  229. .build())
  230. .addChangesetIndexByLine(0)
  231. .build());
  232. }
  233. private void addCopyFromPrevious() {
  234. reportReader.putChangesets(Changesets.newBuilder().setComponentRef(FILE_REF).setCopyFromPrevious(true).build());
  235. }
  236. private DbScmInfo createDbScmInfoWithOneLine() {
  237. Line line1 = Line.newBuilder().setLine(1)
  238. .setScmRevision("rev1")
  239. .setScmAuthor("author1")
  240. .setScmDate(10L)
  241. .build();
  242. DbScmInfo scmInfo = DbScmInfo.create(Collections.singletonList(line1), 1, "hash1").get();
  243. when(dbLoader.getScmInfo(FILE)).thenReturn(Optional.of(scmInfo));
  244. return scmInfo;
  245. }
  246. private void addFileSourceInReport(int lineCount) {
  247. reportReader.putFileSourceLines(FILE_REF, generateLines(lineCount));
  248. reportReader.putComponent(ScannerReport.Component.newBuilder()
  249. .setRef(FILE_REF)
  250. .setLines(lineCount)
  251. .build());
  252. }
  253. private static List<String> generateLines(int lineCount) {
  254. ImmutableList.Builder<String> builder = ImmutableList.builder();
  255. for (int i = 0; i < lineCount; i++) {
  256. builder.add("line " + i);
  257. }
  258. return builder.build();
  259. }
  260. }