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