From a0411cfed058e4a5ac5bb9dc633c053d92dc7f72 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Thu, 23 Aug 2012 11:50:12 +0200 Subject: [PATCH] issue 830: fixed CatalogApiLiveTest --- .../v1_5/features/CatalogApiLiveTest.java | 79 +++++++++++-------- .../BaseVCloudDirectorApiLiveTest.java | 10 ++- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/CatalogApiLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/CatalogApiLiveTest.java index 50075aa50c..00be89a328 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/CatalogApiLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/CatalogApiLiveTest.java @@ -46,7 +46,6 @@ import org.jclouds.vcloud.director.v1_5.domain.MetadataValue; import org.jclouds.vcloud.director.v1_5.domain.Reference; import org.jclouds.vcloud.director.v1_5.domain.Task; import org.jclouds.vcloud.director.v1_5.domain.Vdc; -import org.jclouds.vcloud.director.v1_5.domain.org.Org; import org.jclouds.vcloud.director.v1_5.features.admin.AdminCatalogApi; import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorApiLiveTest; import org.testng.annotations.AfterClass; @@ -74,44 +73,29 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest { private AdminCatalog adminCatalog; private Media media; private CatalogItem catalogItem; - + private boolean createdByTest = false; + private AdminCatalogApi adminCatalogApi; + @Override protected void setupRequiredApis() { - // TODO why do I need a guard clause here? - if (adminCatalog != null) - return; catalogApi = context.getApi().getCatalogApi(); - Org org = context.getApi().getOrgApi().get(Iterables.get(context.getApi().getOrgApi().list(), 0).getHref()); - - if (adminContext != null) { + adminCatalogApi = adminContext.getApi().getCatalogApi(); + + if(catalogUrn == null) { AdminCatalog newCatalog = AdminCatalog.builder().name(name("Test Catalog ")) .description("created by CatalogApiLiveTest").build(); - AdminCatalogApi adminCatalogApi = adminContext.getApi().getCatalogApi(); adminCatalog = adminCatalogApi.addCatalogToOrg(newCatalog, org.getId()); catalogUrn = catalogApi.get( find(adminCatalog.getLinks(), and(relEquals("alternate"), typeEquals(VCloudDirectorMediaType.CATALOG))).getHref()).getId(); - Metadata newMetadata = Metadata.builder().entry(MetadataEntry.builder().entry("KEY", "MARMALADE").build()) - .build(); - - Task mergeCatalogMetadata = adminCatalogApi.getMetadataApi(adminCatalog.getId()).merge(newMetadata); - checkTask(mergeCatalogMetadata); - assertTrue(retryTaskSuccess.apply(mergeCatalogMetadata), - String.format(TASK_COMPLETE_TIMELY, "setupRequiredApis")); + createdByTest = true; } } @AfterClass(alwaysRun = true) public void tearDown() { - if (catalogItem != null) { - try { - catalogApi.removeItem(catalogItem.getId()); - } catch (Exception e) { - logger.warn(e, "Error when deleting catalog item '%s'", catalogItem.getName()); - } - } if (media != null) { try { Task remove = context.getApi().getMediaApi().remove(media.getId()); @@ -120,14 +104,22 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest { logger.warn(e, "Error when deleting media '%s'", media.getName()); } } - if (adminContext != null && adminCatalog != null) { - try { - adminContext.getApi().getCatalogApi().remove(adminCatalog.getId()); - } catch (Exception e) { - logger.warn(e, "Error when deleting catalog '%s'", adminCatalog.getName()); + if(createdByTest) { + if (catalogItem != null) { + try { + catalogApi.removeItem(catalogItem.getId()); + } catch (Exception e) { + logger.warn(e, "Error when deleting catalog item '%s'", catalogItem.getName()); + } + } + if (adminContext != null && adminCatalog != null) { + try { + adminContext.getApi().getCatalogApi().remove(adminCatalog.getId()); + } catch (Exception e) { + logger.warn(e, "Error when deleting catalog '%s'", adminCatalog.getName()); + } } } - // TODO wait for tasks } @Test(description = "GET /catalog/{id}") @@ -151,7 +143,7 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest { Vdc vdc = lazyGetVdc(); Link addMedia = find(vdc.getLinks(), and(relEquals("add"), typeEquals(VCloudDirectorMediaType.MEDIA))); - Media sourceMedia = Media.builder().type(VCloudDirectorMediaType.MEDIA).name("Test media 1").size(iso.length) + Media sourceMedia = Media.builder().type(VCloudDirectorMediaType.MEDIA).name(name("TestMedia-")).size(iso.length) .imageType(Media.ImageType.ISO).description("Test media generated by testAddMedia()").build(); media = context.getApi().getMediaApi().add(addMedia.getHref(), sourceMedia); @@ -177,6 +169,7 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest { // Note this runs after all the metadata tests @Test(description = "DELETE /catalogItem/{id}", dependsOnMethods = "testRemoveCatalogItemMetadataValue") public void testRemoveCatalogItem() { + removeMediaAttachedToCatalogItem(catalogItem); catalogApi.removeItem(catalogItem.getId()); catalogItem = catalogApi.getItem(catalogItem.getId()); assertNull(catalogItem); @@ -187,9 +180,16 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest { Metadata catalogMetadata = catalogApi.getMetadataApi(catalogUrn).get(); checkMetadata(catalogMetadata); } - + @Test(description = "GET /catalog/{id}/metadata/{key}") public void testGetCatalogMetadataValue() { + + Metadata newMetadata = Metadata.builder().entry(MetadataEntry.builder().entry("KEY", "MARMALADE").build()) + .build(); + + Task mergeCatalogMetadata = adminCatalogApi.getMetadataApi(catalogUrn).merge(newMetadata); + assertTaskSucceedsLong(mergeCatalogMetadata); + Metadata catalogMetadata = catalogApi.getMetadataApi(catalogUrn).get(); MetadataEntry existingMetadataEntry = Iterables.find(catalogMetadata.getMetadataEntries(), new Predicate() { @@ -198,7 +198,7 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest { return input.getKey().equals("KEY"); } }); - MetadataValue metadataValue = catalogApi.getMetadataApi(catalogApi.get(existingMetadataEntry.getHref()).getId()) + MetadataValue metadataValue = catalogApi.getMetadataApi(catalogUrn) .getValue("KEY"); assertEquals(metadataValue.getValue(), existingMetadataEntry.getValue(), String.format(CORRECT_VALUE_OBJECT_FMT, "Value", "MetadataValue", existingMetadataEntry.getValue(), metadataValue.getValue())); @@ -270,4 +270,19 @@ public class CatalogApiLiveTest extends BaseVCloudDirectorApiLiveTest { MetadataValue removed = catalogApi.getItemMetadataApi(catalogItem.getId()).getValue("KEY"); assertNull(removed); } + + + private void removeMediaAttachedToCatalogItem(CatalogItem catalogItem) { + if (media != null) { + if (catalogItem.getEntity().getHref().equals(media.getHref())) { + try { + Task remove = context.getApi().getMediaApi().remove(media.getId()); + taskDoneEventually(remove); + media = null; + } catch (Exception e) { + logger.warn(e, "Error when deleting media '%s'", media.getName()); + } + } + } + } } diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorApiLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorApiLiveTest.java index 55fef1fe97..4ab6744344 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorApiLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorApiLiveTest.java @@ -88,6 +88,8 @@ import org.jclouds.vcloud.director.v1_5.domain.org.Org; import org.jclouds.vcloud.director.v1_5.domain.params.InstantiateVAppTemplateParams; import org.jclouds.vcloud.director.v1_5.domain.params.InstantiationParams; import org.jclouds.vcloud.director.v1_5.domain.params.UndeployVAppParams; +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.domain.section.NetworkConfigSection; import org.jclouds.vcloud.director.v1_5.features.TaskApi; import org.jclouds.vcloud.director.v1_5.features.VAppApi; @@ -246,6 +248,13 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest< userUrn = emptyToNull(System.getProperty("test." + provider + ".user-id")); +// QueryResultRecords queryResult = adminContext.getApi().getQueryApi().usersQueryAll(); +// for (QueryResultRecordType recordType : queryResult.getRecords()) { +// User user = adminContext.getApi().getUserApi().get(recordType.getHref()); +// System.out.println(user.getName()); +// System.out.println(user.getId()); +// } + org = context .getApi() .getOrgApi() @@ -267,7 +276,6 @@ public abstract class BaseVCloudDirectorApiLiveTest extends BaseContextLiveTest< vAppTemplateUrn = vAppTemplate.getId(); } } - } if (networkUrn == null) {