From: Sébastien Lesaint Date: Mon, 15 Jun 2015 16:58:16 +0000 (+0200) Subject: SONAR-6620 add MeasureRepository.update method X-Git-Tag: 5.2-RC1~1474 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2ec5140b9abc36c4c029984dd36b70e096fd2adf;p=sonarqube.git SONAR-6620 add MeasureRepository.update method --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepository.java b/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepository.java index 5b63d22060b..f20322aea01 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepository.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepository.java @@ -77,10 +77,19 @@ public interface MeasureRepository { /** * Adds the specified measure for the specified Component and Metric. There can be no more than one measure for a - * specific combination of Component and Metric. + * specific combination of Component, Metric and association to a specific rule or characteristic. * - * @throws NullPointerException if any of the argument is null + * @throws NullPointerException if any of the arguments is null * @throws UnsupportedOperationException when trying to add a measure when one already exists for the specified Component/Metric paar */ void add(Component component, Metric metric, Measure measure); + + /** + * Updates the specified measure for the specified Component and Metric. There can be no more than one measure for a + * specific combination of Component, Metric and association to a specific rule or characteristic. + * + * @throws NullPointerException if any of the arguments is null + * @throws UnsupportedOperationException when trying to update a non existing measure + */ + void update(Component component, Metric metric, Measure measure); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepositoryImpl.java index 720a018aa40..0e54bd0080c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepositoryImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepositoryImpl.java @@ -127,15 +127,44 @@ public class MeasureRepositoryImpl implements MeasureRepository { if (existingMeasure.isPresent()) { throw new UnsupportedOperationException( String.format( - "a measure can be set only once for a specific Component (ref=%s) and Metric (key=%s). Use update method", + "a measure can be set only once for a specific Component (ref=%s), Metric (key=%s)%s. Use update method", component.getRef(), - metric.getKey() + metric.getKey(), + buildRuleOrCharacteristicMsgPart(measure) )); } - // assuming the measure comes from batch addLocal(component, metric, measure); } + @Override + public void update(Component component, Metric metric, Measure measure) { + requireNonNull(component); + requireNonNull(metric); + requireNonNull(measure); + + Optional existingMeasure = findLocal(component, metric, measure); + if (!existingMeasure.isPresent()) { + throw new UnsupportedOperationException( + String.format( + "a measure can be updated only if one already exists for a specific Component (ref=%s), Metric (key=%s)%s. Use add method", + component.getRef(), + metric.getKey(), + buildRuleOrCharacteristicMsgPart(measure) + )); + } + addLocal(component, metric, measure); + } + + private static String buildRuleOrCharacteristicMsgPart(Measure measure) { + if (measure.getRuleId() != null) { + return " and rule (id=" + measure.getRuleId() + ")"; + } + if (measure.getCharacteristicId() != null) { + return " and Characteristic (id=" + measure.getCharacteristicId() + ")"; + } + return ""; + } + @Override public SetMultimap getRawMeasures(Component component) { Map rawMeasures = measures.get(component.getRef()); @@ -161,14 +190,14 @@ public class MeasureRepositoryImpl implements MeasureRepository { private Optional findInBatch(Component component, final Metric metric) { BatchReport.Measure batchMeasure = Iterables.find( - reportReader.readComponentMeasures(component.getRef()), - new Predicate() { - @Override - public boolean apply(@Nonnull BatchReport.Measure input) { - return input.getMetricKey().equals(metric.getKey()); - } + reportReader.readComponentMeasures(component.getRef()), + new Predicate() { + @Override + public boolean apply(@Nonnull BatchReport.Measure input) { + return input.getMetricKey().equals(metric.getKey()); } - , null); + } + , null); return batchMeasureToMeasure.toMeasure(batchMeasure, metric); } @@ -253,10 +282,10 @@ public class MeasureRepositoryImpl implements MeasureRepository { @Override public String toString() { return com.google.common.base.Objects.toStringHelper(this) - .add("metricKey", metricKey) - .add("ruleId", ruleId) - .add("characteristicId", characteristicId) - .toString(); + .add("metricKey", metricKey) + .add("ruleId", ruleId) + .add("characteristicId", characteristicId) + .toString(); } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryImplTest.java index 191daf4d6ff..bbb373be528 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryImplTest.java @@ -178,6 +178,64 @@ public class MeasureRepositoryImplTest { underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); } + @Test(expected = NullPointerException.class) + public void update_throws_NPE_if_Component_argument_is_null() { + underTest.update(null, metric1, SOME_MEASURE); + } + + @Test(expected = NullPointerException.class) + public void update_throws_NPE_if_Component_metric_is_null() { + underTest.update(FILE_COMPONENT, null, SOME_MEASURE); + } + + @Test(expected = NullPointerException.class) + public void update_throws_NPE_if_Component_measure_is_null() { + underTest.update(FILE_COMPONENT, metric1, null); + } + + @Test(expected = UnsupportedOperationException.class) + public void update_throws_UOE_if_measure_does_not_exists() { + underTest.update(FILE_COMPONENT, metric1, SOME_MEASURE); + } + + @Test + public void update_supports_updating_to_the_same_value() { + underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); + underTest.update(FILE_COMPONENT, metric1, SOME_MEASURE); + } + + @Test + public void update_updates_the_stored_value() { + Measure newMeasure = Measure.updateMeasure(SOME_MEASURE).create(); + + underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE); + underTest.update(FILE_COMPONENT, metric1, newMeasure); + + assertThat(underTest.getRawMeasure(FILE_COMPONENT, metric1).get()).isSameAs(newMeasure); + } + + @Test + public void update_updates_the_stored_value_for_rule() { + Measure initialMeasure = Measure.newMeasure().forRule(123).createNoValue(); + Measure newMeasure = Measure.updateMeasure(initialMeasure).create(); + + underTest.add(FILE_COMPONENT, metric1, initialMeasure); + underTest.update(FILE_COMPONENT, metric1, newMeasure); + + assertThat(underTest.getRawMeasures(FILE_COMPONENT).get(metric1.getKey()).iterator().next()).isSameAs(newMeasure); + } + + @Test + public void update_updates_the_stored_value_for_characteristic() { + Measure initialMeasure = Measure.newMeasure().forCharacteristic(952).createNoValue(); + Measure newMeasure = Measure.updateMeasure(initialMeasure).create(); + + underTest.add(FILE_COMPONENT, metric1, initialMeasure); + underTest.update(FILE_COMPONENT, metric1, newMeasure); + + assertThat(underTest.getRawMeasures(FILE_COMPONENT).get(metric1.getKey()).iterator().next()).isSameAs(newMeasure); + } + @Test public void getRawMeasure_throws_NPE_without_reading_batch_report_if_component_arg_is_null() { try {