]> source.dussan.org Git - sonarqube.git/blob
09da1b863daef39b3c584b78355a31aada140a6d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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 org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.measures.CoreMetrics;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
27 import org.sonar.ce.task.projectanalysis.measure.Measure;
28 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
29 import org.sonar.ce.task.projectanalysis.metric.Metric;
30 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
31 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
32 import org.sonar.ce.task.projectanalysis.qualitygate.EvaluationResult;
33
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.sonar.api.measures.CoreMetrics.NEW_BUGS_KEY;
37 import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY;
38 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
39 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
40 import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.ERROR;
41 import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.OK;
42
43 public class SmallChangesetQualityGateSpecialCaseTest {
44
45   public static final int PROJECT_REF = 1234;
46   @Rule
47   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
48   @Rule
49   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
50     .add(CoreMetrics.NEW_LINES)
51     .add(CoreMetrics.NEW_COVERAGE)
52     .add(CoreMetrics.NEW_BUGS);
53   @Rule
54   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
55   private final SmallChangesetQualityGateSpecialCase underTest = new SmallChangesetQualityGateSpecialCase(measureRepository, metricRepository);
56
57   @Test
58   public void ignore_errors_about_new_coverage_for_small_changesets() {
59     QualityGateMeasuresStep.MetricEvaluationResult metricEvaluationResult = generateEvaluationResult(NEW_COVERAGE_KEY, ERROR);
60     Component project = generateNewRootProject();
61     measureRepository.addRawMeasure(PROJECT_REF, CoreMetrics.NEW_LINES_KEY, newMeasureBuilder().setVariation(19).create(1000));
62
63     boolean result = underTest.appliesTo(project, metricEvaluationResult);
64
65     assertThat(result).isTrue();
66   }
67
68   @Test
69   public void should_not_change_for_bigger_changesets() {
70     QualityGateMeasuresStep.MetricEvaluationResult metricEvaluationResult = generateEvaluationResult(NEW_COVERAGE_KEY, ERROR);
71     Component project = generateNewRootProject();
72     measureRepository.addRawMeasure(PROJECT_REF, CoreMetrics.NEW_LINES_KEY, newMeasureBuilder().setVariation(20).create(1000));
73
74     boolean result = underTest.appliesTo(project, metricEvaluationResult);
75
76     assertThat(result).isFalse();
77   }
78
79   @Test
80   public void should_not_change_issue_related_metrics() {
81     QualityGateMeasuresStep.MetricEvaluationResult metricEvaluationResult = generateEvaluationResult(NEW_BUGS_KEY, ERROR);
82     Component project = generateNewRootProject();
83     measureRepository.addRawMeasure(PROJECT_REF, CoreMetrics.NEW_LINES_KEY, newMeasureBuilder().setVariation(19).create(1000));
84
85     boolean result = underTest.appliesTo(project, metricEvaluationResult);
86
87     assertThat(result).isFalse();
88   }
89
90   @Test
91   public void should_not_change_green_conditions() {
92     QualityGateMeasuresStep.MetricEvaluationResult metricEvaluationResult = generateEvaluationResult(NEW_BUGS_KEY, OK);
93     Component project = generateNewRootProject();
94     measureRepository.addRawMeasure(PROJECT_REF, CoreMetrics.NEW_LINES_KEY, newMeasureBuilder().setVariation(19).create(1000));
95
96     boolean result = underTest.appliesTo(project, metricEvaluationResult);
97
98     assertThat(result).isFalse();
99   }
100
101   @Test
102   public void should_not_change_quality_gate_if_new_lines_is_not_defined() {
103     QualityGateMeasuresStep.MetricEvaluationResult metricEvaluationResult = generateEvaluationResult(NEW_COVERAGE_KEY, ERROR);
104     Component project = generateNewRootProject();
105
106     boolean result = underTest.appliesTo(project, metricEvaluationResult);
107
108     assertThat(result).isFalse();
109   }
110
111   @Test
112   public void should_silently_ignore_null_values() {
113
114     boolean result = underTest.appliesTo(mock(Component.class), null);
115
116     assertThat(result).isFalse();
117   }
118
119   @Test
120   public void apply() {
121     Comparable<?> value = mock(Comparable.class);
122     Condition condition = mock(Condition.class);
123     QualityGateMeasuresStep.MetricEvaluationResult original = new QualityGateMeasuresStep.MetricEvaluationResult(
124       new EvaluationResult(Measure.Level.ERROR, value), condition);
125
126     QualityGateMeasuresStep.MetricEvaluationResult modified = underTest.apply(original);
127
128     assertThat(modified.evaluationResult.getLevel()).isSameAs(OK);
129     assertThat(modified.evaluationResult.getValue()).isSameAs(value);
130     assertThat(modified.condition).isSameAs(condition);
131   }
132
133   private Component generateNewRootProject() {
134     treeRootHolder.setRoot(builder(Component.Type.PROJECT, PROJECT_REF).build());
135     return treeRootHolder.getRoot();
136   }
137
138   private QualityGateMeasuresStep.MetricEvaluationResult generateEvaluationResult(String metric, Measure.Level level) {
139     Metric newCoverageMetric = metricRepository.getByKey(metric);
140     Condition condition = new Condition(newCoverageMetric, "LT", "80");
141     EvaluationResult evaluationResult = new EvaluationResult(level, mock(Comparable.class));
142     return new QualityGateMeasuresStep.MetricEvaluationResult(evaluationResult, condition);
143   }
144 }