]> source.dussan.org Git - sonarqube.git/blob
59c0a71b6040fd55178ca9ccc401ee9580213c9a
[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.server.measure.live;
21
22 import com.google.common.collect.ImmutableSet;
23 import java.util.Collections;
24 import java.util.Set;
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.project.ProjectDto;
39 import org.sonar.db.qualitygate.QualityGateConditionDto;
40 import org.sonar.db.qualitygate.QualityGateDto;
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
48 import static com.google.common.base.Preconditions.checkState;
49 import static java.util.Arrays.asList;
50 import static java.util.Collections.emptyList;
51 import static java.util.Collections.singleton;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.assertj.core.groups.Tuple.tuple;
54 import static org.sonar.db.metric.MetricTesting.newMetricDto;
55
56 public class LiveQualityGateComputerImplTest {
57
58   @Rule
59   public ExpectedException expectedException = ExpectedException.none();
60
61   @Rule
62   public DbTester db = DbTester.create();
63
64   private final TestQualityGateEvaluator qualityGateEvaluator = new TestQualityGateEvaluator();
65   private final LiveQualityGateComputerImpl underTest = new LiveQualityGateComputerImpl(db.getDbClient(), new QualityGateFinder(db.getDbClient()), qualityGateEvaluator);
66
67   @Test
68   public void loadQualityGate_returns_hardcoded_gate_for_pull_requests() {
69     ProjectDto project = db.components().insertPublicProjectDto();
70     BranchDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST));
71     MetricDto metric1 = db.measures().insertMetric(m -> m.setKey("new_metric"));
72     MetricDto metric2 = db.measures().insertMetric(m -> m.setKey("metric"));
73
74     QualityGateDto gate = db.qualityGates().insertQualityGate();
75     db.qualityGates().setDefaultQualityGate(gate);
76
77     db.qualityGates().addCondition(gate, metric1);
78     db.qualityGates().addCondition(gate, metric2);
79
80     QualityGate result = underTest.loadQualityGate(db.getSession(), project, branch);
81     assertThat(result.getConditions()).extracting(Condition::getMetricKey).containsExactly("new_metric");
82   }
83
84   @Test
85   public void loadQualityGate_on_branch_returns_default_gate() {
86     ProjectDto project = db.components().insertPublicProjectDto();
87     BranchDto branch = db.components().insertProjectBranch(project).setBranchType(BranchType.BRANCH);
88
89     MetricDto metric = db.measures().insertMetric();
90     QualityGateDto gate = db.qualityGates().insertQualityGate();
91     db.qualityGates().setDefaultQualityGate(gate);
92     QualityGateConditionDto condition = db.qualityGates().addCondition(gate, metric);
93
94     QualityGate result = underTest.loadQualityGate(db.getSession(), project, branch);
95
96     assertThat(result.getId()).isEqualTo("" + gate.getUuid());
97     assertThat(result.getConditions())
98       .extracting(Condition::getMetricKey, Condition::getOperator, Condition::getErrorThreshold)
99       .containsExactlyInAnyOrder(
100         tuple(metric.getKey(), Condition.Operator.fromDbValue(condition.getOperator()), condition.getErrorThreshold()));
101   }
102
103   @Test
104   public void getMetricsRelatedTo() {
105     Condition condition = new Condition("metric1", Condition.Operator.GREATER_THAN, "10");
106     QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition));
107
108     Set<String> result = underTest.getMetricsRelatedTo(gate);
109
110     assertThat(result).containsExactlyInAnyOrder(
111       // the metrics needed to compute the status of gate
112       condition.getMetricKey(),
113       // generated metrics
114       CoreMetrics.ALERT_STATUS_KEY, CoreMetrics.QUALITY_GATE_DETAILS_KEY);
115   }
116
117   @Test
118   public void refreshGateStatus_generates_gate_related_measures() {
119     ComponentDto project = ComponentTesting.newPublicProjectDto();
120     MetricDto conditionMetric = newMetricDto();
121     MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY);
122     MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
123     Condition condition = new Condition(conditionMetric.getKey(), Condition.Operator.GREATER_THAN, "10");
124     QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition));
125     MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(conditionMetric, statusMetric, detailsMetric), emptyList());
126
127     EvaluatedQualityGate result = underTest.refreshGateStatus(project, gate, matrix);
128
129     QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
130     assertThat(measures.get(conditionMetric.getKey())).isEmpty();
131
132     assertThat(result.getStatus()).isEqualTo(Metric.Level.OK);
133     assertThat(result.getEvaluatedConditions())
134       .extracting(EvaluatedCondition::getStatus)
135       .containsExactly(EvaluatedCondition.EvaluationStatus.OK);
136     assertThat(matrix.getMeasure(project, CoreMetrics.ALERT_STATUS_KEY).get().getDataAsString()).isEqualTo(Metric.Level.OK.name());
137     assertThat(matrix.getMeasure(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY).get().getDataAsString())
138       .isNotEmpty()
139       // json format
140       .startsWith("{").endsWith("}");
141   }
142
143   @Test
144   public void refreshGateStatus_provides_measures_to_evaluator() {
145     ComponentDto project = ComponentTesting.newPublicProjectDto();
146     MetricDto numericMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name());
147     MetricDto numericNewMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name()).setKey("new_metric");
148     MetricDto stringMetric = newMetricDto().setValueType(Metric.ValueType.STRING.name());
149     MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY);
150     MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
151     QualityGate gate = new QualityGate("1", "foo", Collections.emptySet());
152     LiveMeasureDto numericMeasure = new LiveMeasureDto().setMetricUuid(numericMetric.getUuid()).setValue(1.23).setVariation(4.56).setComponentUuid(project.uuid());
153     LiveMeasureDto numericNewMeasure = new LiveMeasureDto().setMetricUuid(numericNewMetric.getUuid()).setValue(7.8).setVariation(8.9).setComponentUuid(project.uuid());
154     LiveMeasureDto stringMeasure = new LiveMeasureDto().setMetricUuid(stringMetric.getUuid()).setData("bar").setComponentUuid(project.uuid());
155     MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(statusMetric, detailsMetric, numericMetric, numericNewMetric, stringMetric),
156       asList(numericMeasure, numericNewMeasure, stringMeasure));
157
158     underTest.refreshGateStatus(project, gate, matrix);
159
160     QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
161
162     QualityGateEvaluator.Measure loadedStringMeasure = measures.get(stringMetric.getKey()).get();
163     assertThat(loadedStringMeasure.getStringValue()).hasValue("bar");
164     assertThat(loadedStringMeasure.getValue()).isEmpty();
165     assertThat(loadedStringMeasure.getType()).isEqualTo(Metric.ValueType.STRING);
166
167     QualityGateEvaluator.Measure loadedNumericMeasure = measures.get(numericMetric.getKey()).get();
168     assertThat(loadedNumericMeasure.getStringValue()).isEmpty();
169     assertThat(loadedNumericMeasure.getValue()).hasValue(1.23);
170     assertThat(loadedNumericMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT);
171
172     QualityGateEvaluator.Measure loadedNumericNewMeasure = measures.get(numericNewMetric.getKey()).get();
173     assertThat(loadedNumericNewMeasure.getStringValue()).isEmpty();
174     assertThat(loadedNumericNewMeasure.getNewMetricValue()).hasValue(8.9);
175     assertThat(loadedNumericNewMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT);
176   }
177
178   private static class TestQualityGateEvaluator implements QualityGateEvaluator {
179     private Measures measures;
180
181     @Override
182     public EvaluatedQualityGate evaluate(QualityGate gate, Measures measures) {
183       checkState(this.measures == null);
184       this.measures = measures;
185       EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder().setQualityGate(gate).setStatus(Metric.Level.OK);
186       for (Condition condition : gate.getConditions()) {
187         builder.addEvaluatedCondition(condition, EvaluatedCondition.EvaluationStatus.OK, "bar");
188       }
189       return builder.build();
190     }
191
192     private Measures getCalledMeasures() {
193       return measures;
194     }
195
196     @Override
197     public Set<String> getMetricKeys(QualityGate gate) {
198       return gate.getConditions().stream().map(Condition::getMetricKey).collect(Collectors.toSet());
199     }
200   }
201 }