aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-06-18 11:58:13 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-06-18 13:47:22 +0200
commit1828a70ef3e1d50a2a84af31aa9834f24b801f03 (patch)
treee8cabe0dfb8a0578dde1db7de7ae157d5872120e
parentab4a412527f1e29c7056a2edde085126b3c702ab (diff)
downloadsonarqube-1828a70ef3e1d50a2a84af31aa9834f24b801f03.tar.gz
sonarqube-1828a70ef3e1d50a2a84af31aa9834f24b801f03.zip
SONAR-6610 WS custom_measures/create work duration sends the raw data (number of minutes)
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java11
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/util/LongTypeValidation.java (renamed from server/sonar-server/src/main/java/org/sonar/server/util/MetricWorkDurationTypeValidation.java)17
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java15
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java3
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/util/LongTypeValidationTest.java61
-rw-r--r--sonar-core/src/main/resources/org/sonar/l10n/core.properties2
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java4
7 files changed, 79 insertions, 34 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java
index 39b9428308d..8d49958fb5d 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java
@@ -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/MetricWorkDurationTypeValidation.java b/server/sonar-server/src/main/java/org/sonar/server/util/LongTypeValidation.java
index c2bbf3c201c..99042ab1e65 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/util/MetricWorkDurationTypeValidation.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/util/LongTypeValidation.java
@@ -23,27 +23,20 @@ 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;
- }
-
+public class LongTypeValidation implements TypeValidation {
@Override
public String key() {
- return PropertyType.METRIC_WORK_DURATION.name();
+ return PropertyType.LONG.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);
+ Long.parseLong(value);
+ } catch (NumberFormatException e) {
+ throw new BadRequestException("errors.type.notLong", value);
}
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java
index 532d73e78b9..a781f2c004d 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java
@@ -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
diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java
index 4a7a34fed55..e9fcb9309d1 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java
@@ -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
index 00000000000..a3a4b5a6a36
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/util/LongTypeValidationTest.java
@@ -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);
+ }
+}
diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties
index ea25e50f369..f0c2966f74c 100644
--- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties
+++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties
@@ -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.
#------------------------------------------------------------------------------
#
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java b/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java
index 06c2e867c3a..b32557f639c 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java
@@ -94,7 +94,7 @@ public enum PropertyType {
METRIC_LEVEL,
/**
- * Work duration metric type
+ * Long value, positivie or negative
*/
- METRIC_WORK_DURATION
+ LONG
}
Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
summaryrefslogtreecommitdiffstats
path: root/occ
blob: 7e47585b01ef78f4d8bc1c81b608ee50b29e56d6 (plain)
1
2
3
4
5
6
7
8
9
10
11