Search in sources :

Example 1 with Condition

use of org.sonar.ce.task.projectanalysis.qualitygate.Condition in project sonarqube by SonarSource.

the class ConditionToConditionTest method apply_copies_value.

@Test
public void apply_copies_value() {
    Condition otherCondition = new Condition(newMetric(METRIC_KEY), Condition.Operator.LESS_THAN.getDbValue(), ERROR_THRESHOLD);
    ConditionToCondition underTest = new ConditionToCondition(of(SOME_CONDITION, SOME_CONDITION_STATUS, otherCondition, ConditionStatus.NO_VALUE_STATUS));
    assertThat(underTest.apply(SOME_CONDITION).getValue()).isEqualTo(SOME_VALUE);
    QualityGate.Condition res = underTest.apply(otherCondition);
    assertThatThrownBy(res::getValue).isInstanceOf(IllegalStateException.class).hasMessage("There is no value when status is NO_VALUE");
}
Also used : Condition(org.sonar.ce.task.projectanalysis.qualitygate.Condition) QualityGate(org.sonar.api.ce.posttask.QualityGate) Test(org.junit.Test)

Example 2 with Condition

use of org.sonar.ce.task.projectanalysis.qualitygate.Condition in project sonarqube by SonarSource.

the class ConditionToConditionTest method apply_converts_all_values_of_operator.

@Test
@UseDataProvider("allOperatorValues")
public void apply_converts_all_values_of_operator(Condition.Operator operator) {
    Condition condition = new Condition(newMetric(METRIC_KEY), operator.getDbValue(), ERROR_THRESHOLD);
    ConditionToCondition underTest = new ConditionToCondition(of(condition, SOME_CONDITION_STATUS));
    assertThat(underTest.apply(condition).getOperator().name()).isEqualTo(operator.name());
}
Also used : Condition(org.sonar.ce.task.projectanalysis.qualitygate.Condition) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 3 with Condition

use of org.sonar.ce.task.projectanalysis.qualitygate.Condition in project sonarqube by SonarSource.

the class QualityGateMeasuresStep method updateMeasures.

private void updateMeasures(Component project, Set<Condition> conditions, QualityGateDetailsDataBuilder builder) {
    Multimap<Metric, Condition> conditionsPerMetric = conditions.stream().collect(MoreCollectors.index(Condition::getMetric, java.util.function.Function.identity()));
    boolean ignoredConditions = false;
    for (Map.Entry<Metric, Collection<Condition>> entry : conditionsPerMetric.asMap().entrySet()) {
        Metric metric = entry.getKey();
        Optional<Measure> measure = measureRepository.getRawMeasure(project, metric);
        if (!measure.isPresent()) {
            continue;
        }
        final MetricEvaluationResult metricEvaluationResult = evaluateQualityGate(measure.get(), entry.getValue());
        final MetricEvaluationResult finalMetricEvaluationResult;
        if (smallChangesetQualityGateSpecialCase.appliesTo(project, metricEvaluationResult)) {
            finalMetricEvaluationResult = smallChangesetQualityGateSpecialCase.apply(metricEvaluationResult);
            ignoredConditions = true;
        } else {
            finalMetricEvaluationResult = metricEvaluationResult;
        }
        String text = evaluationResultTextConverter.asText(finalMetricEvaluationResult.condition, finalMetricEvaluationResult.evaluationResult);
        builder.addLabel(text);
        Measure updatedMeasure = Measure.updatedMeasureBuilder(measure.get()).setQualityGateStatus(new QualityGateStatus(finalMetricEvaluationResult.evaluationResult.getLevel(), text)).create();
        measureRepository.update(project, metric, updatedMeasure);
        builder.addEvaluatedCondition(finalMetricEvaluationResult);
    }
    builder.setIgnoredConditions(ignoredConditions);
}
Also used : EvaluatedCondition(org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) Condition(org.sonar.ce.task.projectanalysis.qualitygate.Condition) QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) Measure(org.sonar.ce.task.projectanalysis.measure.Measure) Collection(java.util.Collection) Metric(org.sonar.ce.task.projectanalysis.metric.Metric) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with Condition

use of org.sonar.ce.task.projectanalysis.qualitygate.Condition in project sonarqube by SonarSource.

the class QualityGateMeasuresStep method evaluateQualityGate.

private static MetricEvaluationResult evaluateQualityGate(Measure measure, Collection<Condition> conditions) {
    ConditionEvaluator conditionEvaluator = new ConditionEvaluator();
    MetricEvaluationResult metricEvaluationResult = null;
    for (Condition newCondition : conditions) {
        EvaluationResult newEvaluationResult = conditionEvaluator.evaluate(newCondition, measure);
        if (metricEvaluationResult == null || newEvaluationResult.getLevel().ordinal() > metricEvaluationResult.evaluationResult.getLevel().ordinal()) {
            metricEvaluationResult = new MetricEvaluationResult(newEvaluationResult, newCondition);
        }
    }
    return metricEvaluationResult;
}
Also used : EvaluatedCondition(org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) Condition(org.sonar.ce.task.projectanalysis.qualitygate.Condition) ConditionEvaluator(org.sonar.ce.task.projectanalysis.qualitygate.ConditionEvaluator) EvaluationResult(org.sonar.ce.task.projectanalysis.qualitygate.EvaluationResult)

Example 5 with Condition

use of org.sonar.ce.task.projectanalysis.qualitygate.Condition in project sonarqube by SonarSource.

the class LoadQualityGateStepTest method filter_conditions_on_pull_request.

@Test
public void filter_conditions_on_pull_request() {
    Metric newMetric = new MetricImpl("1", "new_key", "name", Metric.MetricType.INT);
    Metric metric = new MetricImpl("2", "key", "name", Metric.MetricType.INT);
    Condition variation = new Condition(newMetric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
    Condition condition = new Condition(metric, Condition.Operator.GREATER_THAN.getDbValue(), "1.0");
    when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
    QualityGate defaultGate = new QualityGate("1", "qg", Arrays.asList(variation, condition));
    when(qualityGateService.findDefaultQualityGate()).thenReturn(defaultGate);
    underTest.execute(new TestComputationStepContext());
    assertThat(mutableQualityGateHolder.getQualityGate().get().getConditions()).containsExactly(variation);
}
Also used : Condition(org.sonar.ce.task.projectanalysis.qualitygate.Condition) MetricImpl(org.sonar.ce.task.projectanalysis.metric.MetricImpl) Metric(org.sonar.ce.task.projectanalysis.metric.Metric) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) QualityGate(org.sonar.ce.task.projectanalysis.qualitygate.QualityGate) Test(org.junit.Test)

Aggregations

Condition (org.sonar.ce.task.projectanalysis.qualitygate.Condition)17 Test (org.junit.Test)11 EvaluatedCondition (org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition)8 Measure (org.sonar.ce.task.projectanalysis.measure.Measure)6 QualityGate (org.sonar.ce.task.projectanalysis.qualitygate.QualityGate)6 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)6 Metric (org.sonar.ce.task.projectanalysis.metric.Metric)4 QualityGateDetailsData (org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData)3 MetricImpl (org.sonar.ce.task.projectanalysis.metric.MetricImpl)3 EvaluationResult (org.sonar.ce.task.projectanalysis.qualitygate.EvaluationResult)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 JsonObject (com.google.gson.JsonObject)1 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 Collection (java.util.Collection)1 Map (java.util.Map)1 QualityGate (org.sonar.api.ce.posttask.QualityGate)1 QualityGateStatus (org.sonar.ce.task.projectanalysis.measure.QualityGateStatus)1 ConditionEvaluator (org.sonar.ce.task.projectanalysis.qualitygate.ConditionEvaluator)1 ConditionStatus (org.sonar.ce.task.projectanalysis.qualitygate.ConditionStatus)1