]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6572 WS metrics/update clean error message when trying to update with an existi...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 8 Jun 2015 07:55:10 +0000 (09:55 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 8 Jun 2015 07:55:21 +0000 (09:55 +0200)
server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java
server/sonar-server/src/test/java/org/sonar/server/metric/ws/UpdateActionTest.java

index 4a06fac6138c80a7343d2c10cb14d20a5bcb9dde..ecd309165d847c78f4adb6a5dce388ff5f83e089 100644 (file)
@@ -169,10 +169,10 @@ public class UpdateAction implements MetricsWsAction {
   }
 
   private void checkMetricInDbAndTemplate(DbSession dbSession, @Nullable MetricDto metricInDb, MetricDto template) {
-    if (isMetricFoundInDb(metricInDb) || isMetricDisabled(metricInDb) || isMetricNonCustom(metricInDb)) {
-      throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("No active custom metric has been found for id '%d'. Maybe you want to create a metric ?",
-        template.getId()));
+    if (!isMetricFoundInDb(metricInDb) || isMetricDisabled(metricInDb) || !isMetricCustom(metricInDb)) {
+      throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("No active custom metric has been found for id '%d'.", template.getId()));
     }
+    checkNoOtherMetricWithTargetKey(dbSession, metricInDb, template);
     if (haveMetricTypeChanged(metricInDb, template)) {
       List<CustomMeasureDto> customMeasures = dbClient.customMeasureDao().selectByMetricId(dbSession, metricInDb.getId());
       if (haveAssociatedCustomMeasures(customMeasures)) {
@@ -182,6 +182,13 @@ public class UpdateAction implements MetricsWsAction {
     }
   }
 
+  private void checkNoOtherMetricWithTargetKey(DbSession dbSession, MetricDto metricInDb, MetricDto template) {
+    MetricDto metricWithTargetKey = dbClient.metricDao().selectNullableByKey(dbSession, template.getKey());
+    if (isMetricFoundInDb(metricWithTargetKey) && !metricInDb.getId().equals(metricWithTargetKey.getId())) {
+      throw new ServerException(HttpURLConnection.HTTP_CONFLICT, "A me metric exists with the key: " + metricInDb.getKey());
+    }
+  }
+
   private static void writeMetric(JsonWriter json, MetricDto metric) {
     json.beginObject();
     json.prop(FIELD_ID, String.valueOf(metric.getId()));
@@ -193,8 +200,8 @@ public class UpdateAction implements MetricsWsAction {
     json.endObject();
   }
 
-  private static boolean isMetricNonCustom(MetricDto metricInDb) {
-    return !metricInDb.isUserManaged();
+  private static boolean isMetricCustom(MetricDto metricInDb) {
+    return metricInDb.isUserManaged();
   }
 
   private static boolean isMetricDisabled(MetricDto metricInDb) {
@@ -202,7 +209,7 @@ public class UpdateAction implements MetricsWsAction {
   }
 
   private static boolean isMetricFoundInDb(@Nullable MetricDto metricInDb) {
-    return metricInDb == null;
+    return metricInDb != null;
   }
 
   private static boolean haveAssociatedCustomMeasures(List<CustomMeasureDto> customMeasures) {
index 1c1a47238e8fceb405faa5aaaff7813492b90799..59a4ff55d0defdb0ac8ee43642c4bc5b3bd9f4ae 100644 (file)
@@ -140,6 +140,18 @@ public class UpdateActionTest {
     assertThat(requestResult.outputAsString()).matches(".*\"id\"\\s*:\\s*\"" + id + "\".*");
   }
 
+  @Test
+  public void fail_when_changing_key_for_an_existing_one() throws Exception {
+    expectedException.expect(ServerException.class);
+    insertMetric(newDefaultMetric().setKey("metric-key"));
+    int id = insertMetric(newDefaultMetric().setKey("another-key"));
+
+    newRequest()
+      .setParam(PARAM_ID, String.valueOf(id))
+      .setParam(PARAM_KEY, "metric-key")
+      .execute();
+  }
+
   @Test
   public void fail_when_metric_not_in_db() throws Exception {
     expectedException.expect(ServerException.class);