From b309dd60df74dc6d6ea2d5eefa397953be72afd0 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 11 Mar 2012 12:56:25 -0700 Subject: [PATCH] upload client now works --- .../vcloud/director/v1_5/domain/Task.java | 1 + .../v1_5/features/UploadAsyncClient.java | 23 +++------- .../director/v1_5/features/UploadClient.java | 12 ++---- .../v1_5/features/MediaClientLiveTest.java | 42 ++++++++++++------- 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Task.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Task.java index 5a02c8e348..1ddfda0972 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Task.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Task.java @@ -384,6 +384,7 @@ public class Task extends EntityType { *
  • aborted - The task was aborted by an administrative action. * */ + // TODO: enum!!! public String getStatus() { return status; } diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/UploadAsyncClient.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/UploadAsyncClient.java index 3921252314..fecd69710e 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/UploadAsyncClient.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/UploadAsyncClient.java @@ -18,38 +18,27 @@ */ package org.jclouds.vcloud.director.v1_5.features; -import java.io.File; import java.net.URI; import javax.ws.rs.PUT; -import org.jclouds.rest.annotations.ExceptionParser; +import org.jclouds.io.Payload; +import org.jclouds.rest.annotations.EndpointParam; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest; -import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xx; import com.google.common.util.concurrent.ListenableFuture; /** - + * * @see UploadClient * @author danikov */ @RequestFilters(AddVCloudAuthorizationToRequest.class) -public interface UploadAsyncClient { // TODO: implement these operations correctly - +public interface UploadAsyncClient { /** - * @see UploadClient#uploadFile(URI, File) + * @see UploadClient#put */ @PUT - @ExceptionParser(ThrowVCloudErrorOn4xx.class) - ListenableFuture uploadFile(URI uri, File file); - - /** - * @see UploadClient#uploadBigFile(URI, File) - */ - @PUT - @ExceptionParser(ThrowVCloudErrorOn4xx.class) - ListenableFuture uploadBigFile(URI target, File file); - + ListenableFuture upload(@EndpointParam URI location, Payload payload); } diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/UploadClient.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/UploadClient.java index 147c7a415f..4311cda021 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/UploadClient.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/UploadClient.java @@ -18,31 +18,25 @@ */ package org.jclouds.vcloud.director.v1_5.features; -import java.io.File; import java.net.URI; import java.util.concurrent.TimeUnit; import org.jclouds.concurrent.Timeout; +import org.jclouds.io.Payload; /** * Provides synchronous access to Upload. *

    * * @see UploadAsyncClient - * @see * @author danikov */ @Timeout(duration = 180, timeUnit = TimeUnit.SECONDS) public interface UploadClient { /** - * Uploads a file. + * @return eTag */ - void uploadFile(URI uri, File file); - - /** - * Uploads a file using ranged PUTs. - */ - void uploadBigFile(URI uri, File file); + void upload(URI location, Payload payload); } 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 795882d43c..95291310cd 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 @@ -47,6 +47,7 @@ import static org.testng.Assert.fail; import java.net.URISyntaxException; import java.util.Set; +import org.jclouds.io.Payloads; 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; @@ -102,19 +103,20 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { private MetadataValue metadataValue; private String metadataEntryValue = "value"; - @Test(testName = "POST /vdc/{id}/media", enabled = false) + @Test(testName = "POST /vdc/{id}/media") public void testCreateMedia() throws URISyntaxException { assertNotNull(vdcURI, String.format(REF_REQ_LIVE, VDC)); Vdc vdc = vdcClient.getVdc(vdcURI); assertNotNull(vdc, String.format(OBJ_REQ_LIVE, VDC)); Link addMedia = find(vdc.getLinks(), and(relEquals("add"), typeEquals(VCloudDirectorMediaType.MEDIA))); - java.io.File sourceFile = new java.io.File(getClass().getResource("/media/test.iso").toURI()); + // TODO: generate an iso + byte[] iso = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; Media sourceMedia = Media.builder() .type(VCloudDirectorMediaType.MEDIA) .name("Test media 1") - .size(sourceFile.length()) + .size(iso.length) .imageType(Media.ImageType.ISO) .description("Test media generated by testCreateMedia()") .build(); @@ -136,12 +138,22 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { links.size())); Link uploadLink = getFirst(links, null); assertTrue(equal(uploadLink.getRel(), Link.Rel.UPLOAD_DEFAULT), String.format(OBJ_FIELD_REQ, MEDIA, "uploadFile.links.first")); + + context.getApi().getUploadClient().upload(uploadLink.getHref(), Payloads.newByteArrayPayload(iso)); - fail(); //TODO upload file and assert it succeeds - context.getApi().getUploadClient().uploadFile(uploadLink.getHref(), sourceFile); + media = mediaClient.getMedia(media.getHref()); + + Task task = Iterables.getOnlyElement(media.getTasksInProgress().getTasks()); + + assertEquals(task.getStatus(), "running"); + + File file = Iterables.getOnlyElement(media.getFiles().getFiles()); + assertEquals(file.getSize(), new Long(iso.length)); + assertEquals(file.getBytesTransferred(), new Long(iso.length)); + } - @Test(testName = "GET /media/{id}", dependsOnMethods = { "testCreateMedia" }, enabled = false) + @Test(testName = "GET /media/{id}", dependsOnMethods = { "testCreateMedia" }) public void testGetMedia() { media = mediaClient.getMedia(media.getHref()); assertNotNull(media, String.format(OBJ_REQ_LIVE, MEDIA)); @@ -154,7 +166,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { } @Test(testName = "GET /media/{id}/owner", - dependsOnMethods = { "testGetMedia" }, enabled = false) + dependsOnMethods = { "testGetMedia" }) public void testGetMediaOwner() { Owner directOwner = mediaClient.getOwner(media.getHref()); assertEquals(owner, directOwner, String.format(GETTER_RETURNS_SAME_OBJ, @@ -169,7 +181,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { } @Test(testName = "POST /vdc/{id}/action/cloneMedia", - dependsOnMethods = { "testGetMediaOwner" }, enabled = false) + dependsOnMethods = { "testGetMediaOwner" }) public void testCloneMedia() { oldMedia = media; media = vdcClient.cloneMedia(vdcURI, CloneMediaParams.builder() @@ -219,7 +231,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { } @Test(testName = "PUT /media/{id}", - dependsOnMethods = { "testCloneMedia" }, enabled = false) + dependsOnMethods = { "testCloneMedia" }) public void testSetMedia() { String oldName = media.getName(); String newName = "new "+oldName; @@ -249,7 +261,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { } @Test(testName = "GET /media/{id}/metadata", - dependsOnMethods = { "testGetMedia" }, enabled = false) + dependsOnMethods = { "testGetMedia" }) public void testGetMetadata() { metadata = mediaClient.getMetadataClient().getMetadata(media.getHref()); // required for testing @@ -260,7 +272,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { } @Test(testName = "POST /media/{id}/metadata", - dependsOnMethods = { "testGetMetadata" }, enabled = false) + dependsOnMethods = { "testGetMetadata" }) public void testMergeMetadata() { // test new Set inputEntries = ImmutableSet.of(MetadataEntry.builder().entry("testKey", "testValue").build()); @@ -313,14 +325,14 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { } @Test(testName = "GET /media/{id}/metadata/{key}", - dependsOnMethods = { "testMergeMetadata" }, enabled = false) + dependsOnMethods = { "testMergeMetadata" }) public void testGetMetadataValue() { metadataValue = mediaClient.getMetadataClient().getMetadataValue(media.getHref(), "key"); Checks.checkMetadataValueFor(MEDIA, metadataValue); } @Test(testName = "PUT /media/{id}/metadata/{key}", - dependsOnMethods = { "testGetMetadataValue" }, enabled = false) + dependsOnMethods = { "testGetMetadataValue" }) public void testSetMetadataValue() { metadataEntryValue = "newValue"; MetadataValue newValue = MetadataValue.builder().value(metadataEntryValue).build(); @@ -334,7 +346,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { } @Test(testName = "DELETE /media/{id}/metadata/{key}", - dependsOnMethods = { "testSetMetadataValue" }, enabled = false ) + dependsOnMethods = { "testSetMetadataValue" } ) public void testDeleteMetadata() { Task deleteMetadataEntry = mediaClient.getMetadataClient().deleteMetadataEntry(media.getHref(), "testKey"); Checks.checkTask(deleteMetadataEntry); @@ -371,7 +383,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { } @Test(testName = "DELETE /media/{id}", - dependsOnMethods = { "testDeleteMetadata" }, enabled = false ) + dependsOnMethods = { "testDeleteMetadata" } ) public void testDeleteMedia() { Task deleteMedia = mediaClient.deleteMedia(media.getHref()); Checks.checkTask(deleteMedia);