upload client now works

This commit is contained in:
Adrian Cole 2012-03-11 12:56:25 -07:00
parent d820e4fab8
commit b309dd60df
4 changed files with 37 additions and 41 deletions

View File

@ -384,6 +384,7 @@ public class Task extends EntityType<Task> {
* <li>aborted - The task was aborted by an administrative action. * <li>aborted - The task was aborted by an administrative action.
* </ul> * </ul>
*/ */
// TODO: enum!!!
public String getStatus() { public String getStatus() {
return status; return status;
} }

View File

@ -18,38 +18,27 @@
*/ */
package org.jclouds.vcloud.director.v1_5.features; package org.jclouds.vcloud.director.v1_5.features;
import java.io.File;
import java.net.URI; import java.net.URI;
import javax.ws.rs.PUT; 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.rest.annotations.RequestFilters;
import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest; 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; import com.google.common.util.concurrent.ListenableFuture;
/** /**
*
* @see UploadClient * @see UploadClient
* @author danikov * @author danikov
*/ */
@RequestFilters(AddVCloudAuthorizationToRequest.class) @RequestFilters(AddVCloudAuthorizationToRequest.class)
public interface UploadAsyncClient { // TODO: implement these operations correctly public interface UploadAsyncClient {
/** /**
* @see UploadClient#uploadFile(URI, File) * @see UploadClient#put
*/ */
@PUT @PUT
@ExceptionParser(ThrowVCloudErrorOn4xx.class) ListenableFuture<Void> upload(@EndpointParam URI location, Payload payload);
ListenableFuture<Object> uploadFile(URI uri, File file);
/**
* @see UploadClient#uploadBigFile(URI, File)
*/
@PUT
@ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<Object> uploadBigFile(URI target, File file);
} }

View File

@ -18,31 +18,25 @@
*/ */
package org.jclouds.vcloud.director.v1_5.features; package org.jclouds.vcloud.director.v1_5.features;
import java.io.File;
import java.net.URI; import java.net.URI;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout; import org.jclouds.concurrent.Timeout;
import org.jclouds.io.Payload;
/** /**
* Provides synchronous access to Upload. * Provides synchronous access to Upload.
* <p/> * <p/>
* *
* @see UploadAsyncClient * @see UploadAsyncClient
* @see <a href= "http://support.theenterprisecloud.com/kb/default.asp?id=984&Lang=1&SID=" />
* @author danikov * @author danikov
*/ */
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS) @Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
public interface UploadClient { public interface UploadClient {
/** /**
* Uploads a file. * @return eTag
*/ */
void uploadFile(URI uri, File file); void upload(URI location, Payload payload);
/**
* Uploads a file using ranged PUTs.
*/
void uploadBigFile(URI uri, File file);
} }

View File

@ -47,6 +47,7 @@ import static org.testng.Assert.fail;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Set; import java.util.Set;
import org.jclouds.io.Payloads;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; 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.Checks;
@ -102,19 +103,20 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
private MetadataValue metadataValue; private MetadataValue metadataValue;
private String metadataEntryValue = "value"; private String metadataEntryValue = "value";
@Test(testName = "POST /vdc/{id}/media", enabled = false) @Test(testName = "POST /vdc/{id}/media")
public void testCreateMedia() throws URISyntaxException { public void testCreateMedia() throws URISyntaxException {
assertNotNull(vdcURI, String.format(REF_REQ_LIVE, VDC)); assertNotNull(vdcURI, String.format(REF_REQ_LIVE, VDC));
Vdc vdc = vdcClient.getVdc(vdcURI); Vdc vdc = vdcClient.getVdc(vdcURI);
assertNotNull(vdc, String.format(OBJ_REQ_LIVE, VDC)); assertNotNull(vdc, String.format(OBJ_REQ_LIVE, VDC));
Link addMedia = find(vdc.getLinks(), and(relEquals("add"), typeEquals(VCloudDirectorMediaType.MEDIA))); 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() Media sourceMedia = Media.builder()
.type(VCloudDirectorMediaType.MEDIA) .type(VCloudDirectorMediaType.MEDIA)
.name("Test media 1") .name("Test media 1")
.size(sourceFile.length()) .size(iso.length)
.imageType(Media.ImageType.ISO) .imageType(Media.ImageType.ISO)
.description("Test media generated by testCreateMedia()") .description("Test media generated by testCreateMedia()")
.build(); .build();
@ -136,12 +138,22 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
links.size())); links.size()));
Link uploadLink = getFirst(links, null); Link uploadLink = getFirst(links, null);
assertTrue(equal(uploadLink.getRel(), Link.Rel.UPLOAD_DEFAULT), String.format(OBJ_FIELD_REQ, MEDIA, "uploadFile.links.first")); 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 media = mediaClient.getMedia(media.getHref());
context.getApi().getUploadClient().uploadFile(uploadLink.getHref(), sourceFile);
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() { public void testGetMedia() {
media = mediaClient.getMedia(media.getHref()); media = mediaClient.getMedia(media.getHref());
assertNotNull(media, String.format(OBJ_REQ_LIVE, MEDIA)); assertNotNull(media, String.format(OBJ_REQ_LIVE, MEDIA));
@ -154,7 +166,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "GET /media/{id}/owner", @Test(testName = "GET /media/{id}/owner",
dependsOnMethods = { "testGetMedia" }, enabled = false) dependsOnMethods = { "testGetMedia" })
public void testGetMediaOwner() { public void testGetMediaOwner() {
Owner directOwner = mediaClient.getOwner(media.getHref()); Owner directOwner = mediaClient.getOwner(media.getHref());
assertEquals(owner, directOwner, String.format(GETTER_RETURNS_SAME_OBJ, 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", @Test(testName = "POST /vdc/{id}/action/cloneMedia",
dependsOnMethods = { "testGetMediaOwner" }, enabled = false) dependsOnMethods = { "testGetMediaOwner" })
public void testCloneMedia() { public void testCloneMedia() {
oldMedia = media; oldMedia = media;
media = vdcClient.cloneMedia(vdcURI, CloneMediaParams.builder() media = vdcClient.cloneMedia(vdcURI, CloneMediaParams.builder()
@ -219,7 +231,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "PUT /media/{id}", @Test(testName = "PUT /media/{id}",
dependsOnMethods = { "testCloneMedia" }, enabled = false) dependsOnMethods = { "testCloneMedia" })
public void testSetMedia() { public void testSetMedia() {
String oldName = media.getName(); String oldName = media.getName();
String newName = "new "+oldName; String newName = "new "+oldName;
@ -249,7 +261,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "GET /media/{id}/metadata", @Test(testName = "GET /media/{id}/metadata",
dependsOnMethods = { "testGetMedia" }, enabled = false) dependsOnMethods = { "testGetMedia" })
public void testGetMetadata() { public void testGetMetadata() {
metadata = mediaClient.getMetadataClient().getMetadata(media.getHref()); metadata = mediaClient.getMetadataClient().getMetadata(media.getHref());
// required for testing // required for testing
@ -260,7 +272,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "POST /media/{id}/metadata", @Test(testName = "POST /media/{id}/metadata",
dependsOnMethods = { "testGetMetadata" }, enabled = false) dependsOnMethods = { "testGetMetadata" })
public void testMergeMetadata() { public void testMergeMetadata() {
// test new // test new
Set<MetadataEntry> inputEntries = ImmutableSet.of(MetadataEntry.builder().entry("testKey", "testValue").build()); Set<MetadataEntry> 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}", @Test(testName = "GET /media/{id}/metadata/{key}",
dependsOnMethods = { "testMergeMetadata" }, enabled = false) dependsOnMethods = { "testMergeMetadata" })
public void testGetMetadataValue() { public void testGetMetadataValue() {
metadataValue = mediaClient.getMetadataClient().getMetadataValue(media.getHref(), "key"); metadataValue = mediaClient.getMetadataClient().getMetadataValue(media.getHref(), "key");
Checks.checkMetadataValueFor(MEDIA, metadataValue); Checks.checkMetadataValueFor(MEDIA, metadataValue);
} }
@Test(testName = "PUT /media/{id}/metadata/{key}", @Test(testName = "PUT /media/{id}/metadata/{key}",
dependsOnMethods = { "testGetMetadataValue" }, enabled = false) dependsOnMethods = { "testGetMetadataValue" })
public void testSetMetadataValue() { public void testSetMetadataValue() {
metadataEntryValue = "newValue"; metadataEntryValue = "newValue";
MetadataValue newValue = MetadataValue.builder().value(metadataEntryValue).build(); MetadataValue newValue = MetadataValue.builder().value(metadataEntryValue).build();
@ -334,7 +346,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "DELETE /media/{id}/metadata/{key}", @Test(testName = "DELETE /media/{id}/metadata/{key}",
dependsOnMethods = { "testSetMetadataValue" }, enabled = false ) dependsOnMethods = { "testSetMetadataValue" } )
public void testDeleteMetadata() { public void testDeleteMetadata() {
Task deleteMetadataEntry = mediaClient.getMetadataClient().deleteMetadataEntry(media.getHref(), "testKey"); Task deleteMetadataEntry = mediaClient.getMetadataClient().deleteMetadataEntry(media.getHref(), "testKey");
Checks.checkTask(deleteMetadataEntry); Checks.checkTask(deleteMetadataEntry);
@ -371,7 +383,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "DELETE /media/{id}", @Test(testName = "DELETE /media/{id}",
dependsOnMethods = { "testDeleteMetadata" }, enabled = false ) dependsOnMethods = { "testDeleteMetadata" } )
public void testDeleteMedia() { public void testDeleteMedia() {
Task deleteMedia = mediaClient.deleteMedia(media.getHref()); Task deleteMedia = mediaClient.deleteMedia(media.getHref());
Checks.checkTask(deleteMedia); Checks.checkTask(deleteMedia);