Search in sources :

Example 6 with Measure

use of org.sonar.ce.task.projectanalysis.measure.Measure in project sonarqube by SonarSource.

the class ConditionEvaluatorTest method fail_when_metric_is_not_supported.

@Test
@UseDataProvider("unsupportedMetricTypes")
public void fail_when_metric_is_not_supported(MetricType metricType) {
    Metric metric = createMetric(metricType);
    Measure measure = newMeasureBuilder().create("3.14159265358");
    assertThatThrownBy(() -> underTest.evaluate(createCondition(metric, LESS_THAN, "1.60217657"), measure)).isInstanceOf(IllegalArgumentException.class).hasMessage(String.format("Conditions on MetricType %s are not supported", metricType));
}
Also used : Measure(org.sonar.ce.task.projectanalysis.measure.Measure) Metric(org.sonar.ce.task.projectanalysis.metric.Metric) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 7 with Measure

use of org.sonar.ce.task.projectanalysis.measure.Measure in project sonarqube by SonarSource.

the class ConditionEvaluatorTest method test_condition_on_numeric_new_metric.

@Test
@UseDataProvider("numericNewMetricTypes")
public void test_condition_on_numeric_new_metric(MetricType metricType) {
    Metric metric = createNewMetric(metricType);
    Measure measure = newMeasureBuilder().setVariation(3d).createNoValue();
    assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "3"), measure)).hasLevel(OK);
    assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "2"), measure)).hasLevel(ERROR);
}
Also used : Measure(org.sonar.ce.task.projectanalysis.measure.Measure) Metric(org.sonar.ce.task.projectanalysis.metric.Metric) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 8 with Measure

use of org.sonar.ce.task.projectanalysis.measure.Measure in project sonarqube by SonarSource.

the class ConditionEvaluatorTest method test_condition_on_rating.

@Test
public void test_condition_on_rating() {
    Metric metric = createMetric(RATING);
    Measure measure = newMeasureBuilder().create(4, "D");
    assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "4"), measure)).hasLevel(OK).hasValue(4);
    assertThat(underTest.evaluate(new Condition(metric, GREATER_THAN.getDbValue(), "2"), measure)).hasLevel(ERROR).hasValue(4);
}
Also used : Measure(org.sonar.ce.task.projectanalysis.measure.Measure) Metric(org.sonar.ce.task.projectanalysis.metric.Metric) Test(org.junit.Test)

Example 9 with Measure

use of org.sonar.ce.task.projectanalysis.measure.Measure in project sonarqube by SonarSource.

the class QualityGateEventsStep method checkQualityGateStatusChange.

private void checkQualityGateStatusChange(Component project, Metric metric, QualityGateStatus rawStatus) {
    Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(project, metric);
    if (!baseMeasure.isPresent()) {
        checkNewQualityGate(project, rawStatus);
        return;
    }
    if (!baseMeasure.get().hasQualityGateStatus()) {
        LOGGER.warn(String.format("Previous Quality gate status for project %s is not a supported value. Can not compute Quality Gate event", project.getDbKey()));
        checkNewQualityGate(project, rawStatus);
        return;
    }
    QualityGateStatus baseStatus = baseMeasure.get().getQualityGateStatus();
    if (baseStatus.getStatus() != rawStatus.getStatus()) {
        // The QualityGate status has changed
        createEvent(project, rawStatus.getStatus().getLabel(), rawStatus.getText());
        boolean isNewKo = rawStatus.getStatus() == Measure.Level.OK;
        notifyUsers(project, rawStatus, isNewKo);
    }
}
Also used : QualityGateStatus(org.sonar.ce.task.projectanalysis.measure.QualityGateStatus) Measure(org.sonar.ce.task.projectanalysis.measure.Measure)

Example 10 with Measure

use of org.sonar.ce.task.projectanalysis.measure.Measure 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)

Aggregations

Measure (org.sonar.ce.task.projectanalysis.measure.Measure)27 Test (org.junit.Test)19 Metric (org.sonar.ce.task.projectanalysis.metric.Metric)15 EvaluatedCondition (org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition)6 Condition (org.sonar.ce.task.projectanalysis.qualitygate.Condition)6 QualityGate (org.sonar.ce.task.projectanalysis.qualitygate.QualityGate)5 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)5 ReportComponent (org.sonar.ce.task.projectanalysis.component.ReportComponent)4 QualityGateDetailsData (org.sonar.ce.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData)4 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)3 FileAttributes (org.sonar.ce.task.projectanalysis.component.FileAttributes)3 QualityGateStatus (org.sonar.ce.task.projectanalysis.measure.QualityGateStatus)3 Component (org.sonar.ce.task.projectanalysis.component.Component)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Collection (java.util.Collection)1 Map (java.util.Map)1 VisitorsCrawler (org.sonar.ce.task.projectanalysis.component.VisitorsCrawler)1 MetricType (org.sonar.ce.task.projectanalysis.metric.Metric.MetricType)1