Merge pull request #368 from danikov/vclouds-director-bugfixes

Issue 830: vCloud director bugfixes & common changes
This commit is contained in:
Andrei Savu 2012-02-14 22:45:18 -08:00
commit b0c17b8a9b
4 changed files with 84 additions and 7 deletions

View File

@ -143,6 +143,7 @@ public class EntityType<T extends EntityType<T>> extends ResourceType<T> {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@SuppressWarnings("unchecked")
@Override @Override
public Builder<T> fromResourceType(ResourceType<T> in) { public Builder<T> fromResourceType(ResourceType<T> in) {
return Builder.class.cast(super.fromResourceType(in)); return Builder.class.cast(super.fromResourceType(in));

View File

@ -18,9 +18,9 @@
*/ */
package org.jclouds.vcloud.director.v1_5.domain; package org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Objects.*; import static com.google.common.base.Objects.equal;
import static com.google.common.base.Preconditions.*; import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorConstants.*; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorConstants.VCLOUD_1_5_NS;
import java.net.URI; import java.net.URI;
import java.util.Set; import java.util.Set;
@ -29,7 +29,6 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry.Builder;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper; import com.google.common.base.Objects.ToStringHelper;

View File

@ -45,7 +45,7 @@ public class ThrowVCloudErrorOn4xx implements Function<Exception, Object> {
public Object apply(Exception from) { public Object apply(Exception from) {
Iterable<HttpResponseException> throwables = Iterables.filter(Throwables.getCausalChain(from), HttpResponseException.class); Iterable<HttpResponseException> throwables = Iterables.filter(Throwables.getCausalChain(from), HttpResponseException.class);
HttpResponseException exception = Iterables.getFirst(throwables, null); HttpResponseException exception = Iterables.getFirst(throwables, null);
if (exception != null && exception.getResponse().getStatusCode() >= 400 && exception.getResponse().getStatusCode() < 500) { if (exception != null && exception.getResponse() != null && exception.getResponse().getStatusCode() >= 400 && exception.getResponse().getStatusCode() < 500) {
try { try {
Error error = JAXB.unmarshal(InputSuppliers.of(exception.getContent()).getInput(), Error.class); Error error = JAXB.unmarshal(InputSuppliers.of(exception.getContent()).getInput(), Error.class);
throw new VCloudDirectorException(error); throw new VCloudDirectorException(error);

View File

@ -18,7 +18,8 @@
*/ */
package org.jclouds.vcloud.director.v1_5.internal; package org.jclouds.vcloud.director.v1_5.internal;
import static org.testng.Assert.*; 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;
/** /**
@ -90,6 +93,16 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect
return getStandardRequest(method, URI.create(endpoint + path), VCloudDirectorMediaType.ANY); return getStandardRequest(method, URI.create(endpoint + path), VCloudDirectorMediaType.ANY);
} }
protected HttpRequest getStandardPayloadRequest(String method, String command, String relativeFilePath,
String postMediaType) {
return getStandardPayloadRequest(method, URI.create(endpoint + command), relativeFilePath, postMediaType);
}
protected HttpRequest getStandardPayloadRequest(String method, URI uri, String relativeFilePath,
String 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) {
return HttpRequest.builder() return HttpRequest.builder()
.method(method) .method(method)
@ -136,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;
}
}
} }