aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-11-06 11:25:35 +0100
committerGuillaume Jambet <guillaume.jambet@gmail.com>2017-11-08 13:51:31 +0100
commitcfed131070e85a90c1e41af492235b8cf488a8eb (patch)
tree4953dae05ca16ceb0a0aac21092dc2789e701be2 /sonar-plugin-api
parente9de4d178493dc10e411e9135159319e8e6873cb (diff)
downloadsonarqube-cfed131070e85a90c1e41af492235b8cf488a8eb.tar.gz
sonarqube-cfed131070e85a90c1e41af492235b8cf488a8eb.zip
SONAR-10040 add maximum value to ws API
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java30
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java59
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java24
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java14
4 files changed, 105 insertions, 22 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java
index 9d07ee5307a..02285cab9ba 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java
@@ -386,10 +386,11 @@ public interface WebService extends Definable<WebService.Context> {
public NewParam createPageSize(int defaultPageSize, int maxPageSize) {
return createParam(Param.PAGE_SIZE)
- .setDescription("Page size. Must be greater than 0 and less than " + maxPageSize)
- .setExampleValue("20")
.setDeprecatedKey("pageSize", "5.2")
- .setDefaultValue(String.valueOf(defaultPageSize));
+ .setDefaultValue(String.valueOf(defaultPageSize))
+ .setMaximumValue(maxPageSize)
+ .setDescription("Page size. Must be greater than 0 and less than " + maxPageSize)
+ .setExampleValue("20");
}
/**
@@ -635,6 +636,7 @@ public interface WebService extends Definable<WebService.Context> {
private Set<String> possibleValues = null;
private Integer maxValuesAllowed;
private Integer maximumLength;
+ private Integer maximumValue;
private NewParam(String key) {
this.key = key;
@@ -779,6 +781,14 @@ public interface WebService extends Definable<WebService.Context> {
return this;
}
+ /**
+ * @since 7.0
+ */
+ public NewParam setMaximumValue(@Nullable Integer maximumValue) {
+ this.maximumValue = maximumValue;
+ return this;
+ }
+
@Override
public String toString() {
return key;
@@ -834,6 +844,7 @@ public interface WebService extends Definable<WebService.Context> {
private final boolean internal;
private final Set<String> possibleValues;
private final Integer maximumLength;
+ private final Integer maximumValue;
private final Integer maxValuesAllowed;
protected Param(Action action, NewParam newParam) {
@@ -850,6 +861,7 @@ public interface WebService extends Definable<WebService.Context> {
this.possibleValues = newParam.possibleValues;
this.maxValuesAllowed = newParam.maxValuesAllowed;
this.maximumLength = newParam.maximumLength;
+ this.maximumValue = newParam.maximumValue;
checkArgument(!required || defaultValue == null, "Default value must not be set on parameter '%s?%s' as it's marked as required", action, key);
}
@@ -938,7 +950,7 @@ public interface WebService extends Definable<WebService.Context> {
}
/**
- * Specify the maximum number of values allowed when using this a parameter
+ * Specify the maximum number of values allowed when using {@link Request#multiParam(String)}
*
* @since 6.4
*/
@@ -956,6 +968,16 @@ public interface WebService extends Definable<WebService.Context> {
return maximumLength;
}
+ /**
+ * Specify the maximum value of the numeric variable used in this parameter
+ *
+ * @since 7.0
+ */
+ @CheckForNull
+ public Integer maximumValue() {
+ return maximumValue;
+ }
+
@Override
public String toString() {
return key;
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java
index 495cd8e5d82..a9141a3943d 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java
@@ -32,6 +32,7 @@ import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
import static com.google.common.base.Preconditions.checkArgument;
+import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
@@ -68,21 +69,15 @@ public abstract class ValidatingRequest extends Request {
@CheckForNull
public String param(String key) {
WebService.Param definition = action.param(key);
- String value = defaultString(readParam(key, definition), definition.defaultValue());
-
- Integer maximumLength = definition.maximumLength();
- if (value != null && maximumLength != null) {
- int valueLength = value.length();
- checkArgument(valueLength <= maximumLength,
- "'%s' length (%s) is longer than the maximum authorized (%s)",
- key, valueLength, maximumLength);
- }
-
- String trimmedValue = value == null ? null : CharMatcher.WHITESPACE.trimFrom(value);
- if (trimmedValue != null) {
- validateValue(trimmedValue, definition);
+ String valueOrDefault = defaultString(readParam(key, definition), definition.defaultValue());
+ String value = valueOrDefault == null ? null : CharMatcher.WHITESPACE.trimFrom(valueOrDefault);
+ if (value == null) {
+ return null;
}
- return trimmedValue;
+ validatePossibleValues(key, value, definition);
+ validateMaximumLength(key, definition, valueOrDefault);
+ validateMaximumValue(key, definition, value);
+ return value;
}
@Override
@@ -167,12 +162,42 @@ public abstract class ValidatingRequest extends Request {
private static List<String> validateValues(List<String> values, WebService.Param definition) {
Integer maximumValues = definition.maxValuesAllowed();
checkArgument(maximumValues == null || values.size() <= maximumValues, "'%s' can contains only %s values, got %s", definition.key(), maximumValues, values.size());
- values.forEach(value -> validateValue(value, definition));
+ values.forEach(value -> validatePossibleValues(definition.key(), value, definition));
return values;
}
- private static void validateValue(String value, WebService.Param definition) {
+ private static void validatePossibleValues(String key, String value, WebService.Param definition) {
Set<String> possibleValues = definition.possibleValues();
- checkArgument(possibleValues == null || possibleValues.contains(value), "Value of parameter '%s' (%s) must be one of: %s", definition.key(), value, possibleValues);
+ if (possibleValues == null) {
+ return;
+ }
+ checkArgument(possibleValues.contains(value), "Value of parameter '%s' (%s) must be one of: %s", key, value, possibleValues);
}
+
+ private static void validateMaximumLength(String key, WebService.Param definition, String valueOrDefault) {
+ Integer maximumLength = definition.maximumLength();
+ if (maximumLength == null) {
+ return;
+ }
+ int valueLength = valueOrDefault.length();
+ checkArgument(valueLength <= maximumLength, "'%s' length (%s) is longer than the maximum authorized (%s)", key, valueLength, maximumLength);
+ }
+
+ private static void validateMaximumValue(String key, WebService.Param definition, String value) {
+ Integer maximumValue = definition.maximumValue();
+ if (maximumValue == null) {
+ return;
+ }
+ int valueAsInt = validateAsNumeric(key, value);
+ checkArgument(valueAsInt <= maximumValue, "'%s' value (%s) must be less than %s", key, valueAsInt, maximumValue);
+ }
+
+ private static int validateAsNumeric(String key, String value) {
+ try {
+ return Integer.parseInt(value);
+ } catch (NumberFormatException exception) {
+ throw new IllegalStateException(format("'%s' value '%s' cannot be parsed as an integer", key, value), exception);
+ }
+ }
+
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java
index e95cf3077db..4f3ee5aaa69 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java
@@ -122,6 +122,28 @@ public class RequestTest {
}
@Test
+ public void maximum_value_ok() {
+ String param = "maximum_value_param";
+ defineParameterTestAction(newParam -> newParam.setMaximumValue(10), param);
+ String expected = "10";
+
+ String actual = underTest.setParam(param, expected).param(param);
+
+ assertThat(actual).isEqualTo(expected);
+ }
+
+ @Test
+ public void maximum_value_not_ok() {
+ String param = "maximum_value_param";
+ defineParameterTestAction(newParam -> newParam.setMaximumValue(10), param);
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage(format("'%s' value (11) must be less than 10", param));
+
+ underTest.setParam(param, "11").param(param);
+ }
+
+ @Test
public void required_param_as_strings() {
underTest.setParam("a_required_string", "foo,bar");
@@ -495,7 +517,7 @@ public class RequestTest {
@DataProvider
public static Object[][] date_times() {
- return new Object[][]{
+ return new Object[][] {
{"2014-05-27", parseDate("2014-05-27")},
{"2014-05-27T15:50:45+0100", parseDateTime("2014-05-27T15:50:45+0100")},
{null, null}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java
index 4b674ea392c..3cf9d04101f 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/ws/WebServiceTest.java
@@ -316,6 +316,20 @@ public class WebServiceTest {
}
@Test
+ public void param_with_maximum_value() {
+ ((WebService) context -> {
+ NewController newController = context.createController("api/custom_measures");
+ NewAction create = newDefaultAction(newController, "create");
+ create.createParam("numeric_value")
+ .setMaximumValue(10);
+ newController.done();
+ }).define(context);
+
+ WebService.Action action = context.controller("api/custom_measures").action("create");
+ assertThat(action.param("numeric_value").maximumValue()).isEqualTo(10);
+ }
+
+ @Test
public void fail_if_required_param_has_default_value() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Default value must not be set on parameter 'api/rule/create?key' as it's marked as required");