]> source.dussan.org Git - sonarqube.git/blob
dc3b856211e5d4d8aef0e6b1de560b120950ec8b
[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.step;
21
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;
40
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44
45 public class PullRequestFixedIssuesMeasureStepTest {
46
47   private static final int ROOT_REF = 1;
48
49   @Rule
50   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
51   @Rule
52   public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
53   @Rule
54   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
55   private final PullRequestFixedIssueRepository pullRequestFixedIssueRepository = mock(PullRequestFixedIssueRepository.class);
56   private final AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
57
58   private final PullRequestFixedIssuesMeasureStep underTest = new PullRequestFixedIssuesMeasureStep(treeRootHolder, metricRepository,
59     measureRepository, pullRequestFixedIssueRepository, analysisMetadataHolder);
60
61   @Before
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);
65   }
66
67   @Test
68   public void execute_whenComponentIsPullRequest_shouldCreateMeasure() {
69     when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
70     when(pullRequestFixedIssueRepository.getFixedIssues()).thenReturn(List.of(new DefaultIssue(), new DefaultIssue()));
71
72     underTest.execute(new TestComputationStepContext());
73
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);
77   }
78
79   @Test
80   public void execute_whenComponentIsNotPullRequest_shouldNotCreateMeasure() {
81     when(analysisMetadataHolder.isPullRequest()).thenReturn(false);
82
83     underTest.execute(new TestComputationStepContext());
84
85     assertThat(measureRepository.getAddedRawMeasures(ROOT_REF)).isEmpty();
86   }
87
88   @Test
89   public void execute_whenNoFixedIssues_shouldCreateMeasureWithValueZero() {
90     when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
91     when(pullRequestFixedIssueRepository.getFixedIssues()).thenReturn(Collections.emptyList());
92
93     underTest.execute(new TestComputationStepContext());
94
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);
98   }
99 }