From 30f5f0d6b4f052c3cd11317ba12d249e0bd68fc3 Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Fri, 16 Mar 2012 22:28:17 +0000 Subject: [PATCH] Issue #830 vcloud-director: QueryClient live tests and fixes --- .../domain/query/QueryResultRecordType.java | 6 +- .../v1_5/features/QueryAsyncClient.java | 28 +++++++ .../director/v1_5/features/QueryClient.java | 33 ++++++++ .../v1_5/features/QueryClientLiveTest.java | 84 ++++++++++++++++--- 4 files changed, 137 insertions(+), 14 deletions(-) diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/query/QueryResultRecordType.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/query/QueryResultRecordType.java index a671c01d0d..4de85a2589 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/query/QueryResultRecordType.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/query/QueryResultRecordType.java @@ -45,6 +45,9 @@ import com.google.common.collect.Sets; * @author grkvlt@apache.org */ @XmlSeeAlso({ + QueryResultVAppTemplateRecord.class, + QueryResultVAppRecord.class, + QueryResultDatastoreRecord.class, QueryResultCatalogRecord.class, QueryResultNetworkRecord.class} ) @@ -125,7 +128,6 @@ public class QueryResultRecordType { @XmlElement(name = "Link") private Set links = Sets.newLinkedHashSet(); @XmlAttribute - @XmlSchemaType(name = "anyURI") private URI href; @XmlAttribute private String id; @@ -150,7 +152,7 @@ public class QueryResultRecordType { this.href = href; } - public QueryResultRecordType() { + protected QueryResultRecordType() { // For JAXB } diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/QueryAsyncClient.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/QueryAsyncClient.java index 8685f20c85..a00ddeb542 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/QueryAsyncClient.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/QueryAsyncClient.java @@ -127,6 +127,20 @@ public interface QueryAsyncClient { ListenableFuture catalogReferencesQuery(@QueryParam("page") Integer page, @QueryParam("pageSize") Integer pageSize, @QueryParam("filter") String filter); + @GET + @Path("/vAppTemplates/query") + @Consumes + @JAXBResponseParser + @ExceptionParser(ThrowVCloudErrorOn4xx.class) + ListenableFuture vAppTemplatesQueryAll(); + + @GET + @Path("/vAppTemplates/query") + @Consumes + @JAXBResponseParser + @ExceptionParser(ThrowVCloudErrorOn4xx.class) + ListenableFuture vAppTemplatesQuery(@QueryParam("filter") String filter); + /** * Retrieves a list of {@link VApp}s by using REST API general QueryHandler. */ @@ -176,4 +190,18 @@ public interface QueryAsyncClient { @ExceptionParser(ThrowVCloudErrorOn4xx.class) ListenableFuture vAppReferencesQuery(@QueryParam("page") Integer page, @QueryParam("pageSize") Integer pageSize, @QueryParam("filter") String filter); + + @GET + @Path("/vms/query") + @Consumes + @JAXBResponseParser + @ExceptionParser(ThrowVCloudErrorOn4xx.class) + ListenableFuture vmsQueryAll(); + + @GET + @Path("/vms/query") + @Consumes + @JAXBResponseParser + @ExceptionParser(ThrowVCloudErrorOn4xx.class) + ListenableFuture vmsQuery(@QueryParam("filter") String filter); } diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/QueryClient.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/QueryClient.java index e6e29931d4..7d8d907379 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/QueryClient.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/QueryClient.java @@ -21,6 +21,11 @@ package org.jclouds.vcloud.director.v1_5.features; import java.util.concurrent.TimeUnit; import org.jclouds.concurrent.Timeout; +import org.jclouds.vcloud.director.v1_5.domain.Catalog; +import org.jclouds.vcloud.director.v1_5.domain.CatalogReference; +import org.jclouds.vcloud.director.v1_5.domain.Link; +import org.jclouds.vcloud.director.v1_5.domain.VApp; +import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate; import org.jclouds.vcloud.director.v1_5.domain.query.CatalogReferences; import org.jclouds.vcloud.director.v1_5.domain.query.QueryList; import org.jclouds.vcloud.director.v1_5.domain.query.QueryResultRecords; @@ -102,6 +107,20 @@ public interface QueryClient { /** @see #catalogReferencesQueryAll() */ CatalogReferences catalogReferencesQuery(Integer page, Integer pageSize, String filter); + /** + * Retrieves a list of {@link VAppTemplate}s by using REST API general QueryHandler. + * + *
+    * GET /vAppTemplates/query
+    * 
+ * + * @see #queryAll(String) + */ + QueryResultRecords vAppTemplatesQueryAll(); + + /** @see #queryAll() */ + QueryResultRecords vAppTemplatesQuery(String filter); + /** * Retrieves a list of {@link VApp}s by using REST API general QueryHandler. * @@ -119,6 +138,20 @@ public interface QueryClient { /** @see #queryAll() */ QueryResultRecords vAppsQuery(Integer page, Integer pageSize, String filter); + /** + * Retrieves a list of {@link Vm}s by using REST API general QueryHandler. + * + *
+    * GET /vms/query
+    * 
+ * + * @see #queryAll(String) + */ + QueryResultRecords vmsQueryAll(); + + /** @see #queryAll() */ + QueryResultRecords vmsQuery(String filter); + /** * Retrieves a list of {@link VAppReference}s by using REST API general QueryHandler. * diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/QueryClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/QueryClientLiveTest.java index 551fe66683..64c07737f1 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/QueryClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/QueryClientLiveTest.java @@ -19,9 +19,21 @@ package org.jclouds.vcloud.director.v1_5.features; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.NOT_EMPTY_OBJECT_FMT; +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import java.net.URI; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; +import org.jclouds.vcloud.director.v1_5.domain.VAppTemplate; import org.jclouds.vcloud.director.v1_5.domain.query.CatalogReferences; +import org.jclouds.vcloud.director.v1_5.domain.query.QueryResultRecordType; import org.jclouds.vcloud.director.v1_5.domain.query.QueryResultRecords; import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest; import org.testng.annotations.BeforeClass; @@ -39,32 +51,80 @@ public class QueryClientLiveTest extends BaseVCloudDirectorClientLiveTest { * Convenience references to API clients. */ - private CatalogClient catalogClient; private QueryClient queryClient; + private VAppTemplateClient vappTemplateClient; @Override @BeforeClass(inheritGroups = true) public void setupRequiredClients() { - catalogClient = context.getApi().getCatalogClient(); queryClient = context.getApi().getQueryClient(); + vappTemplateClient = context.getApi().getVAppTemplateClient(); } - /* - * Shared state between dependant tests. - */ - - private QueryResultRecords catalogRecords; - private CatalogReferences catalogReferences; - @Test(testName = "GET /catalogs/query") public void testQueryAllCatalogs() { - catalogRecords = queryClient.catalogsQueryAll(); + QueryResultRecords catalogRecords = queryClient.catalogsQueryAll(); assertFalse(catalogRecords.getRecords().isEmpty(), String.format(NOT_EMPTY_OBJECT_FMT, "CatalogRecord", "QueryResultRecords")); } @Test(testName = "GET /catalogs/query?format=references", dependsOnMethods = { "testQueryAllCatalogs" }) public void testQueryAllCatalogReferences() { - catalogReferences = queryClient.catalogReferencesQueryAll(); + CatalogReferences catalogReferences = queryClient.catalogReferencesQueryAll(); assertFalse(catalogReferences.getReferences().isEmpty(), String.format(NOT_EMPTY_OBJECT_FMT, "CatalogReference", "CatalogReferences")); } -} \ No newline at end of file + + @Test(testName = "GET /vAppTemplates/query") + public void testQueryAllVAppTemplates() { + QueryResultRecords queryResult = queryClient.vAppTemplatesQueryAll(); + Set hrefs = toHrefs(queryResult); + + assertRecordTypes(queryResult, Arrays.asList(VCloudDirectorMediaType.VAPP_TEMPLATE, null)); + assertTrue(hrefs.contains(vAppTemplateURI), "VAppTemplates query result should include vAppTemplate "+vAppTemplateURI+"; but only has "+hrefs); + } + + @Test(testName = "GET /vAppTemplates/query?filter)") + public void testQueryVAppTemplates() { + VAppTemplate vAppTemplate = vappTemplateClient.getVAppTemplate(vAppTemplateURI); + QueryResultRecords queryResult = queryClient.vAppTemplatesQuery(String.format("name==%s", vAppTemplate.getName())); + Set hrefs = toHrefs(queryResult); + + assertRecordTypes(queryResult, Arrays.asList(VCloudDirectorMediaType.VAPP_TEMPLATE, null)); + assertEquals(hrefs, Collections.singleton(vAppTemplateURI), "VAppTemplates query result should have found vAppTemplate "+vAppTemplateURI); + } + + @Test(testName = "GET /vApps/query") + public void testQueryAllVApps() { + // TODO instantiate a vApp, so can assert it's included + + QueryResultRecords queryResult = queryClient.vAppsQueryAll(); + Set hrefs = toHrefs(queryResult); + + assertRecordTypes(queryResult, Arrays.asList(VCloudDirectorMediaType.VAPP, null)); + //assertTrue(hrefs.contains(vappUri), "VApp query result should include vapp "+vappUri+"; but only has "+hrefs); + } + + @Test(testName = "GET /vms/query") + public void testQueryAllVms() { + // TODO instantiate a vApp + vms, so can assert it's included + + QueryResultRecords queryResult = queryClient.vmsQueryAll(); + Set hrefs = toHrefs(queryResult); + + assertRecordTypes(queryResult, Arrays.asList(VCloudDirectorMediaType.VM, null)); + //assertTrue(hrefs.contains(vappUri), "VApp query result should include vapp "+vappUri+"; but only has "+hrefs); + } + + private void assertRecordTypes(QueryResultRecords queryResult, Collection validTypes) { + for (QueryResultRecordType record : queryResult.getRecords()) { + assertTrue(validTypes.contains(record.getType()), "invalid type for query result record, "+record.getType()+"; valid types are "+validTypes); + } + } + + private Set toHrefs(QueryResultRecords queryResult) { + Set hrefs = new LinkedHashSet(); + for (QueryResultRecordType record : queryResult.getRecords()) { + hrefs.add(record.getHref()); + } + return hrefs; + } +}