Issue 75: cleaned up context bulding and added logging

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1638 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-07-17 15:26:09 +00:00
parent 2d68a4e748
commit cf8fa7aa33
1 changed files with 20 additions and 22 deletions

View File

@ -36,11 +36,13 @@ import java.util.concurrent.TimeUnit;
import org.jclouds.http.HttpResponseException;
import org.jclouds.http.HttpUtils;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rackspace.cloudfiles.domain.AccountMetadata;
import org.jclouds.rackspace.cloudfiles.domain.CFObject;
import org.jclouds.rackspace.cloudfiles.domain.ContainerMetadata;
import org.jclouds.rackspace.cloudfiles.options.ListContainerOptions;
import org.jclouds.rackspace.cloudfiles.reference.CloudFilesHeaders;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
/**
@ -55,11 +57,16 @@ public class CloudFilesConnectionLiveTest {
protected static final String sysRackspaceKey = System.getProperty(PROPERTY_RACKSPACE_KEY);
private String bucketPrefix = System.getProperty("user.name") + ".cfint";
CloudFilesConnection connection;
@BeforeGroups(groups = { "live" })
public void setupConnection() {
connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser, sysRackspaceKey)
.withModule(new Log4JLoggingModule()).withJsonDebug().buildContext().getConnection();
}
@Test
public void testListOwnedContainers() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
List<ContainerMetadata> response = connection.listOwnedContainers();
assertNotNull(response);
long initialContainerCount = response.size();
@ -80,8 +87,6 @@ public class CloudFilesConnectionLiveTest {
@Test
public void testHeadAccountMetadata() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
AccountMetadata metadata = connection.getAccountMetadata();
assertNotNull(metadata);
long initialContainerCount = metadata.getContainerCount();
@ -98,9 +103,6 @@ public class CloudFilesConnectionLiveTest {
@Test
public void testDeleteContainer() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
assertTrue(connection.deleteContainerIfEmpty("does-not-exist"));
String containerName = bucketPrefix + ".testDeleteContainer";
@ -110,8 +112,6 @@ public class CloudFilesConnectionLiveTest {
@Test
public void testPutContainers() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
String containerName1 = bucketPrefix + ".hello";
assertTrue(connection.putContainer(containerName1));
// List only the container just created, using a marker with the container name less 1 char
@ -140,16 +140,14 @@ public class CloudFilesConnectionLiveTest {
assertTrue(connection.deleteContainerIfEmpty(containerName1));
assertTrue(connection.deleteContainerIfEmpty(containerName2));
}
@Test
public void testPutAndDeleteObjects() throws Exception {
CloudFilesConnection connection = CloudFilesContextBuilder.newBuilder(sysRackspaceUser,
sysRackspaceKey).withJsonDebug().buildContext().getConnection();
String containerName = bucketPrefix + ".testPutAndDeleteObjects";
String data = "Here is my data";
assertTrue(connection.putContainer(containerName));
// Test with string data, ETag hash, and a piece of metadata
CFObject object = new CFObject("object", data);
object.setContentLength(data.length());
@ -157,12 +155,12 @@ public class CloudFilesConnectionLiveTest {
object.getMetadata().setContentType("text/plain");
// TODO: Metadata values aren't being stored by CF, but the names are. Odd...
object.getMetadata().getUserMetadata().put(
CloudFilesHeaders.USER_METADATA_PREFIX + "metadata", "metadata-value");
CloudFilesHeaders.USER_METADATA_PREFIX + "metadata", "metadata-value");
byte[] md5 = connection.putObject(containerName, object).get(10, TimeUnit.SECONDS);
assertEquals(HttpUtils.toHexString(md5),
HttpUtils.toHexString(object.getMetadata().getETag()));
assertEquals(HttpUtils.toHexString(md5), HttpUtils
.toHexString(object.getMetadata().getETag()));
// TODO: Get and confirm data
// Test with invalid ETag (as if object's data was corrupted in transit)
String correctEtag = HttpUtils.toHexString(object.getMetadata().getETag());
String incorrectEtag = "0" + correctEtag.substring(1);
@ -171,19 +169,19 @@ public class CloudFilesConnectionLiveTest {
connection.putObject(containerName, object).get(10, TimeUnit.SECONDS);
} catch (Throwable e) {
assertEquals(e.getCause().getClass(), HttpResponseException.class);
assertEquals(((HttpResponseException)e.getCause()).getResponse().getStatusCode(), 422);
assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422);
}
// Test chunked/streamed upload with data of "unknown" length
ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes("UTF-8"));
object = new CFObject("chunked-object", bais);
md5 = connection.putObject(containerName, object).get(10, TimeUnit.SECONDS);
assertEquals(HttpUtils.toHexString(md5), correctEtag);
// TODO: Get and confirm data
assertTrue(connection.deleteObject(containerName, "object"));
assertTrue(connection.deleteObject(containerName, "chunked-object"));
assertTrue(connection.deleteContainerIfEmpty(containerName));
}
}