3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.source;
22 import java.util.Arrays;
23 import java.util.Optional;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
28 import org.sonar.ce.task.projectanalysis.analysis.Branch;
29 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
30 import org.sonar.ce.task.projectanalysis.component.Component;
31 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
32 import org.sonar.ce.task.projectanalysis.period.Period;
33 import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
34 import org.sonar.ce.task.projectanalysis.scm.Changeset;
35 import org.sonar.ce.task.projectanalysis.scm.ScmInfoRepositoryRule;
36 import org.sonar.db.component.BranchType;
37 import org.sonar.db.newcodeperiod.NewCodePeriodType;
38 import org.sonar.scanner.protocol.output.ScannerReport;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
44 public class NewLinesRepositoryTest {
45 private final static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1).build();
48 public BatchReportReaderRule reader = new BatchReportReaderRule();
50 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
52 public PeriodHolderRule periodHolder = new PeriodHolderRule();
54 public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
56 private final NewLinesRepository repository = new NewLinesRepository(reader, analysisMetadataHolder, periodHolder, scmInfoRepository);
59 public void load_new_lines_from_report_when_available_and_pullrequest() {
61 createChangedLinesInReport(1, 2, 5);
63 Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
65 assertThat(newLines).isPresent();
66 assertThat(newLines.get()).containsOnly(1, 2, 5);
67 assertThat(repository.newLinesAvailable()).isTrue();
71 public void load_new_lines_from_report_when_available_and_using_reference_branch() {
72 periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), null, null));
73 createChangedLinesInReport(1, 2, 5);
75 Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
77 assertThat(newLines).isPresent();
78 assertThat(newLines.get()).containsOnly(1, 2, 5);
79 assertThat(repository.newLinesAvailable()).isTrue();
83 public void compute_new_lines_using_scm_info_for_period() {
84 periodHolder.setPeriod(new Period("", null, 1000L));
85 scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
87 Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
89 assertThat(newLines).isPresent();
90 assertThat(newLines.get()).containsOnly(1);
91 assertThat(repository.newLinesAvailable()).isTrue();
95 public void compute_new_lines_using_scm_info_for_pullrequest() {
96 periodHolder.setPeriod(null);
98 analysisMetadataHolder.setAnalysisDate(1000L);
99 scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
101 Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
103 assertThat(newLines).isPresent();
104 assertThat(newLines.get()).containsOnly(1, 3);
105 assertThat(repository.newLinesAvailable()).isTrue();
109 public void return_empty_if_no_period_no_pullrequest_and_no_reference_branch() {
110 periodHolder.setPeriod(null);
112 // even though we have lines in the report and scm data, nothing should be returned since we have no period
113 createChangedLinesInReport(1, 2, 5);
114 scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
116 Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
118 assertThat(newLines).isNotPresent();
119 assertThat(repository.newLinesAvailable()).isFalse();
123 public void return_empty_if_no_report_and_no_scm_info() {
124 periodHolder.setPeriod(new Period("", null, 1000L));
126 Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
128 assertThat(newLines).isNotPresent();
129 assertThat(repository.newLinesAvailable()).isTrue();
132 private void setPullRequest() {
133 Branch branch = mock(Branch.class);
134 when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
135 analysisMetadataHolder.setBranch(branch);
138 private Changeset[] createChangesets(Long... dates) {
139 return Arrays.stream(dates)
140 .map(l -> Changeset.newChangesetBuilder().setDate(l).build())
141 .toArray(Changeset[]::new);
144 private void createChangedLinesInReport(Integer... lines) {
145 ScannerReport.ChangedLines changedLines = ScannerReport.ChangedLines.newBuilder()
146 .addAllLine(Arrays.asList(lines))
148 reader.putChangedLines(FILE.getReportAttributes().getRef(), changedLines);