123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.ce.task.projectanalysis.source;
-
- import java.util.Arrays;
- import java.util.Optional;
- import java.util.Set;
- import org.junit.Rule;
- import org.junit.Test;
- import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
- import org.sonar.ce.task.projectanalysis.analysis.Branch;
- import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
- import org.sonar.ce.task.projectanalysis.component.Component;
- import org.sonar.ce.task.projectanalysis.component.ReportComponent;
- import org.sonar.ce.task.projectanalysis.period.Period;
- import org.sonar.ce.task.projectanalysis.period.PeriodHolderRule;
- import org.sonar.ce.task.projectanalysis.scm.Changeset;
- import org.sonar.ce.task.projectanalysis.scm.ScmInfoRepositoryRule;
- import org.sonar.db.component.BranchType;
- import org.sonar.db.newcodeperiod.NewCodePeriodType;
- import org.sonar.scanner.protocol.output.ScannerReport;
-
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.when;
-
- public class NewLinesRepositoryTest {
- private final static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1).build();
-
- @Rule
- public BatchReportReaderRule reader = new BatchReportReaderRule();
- @Rule
- public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
- @Rule
- public PeriodHolderRule periodHolder = new PeriodHolderRule();
- @Rule
- public ScmInfoRepositoryRule scmInfoRepository = new ScmInfoRepositoryRule();
-
- private final NewLinesRepository repository = new NewLinesRepository(reader, analysisMetadataHolder, periodHolder, scmInfoRepository);
-
- @Test
- public void load_new_lines_from_report_when_available_and_pullrequest() {
- setPullRequest();
- createChangedLinesInReport(1, 2, 5);
-
- Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
-
- assertThat(newLines).isPresent();
- assertThat(newLines.get()).containsOnly(1, 2, 5);
- assertThat(repository.newLinesAvailable()).isTrue();
- }
-
- @Test
- public void load_new_lines_from_report_when_available_and_using_reference_branch() {
- periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), null, null));
- createChangedLinesInReport(1, 2, 5);
-
- Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
-
- assertThat(newLines).isPresent();
- assertThat(newLines.get()).containsOnly(1, 2, 5);
- assertThat(repository.newLinesAvailable()).isTrue();
- }
-
- @Test
- public void compute_new_lines_using_scm_info_for_period() {
- periodHolder.setPeriod(new Period("", null, 1000L));
- scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
-
- Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
-
- assertThat(newLines).isPresent();
- assertThat(newLines.get()).containsOnly(1);
- assertThat(repository.newLinesAvailable()).isTrue();
- }
-
- @Test
- public void compute_new_lines_using_scm_info_for_pullrequest() {
- periodHolder.setPeriod(null);
- setPullRequest();
- analysisMetadataHolder.setAnalysisDate(1000L);
- scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
-
- Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
-
- assertThat(newLines).isPresent();
- assertThat(newLines.get()).containsOnly(1, 3);
- assertThat(repository.newLinesAvailable()).isTrue();
- }
-
- @Test
- public void return_empty_if_no_period_no_pullrequest_and_no_reference_branch() {
- periodHolder.setPeriod(null);
-
- // even though we have lines in the report and scm data, nothing should be returned since we have no period
- createChangedLinesInReport(1, 2, 5);
- scmInfoRepository.setScmInfo(FILE.getReportAttributes().getRef(), createChangesets(1100L, 900L, 1000L, 800L));
-
- Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
-
- assertThat(newLines).isNotPresent();
- assertThat(repository.newLinesAvailable()).isFalse();
- }
-
- @Test
- public void return_empty_if_no_report_and_no_scm_info() {
- periodHolder.setPeriod(new Period("", null, 1000L));
-
- Optional<Set<Integer>> newLines = repository.getNewLines(FILE);
-
- assertThat(newLines).isNotPresent();
- assertThat(repository.newLinesAvailable()).isTrue();
- }
-
- private void setPullRequest() {
- Branch branch = mock(Branch.class);
- when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
- analysisMetadataHolder.setBranch(branch);
- }
-
- private Changeset[] createChangesets(Long... dates) {
- return Arrays.stream(dates)
- .map(l -> Changeset.newChangesetBuilder().setDate(l).build())
- .toArray(Changeset[]::new);
- }
-
- private void createChangedLinesInReport(Integer... lines) {
- ScannerReport.ChangedLines changedLines = ScannerReport.ChangedLines.newBuilder()
- .addAllLine(Arrays.asList(lines))
- .build();
- reader.putChangedLines(FILE.getReportAttributes().getRef(), changedLines);
- }
- }
|