diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/EntityType.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/EntityType.java index ced3b93561..b66b2f8163 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/EntityType.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/EntityType.java @@ -143,6 +143,7 @@ public class EntityType> extends ResourceType { /** * {@inheritDoc} */ + @SuppressWarnings("unchecked") @Override public Builder fromResourceType(ResourceType in) { return Builder.class.cast(super.fromResourceType(in)); diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Metadata.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Metadata.java index 8589652fc1..11fab28010 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Metadata.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Metadata.java @@ -18,9 +18,9 @@ */ package org.jclouds.vcloud.director.v1_5.domain; -import static com.google.common.base.Objects.*; -import static com.google.common.base.Preconditions.*; -import static org.jclouds.vcloud.director.v1_5.VCloudDirectorConstants.*; +import static com.google.common.base.Objects.equal; +import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.vcloud.director.v1_5.VCloudDirectorConstants.VCLOUD_1_5_NS; import java.net.URI; import java.util.Set; @@ -29,7 +29,6 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; 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.ToStringHelper; diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/functions/ThrowVCloudErrorOn4xx.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/functions/ThrowVCloudErrorOn4xx.java index 9de694e36b..89d959711c 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/functions/ThrowVCloudErrorOn4xx.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/functions/ThrowVCloudErrorOn4xx.java @@ -45,7 +45,7 @@ public class ThrowVCloudErrorOn4xx implements Function { public Object apply(Exception from) { Iterable throwables = Iterables.filter(Throwables.getCausalChain(from), HttpResponseException.class); 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 { Error error = JAXB.unmarshal(InputSuppliers.of(exception.getContent()).getInput(), Error.class); throw new VCloudDirectorException(error); diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorRestClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorRestClientExpectTest.java index 29a4231d65..05c85183a8 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorRestClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorRestClientExpectTest.java @@ -18,7 +18,8 @@ */ 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; @@ -30,7 +31,9 @@ import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; import org.testng.annotations.BeforeGroups; +import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.Multimap; import com.google.inject.Guice; /** @@ -90,6 +93,16 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect 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) { return HttpRequest.builder() .method(method) @@ -100,7 +113,7 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect .build()) .build(); } - + protected HttpRequest getStandardRequestWithPayload(String method, String path, String relativeFilePath, String mediaType) { return getStandardRequestWithPayload(method, path, VCloudDirectorMediaType.ANY, relativeFilePath, mediaType); } @@ -136,4 +149,68 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect .payload(payloadFromResourceWithContentType(relativeFilePath, mediaType + ";version=1.5")) .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 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 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; + } + } }