aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws/src
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-09-01 16:06:04 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2016-09-01 16:06:04 +0200
commit11de4cfe580382a23ac664c5b4464d426b742a3e (patch)
tree270e9b80ab2f093b283baaaaadd09166ed53e3cc /sonar-ws/src
parentd65ffe61517e311faf355a4d4c8134d3e73091e5 (diff)
downloadsonarqube-11de4cfe580382a23ac664c5b4464d426b742a3e.tar.gz
sonarqube-11de4cfe580382a23ac664c5b4464d426b742a3e.zip
SONAR-8004 WS settings/set fix BaseRequest
Diffstat (limited to 'sonar-ws/src')
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java49
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java10
2 files changed, 46 insertions, 13 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java
index 57846b6f2c2..5357d581a7d 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseRequest.java
@@ -25,6 +25,7 @@ import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -68,24 +69,48 @@ abstract class BaseRequest<SELF extends BaseRequest> implements WsRequest {
return (SELF) this;
}
- /**
- * To set a multi value parameters, provide a Collection with the values
- */
- public SELF setParam(String key, @Nullable Object value) {
+ public SELF setParam(String key, @Nullable String value) {
+ return setSingleValueParam(key, value);
+ }
+
+ public SELF setParam(String key, @Nullable Integer value) {
+ return setSingleValueParam(key, value);
+ }
+
+ public SELF setParam(String key, @Nullable Long value) {
+ return setSingleValueParam(key, value);
+ }
+
+ public SELF setParam(String key, @Nullable Float value) {
+ return setSingleValueParam(key, value);
+ }
+
+ public SELF setParam(String key, @Nullable Boolean value) {
+ return setSingleValueParam(key, value);
+ }
+
+ private SELF setSingleValueParam(String key, @Nullable Object value) {
checkArgument(!isNullOrEmpty(key), "a WS parameter key cannot be null");
if (value == null) {
return (SELF) this;
}
+ parameters.setValue(key, value.toString());
+
+ return (SELF) this;
+ }
- if (value instanceof Collection) {
- Collection<Object> values = (Collection<Object>) value;
- if (values.isEmpty()) {
- return (SELF) this;
- }
- parameters.setValues(key, values.stream().map(Object::toString).collect(Collectors.toList()));
- } else {
- parameters.setValue(key, value.toString());
+ public SELF setParam(String key, @Nullable Collection<? extends Object> values) {
+ checkArgument(!isNullOrEmpty(key), "a WS parameter key cannot be null");
+ if (values == null || values.isEmpty()) {
+ return (SELF) this;
}
+
+ parameters.setValues(key, values.stream()
+ .filter(Objects::nonNull)
+ .map(Object::toString)
+ .filter(value -> !value.isEmpty())
+ .collect(Collectors.toList()));
+
return (SELF) this;
}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java
index 1abb05f2db3..ce2f0ad45f9 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java
@@ -28,6 +28,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonarqube.ws.MediaTypes;
+import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.MapEntry.entry;
@@ -77,8 +78,15 @@ public class BaseRequestTest {
}
@Test
+ public void skip_null_value_in_multi_param() {
+ underTest.setParam("key", newArrayList("v1", null, "v3"));
+
+ }
+
+ @Test
public void null_param_value() {
- underTest.setParam("key", null);
+ Boolean nullBool = null;
+ underTest.setParam("key", nullBool);
assertThat(underTest.getParams()).isEmpty();
}