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.

NewLinesRepositoryTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.source;
  21. import java.util.Arrays;
  22. import java.util.Optional;
  23. import java.util.Set;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  27. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  28. import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
  29. import org.sonar.ce.task.projectanalysis.component.Component;
  30. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  31. import org.sonar.ce.task.projectanalysis.period.Period;
  32. import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
  33. import org.sonar.ce.task.projectanalysis.scm.Changeset;
  34. import org.sonar.ce.task.projectanalysis.scm.ScmInfoRepositoryRule;
  35. import org.sonar.db.component.BranchType;
  36. import org.sonar.db.newcodeperiod.NewCodePeriodType;
  37. import org.sonar.scanner.protocol.output.ScannerReport;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.mockito.Mockito.mock;
  40. import static org.mockito.Mockito.when;
  41. public class NewLinesRepositoryTest {
  42. private final static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1).build();
  43. @Rule
  44. public BatchReportReaderRule reader = new BatchReportReaderRule();
  45. @Rule
  46. public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
  47. @Rule
  48. public PeriodHolderRule periodHolder = new PeriodHolderRule();
  49. @Rule
  50. public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
  51. private final NewLinesRepository repository = new NewLinesRepository(reader, analysisMetadataHolder, periodHolder, scmInfoRepository);
  52. @Test
  53. public void load_new_lines_from_report_when_available_and_pullrequest() {
  54. setPullRequest();
  55. createChangedLinesInReport(1, 2, 5);
  56. Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
  57. assertThat(newLines).isPresent();
  58. assertThat(newLines.get()).containsOnly(1, 2, 5);
  59. assertThat(repository.newLinesAvailable()).isTrue();
  60. }
  61. @Test
  62. public void load_new_lines_from_report_when_available_and_using_reference_branch() {
  63. periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), null, null));
  64. createChangedLinesInReport(1, 2, 5);
  65. Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
  66. assertThat(newLines).isPresent();
  67. assertThat(newLines.get()).containsOnly(1, 2, 5);
  68. assertThat(repository.newLinesAvailable()).isTrue();
  69. }
  70. @Test
  71. public void compute_new_lines_using_scm_info_for_period() {
  72. periodHolder.setPeriod(new Period("", null, 1000L));
  73. scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
  74. Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
  75. assertThat(newLines).isPresent();
  76. assertThat(newLines.get()).containsOnly(1);
  77. assertThat(repository.newLinesAvailable()).isTrue();
  78. }
  79. @Test
  80. public void compute_new_lines_using_scm_info_for_pullrequest() {
  81. periodHolder.setPeriod(null);
  82. setPullRequest();
  83. analysisMetadataHolder.setAnalysisDate(1000L);
  84. scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
  85. Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
  86. assertThat(newLines).isPresent();
  87. assertThat(newLines.get()).containsOnly(1, 3);
  88. assertThat(repository.newLinesAvailable()).isTrue();
  89. }
  90. @Test
  91. public void return_empty_if_no_period_no_pullrequest_and_no_reference_branch() {
  92. periodHolder.setPeriod(null);
  93. // even though we have lines in the report and scm data, nothing should be returned since we have no period
  94. createChangedLinesInReport(1, 2, 5);
  95. scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
  96. Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
  97. assertThat(newLines).isNotPresent();
  98. assertThat(repository.newLinesAvailable()).isFalse();
  99. }
  100. @Test
  101. public void return_empty_if_no_report_and_no_scm_info() {
  102. periodHolder.setPeriod(new Period("", null, 1000L));
  103. Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
  104. assertThat(newLines).isNotPresent();
  105. assertThat(repository.newLinesAvailable()).isTrue();
  106. }
  107. private void setPullRequest() {
  108. Branch branch = mock(Branch.class);
  109. when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
  110. analysisMetadataHolder.setBranch(branch);
  111. }
  112. private Changeset[] createChangesets(Long... dates) {
  113. return Arrays.stream(dates)
  114. .map(l -> Changeset.newChangesetBuilder().setDate(l).build())
  115. .toArray(Changeset[]::new);
  116. }
  117. private void createChangedLinesInReport(Integer... lines) {
  118. ScannerReport.ChangedLines changedLines = ScannerReport.ChangedLines.newBuilder()
  119. .addAllLine(Arrays.asList(lines))
  120. .build();
  121. reader.putChangedLines(FILE.getReportAttributes().getRef(), changedLines);
  122. }
  123. }