]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6610 WS custom_measures/create work duration sends the raw data (number of...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 18 Jun 2015 09:58:13 +0000 (11:58 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 18 Jun 2015 11:47:22 +0000 (13:47 +0200)
server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java
server/sonar-server/src/main/java/org/sonar/server/util/LongTypeValidation.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/util/MetricWorkDurationTypeValidation.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java
server/sonar-server/src/test/java/org/sonar/server/util/LongTypeValidationTest.java [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/l10n/core.properties
sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java

index 39b9428308dc1f015bbbedf7b7ffc0b51e4d69bd..8d49958fb5d7a9b26e4b155ae19a62193dd0b11b 100644 (file)
@@ -27,7 +27,6 @@ import org.sonar.api.measures.Metric;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.Durations;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.api.web.UserRole;
@@ -66,14 +65,12 @@ public class CreateAction implements CustomMeasuresWsAction {
   private final UserSession userSession;
   private final System2 system;
   private final TypeValidations typeValidations;
-  private final Durations durations;
 
-  public CreateAction(DbClient dbClient, UserSession userSession, System2 system, TypeValidations typeValidations, Durations durations) {
+  public CreateAction(DbClient dbClient, UserSession userSession, System2 system, TypeValidations typeValidations) {
     this.dbClient = dbClient;
     this.userSession = userSession;
     this.system = system;
     this.typeValidations = typeValidations;
-    this.durations = durations;
   }
 
   @Override
@@ -216,8 +213,8 @@ public class CreateAction implements CustomMeasuresWsAction {
   }
 
   private void checkAndSetWorkDurationMeasureValue(CustomMeasureDto measure, String valueAsString) {
-    typeValidations.validate(valueAsString, PropertyType.METRIC_WORK_DURATION.name(), null);
-    measure.setValue(durations.decode(valueAsString).toMinutes());
+    typeValidations.validate(valueAsString, PropertyType.LONG.name(), null);
+    measure.setValue(Long.parseLong(valueAsString));
   }
 
   private void checkAndSetIntegerMeasureValue(CustomMeasureDto measure, String valueAsString) {
@@ -295,7 +292,7 @@ public class CreateAction implements CustomMeasuresWsAction {
       case DISTRIB:
         return "type: string";
       case WORK_DUR:
-        return "duration format: 12d 5h 30min";
+        return "long representing the number of minutes";
       default:
         return "metric type not supported";
     }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/util/LongTypeValidation.java b/server/sonar-server/src/main/java/org/sonar/server/util/LongTypeValidation.java
new file mode 100644 (file)
index 0000000..99042ab
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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.util;
+
+import java.util.List;
+import javax.annotation.Nullable;
+import org.sonar.api.PropertyType;
+import org.sonar.server.exceptions.BadRequestException;
+
+public class LongTypeValidation implements TypeValidation {
+  @Override
+  public String key() {
+    return PropertyType.LONG.name();
+  }
+
+  @Override
+  public void validate(String value, @Nullable List<String> options) {
+    try {
+      Long.parseLong(value);
+    } catch (NumberFormatException e) {
+      throw new BadRequestException("errors.type.notLong", value);
+    }
+  }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/util/MetricWorkDurationTypeValidation.java b/server/sonar-server/src/main/java/org/sonar/server/util/MetricWorkDurationTypeValidation.java
deleted file mode 100644 (file)
index c2bbf3c..0000000
+++ /dev/null
@@ -1,49 +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.util;
-
-import java.util.List;
-import javax.annotation.Nullable;
-import org.sonar.api.PropertyType;
-import org.sonar.api.utils.Durations;
-import org.sonar.server.exceptions.BadRequestException;
-
-public class MetricWorkDurationTypeValidation implements TypeValidation {
-  private final Durations durations;
-
-  public MetricWorkDurationTypeValidation(Durations durations) {
-    this.durations = durations;
-  }
-
-  @Override
-  public String key() {
-    return PropertyType.METRIC_WORK_DURATION.name();
-  }
-
-  @Override
-  public void validate(String value, @Nullable List<String> options) {
-    try {
-      durations.decode(value);
-    } catch (IllegalArgumentException e) {
-      throw new BadRequestException("errors.type.notMetricWorkDuration", value);
-    }
-  }
-}
index 532d73e78b9f09c6c6aafe5e3cbb20690f217c85..a781f2c004de954b97a8dba1d754b3b06277732b 100644 (file)
@@ -30,11 +30,8 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
-import org.sonar.api.config.Settings;
-import org.sonar.api.i18n.I18n;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.measures.Metric.ValueType;
-import org.sonar.api.utils.Durations;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.measure.custom.db.CustomMeasureDto;
@@ -55,15 +52,14 @@ import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.util.BooleanTypeValidation;
 import org.sonar.server.util.FloatTypeValidation;
 import org.sonar.server.util.IntegerTypeValidation;
+import org.sonar.server.util.LongTypeValidation;
 import org.sonar.server.util.MetricLevelTypeValidation;
-import org.sonar.server.util.MetricWorkDurationTypeValidation;
 import org.sonar.server.util.TypeValidations;
 import org.sonar.server.ws.WsTester;
 import org.sonar.test.DbTests;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.data.Offset.offset;
-import static org.mockito.Mockito.mock;
 
 @Category(DbTests.class)
 public class CreateActionTest {
@@ -85,10 +81,9 @@ public class CreateActionTest {
   public void setUp() {
     dbClient = new DbClient(db.database(), db.myBatis(), new CustomMeasureDao(), new MetricDao(), new ComponentDao());
     dbSession = dbClient.openSession(false);
-    Durations durations = new Durations(mock(Settings.class), mock(I18n.class));
     TypeValidations typeValidations = new TypeValidations(Arrays.asList(new BooleanTypeValidation(), new IntegerTypeValidation(), new FloatTypeValidation(),
-      new MetricLevelTypeValidation(), new MetricWorkDurationTypeValidation(durations)));
-    ws = new WsTester(new CustomMeasuresWs(new CreateAction(dbClient, userSession, System2.INSTANCE, typeValidations, durations)));
+      new MetricLevelTypeValidation(), new LongTypeValidation()));
+    ws = new WsTester(new CustomMeasuresWs(new CreateAction(dbClient, userSession, System2.INSTANCE, typeValidations)));
     db.truncateTables();
     userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
   }
@@ -212,12 +207,12 @@ public class CreateActionTest {
     newRequest()
       .setParam(CreateAction.PARAM_PROJECT_ID, DEFAULT_PROJECT_UUID)
       .setParam(CreateAction.PARAM_METRIC_ID, metric.getId().toString())
-      .setParam(CreateAction.PARAM_VALUE, "4h 12min")
+      .setParam(CreateAction.PARAM_VALUE, "253")
       .execute();
 
     CustomMeasureDto customMeasure = dbClient.customMeasureDao().selectByMetricId(dbSession, metric.getId()).get(0);
     assertThat(customMeasure.getTextValue()).isNullOrEmpty();
-    assertThat(customMeasure.getValue()).isCloseTo(4 * 60 + 12, offset(0.01d));
+    assertThat(customMeasure.getValue()).isCloseTo(253, offset(0.01d));
   }
 
   @Test
index 4a7a34fed552e8eb3252c8e611dbec319c6dff40..e9fcb9309d1565f1a5d5b8d08cd7372b212dab33 100644 (file)
@@ -23,7 +23,6 @@ package org.sonar.server.measure.custom.ws;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.Durations;
 import org.sonar.api.utils.System2;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.user.UserSession;
@@ -42,7 +41,7 @@ public class CustomMeasuresWsTest {
     UserSession userSession = mock(UserSession.class);
     ws = new WsTester(new CustomMeasuresWs(
       new DeleteAction(dbClient, userSession),
-      new CreateAction(dbClient, userSession, System2.INSTANCE, mock(TypeValidations.class), mock(Durations.class))
+      new CreateAction(dbClient, userSession, System2.INSTANCE, mock(TypeValidations.class))
       ));
   }
 
diff --git a/server/sonar-server/src/test/java/org/sonar/server/util/LongTypeValidationTest.java b/server/sonar-server/src/test/java/org/sonar/server/util/LongTypeValidationTest.java
new file mode 100644 (file)
index 0000000..a3a4b5a
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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.util;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.PropertyType;
+import org.sonar.server.exceptions.BadRequestException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class LongTypeValidationTest {
+
+  LongTypeValidation sut = new LongTypeValidation();
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void key_is_long_type_name() {
+    assertThat(sut.key()).isEqualTo(PropertyType.LONG.name());
+  }
+
+  @Test
+  public void do_not_fail_with_long_values() {
+    sut.validate("1984", null);
+    sut.validate("-1984", null);
+  }
+
+  @Test
+  public void fail_when_float() {
+    expectedException.expect(BadRequestException.class);
+
+    sut.validate("3.14", null);
+  }
+
+  @Test
+  public void fail_when_string() {
+    expectedException.expect(BadRequestException.class);
+
+    sut.validate("original string", null);
+  }
+}
index ea25e50f3693f5e28be23da2a4b39d8578f3e2e5..f0c2966f74ca94bba2782192182d216e12ff4a57 100644 (file)
@@ -2823,10 +2823,10 @@ errors.is_not_valid={0} is not valid
 
 errors.type.notBoolean=Value '{0}' must be one of "true" or "false".
 errors.type.notInteger=Value '{0}' must be an integer.
+errors.type.notLong=Value '{0}' must be a long.
 errors.type.notFloat=Value '{0}' must be an floating point number.
 errors.type.notInOptions=Value '{0}' must be one of : {1}.
 errors.type.notMetricLevel=Value '{0}' must be one of "OK", "WARN", "ERROR".
-errors.type.notMetricWorkDuration=Value '{0}' is not well formatted.
 
 #------------------------------------------------------------------------------
 #
index 06c2e867c3a40c35bc720755f669b8512f102c5f..b32557f639c3d891ff17c650c16a5253d1890cd8 100644 (file)
@@ -94,7 +94,7 @@ public enum PropertyType {
   METRIC_LEVEL,
 
   /**
-   * Work duration metric type
+   * Long value, positivie or negative
    */
-  METRIC_WORK_DURATION
+  LONG
 }