From f2f2233db0677471f6914579dacd323db3b3c4d6 Mon Sep 17 00:00:00 2001 From: danikov Date: Wed, 29 Feb 2012 11:39:26 +0000 Subject: [PATCH] update and inline vdc clone/create methods into media testing --- .../v1_5/features/MediaClientLiveTest.java | 169 +++++++++++++----- 1 file changed, 122 insertions(+), 47 deletions(-) diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientLiveTest.java index edecf90da7..633fffc339 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientLiveTest.java @@ -23,11 +23,16 @@ import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.* import static org.testng.Assert.*; import java.net.URI; +import java.net.URISyntaxException; import java.util.Set; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; +import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; import org.jclouds.vcloud.director.v1_5.domain.Checks; +import org.jclouds.vcloud.director.v1_5.domain.CloneMediaParams; import org.jclouds.vcloud.director.v1_5.domain.Error; +import org.jclouds.vcloud.director.v1_5.domain.File; +import org.jclouds.vcloud.director.v1_5.domain.Link; import org.jclouds.vcloud.director.v1_5.domain.Media; import org.jclouds.vcloud.director.v1_5.domain.Metadata; import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry; @@ -35,9 +40,9 @@ import org.jclouds.vcloud.director.v1_5.domain.MetadataValue; import org.jclouds.vcloud.director.v1_5.domain.Owner; 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.URISupplier; import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest; import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @@ -52,49 +57,77 @@ import com.google.common.collect.Iterables; public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { public static final String MEDIA = "media"; + public static final String VDC = "vdc"; /* * Convenience references to API clients. */ - private Reference mediaRef; + private URISupplier vdcRef; + protected VdcClient vdcClient; protected MediaClient mediaClient; @BeforeClass(inheritGroups = true) @Override public void setupRequiredClients() { - mediaRef = Reference.builder() - .type("application/vnd.vmware.vcloud.media+xml") + vdcRef = Reference.builder() + .type("application/vnd.vmware.vcloud.vdc+xml") .name("") - .href(URI.create(endpoint+"/media/" + mediaId)) - .id(mediaId) + .href(URI.create(endpoint+"/vdc/"+vdcId)) + .id(vdcId) .build(); + + vdcClient = context.getApi().getVdcClient(); mediaClient = context.getApi().getMediaClient(); } /* * Shared state between dependent tests. */ - private Media media; + private Media media, oldMedia; private Owner owner; private Metadata metadata; private MetadataValue metadataValue; private String metadataEntryValue = "value"; - @BeforeGroups(groups = { "live" }) - public void createReferenceData() { - // FIXME: don't want to be modifying anything here! - mediaClient.setMetadata(mediaRef, "key", MetadataValue.builder().value("value").build()); + @Test(testName = "POST /vdc/{id}/media") + public void testCreateMedia() throws URISyntaxException { + assertNotNull(vdcRef, String.format(REF_REQ_LIVE, VDC)); + assertNotNull(vdcClient.getVdc(vdcRef), String.format(OBJ_REQ_LIVE, VDC)); + + java.io.File sourceFile = new java.io.File(getClass().getResource("/media/test.iso").toURI()); + + Media sourceMedia = Media.builder() + .type(VCloudDirectorMediaType.MEDIA) + .name("Test media 1") + .size(sourceFile.length()) + .imageType(Media.ImageType.ISO) + .description("Test media generated by testCreateMedia()") + .build(); + media = vdcClient.createMedia(vdcRef, sourceMedia); + + Checks.checkMediaFor(MEDIA, media); + + assertNotNull(media.getFiles(), ""); + assertTrue(media.getFiles().getFiles().size() == 1, ""); + File uploadFile = Iterables.getFirst(media.getFiles().getFiles(), null); + assertNotNull(uploadFile, ""); + assertTrue(equal(uploadFile.getSize(), sourceMedia.getSize()), ""); + + Set links = uploadFile.getLinks(); + assertNotNull(links, ""); + assertTrue(links.size() == 1, ""); + Link uploadLink = Iterables.getFirst(links, null); + + //TODO upload file +// context.getApi().getUploadClient().uploadFile(uploadFile, sourceFile); + fail(); } - @Test(testName = "GET /media/{id}") + @Test(testName = "GET /media/{id}", dependsOnMethods = { "testCreateMedia" }) public void testGetMedia() { - // required for testing - assertNotNull(mediaRef, String.format(REF_REQ_LIVE, MEDIA)); - - media = mediaClient.getMedia(mediaRef); + media = mediaClient.getMedia(media); assertNotNull(media, String.format(OBJ_REQ_LIVE, MEDIA)); - assertTrue(!media.getDescription().equals("DO NOT USE"), "Media isn't to be used for testing"); owner = media.getOwner(); assertNotNull(owner, String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "owner")); @@ -106,7 +139,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { @Test(testName = "GET /media/{id}/owner", dependsOnMethods = { "testGetMedia" }) public void testGetMediaOwner() { - Owner directOwner = mediaClient.getOwner(mediaRef); + Owner directOwner = mediaClient.getOwner(media); assertEquals(owner, directOwner, String.format(GETTER_RETURNS_SAME_OBJ, "getOwner()", "owner", "media.getOwner()", owner.toString(), directOwner.toString())); @@ -118,8 +151,50 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { Checks.checkReferenceType(directOwner.getUser()); } + @Test(testName = "POST /vdc/{id}/action/cloneMedia", + dependsOnMethods = { "testGetMediaOwner" }) + public void testCloneMedia() { + oldMedia = media; + media = vdcClient.cloneMedia(vdcRef, CloneMediaParams.builder() + .source(Reference.builder().fromEntity(media).build()) + .build()); + + Checks.checkMediaFor(VDC, media); + + Task copyTask = Iterables.getFirst(media.getTasksInProgress().getTasks(), null); + if (copyTask != null) { + Checks.checkTask(copyTask); + assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask")); + media = mediaClient.getMedia(media); + } + + Checks.checkMediaFor(MEDIA, media); + assertTrue(media.clone(oldMedia), ""); + + mediaClient.setMetadata(media, "key", MetadataValue.builder().value("value").build()); + + media = vdcClient.cloneMedia(vdcRef, CloneMediaParams.builder() + .source(Reference.builder().fromEntity(media).build()) + .name("copied test media") + .description("copied by testCloneMedia()") + .isSourceDelete(true) + .build()); + + Checks.checkMediaFor(VDC, media); + + copyTask = Iterables.getFirst(media.getTasksInProgress().getTasks(), null); + if (copyTask != null) { + Checks.checkTask(copyTask); + assertTrue(retryTaskSuccess.apply(copyTask), String.format(TASK_COMPLETE_TIMELY, "copyTask")); + media = mediaClient.getMedia(media); + } + + Checks.checkMediaFor(MEDIA, media); + assertTrue(media.clone(oldMedia), ""); + } + @Test(testName = "PUT /media/{id}", - dependsOnMethods = { "testGetMedia" }) + dependsOnMethods = { "testCloneMedia" }) public void testSetMedia() { String oldName = media.getName(); String newName = "new "+oldName; @@ -127,10 +202,10 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { String newDescription = "new "+oldDescription; media = media.toBuilder().name(newName).description(newDescription).build(); - Task updateMedia = mediaClient.updateMedia(mediaRef, media); + Task updateMedia = mediaClient.updateMedia(media, media); Checks.checkTask(updateMedia); - assertTrue(retryTaskSuccess.apply(updateMedia.getHref()), String.format(TASK_COMPLETE_TIMELY, "updateMedia")); - media = mediaClient.getMedia(mediaRef); + assertTrue(retryTaskSuccess.apply(updateMedia), String.format(TASK_COMPLETE_TIMELY, "updateMedia")); + media = mediaClient.getMedia(media); assertTrue(equal(media.getName(), newName), String.format(OBJ_FIELD_UPDATABLE, MEDIA, "name")); assertTrue(equal(media.getDescription(), newDescription), @@ -142,16 +217,16 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { media = media.toBuilder().name(oldName).description(oldDescription).build(); - updateMedia = mediaClient.updateMedia(mediaRef, media); + updateMedia = mediaClient.updateMedia(media, media); Checks.checkTask(updateMedia); - assertTrue(retryTaskSuccess.apply(updateMedia.getHref()), String.format(TASK_COMPLETE_TIMELY, "updateMedia")); - media = mediaClient.getMedia(mediaRef); + assertTrue(retryTaskSuccess.apply(updateMedia), String.format(TASK_COMPLETE_TIMELY, "updateMedia")); + media = mediaClient.getMedia(media); } @Test(testName = "GET /media/{id}/metadata", dependsOnMethods = { "testGetMedia" }) public void testGetMetadata() { - metadata = mediaClient.getMetadata(mediaRef); + metadata = mediaClient.getMetadata(media); // required for testing assertFalse(Iterables.isEmpty(metadata.getMetadataEntries()), String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "metadata.entries")); @@ -168,14 +243,14 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { .entries(inputEntries) .build(); - Task mergeMetadata = mediaClient.mergeMetadata(mediaRef, inputMetadata); + Task mergeMetadata = mediaClient.mergeMetadata(media, inputMetadata); Checks.checkTask(mergeMetadata); - assertTrue(retryTaskSuccess.apply(mergeMetadata.getHref()), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(new)")); - metadata = mediaClient.getMetadata(mediaRef); + assertTrue(retryTaskSuccess.apply(mergeMetadata), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(new)")); + metadata = mediaClient.getMetadata(media); Checks.checkMetadataFor(MEDIA, metadata); checkMetadataContainsEntries(metadata, inputEntries); - media = mediaClient.getMedia(mediaRef); + media = mediaClient.getMedia(media); Checks.checkMediaFor(MEDIA, media); // test modify @@ -184,14 +259,14 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { .entries(inputEntries) .build(); - mergeMetadata = mediaClient.mergeMetadata(mediaRef, inputMetadata); + mergeMetadata = mediaClient.mergeMetadata(media, inputMetadata); Checks.checkTask(mergeMetadata); - assertTrue(retryTaskSuccess.apply(mergeMetadata.getHref()), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(modify)")); - metadata = mediaClient.getMetadata(mediaRef); + assertTrue(retryTaskSuccess.apply(mergeMetadata), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(modify)")); + metadata = mediaClient.getMetadata(media); Checks.checkMetadataFor(MEDIA, metadata); checkMetadataContainsEntries(metadata, inputEntries); - media = mediaClient.getMedia(mediaRef); + media = mediaClient.getMedia(media); Checks.checkMediaFor(MEDIA, media); } @@ -215,7 +290,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { @Test(testName = "GET /media/{id}/metadata/{key}", dependsOnMethods = { "testMergeMetadata" }) public void testGetMetadataValue() { - metadataValue = mediaClient.getMetadataValue(mediaRef, "key"); + metadataValue = mediaClient.getMetadataValue(media, "key"); Checks.checkMetadataValueFor(MEDIA, metadataValue); } @@ -225,20 +300,20 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { metadataEntryValue = "newValue"; MetadataValue newValue = MetadataValue.builder().value(metadataEntryValue).build(); - Task setMetadataEntry = mediaClient.setMetadata(mediaRef, "key", newValue); + Task setMetadataEntry = mediaClient.setMetadata(media, "key", newValue); Checks.checkTask(setMetadataEntry); - assertTrue(retryTaskSuccess.apply(setMetadataEntry.getHref()), + assertTrue(retryTaskSuccess.apply(setMetadataEntry), String.format(TASK_COMPLETE_TIMELY, "setMetadataEntry")); - metadataValue = mediaClient.getMetadataValue(mediaRef, "key"); + metadataValue = mediaClient.getMetadataValue(media, "key"); Checks.checkMetadataValueFor(MEDIA, metadataValue); } @Test(testName = "DELETE /media/{id}/metadata/{key}", dependsOnMethods = { "testSetMetadataValue" } ) public void testDeleteMetadata() { - Task deleteMetadataEntry = mediaClient.deleteMetadataEntry(mediaRef, "testKey"); + Task deleteMetadataEntry = mediaClient.deleteMetadataEntry(media, "testKey"); Checks.checkTask(deleteMetadataEntry); - assertTrue(retryTaskSuccess.apply(deleteMetadataEntry.getHref()), + assertTrue(retryTaskSuccess.apply(deleteMetadataEntry), String.format(TASK_COMPLETE_TIMELY, "deleteMetadataEntry")); Error expected = Error.builder() @@ -248,7 +323,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { .build(); try { - metadataValue = mediaClient.getMetadataValue(mediaRef, "testKey"); + metadataValue = mediaClient.getMetadataValue(media, "testKey"); fail("Should give HTTP 403 error"); } catch (VCloudDirectorException vde) { assertEquals(vde.getError(), expected); @@ -263,31 +338,31 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { "metadataEntry", metadataValue.toString())); } - metadataValue = mediaClient.getMetadataValue(mediaRef, "key"); + metadataValue = mediaClient.getMetadataValue(media, "key"); Checks.checkMetadataValueFor(MEDIA, metadataValue); - media = mediaClient.getMedia(mediaRef); + media = mediaClient.getMedia(media); Checks.checkMediaFor(MEDIA, media); } @Test(testName = "DELETE /media/{id}", dependsOnMethods = { "testDeleteMetadata" } ) public void testDeleteMedia() { - Task deleteMedia = mediaClient.deleteMedia(mediaRef); + Task deleteMedia = mediaClient.deleteMedia(media); Checks.checkTask(deleteMedia); - assertTrue(retryTaskSuccess.apply(deleteMedia.getHref()), + assertTrue(retryTaskSuccess.apply(deleteMedia), String.format(TASK_COMPLETE_TIMELY, "deleteMedia")); Error expected = Error.builder() .message(String.format( "No access to entity \"(com.vmware.vcloud.entity.media:%s)\".", - mediaRef.getId())) + media.getId())) .majorErrorCode(403) .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") .build(); try { - media = mediaClient.getMedia(mediaRef); + media = mediaClient.getMedia(media); fail("Should give HTTP 403 error"); } catch (VCloudDirectorException vde) { assertEquals(vde.getError(), expected);