use extracted checks, nicer test names

This commit is contained in:
danikov 2012-02-23 13:48:09 +00:00
parent 099f9137d0
commit 2ff4e8317c
1 changed files with 45 additions and 75 deletions

View File

@ -22,17 +22,13 @@ import static com.google.common.base.Objects.equal;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.GETTER_RETURNS_SAME_OBJ; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.GETTER_RETURNS_SAME_OBJ;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_DEL; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_DEL;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_ATTRB_DEL; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_ATTRB_DEL;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_ATTRB_REQ;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_CONTAINS; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_CONTAINS;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_EQ;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_GTE_0;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_REQ; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_REQ;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_REQ_LIVE; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_REQ_LIVE;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_UPDATABLE; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_UPDATABLE;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_REQ_LIVE; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_REQ_LIVE;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.TASK_COMPLETE_TIMELY; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.TASK_COMPLETE_TIMELY;
import static org.jclouds.vcloud.director.v1_5.domain.Checks.checkResourceType;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
@ -75,18 +71,24 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
* Convenience references to API clients. * Convenience references to API clients.
*/ */
private Reference mediaRef;
protected MediaClient mediaClient; protected MediaClient mediaClient;
@BeforeClass(inheritGroups = true) @BeforeClass(inheritGroups = true)
@Override @Override
public void setupRequiredClients() { public void setupRequiredClients() {
mediaRef = Reference.builder()
.type("application/vnd.vmware.vcloud.media+xml")
.name("")
.href(URI.create(endpoint+"/media/" + mediaId))
.id(mediaId)
.build();
mediaClient = context.getApi().getMediaClient(); mediaClient = context.getApi().getMediaClient();
} }
/* /*
* Shared state between dependent tests. * Shared state between dependent tests.
*/ */
private Reference mediaRef;
private Media media; private Media media;
private Owner owner; private Owner owner;
private Metadata metadata; private Metadata metadata;
@ -95,17 +97,12 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
@BeforeGroups(groups = { "live" }) @BeforeGroups(groups = { "live" })
public void createReferenceData() { public void createReferenceData() {
mediaRef = Reference.builder() // FIXME: don't want to be modifying anything here!
.type("application/vnd.vmware.vcloud.media+xml")
.name("")
.href(URI.create(endpoint+"/media/" + mediaId))
.id(mediaId)
.build();
mediaClient.setMetadata(mediaRef, "key", MetadataValue.builder().value("value").build()); mediaClient.setMetadata(mediaRef, "key", MetadataValue.builder().value("value").build());
} }
@Test(testName = "GET /media/{id}") @Test(testName = "GET /media/{id}")
public void testWhenResponseIs2xxLoginReturnsValidMedia() { public void testGetMedia() {
// required for testing // required for testing
assertNotNull(mediaRef, String.format(REF_REQ_LIVE, MEDIA)); assertNotNull(mediaRef, String.format(REF_REQ_LIVE, MEDIA));
@ -117,20 +114,12 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
assertNotNull(owner, String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "owner")); assertNotNull(owner, String.format(OBJ_FIELD_REQ_LIVE, MEDIA, "owner"));
Checks.checkResourceType(media.getOwner()); Checks.checkResourceType(media.getOwner());
// parent type Checks.checkMediaFor(MEDIA, media);
Checks.checkResourceEntityType(media);
// required
String imageType = media.getImageType();
Checks.checkImageType(imageType);
Long size = media.getSize();
assertNotNull(size, String.format(OBJ_FIELD_REQ, MEDIA, "size"));
assertTrue(size >= 0, String.format(OBJ_FIELD_GTE_0, MEDIA, "size", size));
} }
@Test(testName = "GET /media/{id}/owner", @Test(testName = "GET /media/{id}/owner",
dependsOnMethods = { "testWhenResponseIs2xxLoginReturnsValidMedia" }) dependsOnMethods = { "testGetMedia" })
public void testWhenResponseIs2xxLoginReturnsValidMediaOwner() { public void testGetMediaOwner() {
Owner directOwner = mediaClient.getOwner(mediaRef); Owner directOwner = mediaClient.getOwner(mediaRef);
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()));
@ -144,8 +133,8 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "PUT /media/{id}", @Test(testName = "PUT /media/{id}",
dependsOnMethods = { "testWhenResponseIs2xxLoginReturnsValidMedia" }) dependsOnMethods = { "testGetMedia" })
public void testWhenResponseIs2xxLoginReturnsValidNetwork() { public void testSetMedia() {
String oldName = media.getName(); String oldName = media.getName();
String newName = "new "+oldName; String newName = "new "+oldName;
String oldDescription = media.getDescription(); String oldDescription = media.getDescription();
@ -164,11 +153,10 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
//TODO negative tests? //TODO negative tests?
// ensure media remains valid Checks.checkMediaFor(MEDIA, media);
testWhenResponseIs2xxLoginReturnsValidMedia();
media.setName(newName); media.setName(oldName);
media.setDescription(newDescription); media.setDescription(oldDescription);
updateMedia = mediaClient.updateMedia(mediaRef, media); updateMedia = mediaClient.updateMedia(mediaRef, media);
Checks.checkTask(updateMedia); Checks.checkTask(updateMedia);
@ -177,31 +165,19 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "GET /media/{id}/metadata", @Test(testName = "GET /media/{id}/metadata",
dependsOnMethods = { "testWhenResponseIs2xxLoginReturnsValidMedia" }) dependsOnMethods = { "testGetMedia" })
public void testWhenResponseIs2xxLoginReturnsValidMetadata() { public void testGetMetadata() {
metadata = mediaClient.getMetadata(mediaRef); metadata = mediaClient.getMetadata(mediaRef);
// 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"));
// parent type Checks.checkMetadataFor(MEDIA, metadata);
checkResourceType(metadata);
for (MetadataEntry entry : metadata.getMetadataEntries()) {
// required elements and attributes
assertNotNull(entry.getKey(),
String.format(OBJ_FIELD_ATTRB_REQ, MEDIA, "MetadataEntry", metadataValue, "key"));
assertNotNull(entry.getValue(),
String.format(OBJ_FIELD_ATTRB_REQ, MEDIA, "MetadataEntry", metadataValue, "value"));
// parent type
checkResourceType(entry);
}
} }
@Test(testName = "POST /media/{id}/metadata", @Test(testName = "POST /media/{id}/metadata",
dependsOnMethods = { "testWhenResponseIs2xxLoginReturnsValidMetadata" }) dependsOnMethods = { "testGetMetadata" })
public void testWhenResponseIs2xxLoginMergedMetadata() { 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());
Metadata inputMetadata = Metadata.builder() Metadata inputMetadata = Metadata.builder()
@ -212,9 +188,11 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
Checks.checkTask(mergeMetadata); Checks.checkTask(mergeMetadata);
assertTrue(retryTaskSuccess.apply(mergeMetadata.getHref()), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(new)")); assertTrue(retryTaskSuccess.apply(mergeMetadata.getHref()), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(new)"));
metadata = mediaClient.getMetadata(mediaRef); metadata = mediaClient.getMetadata(mediaRef);
Checks.checkMetadataFor(MEDIA, metadata);
checkMetadataContainsEntries(metadata, inputEntries); checkMetadataContainsEntries(metadata, inputEntries);
testWhenResponseIs2xxLoginReturnsValidMetadata(); media = mediaClient.getMedia(mediaRef);
Checks.checkMediaFor(MEDIA, media);
// test modify // test modify
inputEntries = ImmutableSet.of(MetadataEntry.builder().entry("testKey", "new testValue").build()); inputEntries = ImmutableSet.of(MetadataEntry.builder().entry("testKey", "new testValue").build());
@ -226,9 +204,11 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
Checks.checkTask(mergeMetadata); Checks.checkTask(mergeMetadata);
assertTrue(retryTaskSuccess.apply(mergeMetadata.getHref()), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(modify)")); assertTrue(retryTaskSuccess.apply(mergeMetadata.getHref()), String.format(TASK_COMPLETE_TIMELY, "mergeMetadata(modify)"));
metadata = mediaClient.getMetadata(mediaRef); metadata = mediaClient.getMetadata(mediaRef);
Checks.checkMetadataFor(MEDIA, metadata);
checkMetadataContainsEntries(metadata, inputEntries); checkMetadataContainsEntries(metadata, inputEntries);
testWhenResponseIs2xxLoginReturnsValidMetadata(); media = mediaClient.getMedia(mediaRef);
Checks.checkMediaFor(MEDIA, media);
} }
private void checkMetadataContainsEntries(Metadata metadata, Set<MetadataEntry> entries) { private void checkMetadataContainsEntries(Metadata metadata, Set<MetadataEntry> entries) {
@ -249,25 +229,15 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
} }
@Test(testName = "GET /media/{id}/metadata/{key}", @Test(testName = "GET /media/{id}/metadata/{key}",
dependsOnMethods = { "testWhenResponseIs2xxLoginMergedMetadata" }) dependsOnMethods = { "testMergeMetadata" })
public void testWhenResponseIs2xxLoginReturnsValidMetadataValue() { public void testGetMetadataValue() {
metadataValue = mediaClient.getMetadataValue(mediaRef, "key"); metadataValue = mediaClient.getMetadataValue(mediaRef, "key");
Checks.checkMetadataValueFor(MEDIA, metadataValue);
// Check parent type
checkResourceType(metadataValue);
// Check required elements and attributes
String value = metadataValue.getValue();
assertNotNull(value,
String.format(OBJ_FIELD_ATTRB_REQ, MEDIA, "MetadataEntry",
metadataValue.toString(), metadataEntryValue.toString()));
assertEquals(value, metadataEntryValue,
String.format(OBJ_FIELD_EQ, MEDIA, "metadataEntry.value", metadataEntryValue, value));
} }
@Test(testName = "PUT /media/{id}/metadata/{key}", @Test(testName = "PUT /media/{id}/metadata/{key}",
dependsOnMethods = { "testWhenResponseIs2xxLoginReturnsValidMetadataValue" }) dependsOnMethods = { "testGetMetadataValue" })
public void testWhenResponseIs2xxLoginUpdatesMetadataEntry() { public void testSetMetadataValue() {
metadataEntryValue = "newValue"; metadataEntryValue = "newValue";
MetadataValue newValue = MetadataValue.builder().value(metadataEntryValue).build(); MetadataValue newValue = MetadataValue.builder().value(metadataEntryValue).build();
@ -276,14 +246,12 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
assertTrue(retryTaskSuccess.apply(setMetadataEntry.getHref()), assertTrue(retryTaskSuccess.apply(setMetadataEntry.getHref()),
String.format(TASK_COMPLETE_TIMELY, "setMetadataEntry")); String.format(TASK_COMPLETE_TIMELY, "setMetadataEntry"));
metadataValue = mediaClient.getMetadataValue(mediaRef, "key"); metadataValue = mediaClient.getMetadataValue(mediaRef, "key");
Checks.checkMetadataValueFor(MEDIA, metadataValue);
// ensure metadataEntry remains valid
testWhenResponseIs2xxLoginReturnsValidMetadataValue();
} }
@Test(testName = "DELETE /media/{id}/metadata/{key}", @Test(testName = "DELETE /media/{id}/metadata/{key}",
dependsOnMethods = { "testWhenResponseIs2xxLoginUpdatesMetadataEntry" } ) dependsOnMethods = { "testSetMetadataValue" } )
public void testWhenResponseIs2xxLoginDeletesMetadataEntry() { public void testDeleteMetadata() {
Task deleteMetadataEntry = mediaClient.deleteMetadataEntry(mediaRef, "testKey"); Task deleteMetadataEntry = mediaClient.deleteMetadataEntry(mediaRef, "testKey");
Checks.checkTask(deleteMetadataEntry); Checks.checkTask(deleteMetadataEntry);
assertTrue(retryTaskSuccess.apply(deleteMetadataEntry.getHref()), assertTrue(retryTaskSuccess.apply(deleteMetadataEntry.getHref()),
@ -311,14 +279,16 @@ public class MediaClientLiveTest extends BaseVCloudDirectorClientLiveTest {
"metadataEntry", metadataValue.toString())); "metadataEntry", metadataValue.toString()));
} }
// ensure metadata and media remains valid metadataValue = mediaClient.getMetadataValue(mediaRef, "key");
testWhenResponseIs2xxLoginReturnsValidMetadata(); Checks.checkMetadataValueFor(MEDIA, metadataValue);
testWhenResponseIs2xxLoginReturnsValidMedia();
}
@Test(testName = "DELETE /media/{id}",
dependsOnMethods = { "testWhenResponseIs2xxLoginDeletesMetadataEntry" } )
public void testWhenResponseIs2xxLoginDeletesMedia() {
media = mediaClient.getMedia(mediaRef);
Checks.checkMediaFor(MEDIA, media);
}
@Test(testName = "DELETE /media/{id}",
dependsOnMethods = { "testDeleteMetadata" } )
public void testDeleteMedia() {
Task deleteMedia = mediaClient.deleteMedia(mediaRef); Task deleteMedia = mediaClient.deleteMedia(mediaRef);
Checks.checkTask(deleteMedia); Checks.checkTask(deleteMedia);
assertTrue(retryTaskSuccess.apply(deleteMedia.getHref()), assertTrue(retryTaskSuccess.apply(deleteMedia.getHref()),