backfilled tests for org.jclouds.rest.binders

This commit is contained in:
Adrian Cole 2011-10-22 22:34:45 +02:00
parent 926989c5f1
commit 3bd0be2c24
10 changed files with 326 additions and 35 deletions

View File

@ -44,7 +44,7 @@ public class BindAsHostPrefix implements Binder {
@Inject
public BindAsHostPrefix(Provider<UriBuilder> uriBuilderProvider) {
this.uriBuilderProvider = uriBuilderProvider;
this.uriBuilderProvider = checkNotNull(uriBuilderProvider, "uriBuilderProvider");
}
@Override

View File

@ -20,6 +20,7 @@ package org.jclouds.rest.binders;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.http.utils.ModifyRequest.replaceMatrixParam;
import java.util.Map;
import java.util.Map.Entry;
@ -29,7 +30,6 @@ import javax.inject.Provider;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.utils.ModifyRequest;
import org.jclouds.rest.Binder;
/**
@ -42,16 +42,16 @@ public class BindMapToMatrixParams implements Binder {
@Inject
BindMapToMatrixParams(Provider<UriBuilder> builder) {
this.builder = builder;
this.builder = checkNotNull(builder, "builder");
}
@SuppressWarnings("unchecked") @Override
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
checkArgument(checkNotNull(input, "input") instanceof Map,
"this binder is only valid for Maps!");
checkArgument(checkNotNull(input, "input") instanceof Map, "this binder is only valid for Maps!");
Map<String, String> map = (Map<String, String>) input;
for (Entry<String, String> entry : map.entrySet()) {
request = ModifyRequest.replaceMatrixParam(request, entry.getKey(), entry.getValue(), builder.get());
request = replaceMatrixParam(request, entry.getKey(), entry.getValue(), builder.get());
}
return request;
}

View File

@ -18,6 +18,10 @@
*/
package org.jclouds.rest.binders;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.io.Payloads.newStringPayload;
import java.net.URI;
import java.util.Map;
@ -27,7 +31,6 @@ import javax.inject.Singleton;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.http.HttpRequest;
import org.jclouds.io.Payloads;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.annotations.Payload;
import org.jclouds.rest.internal.GeneratedHttpRequest;
@ -42,22 +45,25 @@ public class BindMapToStringPayload implements MapBinder {
@Inject
public BindMapToStringPayload(Provider<UriBuilder> uriBuilders) {
this.uriBuilders = uriBuilders;
this.uriBuilders = checkNotNull(uriBuilders, "uriBuilders");
}
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
GeneratedHttpRequest<?> r = GeneratedHttpRequest.class.cast(request);
checkNotNull(postParams, "postParams");
GeneratedHttpRequest<?> r = GeneratedHttpRequest.class.cast(checkNotNull(request, "request"));
checkArgument(r.getJavaMethod().isAnnotationPresent(Payload.class),
"method %s must have @Payload annotation to use this binder", r.getJavaMethod());
String payload = r.getJavaMethod().getAnnotation(Payload.class).value();
if (postParams.size() > 0) {
UriBuilder builder = uriBuilders.get();
builder.uri(URI.create("http://test/"));
builder.uri(URI.create("http://fake/"));
builder.path(payload);
URI fake = builder.buildFromMap(postParams);
payload = fake.getPath().substring(1);
}
return (R) request.toBuilder().payload(Payloads.newStringPayload(payload)).build();
return (R) request.toBuilder().payload(newStringPayload(payload)).build();
}
@Override

View File

@ -19,9 +19,10 @@
package org.jclouds.rest;
import static com.google.common.base.Throwables.propagate;
import static com.google.inject.util.Types.newParameterizedType;
import static org.easymock.classextension.EasyMock.createMock;
import static org.jclouds.http.HttpUtils.sortAndConcatHeadersIntoString;
import static org.eclipse.jetty.http.HttpHeaders.TRANSFER_ENCODING;
import static org.jclouds.http.HttpUtils.sortAndConcatHeadersIntoString;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
@ -29,8 +30,6 @@ import java.io.IOException;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.Constants;
import org.jclouds.concurrent.MoreExecutors;
import org.jclouds.concurrent.config.ConfiguresExecutorService;
@ -41,6 +40,7 @@ import org.jclouds.http.TransformingHttpCommandExecutorService;
import org.jclouds.http.config.ConfiguresHttpCommandExecutorService;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.io.MutableContentMetadata;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.rest.functions.MapHttp4xxCodesToExceptions;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.util.Strings2;
@ -48,6 +48,7 @@ import org.testng.annotations.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;
/**
@ -157,4 +158,10 @@ public abstract class BaseRestClientTest {
parserClass);
}
@SuppressWarnings("unchecked")
protected <T> RestAnnotationProcessor<T> factory(Class<T> clazz) {
return ((RestAnnotationProcessor<T>) injector.getInstance(Key.get(newParameterizedType(
RestAnnotationProcessor.class, clazz))));
}
}

View File

@ -0,0 +1,61 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.rest.binders;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.http.HttpRequest;
import org.testng.annotations.Test;
import com.google.inject.util.Providers;
import com.sun.jersey.api.uri.UriBuilderImpl;
/**
* Tests behavior of {@code BindAsHostPrefix}
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during
// surefire
@Test(groups = "unit", testName = "BindAsHostPrefixTest")
public class BindAsHostPrefixIfConfiguredTest {
public void testPrefixValid() {
BindAsHostPrefix binder = new BindAsHostPrefix(Providers.<UriBuilder> of(new UriBuilderImpl()));
HttpRequest request = binder.bindToRequest(new HttpRequest("GET", URI.create("https://s3.amazonaws.com")),
"bucket");
assertEquals(request.getRequestLine(), "GET https://bucket.s3.amazonaws.com HTTP/1.1");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testPrefixInvalidHostname() {
BindAsHostPrefix binder = new BindAsHostPrefix(Providers.<UriBuilder> of(new UriBuilderImpl()));
binder.bindToRequest(new HttpRequest("GET", URI.create("https://s3.amazonaws.com")), "b_ucket");
}
}

View File

@ -23,7 +23,6 @@ import static org.testng.Assert.assertEquals;
import java.io.File;
import java.net.URI;
import javax.inject.Provider;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.UriBuilder;
@ -31,6 +30,7 @@ import org.jclouds.http.HttpRequest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.inject.util.Providers;
import com.sun.jersey.api.uri.UriBuilderImpl;
/**
@ -45,14 +45,7 @@ public class BindMapToMatrixParamsTest {
public void testCorrect() throws SecurityException, NoSuchMethodException {
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
BindMapToMatrixParams binder = new BindMapToMatrixParams(new Provider<UriBuilder>() {
@Override
public UriBuilder get() {
return new UriBuilderImpl();
}
});
BindMapToMatrixParams binder = new BindMapToMatrixParams(Providers.<UriBuilder> of(new UriBuilderImpl()));
assertEquals(binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2")), HttpRequest
.builder().method("GET").endpoint(URI.create("http://momma/;imageName=foo;serverId=2")).build());
@ -61,14 +54,14 @@ public class BindMapToMatrixParamsTest {
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustBeMap() {
BindMapToMatrixParams binder = new BindMapToMatrixParams(null);
BindMapToMatrixParams binder = new BindMapToMatrixParams(Providers.<UriBuilder> of(new UriBuilderImpl()));
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, new File("foo"));
}
@Test(expectedExceptions = { NullPointerException.class, IllegalStateException.class })
@Test(expectedExceptions = NullPointerException.class)
public void testNullIsBad() {
BindMapToMatrixParams binder = new BindMapToMatrixParams(null);
BindMapToMatrixParams binder = new BindMapToMatrixParams(Providers.<UriBuilder> of(new UriBuilderImpl()));
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
binder.bindToRequest(request, null);
}

View File

@ -0,0 +1,94 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.rest.binders;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URI;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.inject.util.Providers;
import com.sun.jersey.api.uri.UriBuilderImpl;
/**
* Tests behavior of {@code BindMapToStringPayload}
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class BindMapToStringPayloadTest {
static interface TestPayload {
@org.jclouds.rest.annotations.Payload("name {fooble}")
void testPayload(@PathParam("foo") String path);
void noPayload(String path);
}
@Test
public void testCorrect() throws SecurityException, NoSuchMethodException {
Method testPayload = TestPayload.class.getMethod("testPayload", String.class);
GeneratedHttpRequest<TestPayload> request = GeneratedHttpRequest.<TestPayload> builder()
.declaring(TestPayload.class).javaMethod(testPayload).args(ImmutableList.<Object> of("robot"))
.method(HttpMethod.POST).endpoint(URI.create("http://localhost")).build();
GeneratedHttpRequest<TestPayload> newRequest = binder()
.bindToRequest(request, ImmutableMap.of("fooble", "robot"));
assertEquals(newRequest.getRequestLine(), request.getRequestLine());
assertEquals(newRequest.getPayload().getRawContent(), "name robot");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustHavePayloadAnnotation() throws SecurityException, NoSuchMethodException {
Method noPayload = TestPayload.class.getMethod("noPayload", String.class);
GeneratedHttpRequest<TestPayload> request = GeneratedHttpRequest.<TestPayload> builder()
.declaring(TestPayload.class).javaMethod(noPayload).args(ImmutableList.<Object> of("robot"))
.method(HttpMethod.POST).endpoint(URI.create("http://localhost")).build();
binder().bindToRequest(request, ImmutableMap.of("fooble", "robot"));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testMustBeMap() {
BindMapToStringPayload binder = binder();
HttpRequest request = new HttpRequest(HttpMethod.POST, URI.create("http://localhost"));
binder.bindToRequest(request, new File("foo"));
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullIsBad() {
BindMapToStringPayload binder = binder();
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
binder.bindToRequest(request, null);
}
public BindMapToStringPayload binder() {
return new BindMapToStringPayload(Providers.<UriBuilder> of(new UriBuilderImpl()));
}
}

View File

@ -0,0 +1,71 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.rest.binders;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.net.URI;
import org.jclouds.http.HttpRequest;
import org.jclouds.json.Json;
import org.jclouds.json.internal.GsonWrapper;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
/**
* Tests behavior of {@code BindToJsonPayload}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "BindToJsonPayloadTest")
public class BindToJsonPayloadTest {
Json json = new GsonWrapper(new Gson());
@Test
public void testMap() throws SecurityException, NoSuchMethodException {
BindToJsonPayload binder = new BindToJsonPayload(json);
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
request = binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2"));
assertEquals(request.getPayload().getRawContent(), "{\"imageName\":\"foo\",\"serverId\":\"2\"}");
assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/json");
}
@Test
public void testSomethingNotAMap() throws SecurityException, NoSuchMethodException {
BindToJsonPayload binder = new BindToJsonPayload(json);
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
request = binder.bindToRequest(request, new File("foo"));
assertEquals(request.getPayload().getRawContent(), "{\"path\":\"foo\"}");
assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/json");
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullIsBad() {
BindToJsonPayload binder = new BindToJsonPayload(json);
binder.bindToRequest(HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build(), null);
}
}

View File

@ -0,0 +1,66 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.rest.binders;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.net.URI;
import org.jclouds.http.HttpRequest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
/**
* Tests behavior of {@code BindToStringPayload}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "BindToStringPayloadTest")
public class BindToStringPayloadTest {
@Test
public void testMap() throws SecurityException, NoSuchMethodException {
BindToStringPayload binder = new BindToStringPayload();
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
request = binder.bindToRequest(request, ImmutableMap.of("imageName", "foo", "serverId", "2"));
assertEquals(request.getPayload().getRawContent(), "{imageName=foo, serverId=2}");
assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/unknown");
}
@Test
public void testSomethingNotAMap() throws SecurityException, NoSuchMethodException {
BindToStringPayload binder = new BindToStringPayload();
HttpRequest request = HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build();
request = binder.bindToRequest(request, new File("foo"));
assertEquals(request.getPayload().getRawContent(), "foo");
assertEquals(request.getPayload().getContentMetadata().getContentType(), "application/unknown");
}
@Test(expectedExceptions = NullPointerException.class)
public void testNullIsBad() {
BindToStringPayload binder = new BindToStringPayload();
binder.bindToRequest(HttpRequest.builder().method("GET").endpoint(URI.create("http://momma")).build(), null);
}
}

View File

@ -20,7 +20,6 @@ package org.jclouds.rest.internal;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.inject.util.Types.newParameterizedType;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.reportMatcher;
@ -55,7 +54,6 @@ import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.jclouds.javax.annotation.Nullable;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Qualifier;
@ -108,6 +106,7 @@ import org.jclouds.http.options.HttpRequestOptions;
import org.jclouds.io.Payload;
import org.jclouds.io.PayloadEnclosing;
import org.jclouds.io.Payloads;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.AsyncClientFactory;
import org.jclouds.rest.AuthorizationException;
@ -161,7 +160,6 @@ import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.AbstractModule;
import com.google.inject.ConfigurationException;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
@ -2457,11 +2455,6 @@ public class RestAnnotationProcessorTest extends BaseRestClientTest {
assertEquals(form, "x-amz-copy-source=/eggs/robot");
}
@SuppressWarnings("unchecked")
private <T> RestAnnotationProcessor<T> factory(Class<T> clazz) {
return ((RestAnnotationProcessor<T>) injector.getInstance(Key.get(newParameterizedType(
RestAnnotationProcessor.class, clazz))));
}
DateService dateService = new SimpleDateFormatDateService();