From 11de4cfe580382a23ac664c5b4464d426b742a3e Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 1 Sep 2016 16:06:04 +0200 Subject: [PATCH] SONAR-8004 WS settings/set fix BaseRequest --- .../org/sonarqube/ws/client/BaseRequest.java | 49 ++++++++++++++----- .../sonarqube/ws/client/BaseRequestTest.java | 10 +++- 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 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 values = (Collection) 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 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; @@ -76,9 +77,16 @@ public class BaseRequestTest { entry("keyC", ImmutableList.of("c1", "c2", "c3"))); } + @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(); } -- 2.39.5