Added support for Iterable collections to QueryParam.

This commit is contained in:
Everett Toews 2012-12-19 17:06:44 -06:00 committed by Adrian Cole
parent 366b762e6b
commit 5a0a7c4b90
3 changed files with 66 additions and 3 deletions

View File

@ -137,7 +137,7 @@ public class NodeApiExpectTest extends BaseCloudLoadBalancerApiExpectTest<CloudL
}
public void testRemoveNodesFromLoadBalancer() {
URI endpoint = URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/123123/loadbalancers/2000/nodes?id=%5B410%2C%20411%5D");
URI endpoint = URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/123123/loadbalancers/2000/nodes?id=410&id=411");
NodeApi api = requestsSendResponses(
rackspaceAuthWithUsernameAndApiKey,
responseWithAccess,

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.rest.internal;
import static com.google.common.base.Functions.toStringFunction;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Collections2.filter;
@ -1318,8 +1319,15 @@ public class RestAnnotationProcessor<T> {
Set<Annotation> extractors = indexToParamExtractor.getUnchecked(entry.getKey());
String paramKey = ((QueryParam) key).value();
Optional<?> paramValue = getParamValue(method, args, extractors, entry, paramKey);
if (paramValue.isPresent())
queryParamValues.put(paramKey, paramValue.get().toString());
if (paramValue.isPresent()) {
if (paramValue.get() instanceof Iterable) {
Iterable<String> iterableStrings = transform(Iterable.class.cast(paramValue.get()), toStringFunction());
queryParamValues.putAll(paramKey, iterableStrings);
}
else {
queryParamValues.put(paramKey, paramValue.get().toString());
}
}
}
}

View File

@ -150,6 +150,7 @@ import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Multimap;
import com.google.common.io.Files;
import com.google.common.util.concurrent.Futures;
@ -506,6 +507,11 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
@QueryParams(keys = { "foo", "fooble" }, values = { "bar", "baz" })
public void foo3Nullable(@Nullable @QueryParam("robbie") String robbie) {
}
@FOO
@Path("/")
public void queryParamIterable(@Nullable @QueryParam("foo") Iterable<String> bars) {
}
}
public void testUnEncodeQuery() {
@ -563,6 +569,55 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
assertEquals(request.getEndpoint().getQuery(), "foo=bar&fooble=baz");
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableOneString() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
Set<String> bars = ImmutableSortedSet.<String> of("1");
HttpRequest request = factory(TestPath.class).createRequest(method, new Object[] { bars });
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), "foo=1");
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableString() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
Set<String> bars = ImmutableSortedSet.<String> of("1", "2", "3");
HttpRequest request = factory(TestPath.class).createRequest(method, new Object[] { bars });
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), "foo=1&foo=2&foo=3");
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableInteger() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
Set<Integer> bars = ImmutableSortedSet.<Integer> of(1, 2, 3);
HttpRequest request = factory(TestPath.class).createRequest(method, new Object[] { bars });
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), "foo=1&foo=2&foo=3");
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableEmpty() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
Set<String> bars = Collections.emptySet();
HttpRequest request = factory(TestPath.class).createRequest(method, new Object[] { bars });
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), null);
assertEquals(request.getMethod(), "FOO");
}
public void testQueryParamIterableNull() throws SecurityException, NoSuchMethodException {
Method method = TestQuery.class.getMethod("queryParamIterable", Iterable.class);
HttpRequest request = factory(TestPath.class).createRequest(method, (Iterable<String>) null);
assertEquals(request.getEndpoint().getHost(), "localhost");
assertEquals(request.getEndpoint().getPath(), "/");
assertEquals(request.getEndpoint().getQuery(), null);
assertEquals(request.getMethod(), "FOO");
}
public interface TestPayloadParamVarargs {
@POST