Merge pull request #1078 from rackspace/jclouds-1077-queryparam-iterable

Added support for Iterable collections to QueryParam.
This commit is contained in:
Adrian Cole 2012-12-20 10:01:03 -08:00
commit 27dbd46409
3 changed files with 67 additions and 3 deletions

View File

@ -137,7 +137,7 @@ public class NodeApiExpectTest extends BaseCloudLoadBalancerApiExpectTest<CloudL
} }
public void testRemoveNodesFromLoadBalancer() { 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( NodeApi api = requestsSendResponses(
rackspaceAuthWithUsernameAndApiKey, rackspaceAuthWithUsernameAndApiKey,
responseWithAccess, responseWithAccess,

View File

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

@ -41,6 +41,7 @@ import java.net.URI;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -151,6 +152,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
@ -507,6 +509,11 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
@QueryParams(keys = { "foo", "fooble" }, values = { "bar", "baz" }) @QueryParams(keys = { "foo", "fooble" }, values = { "bar", "baz" })
public void foo3Nullable(@Nullable @QueryParam("robbie") String robbie) { public void foo3Nullable(@Nullable @QueryParam("robbie") String robbie) {
} }
@FOO
@Path("/")
public void queryParamIterable(@Nullable @QueryParam("foo") Iterable<String> bars) {
}
} }
public void testUnEncodeQuery() { public void testUnEncodeQuery() {
@ -564,6 +571,55 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
assertEquals(request.getEndpoint().getQuery(), "foo=bar&fooble=baz"); assertEquals(request.getEndpoint().getQuery(), "foo=bar&fooble=baz");
assertEquals(request.getMethod(), "FOO"); 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 { public interface TestPayloadParamVarargs {
@POST @POST