changes to common convenience methods

This commit is contained in:
danikov 2012-02-15 00:47:32 +00:00
parent 403c63238b
commit b628daaa4e
1 changed files with 68 additions and 14 deletions

View File

@ -19,6 +19,7 @@
package org.jclouds.vcloud.director.v1_5.internal; package org.jclouds.vcloud.director.v1_5.internal;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI; import java.net.URI;
@ -30,7 +31,9 @@ import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.inject.Guice; import com.google.inject.Guice;
/** /**
@ -97,7 +100,7 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect
protected HttpRequest getStandardPayloadRequest(String method, URI uri, String relativeFilePath, protected HttpRequest getStandardPayloadRequest(String method, URI uri, String relativeFilePath,
String postMediaType) { String postMediaType) {
return getStandardPayloadRequest(method, uri, VCloudDirectorMediaType.ANY, relativeFilePath, postMediaType); return getStandardRequestWithPayload(method, uri, VCloudDirectorMediaType.ANY, relativeFilePath, postMediaType);
} }
protected HttpRequest getStandardRequest(String method, URI uri, String mediaType) { protected HttpRequest getStandardRequest(String method, URI uri, String mediaType) {
@ -111,19 +114,6 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect
.build(); .build();
} }
protected HttpRequest getStandardPayloadRequest(String method, URI uri, String mediaType,
String relativeFilePath, String postMediaType) {
return HttpRequest.builder()
.method(method)
.endpoint(uri)
.headers(ImmutableMultimap.<String, String> builder()
.put("Accept", mediaType)
.put("x-vcloud-authorization", token)
.build())
.payload(payloadFromResourceWithContentType(relativeFilePath, postMediaType))
.build();
}
protected HttpRequest getStandardRequestWithPayload(String method, String path, String relativeFilePath, String mediaType) { protected HttpRequest getStandardRequestWithPayload(String method, String path, String relativeFilePath, String mediaType) {
return getStandardRequestWithPayload(method, path, VCloudDirectorMediaType.ANY, relativeFilePath, mediaType); return getStandardRequestWithPayload(method, path, VCloudDirectorMediaType.ANY, relativeFilePath, mediaType);
} }
@ -159,4 +149,68 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect
.payload(payloadFromResourceWithContentType(relativeFilePath, mediaType + ";version=1.5")) .payload(payloadFromResourceWithContentType(relativeFilePath, mediaType + ";version=1.5"))
.build(); .build();
} }
/**
* Implicitly adds x-vcloud-authorization header with token.
* Provides convenience methods for priming a HttpRequest.Builder for vCloud testing
*
* @author danikov
*
*/
protected class VcloudHttpRequestPrimer {
private Multimap<String, String> headers = ArrayListMultimap.create();
private HttpRequest.Builder builder = HttpRequest.builder();
public VcloudHttpRequestPrimer() {
}
public VcloudHttpRequestPrimer apiCommand(String method, String command) {
builder.method(method).endpoint(URI.create(endpoint + command));
return this;
}
public VcloudHttpRequestPrimer xmlFilePayload(String relativeFilePath, String mediaType) {
builder.payload(payloadFromResourceWithContentType(relativeFilePath, mediaType));
return this;
}
public VcloudHttpRequestPrimer headers(Multimap<String, String> headers) {
this.headers.putAll(ImmutableMultimap.copyOf(checkNotNull(headers, "headers")));
return this;
}
public VcloudHttpRequestPrimer acceptAnyMedia() {
return acceptMedia(VCloudDirectorMediaType.ANY);
}
public VcloudHttpRequestPrimer acceptMedia(String media) {
return header("Accept", media);
}
public VcloudHttpRequestPrimer header(String name, String value) {
headers.put(checkNotNull(name, "header.name"), checkNotNull(value, "header.value"));
return this;
}
public HttpRequest.Builder httpRequestBuilder() {
header("x-vcloud-authorization", token);
builder.headers(headers);
return builder;
}
}
protected class VcloudHttpResponsePrimer {
private HttpResponse.Builder builder = HttpResponse.builder();
public VcloudHttpResponsePrimer() {
}
public VcloudHttpResponsePrimer xmlFilePayload(String relativeFilePath, String mediaType) {
builder.payload(payloadFromResourceWithContentType(relativeFilePath, mediaType));
return this;
}
public HttpResponse.Builder httpResponseBuilder() {
return builder;
}
}
} }