update and inline vdc clone/create methods into media testing

This commit is contained in:
danikov 2012-02-29 11:39:26 +00:00
parent 58744ab91c
commit f2f2233db0
1 changed files with 122 additions and 47 deletions

View File

@ -23,11 +23,16 @@ import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.*
import static org.testng.Assert.*; import static org.testng.Assert.*;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set; import java.util.Set;
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.domain.Checks; 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.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.Media;
import org.jclouds.vcloud.director.v1_5.domain.Metadata; import org.jclouds.vcloud.director.v1_5.domain.Metadata;
import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry; 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.Owner;
import org.jclouds.vcloud.director.v1_5.domain.Reference; 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.Task;
import org.jclouds.vcloud.director.v1_5.domain.URISupplier;
import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest; import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTest;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -52,49 +57,77 @@ import com.google.common.collect.Iterables;
public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest { public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
public static final String MEDIA = "media"; public static final String MEDIA = "media";
public static final String VDC = "vdc";
/* /*
* Convenience references to API clients. * Convenience references to API clients.
*/ */
private Reference mediaRef; private URISupplier vdcRef;
protected VdcClient vdcClient;
protected MediaClient mediaClient; protected MediaClient mediaClient;
@BeforeClass(inheritGroups = true) @BeforeClass(inheritGroups = true)
@Override @Override
public void setupRequiredClients() { public void setupRequiredClients() {
mediaRef = Reference.builder() vdcRef = Reference.builder()
.type("application/vnd.vmware.vcloud.media+xml") .type("application/vnd.vmware.vcloud.vdc+xml")
.name("") .name("")
.href(URI.create(endpoint+"/media/" + mediaId)) .href(URI.create(endpoint+"/vdc/"+vdcId))
.id(mediaId) .id(vdcId)
.build(); .build();
vdcClient = context.getApi().getVdcClient();
mediaClient = context.getApi().getMediaClient(); mediaClient = context.getApi().getMediaClient();
} }
/* /*
* Shared state between dependent tests. * Shared state between dependent tests.
*/ */
private Media media; private Media media, oldMedia;
private Owner owner; private Owner owner;
private Metadata metadata; private Metadata metadata;
private MetadataValue metadataValue; private MetadataValue metadataValue;
private String metadataEntryValue = "value"; private String metadataEntryValue = "value";
@BeforeGroups(groups = { "live" }) @Test(testName = "POST /vdc/{id}/media")
public void createReferenceData() { public void testCreateMedia() throws URISyntaxException {
// FIXME: don't want to be modifying anything here! assertNotNull(vdcRef, String.format(REF_REQ_LIVE, VDC));
mediaClient.setMetadata(mediaRef, "key", MetadataValue.builder().value("value").build()); 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<Link> 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() { public void testGetMedia() {
// required for testing media = mediaClient.getMedia(media);
assertNotNull(mediaRef, String.format(REF_REQ_LIVE, MEDIA));
media = mediaClient.getMedia(mediaRef);
assertNotNull(media, String.format(OBJ_REQ_LIVE, 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(); owner = media.getOwner();
assertNotNull(owner, String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "owner")); 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", @Test(testName = "GET /media/{id}/owner",
dependsOnMethods = { "testGetMedia" }) dependsOnMethods = { "testGetMedia" })
public void testGetMediaOwner() { public void testGetMediaOwner() {
Owner directOwner = mediaClient.getOwner(mediaRef); Owner directOwner = mediaClient.getOwner(media);
assertEquals(owner, directOwner, String.format(GETTER_RETURNS_SAME_OBJ, assertEquals(owner, directOwner, String.format(GETTER_RETURNS_SAME_OBJ,
"getOwner()", "owner", "media.getOwner()", owner.toString(), directOwner.toString())); "getOwner()", "owner", "media.getOwner()", owner.toString(), directOwner.toString()));
@ -118,8 +151,50 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
Checks.checkReferenceType(directOwner.getUser()); 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}", @Test(testName = "PUT /media/{id}",
dependsOnMethods = { "testGetMedia" }) dependsOnMethods = { "testCloneMedia" })
public void testSetMedia() { public void testSetMedia() {
String oldName = media.getName(); String oldName = media.getName();
String newName = "new "+oldName; String newName = "new "+oldName;
@ -127,10 +202,10 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
String newDescription = "new "+oldDescription; String newDescription = "new "+oldDescription;
media = media.toBuilder().name(newName).description(newDescription).build(); media = media.toBuilder().name(newName).description(newDescription).build();
Task updateMedia = mediaClient.updateMedia(mediaRef, media); Task updateMedia = mediaClient.updateMedia(media, media);
Checks.checkTask(updateMedia); Checks.checkTask(updateMedia);
assertTrue(retryTaskSuccess.apply(updateMedia.getHref()), String.format(TASK_COMPLETE_TIMELY, "updateMedia")); assertTrue(retryTaskSuccess.apply(updateMedia), String.format(TASK_COMPLETE_TIMELY, "updateMedia"));
media = mediaClient.getMedia(mediaRef); media = mediaClient.getMedia(media);
assertTrue(equal(media.getName(), newName), String.format(OBJ_FIELD_UPDATABLE, MEDIA, "name")); assertTrue(equal(media.getName(), newName), String.format(OBJ_FIELD_UPDATABLE, MEDIA, "name"));
assertTrue(equal(media.getDescription(), newDescription), assertTrue(equal(media.getDescription(), newDescription),
@ -142,16 +217,16 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
media = media.toBuilder().name(oldName).description(oldDescription).build(); media = media.toBuilder().name(oldName).description(oldDescription).build();
updateMedia = mediaClient.updateMedia(mediaRef, media); updateMedia = mediaClient.updateMedia(media, media);
Checks.checkTask(updateMedia); Checks.checkTask(updateMedia);
assertTrue(retryTaskSuccess.apply(updateMedia.getHref()), String.format(TASK_COMPLETE_TIMELY, "updateMedia")); assertTrue(retryTaskSuccess.apply(updateMedia), String.format(TASK_COMPLETE_TIMELY, "updateMedia"));
media = mediaClient.getMedia(mediaRef); media = mediaClient.getMedia(media);
} }
@Test(testName = "GET /media/{id}/metadata", @Test(testName = "GET /media/{id}/metadata",
dependsOnMethods = { "testGetMedia" }) dependsOnMethods = { "testGetMedia" })
public void testGetMetadata() { public void testGetMetadata() {
metadata = mediaClient.getMetadata(mediaRef); metadata = mediaClient.getMetadata(media);
// required for testing // required for testing
assertFalse(Iterables.isEmpty(metadata.getMetadataEntries()), assertFalse(Iterables.isEmpty(metadata.getMetadataEntries()),
String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "metadata.entries")); String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "metadata.entries"));
@ -168,14 +243,14 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
.entries(inputEntries) .entries(inputEntries)
.build(); .build();
Task mergeMetadata = mediaClient.mergeMetadata(mediaRef, inputMetadata); Task mergeMetadata = mediaClient.mergeMetadata(media, inputMetadata);
Checks.checkTask(mergeMetadata); Checks.checkTask(mergeMetadata);
assertTrue(retryTaskSuccess.apply(mergeMetadata.getHref()), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(new)")); assertTrue(retryTaskSuccess.apply(mergeMetadata), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(new)"));
metadata = mediaClient.getMetadata(mediaRef); metadata = mediaClient.getMetadata(media);
Checks.checkMetadataFor(MEDIA, metadata); Checks.checkMetadataFor(MEDIA, metadata);
checkMetadataContainsEntries(metadata, inputEntries); checkMetadataContainsEntries(metadata, inputEntries);
media = mediaClient.getMedia(mediaRef); media = mediaClient.getMedia(media);
Checks.checkMediaFor(MEDIA, media); Checks.checkMediaFor(MEDIA, media);
// test modify // test modify
@ -184,14 +259,14 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
.entries(inputEntries) .entries(inputEntries)
.build(); .build();
mergeMetadata = mediaClient.mergeMetadata(mediaRef, inputMetadata); mergeMetadata = mediaClient.mergeMetadata(media, inputMetadata);
Checks.checkTask(mergeMetadata); Checks.checkTask(mergeMetadata);
assertTrue(retryTaskSuccess.apply(mergeMetadata.getHref()), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(modify)")); assertTrue(retryTaskSuccess.apply(mergeMetadata), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(modify)"));
metadata = mediaClient.getMetadata(mediaRef); metadata = mediaClient.getMetadata(media);
Checks.checkMetadataFor(MEDIA, metadata); Checks.checkMetadataFor(MEDIA, metadata);
checkMetadataContainsEntries(metadata, inputEntries); checkMetadataContainsEntries(metadata, inputEntries);
media = mediaClient.getMedia(mediaRef); media = mediaClient.getMedia(media);
Checks.checkMediaFor(MEDIA, media); Checks.checkMediaFor(MEDIA, media);
} }
@ -215,7 +290,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
@Test(testName = "GET /media/{id}/metadata/{key}", @Test(testName = "GET /media/{id}/metadata/{key}",
dependsOnMethods = { "testMergeMetadata" }) dependsOnMethods = { "testMergeMetadata" })
public void testGetMetadataValue() { public void testGetMetadataValue() {
metadataValue = mediaClient.getMetadataValue(mediaRef, "key"); metadataValue = mediaClient.getMetadataValue(media, "key");
Checks.checkMetadataValueFor(MEDIA, metadataValue); Checks.checkMetadataValueFor(MEDIA, metadataValue);
} }
@ -225,20 +300,20 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
metadataEntryValue = "newValue"; metadataEntryValue = "newValue";
MetadataValue newValue = MetadataValue.builder().value(metadataEntryValue).build(); MetadataValue newValue = MetadataValue.builder().value(metadataEntryValue).build();
Task setMetadataEntry = mediaClient.setMetadata(mediaRef, "key", newValue); Task setMetadataEntry = mediaClient.setMetadata(media, "key", newValue);
Checks.checkTask(setMetadataEntry); Checks.checkTask(setMetadataEntry);
assertTrue(retryTaskSuccess.apply(setMetadataEntry.getHref()), assertTrue(retryTaskSuccess.apply(setMetadataEntry),
String.format(TASK_COMPLETE_TIMELY, "setMetadataEntry")); String.format(TASK_COMPLETE_TIMELY, "setMetadataEntry"));
metadataValue = mediaClient.getMetadataValue(mediaRef, "key"); metadataValue = mediaClient.getMetadataValue(media, "key");
Checks.checkMetadataValueFor(MEDIA, metadataValue); Checks.checkMetadataValueFor(MEDIA, metadataValue);
} }
@Test(testName = "DELETE /media/{id}/metadata/{key}", @Test(testName = "DELETE /media/{id}/metadata/{key}",
dependsOnMethods = { "testSetMetadataValue" } ) dependsOnMethods = { "testSetMetadataValue" } )
public void testDeleteMetadata() { public void testDeleteMetadata() {
Task deleteMetadataEntry = mediaClient.deleteMetadataEntry(mediaRef, "testKey"); Task deleteMetadataEntry = mediaClient.deleteMetadataEntry(media, "testKey");
Checks.checkTask(deleteMetadataEntry); Checks.checkTask(deleteMetadataEntry);
assertTrue(retryTaskSuccess.apply(deleteMetadataEntry.getHref()), assertTrue(retryTaskSuccess.apply(deleteMetadataEntry),
String.format(TASK_COMPLETE_TIMELY, "deleteMetadataEntry")); String.format(TASK_COMPLETE_TIMELY, "deleteMetadataEntry"));
Error expected = Error.builder() Error expected = Error.builder()
@ -248,7 +323,7 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
.build(); .build();
try { try {
metadataValue = mediaClient.getMetadataValue(mediaRef, "testKey"); metadataValue = mediaClient.getMetadataValue(media, "testKey");
fail("Should give HTTP 403 error"); fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) { } catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected); assertEquals(vde.getError(), expected);
@ -263,31 +338,31 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
"metadataEntry", metadataValue.toString())); "metadataEntry", metadataValue.toString()));
} }
metadataValue = mediaClient.getMetadataValue(mediaRef, "key"); metadataValue = mediaClient.getMetadataValue(media, "key");
Checks.checkMetadataValueFor(MEDIA, metadataValue); Checks.checkMetadataValueFor(MEDIA, metadataValue);
media = mediaClient.getMedia(mediaRef); media = mediaClient.getMedia(media);
Checks.checkMediaFor(MEDIA, media); Checks.checkMediaFor(MEDIA, media);
} }
@Test(testName = "DELETE /media/{id}", @Test(testName = "DELETE /media/{id}",
dependsOnMethods = { "testDeleteMetadata" } ) dependsOnMethods = { "testDeleteMetadata" } )
public void testDeleteMedia() { public void testDeleteMedia() {
Task deleteMedia = mediaClient.deleteMedia(mediaRef); Task deleteMedia = mediaClient.deleteMedia(media);
Checks.checkTask(deleteMedia); Checks.checkTask(deleteMedia);
assertTrue(retryTaskSuccess.apply(deleteMedia.getHref()), assertTrue(retryTaskSuccess.apply(deleteMedia),
String.format(TASK_COMPLETE_TIMELY, "deleteMedia")); String.format(TASK_COMPLETE_TIMELY, "deleteMedia"));
Error expected = Error.builder() Error expected = Error.builder()
.message(String.format( .message(String.format(
"No access to entity \"(com.vmware.vcloud.entity.media:%s)\".", "No access to entity \"(com.vmware.vcloud.entity.media:%s)\".",
mediaRef.getId())) media.getId()))
.majorErrorCode(403) .majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build(); .build();
try { try {
media = mediaClient.getMedia(mediaRef); media = mediaClient.getMedia(media);
fail("Should give HTTP 403 error"); fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) { } catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected); assertEquals(vde.getError(), expected);