diff options
author | Julien HENRY <henryju@yahoo.fr> | 2015-03-23 11:31:45 +0100 |
---|---|---|
committer | Julien HENRY <henryju@yahoo.fr> | 2015-03-23 11:31:45 +0100 |
commit | a79bcf3b32ea3a2e9452bbc0ff1e0f7ba8d2079f (patch) | |
tree | 0968781f2d1e7672f36222e51b69a95d63d2bd36 /server | |
parent | 6c220cf23587774804fea19b2d8d108b8120f163 (diff) | |
parent | 7b2066e95e177700b41f86b30468cbca12cb5f5a (diff) | |
download | sonarqube-a79bcf3b32ea3a2e9452bbc0ff1e0f7ba8d2079f.tar.gz sonarqube-a79bcf3b32ea3a2e9452bbc0ff1e0f7ba8d2079f.zip |
Merge pull request #165 from SonarSource/feature/batch/measures_in_compute
SONAR-6275 Feed measures in compute report
Diffstat (limited to 'server')
4 files changed, 157 insertions, 203 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/BatchReportMeasureUtils.java b/server/sonar-server/src/main/java/org/sonar/server/computation/measure/BatchReportMeasureUtils.java deleted file mode 100644 index 54b224a9651..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/BatchReportMeasureUtils.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.server.computation.measure; - -import org.sonar.batch.protocol.output.BatchReport; - -import javax.annotation.CheckForNull; - -public class BatchReportMeasureUtils { - private BatchReportMeasureUtils() { - // static methods only - } - - /** - * return the numerical value as a double. It's the type used in db. - * Returns null if no numerical value found - */ - @CheckForNull - public static Double valueAsDouble(BatchReport.Measure measure) { - switch (measure.getValueType()) { - case BOOLEAN: - return measure.getBooleanValue() ? 1.0d : 0.0d; - case INT: - return Double.valueOf(measure.getIntValue()); - case LONG: - return Double.valueOf(measure.getLongValue()); - case DOUBLE: - return measure.getDoubleValue(); - default: - return null; - } - } - - /** - * check that measure has a value (numerical or string) and a metric key - */ - public static void checkMeasure(BatchReport.Measure measure) { - if (!hasValueTypeAndMetricKey(measure)) { - throw newIllegalStateException(measure); - } - - boolean hasValueOrData; - switch (measure.getValueType()) { - case DOUBLE: - hasValueOrData = measure.hasDoubleValue(); - break; - case INT: - hasValueOrData = measure.hasIntValue(); - break; - case LONG: - hasValueOrData = measure.hasLongValue(); - break; - case STRING: - hasValueOrData = measure.hasStringValue(); - break; - case BOOLEAN: - hasValueOrData = measure.hasBooleanValue(); - break; - default: - throw newIllegalStateException(measure); - } - - if (!hasValueOrData) { - throw newIllegalStateException(measure); - } - } - - private static boolean hasValueTypeAndMetricKey(BatchReport.Measure measure) { - return measure.hasValueType() && measure.hasMetricKey(); - } - - private static IllegalStateException newIllegalStateException(BatchReport.Measure measure) { - return new IllegalStateException(String.format("Measure %s does not have value", measure)); - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistMeasuresStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistMeasuresStep.java index 4991bab9b7c..4c10a8be07d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistMeasuresStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistMeasuresStep.java @@ -34,10 +34,9 @@ import org.sonar.server.computation.issue.RuleCache; import org.sonar.server.computation.measure.MetricCache; import org.sonar.server.db.DbClient; -import java.util.List; +import javax.annotation.CheckForNull; -import static org.sonar.server.computation.measure.BatchReportMeasureUtils.checkMeasure; -import static org.sonar.server.computation.measure.BatchReportMeasureUtils.valueAsDouble; +import java.util.List; public class PersistMeasuresStep implements ComputationStep { @@ -95,7 +94,12 @@ public class PersistMeasuresStep implements ComputationStep { @VisibleForTesting MeasureDto toMeasureDto(BatchReport.Measure in, BatchReport.Component component) { - checkMeasure(in); + if (!in.hasValueType()) { + throw new IllegalStateException(String.format("Measure %s does not have value type", in)); + } + if (!in.hasMetricKey()) { + throw new IllegalStateException(String.format("Measure %s does not have metric key", in)); + } MeasureDto out = new MeasureDto(); out.setTendency(in.hasTendency() ? in.getTendency() : null); @@ -111,13 +115,33 @@ public class PersistMeasuresStep implements ComputationStep { out.setComponentId(component.getId()); out.setSnapshotId(component.getSnapshotId()); out.setMetricId(metricCache.get(in.getMetricKey()).getId()); - out.setRuleId(ruleCache.get(RuleKey.parse(in.getRuleKey())).getId()); + out.setRuleId(in.hasRuleKey() ? ruleCache.get(RuleKey.parse(in.getRuleKey())).getId() : null); out.setCharacteristicId(in.hasCharactericId() ? in.getCharactericId() : null); out.setValue(valueAsDouble(in)); setData(in, out); return out; } + /** + * return the numerical value as a double. It's the type used in db. + * Returns null if no numerical value found + */ + @CheckForNull + private static Double valueAsDouble(BatchReport.Measure measure) { + switch (measure.getValueType()) { + case BOOLEAN: + return measure.hasBooleanValue() ? (measure.getBooleanValue() ? 1.0d : 0.0d) : null; + case INT: + return measure.hasIntValue() ? Double.valueOf(measure.getIntValue()) : null; + case LONG: + return measure.hasLongValue() ? Double.valueOf(measure.getLongValue()) : null; + case DOUBLE: + return measure.hasDoubleValue() ? measure.getDoubleValue() : null; + default: + return null; + } + } + private MeasureDto setData(BatchReport.Measure in, MeasureDto out) { if (in.hasStringValue()) { out.setData(in.getStringValue()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BatchReportMeasureUtilsTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BatchReportMeasureUtilsTest.java deleted file mode 100644 index 6d743fa2757..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/BatchReportMeasureUtilsTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.server.computation.measure; - -import org.junit.Test; -import org.sonar.batch.protocol.Constants; -import org.sonar.batch.protocol.output.BatchReport; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.batch.protocol.Constants.MeasureValueType.*; -import static org.sonar.server.computation.measure.BatchReportMeasureUtils.checkMeasure; -import static org.sonar.server.computation.measure.BatchReportMeasureUtils.valueAsDouble; - -public class BatchReportMeasureUtilsTest { - - @Test(expected = IllegalStateException.class) - public void fail_when_no_metric_key() throws Exception { - BatchReport.Measure measure = BatchReport.Measure.newBuilder() - .setValueType(STRING) - .setStringValue("string-value") - .build(); - - checkMeasure(measure); - } - - @Test(expected = IllegalStateException.class) - public void fail_when_no_value() throws Exception { - BatchReport.Measure measure = BatchReport.Measure.newBuilder() - .setMetricKey("repo:metric-key") - .build(); - - checkMeasure(measure); - } - - @Test(expected = IllegalStateException.class) - public void fail_when_no_consistency_between_string_value_and_numerical_value_type() throws Exception { - BatchReport.Measure measure = BatchReport.Measure.newBuilder() - .setValueType(DOUBLE) - .setStringValue("string-value") - .build(); - - checkMeasure(measure); - } - - @Test(expected = IllegalStateException.class) - public void fail_when_no_consistence_between_numerical_value_types() throws Exception { - BatchReport.Measure measure = BatchReport.Measure.newBuilder() - .setValueType(DOUBLE) - .setIntValue(3) - .build(); - - checkMeasure(measure); - } - - @Test - public void validate_all_value_type_without_exception_thrown() throws Exception { - checkMeasure(newBuilder().setValueType(STRING).setStringValue("string-value").build()); - checkMeasure(newBuilder().setValueType(DOUBLE).setDoubleValue(1.0d).build()); - checkMeasure(newBuilder().setValueType(Constants.MeasureValueType.INT).setIntValue(1).build()); - checkMeasure(newBuilder().setValueType(Constants.MeasureValueType.LONG).setLongValue(2L).build()); - checkMeasure(newBuilder().setValueType(BOOLEAN).setBooleanValue(true).build()); - } - - @Test - public void value_type_as_double_correct_for_all_numerical_types() throws Exception { - assertThat(valueAsDouble(newBuilder().setBooleanValue(true).setValueType(BOOLEAN).build())).isEqualTo(1.0d); - assertThat(valueAsDouble(newBuilder().setIntValue(2).setValueType(INT).build())).isEqualTo(2.0d); - assertThat(valueAsDouble(newBuilder().setLongValue(3L).setValueType(LONG).build())).isEqualTo(3.0d); - assertThat(valueAsDouble(newBuilder().setDoubleValue(4.4d).setValueType(DOUBLE).build())).isEqualTo(4.4d); - } - - private BatchReport.Measure.Builder newBuilder() { - return BatchReport.Measure.newBuilder().setMetricKey("repo:metric-key"); - } -}
\ No newline at end of file diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistMeasuresStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistMeasuresStepTest.java index 0e5aea549c7..1925596643d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistMeasuresStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistMeasuresStepTest.java @@ -30,6 +30,7 @@ import org.mockito.Mockito; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; import org.sonar.batch.protocol.Constants; +import org.sonar.batch.protocol.Constants.MeasureValueType; import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReportReader; import org.sonar.batch.protocol.output.BatchReportWriter; @@ -49,7 +50,10 @@ import java.util.Date; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class PersistMeasuresStepTest extends BaseStepTest { @@ -63,6 +67,8 @@ public class PersistMeasuresStepTest extends BaseStepTest { PersistMeasuresStep sut; + private BatchReport.Component component; + @Before public void setUp() throws Exception { dbClient = mock(DbClient.class, Mockito.RETURNS_DEEP_STUBS); @@ -73,6 +79,8 @@ public class PersistMeasuresStepTest extends BaseStepTest { when(ruleCache.get(any(RuleKey.class)).getId()).thenReturn(987); sut = new PersistMeasuresStep(dbClient, ruleCache, metricCache); + + component = defaultComponent().build(); } @Test @@ -172,8 +180,6 @@ public class PersistMeasuresStepTest extends BaseStepTest { .setCharactericId(123456) .build(); - BatchReport.Component component = defaultComponent().build(); - MeasureDto measure = sut.toMeasureDto(batchMeasure, component); assertThat(measure).isEqualToComparingFieldByField(expectedFullMeasure()); @@ -183,18 +189,131 @@ public class PersistMeasuresStepTest extends BaseStepTest { public void map_minimal_batch_measure() throws Exception { BatchReport.Measure batchMeasure = BatchReport.Measure.newBuilder() .setValueType(Constants.MeasureValueType.INT) - .setIntValue(123) - .setSeverity(Constants.Severity.INFO) .setMetricKey("metric-key") - .setRuleKey("repo:rule-key") .build(); - BatchReport.Component component = defaultComponent() + MeasureDto measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure).isEqualToComparingFieldByField(expectedMinimalistMeasure()); + } + + @Test + public void map_boolean_batch_measure() throws Exception { + BatchReport.Measure batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.BOOLEAN) + .setBooleanValue(true) + .setMetricKey("metric-key") .build(); MeasureDto measure = sut.toMeasureDto(batchMeasure, component); - assertThat(measure).isEqualToComparingFieldByField(expectedMinimalistMeasure()); + assertThat(measure.getValue()).isEqualTo(1.0); + + batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.BOOLEAN) + .setBooleanValue(false) + .setMetricKey("metric-key") + .build(); + + measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure.getValue()).isEqualTo(0.0); + + batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.BOOLEAN) + .setMetricKey("metric-key") + .build(); + + measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure.getValue()).isNull(); + } + + @Test + public void map_double_batch_measure() throws Exception { + BatchReport.Measure batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.DOUBLE) + .setDoubleValue(3.2) + .setMetricKey("metric-key") + .build(); + + MeasureDto measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure.getValue()).isEqualTo(3.2); + + batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.DOUBLE) + .setMetricKey("metric-key") + .build(); + + measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure.getValue()).isNull(); + } + + @Test + public void map_int_batch_measure() throws Exception { + BatchReport.Measure batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.INT) + .setIntValue(3) + .setMetricKey("metric-key") + .build(); + + MeasureDto measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure.getValue()).isEqualTo(3.0); + + batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.INT) + .setMetricKey("metric-key") + .build(); + + measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure.getValue()).isNull(); + } + + @Test + public void map_long_batch_measure() throws Exception { + BatchReport.Measure batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.LONG) + .setLongValue(3L) + .setMetricKey("metric-key") + .build(); + + MeasureDto measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure.getValue()).isEqualTo(3.0); + + batchMeasure = BatchReport.Measure.newBuilder() + .setValueType(Constants.MeasureValueType.LONG) + .setMetricKey("metric-key") + .build(); + + measure = sut.toMeasureDto(batchMeasure, component); + + assertThat(measure.getValue()).isNull(); + } + + @Test(expected = IllegalStateException.class) + public void fail_when_no_metric_key() throws Exception { + BatchReport.Measure measure = BatchReport.Measure.newBuilder() + .setValueType(MeasureValueType.STRING) + .setStringValue("string-value") + .build(); + BatchReport.Component component = defaultComponent() + .build(); + sut.toMeasureDto(measure, component); + } + + @Test(expected = IllegalStateException.class) + public void fail_when_no_value() throws Exception { + BatchReport.Measure measure = BatchReport.Measure.newBuilder() + .setMetricKey("repo:metric-key") + .build(); + BatchReport.Component component = defaultComponent() + .build(); + sut.toMeasureDto(measure, component); } private MeasureDto expectedFullMeasure() { @@ -221,10 +340,7 @@ public class PersistMeasuresStepTest extends BaseStepTest { return new MeasureDto() .setComponentId(2L) .setSnapshotId(3L) - .setValue(123d) - .setSeverity(Severity.INFO) - .setMetricId(654) - .setRuleId(987); + .setMetricId(654); } @Override |