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