Completed TaskClient and tidied up OrgClient.

Note two types of API call here, one using traditional REST semantics and the other taking ReferenceTypes and extracting URIs from them.
This commit is contained in:
Andrew Donald Kennedy 2012-02-10 13:04:12 +00:00
parent 73a2593f8b
commit d29bd2eec8
12 changed files with 491 additions and 68 deletions

View File

@ -0,0 +1,35 @@
/**
* 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.vcloud.director.v1_5.domain;
import java.net.URI;
/**
* @author grkvlt@apache.org
*/
public interface URISupplier {
/**
* This returns the {@link URI} for a particular {@link ReferenceType} or {@link ResourceType} object.
*
* @see ResourceType#getHref()
* @see ReferenceType#getHref()
*/
public URI getURI();
}

View File

@ -23,6 +23,7 @@ import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.jclouds.rest.annotations.EndpointParam;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.JAXBResponseParser;
import org.jclouds.rest.annotations.RequestFilters;
@ -30,8 +31,10 @@ import org.jclouds.vcloud.director.v1_5.domain.Metadata;
import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry;
import org.jclouds.vcloud.director.v1_5.domain.Org;
import org.jclouds.vcloud.director.v1_5.domain.OrgList;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest;
import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xxOrNull;
import org.jclouds.vcloud.director.v1_5.functions.ReferenceToEndpoint;
import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xx;
import com.google.common.util.concurrent.ListenableFuture;
@ -43,7 +46,7 @@ import com.google.common.util.concurrent.ListenableFuture;
public interface OrgAsyncClient {
/**
* @see OrgClient#getOrgList
* @see OrgClient#getOrgList()
*/
@GET
@Path("/org")
@ -52,32 +55,61 @@ public interface OrgAsyncClient {
ListenableFuture<OrgList> getOrgList();
/**
* @see OrgClient#getOrg
* @see OrgClient#getOrg(String)
*/
@GET
@Path("/org/{id}")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xxOrNull.class)
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Org> getOrg(@PathParam("id") String orgId);
/**
* @see OrgClient#getOrg(ReferenceType)
*/
@GET
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Org> getOrg(@EndpointParam(parser = ReferenceToEndpoint.class) ReferenceType<?> orgRef);
/**
* @see OrgClient#getMetadata
* @see OrgClient#getMetadata(String)
*/
@GET
@Path("/org/{id}/metadata")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xxOrNull.class)
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Metadata> getMetadata(@PathParam("id") String orgId);
/**
* @see OrgClient#getMetadata(ReferenceType)
*/
@GET
@Path("/metadata")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Metadata> getMetadata(@EndpointParam(parser = ReferenceToEndpoint.class) ReferenceType<?> orgRef);
/**
* @see OrgClient#getMetadataEntry
* @see OrgClient#getMetadataEntry(String, String)
*/
@GET
@Path("/org/{id}/metadata/{key}")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xxOrNull.class)
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<MetadataEntry> getMetadataEntry(@PathParam("id") String orgId, @PathParam("key") String key);
/**
* @see OrgClient#getMetadataEntry(ReferenceType, String)
*/
@GET
@Path("/metadata/{key}")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<MetadataEntry> getMetadataEntry(@EndpointParam(parser = ReferenceToEndpoint.class) ReferenceType<?> orgRef, @PathParam("key") String key);
}

View File

@ -25,13 +25,13 @@ import org.jclouds.vcloud.director.v1_5.domain.Metadata;
import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry;
import org.jclouds.vcloud.director.v1_5.domain.Org;
import org.jclouds.vcloud.director.v1_5.domain.OrgList;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
/**
* Provides synchronous access to Org.
* <p/>
*
* @see OrgAsyncClient
* @see <a href= "http://support.theenterprisecloud.com/kb/default.asp?id=984&Lang=1&SID=" />
* @author Adrian Cole
*/
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
@ -50,6 +50,8 @@ public interface OrgClient {
* @return the org or null if not found
*/
Org getOrg(String orgId);
Org getOrg(ReferenceType<?> orgRef);
/**
* Retrieves an list of the organization's metadata
@ -58,10 +60,14 @@ public interface OrgClient {
*/
Metadata getMetadata(String orgId);
Metadata getMetadata(ReferenceType<?> orgRef);
/**
* Retrieves a metadata
*
* @return the metadata or null if not found
*/
MetadataEntry getMetadataEntry(String orgId, String key);
MetadataEntry getMetadataEntry(ReferenceType<?> orgRef, String key);
}

View File

@ -18,8 +18,6 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
import java.net.URI;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
@ -30,10 +28,12 @@ import org.jclouds.rest.annotations.EndpointParam;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.JAXBResponseParser;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import org.jclouds.vcloud.director.v1_5.domain.Task;
import org.jclouds.vcloud.director.v1_5.domain.TasksList;
import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest;
import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xxOrNull;
import org.jclouds.vcloud.director.v1_5.functions.ReferenceToEndpoint;
import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xx;
import com.google.common.util.concurrent.ListenableFuture;
@ -45,23 +45,43 @@ import com.google.common.util.concurrent.ListenableFuture;
public interface TaskAsyncClient {
/**
* @see TaskClient#getTaskList(URI)
* @see TaskClient#getTaskList(String)
*/
@GET
@Path("/tasksList/{id}")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xxOrNull.class)
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<TasksList> getTaskList(@PathParam("id") String orgId);
/**
* @see TaskClient#getTask(URI)
* @see TaskClient#getTask(String id)
*/
@GET
@Path("/task/{id}")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Task> getTask(@PathParam("id") String taskId);
/**
* @see TaskClient#getTask(ReferenceType<?>)
*/
@GET
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xxOrNull.class)
ListenableFuture<Task> getTask(@EndpointParam URI taskHref);
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Task> getTask(@EndpointParam(parser = ReferenceToEndpoint.class) ReferenceType<?> taskRef);
/**
* @see TaskClient#cancelTask(URI)
*/
@POST
@Path("/task/{id}/action/cancel")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Void> cancelTask(@PathParam("id") String taskId);
/**
* @see TaskClient#cancelTask(URI)
@ -70,6 +90,6 @@ public interface TaskAsyncClient {
@Path("/action/cancel")
@Consumes
@JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xxOrNull.class)
void cancelTask(@EndpointParam URI taskHref);
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Void> cancelTask(@EndpointParam(parser = ReferenceToEndpoint.class) ReferenceType<?> taskRef);
}

View File

@ -18,10 +18,10 @@
*/
package org.jclouds.vcloud.director.v1_5.features;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import org.jclouds.vcloud.director.v1_5.domain.Task;
import org.jclouds.vcloud.director.v1_5.domain.TasksList;
@ -47,10 +47,14 @@ public interface TaskClient {
*
* @return the task or null if not found
*/
Task getTask(URI taskHref);
Task getTask(String taskId);
Task getTask(ReferenceType<?> taskRef);
/**
* Cancels a task.
*/
void cancelTask(URI taskHref);
void cancelTask(String taskId);
void cancelTask(ReferenceType<?> taskRef);
}

View File

@ -0,0 +1,40 @@
/**
* 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.vcloud.director.v1_5.functions;
import java.net.URI;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
/**
* @author grkvlt@apache.org
*/
public class ReferenceToEndpoint implements Function<Object, URI> {
@Override
public URI apply(Object input) {
Preconditions.checkNotNull(input);
Preconditions.checkArgument(input instanceof ReferenceType<?>);
ReferenceType<?> reference = (ReferenceType<?>) input;
return reference.getURI();
};
}

View File

@ -37,25 +37,22 @@ import com.google.common.collect.Iterables;
* @author grkvlt@apache.org
*/
@Singleton
public class ThrowVCloudErrorOn4xxOrNull implements Function<Exception, Object> {
public class ThrowVCloudErrorOn4xx implements Function<Exception, Object> {
@Inject
private ThrowVCloudErrorOn4xxOrNull() { }
private ThrowVCloudErrorOn4xx() { }
@Override
public Object apply(Exception from) {
Iterable<HttpResponseException> throwables = Iterables.filter(Throwables.getCausalChain(from), HttpResponseException.class);
if (Iterables.size(throwables) == 1) {
HttpResponseException exception = Iterables.getOnlyElement(throwables);
if (exception.getResponse().getStatusCode() >= 400 && exception.getResponse().getStatusCode() < 500) {
try {
Error error = JAXB.unmarshal(InputSuppliers.of(exception.getContent()).getInput(), Error.class);
throw new VCloudDirectorException(error);
} catch (IOException e) {
Throwables.propagate(e);
}
HttpResponseException exception = Iterables.getFirst(throwables, null);
if (exception != 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);
} catch (IOException e) {
Throwables.propagate(e);
}
}
return null; // TODO is this correct?
throw Throwables.propagate(from);
}
}

View File

@ -35,6 +35,8 @@ import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorRestClientExpectTest;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
/**
* Allows us to test a client via its side effects.
*
@ -42,12 +44,12 @@ import org.testng.annotations.Test;
*/
@Test(groups = "unit", singleThreaded = true, testName = "OrgClientExpectTest")
public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest {
@Test
public void testWhenResponseIs2xxLoginReturnsValidOrgList() {
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org"),
getStandardPayloadResponse("/org/orglist.xml", VCloudDirectorMediaType.ORGLIST_XML+";version=1.5"));
getStandardPayloadResponse("/org/orglist.xml", VCloudDirectorMediaType.ORG_LIST));
OrgList expected = OrgList.builder()
.org(Reference.builder()
@ -60,13 +62,139 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
assertEquals(client.getOrgClient().getOrgList(), expected);
}
@Test
public void testWhenResponseIs2xxLoginReturnsValidOrgFromListByReference() {
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org"),
getStandardPayloadResponse("/org/orglist.xml", VCloudDirectorMediaType.ORG_LIST));
Reference org = Iterables.getOnlyElement(client.getOrgClient().getOrgList().getOrgs());
client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", org.getHref()),
getStandardPayloadResponse("/org/org.xml", VCloudDirectorMediaType.ORG));
Org expected = Org
.builder()
.name("JClouds")
.description("")
.fullName("JClouds")
.id("urn:vcloud:org:6f312e42-cd2b-488d-a2bb-97519cd57ed0")
.type("application/vnd.vmware.vcloud.org+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.vdc+xml")
.name("Cluster01-JClouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/vdc/d16d333b-e3c0-4176-845d-a5ee6392df07"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.tasksList+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/tasksList/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.catalog+xml")
.name("Public")
.href(URI.create("https://vcloudbeta.bluelock.com/api/catalog/9e08c2f6-077a-42ce-bece-d5332e2ebb5c"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.controlAccess+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/catalog/9e08c2f6-077a-42ce-bece-d5332e2ebb5c/controlAccess/"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.orgNetwork+xml")
.name("ilsolation01-Jclouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.orgNetwork+xml")
.name("internet01-Jclouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.metadata+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/metadata"))
.build())
.build();
assertEquals(client.getOrgClient().getOrg(org), expected);
}
@Test
public void testWhenResponseIs2xxLoginReturnsValidOrgFromListById() {
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org"),
getStandardPayloadResponse("/org/orglist.xml", VCloudDirectorMediaType.ORG_LIST));
Reference org = Iterables.getOnlyElement(client.getOrgClient().getOrgList().getOrgs());
client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org/" + org.getUuid()),
getStandardPayloadResponse("/org/org.xml", VCloudDirectorMediaType.ORG));
Org expected = Org
.builder()
.name("JClouds")
.description("")
.fullName("JClouds")
.id("urn:vcloud:org:6f312e42-cd2b-488d-a2bb-97519cd57ed0")
.type("application/vnd.vmware.vcloud.org+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.vdc+xml")
.name("Cluster01-JClouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/vdc/d16d333b-e3c0-4176-845d-a5ee6392df07"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.tasksList+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/tasksList/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.catalog+xml")
.name("Public")
.href(URI.create("https://vcloudbeta.bluelock.com/api/catalog/9e08c2f6-077a-42ce-bece-d5332e2ebb5c"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.controlAccess+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/catalog/9e08c2f6-077a-42ce-bece-d5332e2ebb5c/controlAccess/"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.orgNetwork+xml")
.name("ilsolation01-Jclouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/network/f3ba8256-6f48-4512-aad6-600e85b4dc38"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.orgNetwork+xml")
.name("internet01-Jclouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c"))
.build())
.link(Link.builder()
.rel("down")
.type("application/vnd.vmware.vcloud.metadata+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/metadata"))
.build())
.build();
assertEquals(org.getUuid(), "6f312e42-cd2b-488d-a2bb-97519cd57ed0");
assertEquals(client.getOrgClient().getOrg(org.getUuid()), expected);
}
@Test
public void testWhenResponseIs2xxLoginReturnsValidOrg() {
URI orgRef = URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0");
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"),
getStandardPayloadResponse("/org/org.xml", VCloudDirectorMediaType.ORG_XML+";version=1.5"));
getStandardPayloadResponse("/org/org.xml", VCloudDirectorMediaType.ORG));
Org expected = Org
.builder()
@ -74,7 +202,7 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
.description("")
.fullName("JClouds")
.id("urn:vcloud:org:6f312e42-cd2b-488d-a2bb-97519cd57ed0")
.type(VCloudDirectorMediaType.ORG_XML)
.type("application/vnd.vmware.vcloud.org+xml")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.link(Link.builder()
.rel("down")
@ -127,7 +255,7 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org/NOTAUUID"),
getStandardPayloadResponse(400, "/org/error400.xml", VCloudDirectorMediaType.ERROR_XML));
getStandardPayloadResponse(400, "/org/error400.xml", VCloudDirectorMediaType.ERROR));
Error expected = Error.builder()
.message("validation error on field 'id': String value has invalid format or length")
@ -152,10 +280,10 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org/9e08c2f6-077a-42ce-bece-d5332e2ebb5c"),
getStandardPayloadResponse(403, "/org/error403-catalog.xml", VCloudDirectorMediaType.ERROR_XML));
getStandardPayloadResponse(403, "/org/error403-catalog.xml", VCloudDirectorMediaType.ERROR));
Error expected = Error.builder()
.message("No access to entity &quot;com.vmware.vcloud.entity.org:9e08c2f6-077a-42ce-bece-d5332e2ebb5c&quot;.")
.message("No access to entity \"com.vmware.vcloud.entity.org:9e08c2f6-077a-42ce-bece-d5332e2ebb5c\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
@ -177,10 +305,10 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"),
getStandardPayloadResponse(403, "/org/error403-fake.xml", VCloudDirectorMediaType.ERROR_XML));
getStandardPayloadResponse(403, "/org/error403-fake.xml", VCloudDirectorMediaType.ERROR));
Error expected = Error.builder()
.message("No access to entity &quot;com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee&quot;.")
.message("No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
@ -202,7 +330,7 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/metadata"),
getStandardPayloadResponse("/org/metadata.xml", VCloudDirectorMediaType.METADATA_XML+";version=1.5"));
getStandardPayloadResponse("/org/metadata.xml", VCloudDirectorMediaType.METADATA));
Metadata expected = Metadata.builder()
.type("application/vnd.vmware.vcloud.metadata+xml")
@ -223,8 +351,8 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
URI orgRef = URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0");
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse,
getStandardRequest("GET", "i/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/metadata/KEY"),
getStandardPayloadResponse("/org/metadata.xml", VCloudDirectorMediaType.METADATAENTRY_XML+";version=1.5"));
getStandardRequest("GET", "/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0/metadata/KEY"),
getStandardPayloadResponse("/org/metadata.xml", VCloudDirectorMediaType.METADATA_ENTRY));
MetadataEntry expected = MetadataEntry.builder()
.key("KEY")

View File

@ -46,7 +46,7 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
@Test
public void testTaskListForValidOrg() {
HttpRequest orgListRequest = HttpRequest.builder()
HttpRequest taskRequest = HttpRequest.builder()
.method("GET")
.endpoint(URI.create(endpoint + "/tasksList/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.headers(ImmutableMultimap.<String, String> builder()
@ -55,12 +55,12 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
.build())
.build();
HttpResponse orgListResponse = HttpResponse.builder()
HttpResponse taskResponse = HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResourceWithContentType("/task/taskslist.xml", VCloudDirectorMediaType.TASKSLIST_XML + ";version=1.5"))
.payload(payloadFromResourceWithContentType("/task/taskslist.xml", VCloudDirectorMediaType.TASKS_LIST.getMediaType() + ";version=1.5"))
.build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, orgListRequest, orgListResponse);
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse);
String orgId = getUuidFromReference.apply(Reference.builder().href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0")).build());
@ -124,7 +124,7 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
@Test
public void testTaskListForInvalidOrgId() {
HttpRequest orgListRequest = HttpRequest.builder()
HttpRequest taskRequest = HttpRequest.builder()
.method("GET")
.endpoint(URI.create(endpoint + "/tasksList/NOTAUUID"))
.headers(ImmutableMultimap.<String, String> builder()
@ -133,12 +133,12 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
.build())
.build();
HttpResponse orgListResponse = HttpResponse.builder()
HttpResponse taskResponse = HttpResponse.builder()
.statusCode(400)
.payload(payloadFromResourceWithContentType("/task/error400.xml", VCloudDirectorMediaType.ERROR_XML + ";version=1.5"))
.payload(payloadFromResourceWithContentType("/task/error400.xml", VCloudDirectorMediaType.ERROR.getMediaType() + ";version=1.5"))
.build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, orgListRequest, orgListResponse);
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse);
String orgId = "NOTAUUID";
@ -159,7 +159,7 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
@Test
public void testTaskListForNotFoundOrgId() {
HttpRequest orgListRequest = HttpRequest.builder()
HttpRequest taskRequest = HttpRequest.builder()
.method("GET")
.endpoint(URI.create(endpoint + "/tasksList/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"))
.headers(ImmutableMultimap.<String, String> builder()
@ -168,12 +168,12 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
.build())
.build();
HttpResponse orgListResponse = HttpResponse.builder()
HttpResponse taskResponse = HttpResponse.builder()
.statusCode(403)
.payload(payloadFromResourceWithContentType("/task/error403.xml", VCloudDirectorMediaType.ERROR_XML + ";version=1.5"))
.payload(payloadFromResourceWithContentType("/task/error403.xml", VCloudDirectorMediaType.ERROR.getMediaType() + ";version=1.5"))
.build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, orgListRequest, orgListResponse);
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse);
String orgId = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
@ -192,4 +192,158 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
fail("Should have thrown a VCloudDirectorException");
}
}
@Test
public void testGetTaskForTaskId() {
HttpRequest taskRequest = HttpRequest.builder()
.method("GET")
.endpoint(URI.create(endpoint + "/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b"))
.headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "*/*")
.put("x-vcloud-authorization", token)
.build())
.build();
HttpResponse taskResponse = HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResourceWithContentType("/task/task.xml", VCloudDirectorMediaType.TASK.getMediaType() + ";version=1.5"))
.build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse);
String taskId = "5fcd2af3-d0ec-45ce-9451-8c585a2c766b";
Task expected = Task.builder()
.type("application/vnd.vmware.vcloud.task+xml")
.name("task")
.id("urn:vcloud:task:5fcd2af3-d0ec-45ce-9451-8c585a2c766b")
.href(URI.create("https://vcloudbeta.bluelock.com/api/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b"))
.status("success")
.operation("Created Catalog QunyingTestCatalog(7212e451-76e1-4631-b2de-ba1dfd8080e4)")
.operationName("catalogCreateCatalog")
.startTime(dateService.iso8601DateParse("2012-02-07T00:16:28.450-05:00"))
.endTime(dateService.iso8601DateParse("2012-02-07T00:16:28.867-05:00"))
.expiryTime(dateService.iso8601DateParse("2012-05-07T00:16:28.450-04:00"))
.owner(Reference.builder()
.type("application/vnd.vmware.vcloud.catalog+xml")
.name("QunyingTestCatalog")
.href(URI.create("https://vcloudbeta.bluelock.com/api/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4"))
.build())
.user(Reference.builder()
.type("application/vnd.vmware.admin.user+xml")
.name("JClouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build())
.org(Reference.builder()
.type("application/vnd.vmware.vcloud.org+xml")
.name("JClouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build())
.build();
assertEquals(client.getTaskClient().getTask(taskId), expected);
}
@Test
public void testGetTaskForTaskRef() {
HttpRequest taskRequest = HttpRequest.builder()
.method("GET")
.endpoint(URI.create(endpoint + "/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b"))
.headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "*/*")
.put("x-vcloud-authorization", token)
.build())
.build();
HttpResponse taskResponse = HttpResponse.builder()
.statusCode(200)
.payload(payloadFromResourceWithContentType("/task/task.xml", VCloudDirectorMediaType.TASK.getMediaType() + ";version=1.5"))
.build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse);
Reference taskRef = Reference.builder()
.type("application/vnd.vmware.vcloud.task+xml")
.name("task")
.href(URI.create(endpoint + "/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b"))
.build();
Task expected = Task.builder()
.type("application/vnd.vmware.vcloud.task+xml")
.name("task")
.id("urn:vcloud:task:5fcd2af3-d0ec-45ce-9451-8c585a2c766b")
.href(URI.create("https://vcloudbeta.bluelock.com/api/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b"))
.status("success")
.operation("Created Catalog QunyingTestCatalog(7212e451-76e1-4631-b2de-ba1dfd8080e4)")
.operationName("catalogCreateCatalog")
.startTime(dateService.iso8601DateParse("2012-02-07T00:16:28.450-05:00"))
.endTime(dateService.iso8601DateParse("2012-02-07T00:16:28.867-05:00"))
.expiryTime(dateService.iso8601DateParse("2012-05-07T00:16:28.450-04:00"))
.owner(Reference.builder()
.type("application/vnd.vmware.vcloud.catalog+xml")
.name("QunyingTestCatalog")
.href(URI.create("https://vcloudbeta.bluelock.com/api/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4"))
.build())
.user(Reference.builder()
.type("application/vnd.vmware.admin.user+xml")
.name("JClouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build())
.org(Reference.builder()
.type("application/vnd.vmware.vcloud.org+xml")
.name("JClouds")
.href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"))
.build())
.build();
assertEquals(client.getTaskClient().getTask(taskRef), expected);
}
@Test
public void testCancelTaskByTaskId() {
HttpRequest taskRequest = HttpRequest.builder()
.method("POST")
.endpoint(URI.create(endpoint + "/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b/action/cancel"))
.headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "*/*")
.put("x-vcloud-authorization", token)
.build())
.build();
HttpResponse taskResponse = HttpResponse.builder()
.statusCode(200)
.build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse);
String taskId = "5fcd2af3-d0ec-45ce-9451-8c585a2c766b";
client.getTaskClient().cancelTask(taskId);
}
@Test
public void testCancelTaskByTaskRef() {
HttpRequest taskRequest = HttpRequest.builder()
.method("POST")
.endpoint(URI.create(endpoint + "/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b/action/cancel"))
.headers(ImmutableMultimap.<String, String> builder()
.put("Accept", "*/*")
.put("x-vcloud-authorization", token)
.build())
.build();
HttpResponse taskResponse = HttpResponse.builder()
.statusCode(200)
.build();
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse);
Reference taskRef = Reference.builder()
.type("application/vnd.vmware.vcloud.task+xml")
.name("task")
.href(URI.create(endpoint + "/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b"))
.build();
client.getTaskClient().cancelTask(taskRef);
}
}

View File

@ -27,6 +27,7 @@ import org.jclouds.rest.BaseRestClientExpectTest;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import com.google.common.base.Function;
@ -50,7 +51,7 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect
protected DateService dateService;
@BeforeTest
@BeforeClass
protected void setUpInjector() {
dateService = Guice.createInjector().getInstance(DateService.class);
assert dateService != null;
@ -79,7 +80,7 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect
.put("x-vcloud-authorization", token)
.put("Set-Cookie", String.format("vcloud-token=%s; Secure; Path=/", token))
.build())
.payload(payloadFromResourceWithContentType("/session.xml", VCloudDirectorMediaType.SESSION_XML + ";version=1.5"))
.payload(payloadFromResourceWithContentType("/session.xml", VCloudDirectorMediaType.SESSION.getMediaType() + ";version=1.5"))
.build();
public BaseVCloudDirectorRestClientExpectTest() {
@ -96,24 +97,24 @@ public class BaseVCloudDirectorRestClientExpectTest extends BaseRestClientExpect
return getStandardRequest(method, uri, VCloudDirectorMediaType.ANY);
}
protected HttpRequest getStandardRequest(String method, URI uri, String mediaType) {
protected HttpRequest getStandardRequest(String method, URI uri, VCloudDirectorMediaType mediaType) {
return HttpRequest.builder()
.method(method)
.endpoint(uri)
.headers(ImmutableMultimap.<String, String> builder()
.put("Accept", mediaType)
.put("Accept", mediaType.getMediaType())
.put("x-vcloud-authorization", token)
.build())
.build();
}
protected HttpResponse getStandardPayloadResponse(String relativeFilePath, String mediaType) {
protected HttpResponse getStandardPayloadResponse(String relativeFilePath, VCloudDirectorMediaType mediaType) {
return getStandardPayloadResponse(200, relativeFilePath, mediaType);
}
protected HttpResponse getStandardPayloadResponse(int statusCode, String relativeFilePath, String mediaType) {
protected HttpResponse getStandardPayloadResponse(int statusCode, String relativeFilePath, VCloudDirectorMediaType mediaType) {
return HttpResponse.builder()
.statusCode(statusCode)
.payload(payloadFromResourceWithContentType(relativeFilePath, mediaType+";version=1.5")).build();
.payload(payloadFromResourceWithContentType(relativeFilePath, mediaType.getMediaType()+";version=1.5")).build();
}
}

View File

@ -72,7 +72,7 @@ public class SessionClientExpectTest extends BaseRestClientExpectTest<SessionCli
ImmutableMultimap.<String, String> builder().put("x-vcloud-authorization", token).put("Set-Cookie",
String.format("vcloud-token=%s; Secure; Path=/", token)).build())
.payload(
payloadFromResourceWithContentType("/session.xml", VCloudDirectorMediaType.SESSION_XML
payloadFromResourceWithContentType("/session.xml", VCloudDirectorMediaType.SESSION.getMediaType()
+ ";version=1.5")).build()
);
@ -97,7 +97,7 @@ public class SessionClientExpectTest extends BaseRestClientExpectTest<SessionCli
HttpResponse.builder().statusCode(200)
.payload(
payloadFromResourceWithContentType("/session.xml", VCloudDirectorMediaType.SESSION_XML
payloadFromResourceWithContentType("/session.xml", VCloudDirectorMediaType.SESSION.getMediaType()
+ ";version=1.5")).build()
);

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Task xmlns="http://www.vmware.com/vcloud/v1.5" status="success" startTime="2012-02-07T00:16:28.450-05:00" operationName="catalogCreateCatalog" operation="Created Catalog QunyingTestCatalog(7212e451-76e1-4631-b2de-ba1dfd8080e4)" expiryTime="2012-05-07T00:16:28.450-04:00" endTime="2012-02-07T00:16:28.867-05:00" name="task" id="urn:vcloud:task:5fcd2af3-d0ec-45ce-9451-8c585a2c766b" type="application/vnd.vmware.vcloud.task+xml" href="https://vcloudbeta.bluelock.com/api/task/5fcd2af3-d0ec-45ce-9451-8c585a2c766b" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://vcloudbeta.bluelock.com/api/v1.5/schema/master.xsd">
<Owner type="application/vnd.vmware.vcloud.catalog+xml" name="QunyingTestCatalog" href="https://vcloudbeta.bluelock.com/api/catalog/7212e451-76e1-4631-b2de-ba1dfd8080e4"/>
<User type="application/vnd.vmware.admin.user+xml" name="qunying.huang@enstratus.com" href="https://vcloudbeta.bluelock.com/api/admin/user/967d317c-4273-4a95-b8a4-bf63b78e9c69"/>
<Organization type="application/vnd.vmware.vcloud.org+xml" name="JClouds" href="https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0"/>
</Task>