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;
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;
}
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;
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();
}