]> source.dussan.org Git - sonarqube.git/blob
a0a33ca30be419a9e6cdd3acbf0d9ee6a6de53ce
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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
22 import java.util.Arrays;
23 import java.util.Optional;
24 import java.util.Set;
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;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43
44 public class NewLinesRepositoryTest {
45   private final static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1).build();
46
47   @Rule
48   public BatchReportReaderRule reader = new BatchReportReaderRule();
49   @Rule
50   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
51   @Rule
52   public PeriodHolderRule periodHolder = new PeriodHolderRule();
53   @Rule
54   public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
55
56   private final NewLinesRepository repository = new NewLinesRepository(reader, analysisMetadataHolder, periodHolder, scmInfoRepository);
57
58   @Test
59   public void load_new_lines_from_report_when_available_and_pullrequest() {
60     setPullRequest();
61     createChangedLinesInReport(1, 2, 5);
62
63     Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
64
65     assertThat(newLines).isPresent();
66     assertThat(newLines.get()).containsOnly(1, 2, 5);
67     assertThat(repository.newLinesAvailable()).isTrue();
68   }
69
70   @Test
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);
74
75     Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
76
77     assertThat(newLines).isPresent();
78     assertThat(newLines.get()).containsOnly(1, 2, 5);
79     assertThat(repository.newLinesAvailable()).isTrue();
80   }
81
82   @Test
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));
86
87     Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
88
89     assertThat(newLines).isPresent();
90     assertThat(newLines.get()).containsOnly(1);
91     assertThat(repository.newLinesAvailable()).isTrue();
92   }
93
94   @Test
95   public void compute_new_lines_using_scm_info_for_pullrequest() {
96     periodHolder.setPeriod(null);
97     setPullRequest();
98     analysisMetadataHolder.setAnalysisDate(1000L);
99     scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
100
101     Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
102
103     assertThat(newLines).isPresent();
104     assertThat(newLines.get()).containsOnly(1, 3);
105     assertThat(repository.newLinesAvailable()).isTrue();
106   }
107
108   @Test
109   public void return_empty_if_no_period_no_pullrequest_and_no_reference_branch() {
110     periodHolder.setPeriod(null);
111
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));
115
116     Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
117
118     assertThat(newLines).isNotPresent();
119     assertThat(repository.newLinesAvailable()).isFalse();
120   }
121
122   @Test
123   public void return_empty_if_no_report_and_no_scm_info() {
124     periodHolder.setPeriod(new Period("", null, 1000L));
125
126     Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
127
128     assertThat(newLines).isNotPresent();
129     assertThat(repository.newLinesAvailable()).isTrue();
130   }
131
132   private void setPullRequest() {
133     Branch branch = mock(Branch.class);
134     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
135     analysisMetadataHolder.setBranch(branch);
136   }
137
138   private Changeset[] createChangesets(Long... dates) {
139     return Arrays.stream(dates)
140       .map(l -> Changeset.newChangesetBuilder().setDate(l).build())
141       .toArray(Changeset[]::new);
142   }
143
144   private void createChangedLinesInReport(Integer... lines) {
145     ScannerReport.ChangedLines changedLines = ScannerReport.ChangedLines.newBuilder()
146       .addAllLine(Arrays.asList(lines))
147       .build();
148     reader.putChangedLines(FILE.getReportAttributes().getRef(), changedLines);
149   }
150 }