3 * Copyright (C) 2009-2021 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;
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;
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;
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();
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";
78 public ExpectedException expectedException = ExpectedException.none();
80 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
82 public QualityGateHolderRule qualityGateHolder = new QualityGateHolderRule();
84 public MutableQualityGateStatusHolderRule qualityGateStatusHolder = new MutableQualityGateStatusHolderRule();
86 public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
88 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
90 private EvaluationResultTextConverter resultTextConverter = mock(EvaluationResultTextConverter.class);
91 private QualityGateMeasuresStep underTest = new QualityGateMeasuresStep(treeRootHolder, qualityGateHolder, qualityGateStatusHolder, measureRepository, metricRepository,
92 resultTextConverter, new SmallChangesetQualityGateSpecialCase(measureRepository, metricRepository));
97 .add(CoreMetrics.ALERT_STATUS)
98 .add(CoreMetrics.QUALITY_GATE_DETAILS)
101 treeRootHolder.setRoot(PROJECT_COMPONENT);
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>() {
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());
114 private static String dumbResultTextAnswer(Condition condition, Measure.Level level, Object value) {
115 return condition.toString() + level + value;
119 public void no_measure_if_tree_has_no_project() {
120 ReportComponent notAProjectComponent = ReportComponent.builder(Component.Type.DIRECTORY, 1).build();
122 treeRootHolder.setRoot(notAProjectComponent);
124 underTest.execute(new TestComputationStepContext());
126 measureRepository.getAddedRawMeasures(1).isEmpty();
130 public void no_measure_if_there_is_no_qualitygate() {
131 qualityGateHolder.setQualityGate(null);
133 underTest.execute(new TestComputationStepContext());
135 measureRepository.getAddedRawMeasures(PROJECT_COMPONENT).isEmpty();
139 public void mutableQualityGateStatusHolder_is_not_populated_if_there_is_no_qualitygate() {
140 qualityGateHolder.setQualityGate(null);
142 underTest.execute(new TestComputationStepContext());
144 expectedException.expect(IllegalStateException.class);
145 expectedException.expectMessage("Quality gate status has not been set yet");
147 qualityGateStatusHolder.getStatus();
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)));
155 underTest.execute(new TestComputationStepContext());
157 Optional<Measure> addedRawMeasure = measureRepository.getAddedRawMeasure(PROJECT_COMPONENT, INT_METRIC_1_KEY);
159 assertThat(addedRawMeasure).isAbsent();
160 assertThat(getAlertStatusMeasure())
161 .hasQualityGateLevel(OK)
162 .hasQualityGateText("");
163 assertThat(getQGDetailsMeasure())
164 .hasValue(new QualityGateDetailsData(OK, Collections.emptyList(), false).toJson());
166 QualityGateStatusHolderAssertions.assertThat(qualityGateStatusHolder)
167 .hasStatus(QualityGateStatus.OK)
168 .hasConditionCount(1)
169 .hasCondition(equals2Condition, ConditionStatus.EvaluationStatus.NO_VALUE, null);
173 public void rawMeasure_is_updated_if_present_and_new_measures_are_created_if_project_has_measure_for_metric_of_condition() {
175 Condition equals2Condition = createLessThanCondition(INT_METRIC_1, "2");
176 Measure rawMeasure = newMeasureBuilder().create(rawValue, null);
178 qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_UUID, SOME_QG_NAME, of(equals2Condition)));
179 measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, rawMeasure);
181 underTest.execute(new TestComputationStepContext());
183 Optional<Measure> addedRawMeasure = measureRepository.getAddedRawMeasure(PROJECT_COMPONENT, INT_METRIC_1_KEY);
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());
194 QualityGateStatusHolderAssertions.assertThat(qualityGateStatusHolder)
195 .hasStatus(QualityGateStatus.OK)
196 .hasConditionCount(1)
197 .hasCondition(equals2Condition, ConditionStatus.EvaluationStatus.OK, String.valueOf(rawValue));
201 public void new_measures_have_ERROR_level_if_at_least_one_updated_measure_has_ERROR_level() {
203 Condition equalsOneErrorCondition = createLessThanCondition(INT_METRIC_1, "4");
204 Condition equalsOneOkCondition = createLessThanCondition(INT_METRIC_2, "2");
205 Measure rawMeasure = newMeasureBuilder().create(rawValue, null);
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);
211 underTest.execute(new TestComputationStepContext());
213 Optional<Measure> rawMeasure1 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_1_KEY);
214 Optional<Measure> rawMeasure2 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_2_KEY);
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());
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));
239 public void new_measure_has_ERROR_level_of_all_conditions_for_a_specific_metric_if_its_the_worst() {
241 Condition fixedCondition = createLessThanCondition(INT_METRIC_1, "4");
242 Condition periodCondition = createLessThanCondition(INT_METRIC_1, "2");
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);
248 underTest.execute(new TestComputationStepContext());
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));
257 public void new_measure_has_condition_on_leak_period_when_all_conditions_on_specific_metric_has_same_QG_level() {
259 Condition fixedCondition = createLessThanCondition(INT_METRIC_1, "1");
260 Condition periodCondition = createLessThanCondition(INT_METRIC_1, "1");
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);
268 underTest.execute(new TestComputationStepContext());
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));
276 private Measure getAlertStatusMeasure() {
277 return measureRepository.getAddedRawMeasure(PROJECT_REF, ALERT_STATUS_KEY).get();
280 private Optional<Measure> getQGDetailsMeasure() {
281 return measureRepository.getAddedRawMeasure(PROJECT_REF, CoreMetrics.QUALITY_GATE_DETAILS_KEY);
284 private static Condition createLessThanCondition(Metric metric, String errorThreshold) {
285 return new Condition(metric, Condition.Operator.LESS_THAN.getDbValue(), errorThreshold);
288 private static MetricImpl createIntMetric(int index) {
289 return new MetricImpl("uuid" + index, "metricKey" + index, "metricName" + index, Metric.MetricType.INT);
292 private static class QualityGateStatusHolderAssertions extends AbstractAssert<QualityGateStatusHolderAssertions, QualityGateStatusHolder> {
294 private QualityGateStatusHolderAssertions(QualityGateStatusHolder actual) {
295 super(actual, QualityGateStatusHolderAssertions.class);
298 public static QualityGateStatusHolderAssertions assertThat(QualityGateStatusHolder holder) {
299 return new QualityGateStatusHolderAssertions(holder);
302 public QualityGateStatusHolderAssertions hasStatus(QualityGateStatus status) {
303 if (actual.getStatus() != status) {
305 "Expected QualityGateStatusHolder to have global status <%s> but was <%s>",
306 status, actual.getStatus());
312 public QualityGateStatusHolderAssertions hasConditionCount(int count) {
313 int conditionCount = actual.getStatusPerConditions().size();
314 if (conditionCount != count) {
316 "Expected QualityGateStatusHolder to have <%s> conditions but it has <%s>",
317 count, conditionCount);
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) {
329 "Expected Status of condition <%s> in QualityGateStatusHolder to be <%s> but it was <%s>",
330 condition, evaluationStatus, actualStatus);
332 String actualValue = entry.getValue().getValue();
333 if (!Objects.equals(expectedValue, actualValue)) {
335 "Expected Value of condition <%s> in QualityGateStatusHolder to be <%s> but it was <%s>",
336 condition, expectedValue, actualValue);
343 "Expected QualityGateStatusHolder to have an entry for <%s> but none was found",