]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6620 add MeasureRepository.update method 376/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 15 Jun 2015 16:58:16 +0000 (18:58 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 15 Jun 2015 17:07:45 +0000 (19:07 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepository.java
server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepositoryImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryImplTest.java

index 5b63d22060be7a40ebbbec12bbddd55761006e9c..f20322aea01c1867038723ddcd2eae1459680ef4 100644 (file)
@@ -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);
 }
index 720a018aa404868e1cf348ba5ffdd43174a52043..0e54bd0080c8bcb87236cf7824262382d2f6660e 100644 (file)
@@ -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<Measure> 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<String, Measure> getRawMeasures(Component component) {
     Map<MeasureKey, Measure> rawMeasures = measures.get(component.getRef());
@@ -161,14 +190,14 @@ public class MeasureRepositoryImpl implements MeasureRepository {
 
   private Optional<Measure> findInBatch(Component component, final Metric metric) {
     BatchReport.Measure batchMeasure = Iterables.find(
-        reportReader.readComponentMeasures(component.getRef()),
-        new Predicate<BatchReport.Measure>() {
-          @Override
-          public boolean apply(@Nonnull BatchReport.Measure input) {
-            return input.getMetricKey().equals(metric.getKey());
-          }
+      reportReader.readComponentMeasures(component.getRef()),
+      new Predicate<BatchReport.Measure>() {
+        @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();
     }
   }
 }
index 191daf4d6ffd033ab969c615d2c95bdd52cbe36b..bbb373be52882970b07a125d4cb057eab3247952 100644 (file)
@@ -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 {