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.step;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Optional;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.measures.CoreMetrics;
29 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
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.component.TreeRootHolderRule;
33 import org.sonar.ce.task.projectanalysis.issue.fixedissues.PullRequestFixedIssueRepository;
34 import org.sonar.ce.task.projectanalysis.measure.Measure;
35 import org.sonar.ce.task.projectanalysis.measure.MeasureAssert;
36 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
37 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
38 import org.sonar.ce.task.step.TestComputationStepContext;
39 import org.sonar.core.issue.DefaultIssue;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
45 public class PullRequestFixedIssuesMeasureStepTest {
47 private static final int ROOT_REF = 1;
50 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
52 public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
54 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
55 private final PullRequestFixedIssueRepository pullRequestFixedIssueRepository = mock(PullRequestFixedIssueRepository.class);
56 private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
58 private final PullRequestFixedIssuesMeasureStep underTest = new PullRequestFixedIssuesMeasureStep(treeRootHolder, metricRepository,
59 measureRepository, pullRequestFixedIssueRepository, analysisMetadataHolder);
62 public void setUp() throws Exception {
63 treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, ROOT_REF).build());
64 metricRepository.add(CoreMetrics.PULL_REQUEST_FIXED_ISSUES);
68 public void execute_whenComponentIsPullRequest_shouldCreateMeasure() {
69 when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
70 when(pullRequestFixedIssueRepository.getFixedIssues()).thenReturn(List.of(new DefaultIssue(), new DefaultIssue()));
72 underTest.execute(new TestComputationStepContext());
74 assertThat(measureRepository.getAddedRawMeasures(ROOT_REF)).hasSize(1);
75 Optional<Measure> addedRawMeasure = measureRepository.getAddedRawMeasure(ROOT_REF, CoreMetrics.PULL_REQUEST_FIXED_ISSUES_KEY);
76 MeasureAssert.assertThat(addedRawMeasure).hasValue(2);
80 public void execute_whenComponentIsNotPullRequest_shouldNotCreateMeasure() {
81 when(analysisMetadataHolder.isPullRequest()).thenReturn(false);
83 underTest.execute(new TestComputationStepContext());
85 assertThat(measureRepository.getAddedRawMeasures(ROOT_REF)).isEmpty();
89 public void execute_whenNoFixedIssues_shouldCreateMeasureWithValueZero() {
90 when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
91 when(pullRequestFixedIssueRepository.getFixedIssues()).thenReturn(Collections.emptyList());
93 underTest.execute(new TestComputationStepContext());
95 assertThat(measureRepository.getAddedRawMeasures(ROOT_REF)).hasSize(1);
96 Optional<Measure> addedRawMeasure = measureRepository.getAddedRawMeasure(ROOT_REF, CoreMetrics.PULL_REQUEST_FIXED_ISSUES_KEY);
97 MeasureAssert.assertThat(addedRawMeasure).hasValue(0);