String data = "Here is my data";
URI container = connection.createContainer(containerName);
// Test PUT with string data, ETag hash, and a piece of metadata
PCSFile object = connection.newFile();
object.getMetadata().setName("object");
object.getMetadata().setMimeType("text/plain");
object.setPayload(data);
URI objectURI = connection.uploadFile(container, object);
connection.putMetadataItem(objectURI, "name", "object");
try {
connection.downloadFile(uriBuilderProvider.get().uri(objectURI).path("sad").build());
fail("Expected KeyNotFoundException");
} catch (KeyNotFoundException e) {
}
// Test GET of object (including updated metadata)
InputStream file = connection.downloadFile(objectURI);
assertEquals(Strings2.toStringAndClose(file), data);
validateFileInfoAndNameIsInMetadata(container, objectURI, "object", Long.valueOf(data.length()));
try {
connection.uploadFile(container, object);
} catch (Throwable e) {
assertEquals(e.getCause().getClass(), HttpResponseException.class);
assertEquals(((HttpResponseException) e.getCause()).getResponse().getStatusCode(), 422);
}
connection.deleteFile(objectURI);
try {
connection.getFileInfo(objectURI);
} catch (Throwable e) {
assertEquals(e.getClass(), KeyNotFoundException.class);
}
String name = "sad";
// try sending it in 2 parts
object.getMetadata().setName(name);
objectURI = connection.createFile(container, object);
validateFileInfoAndNameIsInMetadata(container, objectURI, name, Long.valueOf(0));
object.setPayload(data.substring(0, 2));
connection.uploadBlock(objectURI, object, range(0, 2));
validateFileInfoAndNameIsInMetadata(container, objectURI, name, Long.valueOf(2));
object.setPayload(data.substring(2));
connection.uploadBlock(objectURI, object, range(2, data.getBytes().length));
validateFileInfoAndNameIsInMetadata(container, objectURI, name, Long.valueOf(data.length()));
file = connection.downloadFile(objectURI);
assertEquals(Strings2.toStringAndClose(file), data);
// change data in an existing file
data = "Here is my datum";
object.setPayload(data.substring(2));
connection.uploadBlock(objectURI, object, range(2, data.getBytes().length));
validateFileInfoAndNameIsInMetadata(container, objectURI, name, Long.valueOf(data.length()));
file = connection.downloadFile(objectURI);
assertEquals(Strings2.toStringAndClose(file), data);