Search in sources :

Example 16 with Metric

use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.

the class QualityGatesTest method should_do_a_sanity_check_when_listing_conditions.

@Test(expected = IllegalStateException.class)
public void should_do_a_sanity_check_when_listing_conditions() {
    long qGateId = QUALITY_GATE_ID;
    long metric1Id = 1L;
    String metric1Key = "polop";
    long metric2Id = 2L;
    QualityGateConditionDto cond1 = new QualityGateConditionDto().setMetricId(metric1Id);
    QualityGateConditionDto cond2 = new QualityGateConditionDto().setMetricId(metric2Id);
    Collection<QualityGateConditionDto> conditions = ImmutableList.of(cond1, cond2);
    when(conditionDao.selectForQualityGate(dbSession, qGateId)).thenReturn(conditions);
    Metric metric1 = mock(Metric.class);
    when(metric1.getKey()).thenReturn(metric1Key);
    when(metricFinder.findById((int) metric1Id)).thenReturn(metric1);
    underTest.listConditions(qGateId);
}
Also used : QualityGateConditionDto(org.sonar.db.qualitygate.QualityGateConditionDto) Metric(org.sonar.api.measures.Metric) Test(org.junit.Test)

Example 17 with Metric

use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.

the class MeasureSensorTest method testExecution.

@Test
public void testExecution() throws IOException {
    File measures = new File(baseDir, "src/foo.xoo.measures");
    FileUtils.write(measures, "ncloc:12\nbranch_coverage:5.3\nsqale_index:300\nbool:true\n\n#comment");
    InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
    context.fileSystem().add(inputFile);
    Metric<Boolean> booleanMetric = new Metric.Builder("bool", "Bool", Metric.ValueType.BOOL).create();
    when(metricFinder.<Integer>findByKey("ncloc")).thenReturn(CoreMetrics.NCLOC);
    when(metricFinder.<Double>findByKey("branch_coverage")).thenReturn(CoreMetrics.BRANCH_COVERAGE);
    when(metricFinder.<Long>findByKey("sqale_index")).thenReturn(CoreMetrics.TECHNICAL_DEBT);
    when(metricFinder.<Boolean>findByKey("bool")).thenReturn(booleanMetric);
    sensor.execute(context);
    assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.NCLOC).value()).isEqualTo(12);
    assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.BRANCH_COVERAGE).value()).isEqualTo(5.3);
    assertThat(context.measure("foo:src/foo.xoo", CoreMetrics.TECHNICAL_DEBT).value()).isEqualTo(300L);
    assertThat(context.measure("foo:src/foo.xoo", booleanMetric).value()).isTrue();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Metric(org.sonar.api.measures.Metric) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 18 with Metric

use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.

the class RegisterMetricsTest method update_non_custom_metrics.

/**
   * Update existing metrics, except if custom metric
   */
@Test
public void update_non_custom_metrics() {
    dbTester.prepareDbUnit(getClass(), "update_non_custom_metrics.xml");
    RegisterMetrics register = new RegisterMetrics(dbClient);
    Metric m1 = new Metric.Builder("m1", "New name", Metric.ValueType.FLOAT).setDescription("new description").setDirection(-1).setQualitative(true).setDomain("new domain").setUserManaged(false).setDecimalScale(3).setHidden(true).create();
    Metric custom = new Metric.Builder("custom", "New custom", Metric.ValueType.FLOAT).setDescription("New description of custom metric").setUserManaged(true).create();
    register.register(asList(m1, custom));
    dbTester.assertDbUnit(getClass(), "update_non_custom_metrics-result.xml", "metrics");
}
Also used : Metric(org.sonar.api.measures.Metric) Test(org.junit.Test)

Example 19 with Metric

use of org.sonar.api.measures.Metric in project sonarqube by SonarSource.

the class DefaultIndex method addMeasure.

public Measure addMeasure(String key, Measure measure) {
    InputComponent component = componentStore.getByKey(key);
    if (component == null) {
        throw new IllegalStateException("Invalid component key: " + key);
    }
    if (sensorStorage.isDeprecatedMetric(measure.getMetricKey())) {
        // Ignore deprecated metrics
        return measure;
    }
    org.sonar.api.batch.measure.Metric<?> metric = metricFinder.findByKey(measure.getMetricKey());
    if (metric == null) {
        throw new UnsupportedOperationException("Unknown metric: " + measure.getMetricKey());
    }
    DefaultMeasure<?> newMeasure;
    if (Boolean.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<Boolean>().forMetric((Metric<Boolean>) metric).withValue(measure.getValue() != 0.0);
    } else if (Integer.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<Integer>().forMetric((Metric<Integer>) metric).withValue(measure.getValue().intValue());
    } else if (Double.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<Double>().forMetric((Metric<Double>) metric).withValue(measure.getValue());
    } else if (String.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<String>().forMetric((Metric<String>) metric).withValue(measure.getData());
    } else if (Long.class.equals(metric.valueType())) {
        newMeasure = new DefaultMeasure<Long>().forMetric((Metric<Long>) metric).withValue(measure.getValue().longValue());
    } else {
        throw new UnsupportedOperationException("Unsupported type :" + metric.valueType());
    }
    sensorStorage.saveMeasure(component, newMeasure);
    return measure;
}
Also used : InputComponent(org.sonar.api.batch.fs.InputComponent) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) Metric(org.sonar.api.measures.Metric)

Aggregations

Metric (org.sonar.api.measures.Metric)19 Test (org.junit.Test)12 CoreMetrics (org.sonar.api.measures.CoreMetrics)3 Metrics (org.sonar.api.measures.Metrics)3 QualityGateConditionDto (org.sonar.db.qualitygate.QualityGateConditionDto)3 InputFile (org.sonar.api.batch.fs.InputFile)2 MetricProvider (org.sonar.scanner.bootstrap.MetricProvider)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 File (java.io.File)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 InputComponent (org.sonar.api.batch.fs.InputComponent)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)1 NewIssueLocation (org.sonar.api.batch.sensor.issue.NewIssueLocation)1 DefaultMeasure (org.sonar.api.batch.sensor.measure.internal.DefaultMeasure)1 CoreMetrics.getMetric (org.sonar.api.measures.CoreMetrics.getMetric)1 DbSession (org.sonar.db.DbSession)1