]> source.dussan.org Git - sonarqube.git/blob
559264afc618e0d3601b827155c5c7ef5a7cd2f1
[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 java.util.Collections;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Optional;
26 import javax.annotation.Nullable;
27 import org.assertj.core.api.AbstractAssert;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34 import org.sonar.api.measures.CoreMetrics;
35 import org.sonar.ce.task.projectanalysis.component.Component;
36 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
37 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
38 import org.sonar.ce.task.projectanalysis.measure.Measure;
39 import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
40 import org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition;
41 import org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData;
42 import org.sonar.ce.task.projectanalysis.metric.Metric;
43 import org.sonar.ce.task.projectanalysis.metric.MetricImpl;
44 import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
45 import org.sonar.ce.task.projectanalysis.qualitygate.Condition;
46 import org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus;
47 import org.sonar.ce.task.projectanalysis.qualitygate.EvaluationResult;
48 import org.sonar.ce.task.projectanalysis.qualitygate.EvaluationResultTextConverter;
49 import org.sonar.ce.task.projectanalysis.qualitygate.MutableQualityGateStatusHolderRule;
50 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGate;
51 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateHolderRule;
52 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateStatus;
53 import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateStatusHolder;
54 import org.sonar.ce.task.step.TestComputationStepContext;
55
56 import static com.google.common.collect.ImmutableList.of;
57 import static org.mockito.ArgumentMatchers.any;
58 import static org.mockito.Mockito.mock;
59 import static org.mockito.Mockito.when;
60 import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
61 import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.ERROR;
62 import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.OK;
63 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
64 import static org.sonar.ce.task.projectanalysis.measure.MeasureAssert.assertThat;
65
66 public class QualityGateMeasuresStepTest {
67   private static final MetricImpl INT_METRIC_1 = createIntMetric(1);
68   private static final String INT_METRIC_1_KEY = INT_METRIC_1.getKey();
69   private static final MetricImpl INT_METRIC_2 = createIntMetric(2);
70   private static final String INT_METRIC_2_KEY = INT_METRIC_2.getKey();
71
72   private static final int PROJECT_REF = 1;
73   private static final ReportComponent PROJECT_COMPONENT = ReportComponent.builder(Component.Type.PROJECT, PROJECT_REF).build();
74   private static final String SOME_QG_UUID = "7521551";
75   private static final String SOME_QG_NAME = "name";
76
77   @Rule
78   public ExpectedException expectedException = ExpectedException.none();
79   @Rule
80   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
81   @Rule
82   public QualityGateHolderRule qualityGateHolder = new QualityGateHolderRule();
83   @Rule
84   public MutableQualityGateStatusHolderRule qualityGateStatusHolder = new MutableQualityGateStatusHolderRule();
85   @Rule
86   public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
87   @Rule
88   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
89
90   private EvaluationResultTextConverter resultTextConverter = mock(EvaluationResultTextConverter.class);
91   private QualityGateMeasuresStep underTest = new QualityGateMeasuresStep(treeRootHolder, qualityGateHolder, qualityGateStatusHolder, measureRepository, metricRepository,
92     resultTextConverter, new SmallChangesetQualityGateSpecialCase(measureRepository, metricRepository));
93
94   @Before
95   public void setUp() {
96     metricRepository
97       .add(CoreMetrics.ALERT_STATUS)
98       .add(CoreMetrics.QUALITY_GATE_DETAILS)
99       .add(INT_METRIC_1)
100       .add(INT_METRIC_2);
101     treeRootHolder.setRoot(PROJECT_COMPONENT);
102
103     // mock response of asText to any argument to return the result of dumbResultTextAnswer method
104     when(resultTextConverter.asText(any(Condition.class), any(EvaluationResult.class))).thenAnswer(new Answer<String>() {
105       @Override
106       public String answer(InvocationOnMock invocation) {
107         Condition condition = (Condition) invocation.getArguments()[0];
108         EvaluationResult evaluationResult = (EvaluationResult) invocation.getArguments()[1];
109         return dumbResultTextAnswer(condition, evaluationResult.getLevel(), evaluationResult.getValue());
110       }
111     });
112   }
113
114   private static String dumbResultTextAnswer(Condition condition, Measure.Level level, Object value) {
115     return condition.toString() + level + value;
116   }
117
118   @Test
119   public void no_measure_if_tree_has_no_project() {
120     ReportComponent notAProjectComponent = ReportComponent.builder(Component.Type.DIRECTORY, 1).build();
121
122     treeRootHolder.setRoot(notAProjectComponent);
123
124     underTest.execute(new TestComputationStepContext());
125
126     measureRepository.getAddedRawMeasures(1).isEmpty();
127   }
128
129   @Test
130   public void no_measure_if_there_is_no_qualitygate() {
131     qualityGateHolder.setQualityGate(null);
132
133     underTest.execute(new TestComputationStepContext());
134
135     measureRepository.getAddedRawMeasures(PROJECT_COMPONENT).isEmpty();
136   }
137
138   @Test
139   public void mutableQualityGateStatusHolder_is_not_populated_if_there_is_no_qualitygate() {
140     qualityGateHolder.setQualityGate(null);
141
142     underTest.execute(new TestComputationStepContext());
143
144     expectedException.expect(IllegalStateException.class);
145     expectedException.expectMessage("Quality gate status has not been set yet");
146
147     qualityGateStatusHolder.getStatus();
148   }
149
150   @Test
151   public void new_measures_are_created_even_if_there_is_no_rawMeasure_for_metric_of_condition() {
152     Condition equals2Condition = createLessThanCondition(INT_METRIC_1, "2");
153     qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_UUID, SOME_QG_NAME, of(equals2Condition)));
154
155     underTest.execute(new TestComputationStepContext());
156
157     Optional<Measure> addedRawMeasure = measureRepository.getAddedRawMeasure(PROJECT_COMPONENT, INT_METRIC_1_KEY);
158
159     assertThat(addedRawMeasure).isAbsent();
160     assertThat(getAlertStatusMeasure())
161       .hasQualityGateLevel(OK)
162       .hasQualityGateText("");
163     assertThat(getQGDetailsMeasure())
164       .hasValue(new QualityGateDetailsData(OK, Collections.emptyList(), false).toJson());
165
166     QualityGateStatusHolderAssertions.assertThat(qualityGateStatusHolder)
167       .hasStatus(QualityGateStatus.OK)
168       .hasConditionCount(1)
169       .hasCondition(equals2Condition, ConditionStatus.EvaluationStatus.NO_VALUE, null);
170   }
171
172   @Test
173   public void rawMeasure_is_updated_if_present_and_new_measures_are_created_if_project_has_measure_for_metric_of_condition() {
174     int rawValue = 3;
175     Condition equals2Condition = createLessThanCondition(INT_METRIC_1, "2");
176     Measure rawMeasure = newMeasureBuilder().create(rawValue, null);
177
178     qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_UUID, SOME_QG_NAME, of(equals2Condition)));
179     measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, rawMeasure);
180
181     underTest.execute(new TestComputationStepContext());
182
183     Optional<Measure> addedRawMeasure = measureRepository.getAddedRawMeasure(PROJECT_COMPONENT, INT_METRIC_1_KEY);
184
185     assertThat(addedRawMeasure)
186       .hasQualityGateLevel(OK)
187       .hasQualityGateText(dumbResultTextAnswer(equals2Condition, OK, rawValue));
188     assertThat(getAlertStatusMeasure())
189       .hasQualityGateLevel(OK)
190       .hasQualityGateText(dumbResultTextAnswer(equals2Condition, OK, rawValue));
191     assertThat(getQGDetailsMeasure().get())
192       .hasValue(new QualityGateDetailsData(OK, of(new EvaluatedCondition(equals2Condition, OK, rawValue)), false).toJson());
193
194     QualityGateStatusHolderAssertions.assertThat(qualityGateStatusHolder)
195       .hasStatus(QualityGateStatus.OK)
196       .hasConditionCount(1)
197       .hasCondition(equals2Condition, ConditionStatus.EvaluationStatus.OK, String.valueOf(rawValue));
198   }
199
200   @Test
201   public void new_measures_have_ERROR_level_if_at_least_one_updated_measure_has_ERROR_level() {
202     int rawValue = 3;
203     Condition equalsOneErrorCondition = createLessThanCondition(INT_METRIC_1, "4");
204     Condition equalsOneOkCondition = createLessThanCondition(INT_METRIC_2, "2");
205     Measure rawMeasure = newMeasureBuilder().create(rawValue, null);
206
207     qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_UUID, SOME_QG_NAME, of(equalsOneErrorCondition, equalsOneOkCondition)));
208     measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, rawMeasure);
209     measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_2_KEY, rawMeasure);
210
211     underTest.execute(new TestComputationStepContext());
212
213     Optional<Measure> rawMeasure1 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_1_KEY);
214     Optional<Measure> rawMeasure2 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_2_KEY);
215
216     assertThat(rawMeasure1.get())
217       .hasQualityGateLevel(ERROR)
218       .hasQualityGateText(dumbResultTextAnswer(equalsOneErrorCondition, ERROR, rawValue));
219     assertThat(rawMeasure2.get())
220       .hasQualityGateLevel(OK)
221       .hasQualityGateText(dumbResultTextAnswer(equalsOneOkCondition, OK, rawValue));
222     assertThat(getAlertStatusMeasure())
223       .hasQualityGateLevel(ERROR)
224       .hasQualityGateText(dumbResultTextAnswer(equalsOneErrorCondition, ERROR, rawValue) + ", "
225         + dumbResultTextAnswer(equalsOneOkCondition, OK, rawValue));
226     assertThat(getQGDetailsMeasure())
227       .hasValue(new QualityGateDetailsData(ERROR, of(
228         new EvaluatedCondition(equalsOneErrorCondition, ERROR, rawValue),
229         new EvaluatedCondition(equalsOneOkCondition, OK, rawValue)), false).toJson());
230
231     QualityGateStatusHolderAssertions.assertThat(qualityGateStatusHolder)
232       .hasStatus(QualityGateStatus.ERROR)
233       .hasConditionCount(2)
234       .hasCondition(equalsOneErrorCondition, ConditionStatus.EvaluationStatus.ERROR, String.valueOf(rawValue))
235       .hasCondition(equalsOneOkCondition, ConditionStatus.EvaluationStatus.OK, String.valueOf(rawValue));
236   }
237
238   @Test
239   public void new_measure_has_ERROR_level_of_all_conditions_for_a_specific_metric_if_its_the_worst() {
240     int rawValue = 3;
241     Condition fixedCondition = createLessThanCondition(INT_METRIC_1, "4");
242     Condition periodCondition = createLessThanCondition(INT_METRIC_1, "2");
243
244     qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_UUID, SOME_QG_NAME, of(fixedCondition, periodCondition)));
245     Measure measure = newMeasureBuilder().create(rawValue, null);
246     measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, measure);
247
248     underTest.execute(new TestComputationStepContext());
249
250     Optional<Measure> rawMeasure1 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_1_KEY);
251     assertThat(rawMeasure1.get())
252       .hasQualityGateLevel(ERROR)
253       .hasQualityGateText(dumbResultTextAnswer(fixedCondition, ERROR, rawValue));
254   }
255
256   @Test
257   public void new_measure_has_condition_on_leak_period_when_all_conditions_on_specific_metric_has_same_QG_level() {
258     int rawValue = 0;
259     Condition fixedCondition = createLessThanCondition(INT_METRIC_1, "1");
260     Condition periodCondition = createLessThanCondition(INT_METRIC_1, "1");
261
262     qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_UUID, SOME_QG_NAME, of(fixedCondition, periodCondition)));
263     Measure measure = newMeasureBuilder()
264       .setVariation(rawValue)
265       .create(rawValue, null);
266     measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, measure);
267
268     underTest.execute(new TestComputationStepContext());
269
270     Optional<Measure> rawMeasure1 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_1_KEY);
271     assertThat(rawMeasure1.get())
272       .hasQualityGateLevel(ERROR)
273       .hasQualityGateText(dumbResultTextAnswer(periodCondition, ERROR, rawValue));
274   }
275
276   private Measure getAlertStatusMeasure() {
277     return measureRepository.getAddedRawMeasure(PROJECT_REF, ALERT_STATUS_KEY).get();
278   }
279
280   private Optional<Measure> getQGDetailsMeasure() {
281     return measureRepository.getAddedRawMeasure(PROJECT_REF, CoreMetrics.QUALITY_GATE_DETAILS_KEY);
282   }
283
284   private static Condition createLessThanCondition(Metric metric, String errorThreshold) {
285     return new Condition(metric, Condition.Operator.LESS_THAN.getDbValue(), errorThreshold);
286   }
287
288   private static MetricImpl createIntMetric(int index) {
289     return new MetricImpl("uuid" + index, "metricKey" + index, "metricName" + index, Metric.MetricType.INT);
290   }
291
292   private static class QualityGateStatusHolderAssertions extends AbstractAssert<QualityGateStatusHolderAssertions, QualityGateStatusHolder> {
293
294     private QualityGateStatusHolderAssertions(QualityGateStatusHolder actual) {
295       super(actual, QualityGateStatusHolderAssertions.class);
296     }
297
298     public static QualityGateStatusHolderAssertions assertThat(QualityGateStatusHolder holder) {
299       return new QualityGateStatusHolderAssertions(holder);
300     }
301
302     public QualityGateStatusHolderAssertions hasStatus(QualityGateStatus status) {
303       if (actual.getStatus() != status) {
304         failWithMessage(
305           "Expected QualityGateStatusHolder to have global status <%s> but was <%s>",
306           status, actual.getStatus());
307       }
308
309       return this;
310     }
311
312     public QualityGateStatusHolderAssertions hasConditionCount(int count) {
313       int conditionCount = actual.getStatusPerConditions().size();
314       if (conditionCount != count) {
315         failWithMessage(
316           "Expected QualityGateStatusHolder to have <%s> conditions but it has <%s>",
317           count, conditionCount);
318       }
319
320       return this;
321     }
322
323     public QualityGateStatusHolderAssertions hasCondition(Condition condition, ConditionStatus.EvaluationStatus evaluationStatus, @Nullable String expectedValue) {
324       for (Map.Entry<Condition, ConditionStatus> entry : actual.getStatusPerConditions().entrySet()) {
325         if (entry.getKey() == condition) {
326           ConditionStatus.EvaluationStatus actualStatus = entry.getValue().getStatus();
327           if (actualStatus != evaluationStatus) {
328             failWithMessage(
329               "Expected Status of condition <%s> in QualityGateStatusHolder to be <%s> but it was <%s>",
330               condition, evaluationStatus, actualStatus);
331           }
332           String actualValue = entry.getValue().getValue();
333           if (!Objects.equals(expectedValue, actualValue)) {
334             failWithMessage(
335               "Expected Value of condition <%s> in QualityGateStatusHolder to be <%s> but it was <%s>",
336               condition, expectedValue, actualValue);
337           }
338           return this;
339         }
340       }
341
342       failWithMessage(
343         "Expected QualityGateStatusHolder to have an entry for <%s> but none was found",
344         condition);
345
346       return this;
347     }
348   }
349
350 }