Search in sources :

Example 11 with Condition

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

the class QualityGateMeasuresStepTest method new_measures_have_WARNING_level_if_no_updated_measure_has_ERROR_level.

@Test
public void new_measures_have_WARNING_level_if_no_updated_measure_has_ERROR_level() {
    int rawValue = 1;
    Condition equals2Condition = createEqualsCondition(INT_METRIC_1, "2", null);
    Condition equals1WarningCondition = createEqualsCondition(INT_METRIC_2, null, "1");
    Measure rawMeasure = newMeasureBuilder().create(rawValue, null);
    qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(equals2Condition, equals1WarningCondition)));
    measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, rawMeasure);
    measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_2_KEY, rawMeasure);
    underTest.execute();
    Optional<Measure> rawMeasure1 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_1_KEY);
    Optional<Measure> rawMeasure2 = measureRepository.getAddedRawMeasure(PROJECT_REF, INT_METRIC_2_KEY);
    assertThat(rawMeasure1.get()).hasQualityGateLevel(OK).hasQualityGateText(dumbResultTextAnswer(equals2Condition, OK, rawValue));
    assertThat(rawMeasure2.get()).hasQualityGateLevel(WARN).hasQualityGateText(dumbResultTextAnswer(equals1WarningCondition, WARN, rawValue));
    assertThat(getAlertStatusMeasure()).hasQualityGateLevel(WARN).hasQualityGateText(dumbResultTextAnswer(equals2Condition, OK, rawValue) + ", " + dumbResultTextAnswer(equals1WarningCondition, WARN, rawValue));
    assertThat(getQGDetailsMeasure()).hasValue(new QualityGateDetailsData(WARN, of(new EvaluatedCondition(equals2Condition, OK, rawValue), new EvaluatedCondition(equals1WarningCondition, WARN, rawValue))).toJson());
    QualityGateStatusHolderAssertions.assertThat(qualityGateStatusHolder).hasStatus(QualityGateStatus.WARN).hasConditionCount(2).hasCondition(equals2Condition, ConditionStatus.EvaluationStatus.OK, String.valueOf(rawValue)).hasCondition(equals1WarningCondition, ConditionStatus.EvaluationStatus.WARN, String.valueOf(rawValue));
}
Also used : EvaluatedCondition(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) Condition(org.sonar.server.computation.task.projectanalysis.qualitygate.Condition) QualityGateDetailsData(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData) Measure(org.sonar.server.computation.task.projectanalysis.measure.Measure) EvaluatedCondition(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) QualityGate(org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate) Test(org.junit.Test)

Example 12 with Condition

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

the class QualityGateMeasuresStepTest method new_measures_are_created_even_if_there_is_no_rawMeasure_for_metric_of_condition.

@Test
public void new_measures_are_created_even_if_there_is_no_rawMeasure_for_metric_of_condition() {
    Condition equals2Condition = createEqualsCondition(INT_METRIC_1, "2", null);
    qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(equals2Condition)));
    underTest.execute();
    Optional<Measure> addedRawMeasure = measureRepository.getAddedRawMeasure(PROJECT_COMPONENT, INT_METRIC_1_KEY);
    assertThat(addedRawMeasure).isAbsent();
    assertThat(getAlertStatusMeasure()).hasQualityGateLevel(OK).hasQualityGateText("");
    assertThat(getQGDetailsMeasure()).hasValue(new QualityGateDetailsData(OK, Collections.<EvaluatedCondition>emptyList()).toJson());
    QualityGateStatusHolderAssertions.assertThat(qualityGateStatusHolder).hasStatus(QualityGateStatus.OK).hasConditionCount(1).hasCondition(equals2Condition, ConditionStatus.EvaluationStatus.NO_VALUE, null);
}
Also used : EvaluatedCondition(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) Condition(org.sonar.server.computation.task.projectanalysis.qualitygate.Condition) QualityGateDetailsData(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData) Measure(org.sonar.server.computation.task.projectanalysis.measure.Measure) QualityGate(org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate) Test(org.junit.Test)

Example 13 with Condition

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

the class QualityGateMeasuresStepTest method rawMeasure_is_updated_if_present_and_new_measures_are_created_if_project_has_measure_for_metric_of_condition.

@Test
public void rawMeasure_is_updated_if_present_and_new_measures_are_created_if_project_has_measure_for_metric_of_condition() {
    int rawValue = 1;
    Condition equals2Condition = createEqualsCondition(INT_METRIC_1, "2", null);
    Measure rawMeasure = newMeasureBuilder().create(rawValue, null);
    qualityGateHolder.setQualityGate(new QualityGate(SOME_QG_ID, SOME_QG_NAME, of(equals2Condition)));
    measureRepository.addRawMeasure(PROJECT_REF, INT_METRIC_1_KEY, rawMeasure);
    underTest.execute();
    Optional<Measure> addedRawMeasure = measureRepository.getAddedRawMeasure(PROJECT_COMPONENT, INT_METRIC_1_KEY);
    assertThat(addedRawMeasure).hasQualityGateLevel(OK).hasQualityGateText(dumbResultTextAnswer(equals2Condition, OK, rawValue));
    assertThat(getAlertStatusMeasure()).hasQualityGateLevel(OK).hasQualityGateText(dumbResultTextAnswer(equals2Condition, OK, rawValue));
    assertThat(getQGDetailsMeasure().get()).hasValue(new QualityGateDetailsData(OK, of(new EvaluatedCondition(equals2Condition, OK, rawValue))).toJson());
    QualityGateStatusHolderAssertions.assertThat(qualityGateStatusHolder).hasStatus(QualityGateStatus.OK).hasConditionCount(1).hasCondition(equals2Condition, ConditionStatus.EvaluationStatus.OK, String.valueOf(rawValue));
}
Also used : EvaluatedCondition(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) Condition(org.sonar.server.computation.task.projectanalysis.qualitygate.Condition) QualityGateDetailsData(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData) Measure(org.sonar.server.computation.task.projectanalysis.measure.Measure) EvaluatedCondition(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) QualityGate(org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate) Test(org.junit.Test)

Example 14 with Condition

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

the class QualityGateMeasuresStep method createStatusPerCondition.

private static Map<Condition, ConditionStatus> createStatusPerCondition(Iterable<Condition> conditions, Iterable<EvaluatedCondition> evaluatedConditions) {
    Map<Condition, EvaluatedCondition> evaluatedConditionPerCondition = from(evaluatedConditions).uniqueIndex(EvaluatedConditionToCondition.INSTANCE);
    ImmutableMap.Builder<Condition, ConditionStatus> builder = ImmutableMap.builder();
    for (Condition condition : conditions) {
        EvaluatedCondition evaluatedCondition = evaluatedConditionPerCondition.get(condition);
        if (evaluatedCondition == null) {
            builder.put(condition, NO_VALUE_STATUS);
        } else {
            builder.put(condition, create(toEvaluationStatus(evaluatedCondition.getLevel()), evaluatedCondition.getActualValue()));
        }
    }
    return builder.build();
}
Also used : EvaluatedCondition(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) Condition(org.sonar.server.computation.task.projectanalysis.qualitygate.Condition) EvaluatedCondition(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) ConditionStatus(org.sonar.server.computation.task.projectanalysis.qualitygate.ConditionStatus) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 15 with Condition

use of org.sonar.server.computation.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(Collectors.index(Condition::getMetric, java.util.function.Function.identity()));
    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;
        }
        MetricEvaluationResult metricEvaluationResult = evaluateQualityGate(measure.get(), entry.getValue());
        String text = evaluationResultTextConverter.asText(metricEvaluationResult.condition, metricEvaluationResult.evaluationResult);
        builder.addLabel(text);
        Measure updatedMeasure = Measure.updatedMeasureBuilder(measure.get()).setQualityGateStatus(new QualityGateStatus(metricEvaluationResult.evaluationResult.getLevel(), text)).create();
        measureRepository.update(project, metric, updatedMeasure);
        builder.addEvaluatedCondition(metricEvaluationResult);
    }
}
Also used : EvaluatedCondition(org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition) Condition(org.sonar.server.computation.task.projectanalysis.qualitygate.Condition) QualityGateStatus(org.sonar.server.computation.task.projectanalysis.measure.QualityGateStatus) Measure(org.sonar.server.computation.task.projectanalysis.measure.Measure) Collection(java.util.Collection) Metric(org.sonar.server.computation.task.projectanalysis.metric.Metric) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Condition (org.sonar.server.computation.task.projectanalysis.qualitygate.Condition)16 Test (org.junit.Test)11 EvaluatedCondition (org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.EvaluatedCondition)10 Measure (org.sonar.server.computation.task.projectanalysis.measure.Measure)8 QualityGate (org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate)7 QualityGateDetailsData (org.sonar.server.computation.task.projectanalysis.measure.qualitygatedetails.QualityGateDetailsData)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 Metric (org.sonar.server.computation.task.projectanalysis.metric.Metric)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.server.computation.task.projectanalysis.measure.QualityGateStatus)1 MetricImpl (org.sonar.server.computation.task.projectanalysis.metric.MetricImpl)1 ConditionEvaluator (org.sonar.server.computation.task.projectanalysis.qualitygate.ConditionEvaluator)1 ConditionStatus (org.sonar.server.computation.task.projectanalysis.qualitygate.ConditionStatus)1 EvaluationResult (org.sonar.server.computation.task.projectanalysis.qualitygate.EvaluationResult)1