3 * Copyright (C) 2009-2019 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.server.measure.live;
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Collections;
25 import java.util.stream.Collectors;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.measures.CoreMetrics;
30 import org.sonar.api.measures.Metric;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.component.BranchDto;
33 import org.sonar.db.component.BranchType;
34 import org.sonar.db.component.ComponentDto;
35 import org.sonar.db.component.ComponentTesting;
36 import org.sonar.db.measure.LiveMeasureDto;
37 import org.sonar.db.metric.MetricDto;
38 import org.sonar.db.organization.OrganizationDto;
39 import org.sonar.db.qualitygate.QGateWithOrgDto;
40 import org.sonar.db.qualitygate.QualityGateConditionDto;
41 import org.sonar.server.qualitygate.Condition;
42 import org.sonar.server.qualitygate.EvaluatedCondition;
43 import org.sonar.server.qualitygate.EvaluatedQualityGate;
44 import org.sonar.server.qualitygate.QualityGate;
45 import org.sonar.server.qualitygate.QualityGateEvaluator;
46 import org.sonar.server.qualitygate.QualityGateFinder;
47 import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
49 import static com.google.common.base.Preconditions.checkState;
50 import static java.util.Arrays.asList;
51 import static java.util.Collections.emptyList;
52 import static java.util.Collections.singleton;
53 import static org.assertj.core.api.Assertions.assertThat;
54 import static org.assertj.core.groups.Tuple.tuple;
55 import static org.sonar.db.component.ComponentTesting.newBranchDto;
56 import static org.sonar.db.metric.MetricTesting.newMetricDto;
57 import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto;
59 public class LiveQualityGateComputerImplTest {
62 public ExpectedException expectedException = ExpectedException.none();
65 public DbTester db = DbTester.create();
67 private TestQualityGateEvaluator qualityGateEvaluator = new TestQualityGateEvaluator();
68 private LiveQualityGateComputerImpl underTest = new LiveQualityGateComputerImpl(db.getDbClient(), new QualityGateFinder(db.getDbClient()), qualityGateEvaluator);
71 public void loadQualityGate_returns_hardcoded_gate_for_short_living_branches() {
72 OrganizationDto organization = db.organizations().insert();
73 ComponentDto project = db.components().insertPublicProject(organization);
74 BranchDto branch = newBranchDto(project).setBranchType(BranchType.SHORT);
75 db.components().insertProjectBranch(project, branch);
77 QualityGate result = underTest.loadQualityGate(db.getSession(), organization, project, branch);
79 assertThat(result).isSameAs(ShortLivingBranchQualityGate.GATE);
83 public void loadQualityGate_returns_hardcoded_gate_for_pull_requests() {
84 OrganizationDto organization = db.organizations().insert();
85 ComponentDto project = db.components().insertPublicProject(organization);
86 BranchDto pullRequest = newBranchDto(project).setBranchType(BranchType.PULL_REQUEST);
87 db.components().insertProjectBranch(project, pullRequest);
89 QualityGate result = underTest.loadQualityGate(db.getSession(), organization, project, pullRequest);
91 assertThat(result).isSameAs(ShortLivingBranchQualityGate.GATE);
95 public void loadQualityGate_on_long_branch_returns_organization_default_gate() {
96 OrganizationDto organization = db.organizations().insert();
97 ComponentDto project = db.components().insertPublicProject(organization);
98 BranchDto branch = newBranchDto(project).setBranchType(BranchType.LONG);
99 db.components().insertProjectBranch(project, branch);
101 MetricDto metric = db.measures().insertMetric();
102 QGateWithOrgDto gate = db.qualityGates().insertQualityGate(organization);
103 db.qualityGates().setDefaultQualityGate(organization, gate);
104 QualityGateConditionDto condition = db.qualityGates().addCondition(gate, metric);
106 QualityGate result = underTest.loadQualityGate(db.getSession(), organization, project, branch);
108 assertThat(result.getId()).isEqualTo("" + gate.getId());
109 assertThat(result.getConditions())
110 .extracting(Condition::getMetricKey, Condition::getOperator, c -> c.getErrorThreshold().get(), c -> c.getWarningThreshold().get())
111 .containsExactlyInAnyOrder(
112 tuple(metric.getKey(), Condition.Operator.fromDbValue(condition.getOperator()), condition.getErrorThreshold(), condition.getWarningThreshold()));
116 public void getMetricsRelatedTo() {
117 Condition condition = new Condition("metric1", Condition.Operator.EQUALS, "10", null);
118 QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition));
120 Set<String> result = underTest.getMetricsRelatedTo(gate);
122 assertThat(result).containsExactlyInAnyOrder(
123 // the metrics needed to compute the status of gate
124 condition.getMetricKey(),
126 CoreMetrics.ALERT_STATUS_KEY, CoreMetrics.QUALITY_GATE_DETAILS_KEY);
130 public void refreshGateStatus_generates_gate_related_measures() {
131 ComponentDto project = ComponentTesting.newPublicProjectDto(newOrganizationDto());
132 MetricDto conditionMetric = newMetricDto();
133 MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY);
134 MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
135 Condition condition = new Condition(conditionMetric.getKey(), Condition.Operator.GREATER_THAN, "10", null);
136 QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition));
137 MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(conditionMetric, statusMetric, detailsMetric), emptyList());
139 EvaluatedQualityGate result = underTest.refreshGateStatus(project, gate, matrix);
141 QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
142 assertThat(measures.get(conditionMetric.getKey())).isEmpty();
144 assertThat(result.getStatus()).isEqualTo(Metric.Level.OK);
145 assertThat(result.getEvaluatedConditions())
146 .extracting(EvaluatedCondition::getStatus)
147 .containsExactly(EvaluatedCondition.EvaluationStatus.OK);
148 assertThat(matrix.getMeasure(project, CoreMetrics.ALERT_STATUS_KEY).get().getDataAsString()).isEqualTo(Metric.Level.OK.name());
149 assertThat(matrix.getMeasure(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY).get().getDataAsString())
152 .startsWith("{").endsWith("}");
156 public void refreshGateStatus_provides_measures_to_evaluator() {
157 ComponentDto project = ComponentTesting.newPublicProjectDto(newOrganizationDto());
158 MetricDto numericMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name());
159 MetricDto numericNewMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name()).setKey("new_metric");
160 MetricDto stringMetric = newMetricDto().setValueType(Metric.ValueType.STRING.name());
161 MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY);
162 MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
163 QualityGate gate = new QualityGate("1", "foo", Collections.emptySet());
164 LiveMeasureDto numericMeasure = new LiveMeasureDto().setMetricId(numericMetric.getId()).setValue(1.23).setVariation(4.56).setComponentUuid(project.uuid());
165 LiveMeasureDto numericNewMeasure = new LiveMeasureDto().setMetricId(numericNewMetric.getId()).setValue(7.8).setVariation(8.9).setComponentUuid(project.uuid());
166 LiveMeasureDto stringMeasure = new LiveMeasureDto().setMetricId(stringMetric.getId()).setData("bar").setComponentUuid(project.uuid());
167 MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(statusMetric, detailsMetric, numericMetric, numericNewMetric, stringMetric), asList(numericMeasure, numericNewMeasure, stringMeasure));
169 underTest.refreshGateStatus(project, gate, matrix);
171 QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
173 QualityGateEvaluator.Measure loadedStringMeasure = measures.get(stringMetric.getKey()).get();
174 assertThat(loadedStringMeasure.getStringValue()).hasValue("bar");
175 assertThat(loadedStringMeasure.getValue()).isEmpty();
176 assertThat(loadedStringMeasure.getType()).isEqualTo(Metric.ValueType.STRING);
178 QualityGateEvaluator.Measure loadedNumericMeasure = measures.get(numericMetric.getKey()).get();
179 assertThat(loadedNumericMeasure.getStringValue()).isEmpty();
180 assertThat(loadedNumericMeasure.getValue()).hasValue(1.23);
181 assertThat(loadedNumericMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT);
183 QualityGateEvaluator.Measure loadedNumericNewMeasure = measures.get(numericNewMetric.getKey()).get();
184 assertThat(loadedNumericNewMeasure.getStringValue()).isEmpty();
185 assertThat(loadedNumericNewMeasure.getNewMetricValue()).hasValue(8.9);
186 assertThat(loadedNumericNewMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT);
189 private static class TestQualityGateEvaluator implements QualityGateEvaluator {
190 private Measures measures;
193 public EvaluatedQualityGate evaluate(QualityGate gate, Measures measures) {
194 checkState(this.measures == null);
195 this.measures = measures;
196 EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder().setQualityGate(gate).setStatus(Metric.Level.OK);
197 for (Condition condition : gate.getConditions()) {
198 builder.addCondition(condition, EvaluatedCondition.EvaluationStatus.OK, "bar");
200 return builder.build();
203 private Measures getCalledMeasures() {
208 public Set<String> getMetricKeys(QualityGate gate) {
209 return gate.getConditions().stream().map(Condition::getMetricKey).collect(Collectors.toSet());