aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-08-25 16:13:59 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-08-26 16:01:46 +0200
commit5e04bd807b2b6f0ab6e213b5f786a10c35743166 (patch)
tree08c5c0bf23ea8a32c7e58cd55d9acfdd311d3736 /sonar-plugin-api
parentbf23163e41dcdb85b0610ffbfdcdd233d96a682b (diff)
downloadsonarqube-5e04bd807b2b6f0ab6e213b5f786a10c35743166.tar.gz
sonarqube-5e04bd807b2b6f0ab6e213b5f786a10c35743166.zip
SONAR-8003 WS API handles multi param
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java15
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java13
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java40
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/ws/RequestTest.java46
4 files changed, 102 insertions, 12 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java
index 48d9f427b2e..bf32ff65ad8 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java
@@ -19,8 +19,6 @@
*/
package org.sonar.api.server.ws;
-import static com.google.common.base.Preconditions.checkArgument;
-
import com.google.common.annotations.Beta;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
@@ -33,6 +31,8 @@ import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.SonarException;
+import static com.google.common.base.Preconditions.checkArgument;
+
/**
* @since 4.2
*/
@@ -109,6 +109,15 @@ public abstract class Request {
return values;
}
+ public List<String> mandatoryMultiParam(String key) {
+ List<String> values = multiParam(key);
+ if (values.isEmpty()) {
+ throw new IllegalArgumentException(String.format("The '%s' parameter is missing", key));
+ }
+
+ return values;
+ }
+
@CheckForNull
public List<String> paramAsStrings(String key) {
String value = param(key);
@@ -121,6 +130,8 @@ public abstract class Request {
@CheckForNull
public abstract String param(String key);
+ public abstract List<String> multiParam(String key);
+
@CheckForNull
public abstract InputStream paramAsInputStream(String key);
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java
index 9b2348f48d2..e207c01cedb 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/SimpleGetRequest.java
@@ -19,16 +19,19 @@
*/
package org.sonar.api.server.ws.internal;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.collect.Maps;
import java.io.InputStream;
+import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.commons.io.IOUtils;
import org.sonar.api.server.ws.LocalConnector;
import org.sonar.api.server.ws.Request;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
+
/**
* Fake implementation of {@link org.sonar.api.server.ws.Request} used
* for testing. Call the method {@link #setParam(String, String)} to
@@ -68,6 +71,12 @@ public class SimpleGetRequest extends Request {
}
@Override
+ public List<String> multiParam(String key) {
+ String value = params.get(key);
+ return value == null ? emptyList() : singletonList(value);
+ }
+
+ @Override
public InputStream paramAsInputStream(String key) {
return IOUtils.toInputStream(param(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 fd1c2e2270d..389c6e62d3a 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
@@ -19,8 +19,6 @@
*/
package org.sonar.api.server.ws.internal;
-import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
@@ -36,6 +34,10 @@ import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.log.Loggers;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.Collections.emptyList;
+import static java.util.Collections.singletonList;
+
/**
* @since 4.2
*/
@@ -69,6 +71,16 @@ public abstract class ValidatingRequest extends Request {
}
@Override
+ public List<String> multiParam(String key) {
+ WebService.Param definition = action.param(key);
+ List<String> values = readMultiParamOrDefaultValue(key, definition);
+
+ values.forEach(value -> validate(value, definition));
+
+ return values;
+ }
+
+ @Override
@CheckForNull
public InputStream paramAsInputStream(String key) {
return readInputStreamParam(key);
@@ -139,9 +151,33 @@ public abstract class ValidatingRequest extends Request {
return value;
}
+ private List<String> readMultiParamOrDefaultValue(String key, @Nullable WebService.Param definition) {
+ if (definition == null) {
+ String message = String.format("BUG - parameter '%s' is undefined for action '%s'", key, action.key());
+ Loggers.get(getClass()).error(message);
+ throw new IllegalArgumentException(message);
+ }
+
+ List<String> keyValues = readMultiParam(key);
+ if (!keyValues.isEmpty()) {
+ return keyValues;
+ }
+
+ String deprecatedKey = definition.deprecatedKey();
+ List<String> deprecatedKeyValues = deprecatedKey == null ? emptyList() : readMultiParam(deprecatedKey);
+ if (!deprecatedKeyValues.isEmpty()) {
+ return deprecatedKeyValues;
+ }
+
+ String defaultValue = definition.defaultValue();
+ return defaultValue == null ? emptyList() : singletonList(defaultValue);
+ }
+
@CheckForNull
protected abstract String readParam(String key);
+ protected abstract List<String> readMultiParam(String key);
+
@CheckForNull
protected abstract InputStream readInputStreamParam(String key);
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 8f6613176c5..d5580b1681a 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
@@ -19,11 +19,11 @@
*/
package org.sonar.api.server.ws;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-
-import com.google.common.collect.Maps;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
import java.io.InputStream;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.commons.io.IOUtils;
@@ -36,6 +36,10 @@ import org.sonar.api.server.ws.internal.PartImpl;
import org.sonar.api.server.ws.internal.ValidatingRequest;
import org.sonar.api.utils.DateUtils;
+import static com.google.common.collect.Lists.newArrayList;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
public class RequestTest {
@Rule
@@ -97,6 +101,23 @@ public class RequestTest {
}
@Test
+ public void mandatory_multi_param() {
+ underTest.setMultiParam("a_required_multi_param", newArrayList("firstValue", "secondValue", "thirdValue"));
+
+ List<String> result = underTest.mandatoryMultiParam("a_required_multi_param");
+
+ assertThat(result).containsExactly("firstValue", "secondValue", "thirdValue");
+ }
+
+ @Test
+ public void fail_when_no_multi_param() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("The 'a_required_multi_param' parameter is missing");
+
+ underTest.mandatoryMultiParam("a_required_multi_param");
+ }
+
+ @Test
public void default_value_of_optional_param() {
assertThat(underTest.param("has_default_string")).isEqualTo("the_default_string");
}
@@ -282,8 +303,9 @@ public class RequestTest {
private static class FakeRequest extends ValidatingRequest {
- private final Map<String, String> params = Maps.newHashMap();
- private final Map<String, Part> parts = Maps.newHashMap();
+ private final ListMultimap<String, String> multiParams = ArrayListMultimap.create();
+ private final Map<String, String> params = new HashMap<>();
+ private final Map<String, Part> parts = new HashMap<>();
@Override
public String method() {
@@ -312,12 +334,23 @@ public class RequestTest {
return this;
}
+ public FakeRequest setMultiParam(String key, List<String> values) {
+ multiParams.putAll(key, values);
+
+ return this;
+ }
+
@Override
protected String readParam(String key) {
return params.get(key);
}
@Override
+ protected List<String> readMultiParam(String key) {
+ return multiParams.get(key);
+ }
+
+ @Override
protected InputStream readInputStreamParam(String key) {
String param = readParam(key);
@@ -360,6 +393,7 @@ public class RequestTest {
action.createParam("a_required_boolean").setRequired(true);
action.createParam("a_required_number").setRequired(true);
action.createParam("a_required_enum").setRequired(true);
+ action.createParam("a_required_multi_param").setRequired(true);
action.createParam("has_default_string").setDefaultValue("the_default_string");
action.createParam("has_default_number").setDefaultValue("10");