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