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;
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
}
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) {
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";
}
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
+++ /dev/null
-/*
- * 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);
- }
- }
-}
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;
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 {
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);
}
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
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;
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))
));
}
--- /dev/null
+/*
+ * 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);
+ }
+}
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.
#------------------------------------------------------------------------------
#
METRIC_LEVEL,
/**
- * Work duration metric type
+ * Long value, positivie or negative
*/
- METRIC_WORK_DURATION
+ LONG
}