String filename = makeFileName("random-with-meta");
String fullPath = FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filename);
logger.info("Test File Location: " + fullPath);
try {
byte randomData[] = makeRandomFile(fullPath);
FilesClient client = new FilesClient();
client.useSnet();
assertTrue(client.usingSnet());
assertTrue(client.login());
// Set up
client.createContainer(containerName);
// Store it
HashMap<String,String> meta = new HashMap<String,String>();
meta.put("Foo", "bar");
meta.put("Uni", "\u0169\u00f1\u00efcode-test");
meta.put("Width", "336");
meta.put("Height", "183");
assertNotNull(client.storeObjectAs(containerName, new File(fullPath), "application/octet-stream", filename, meta));
// Make sure it's there
List<FilesObject> objects = client.listObjects(containerName);
assertEquals(1, objects.size());
FilesObject obj = objects.get(0);
assertEquals(filename, obj.getName());
assertEquals("application/octet-stream", obj.getMimeType());
// Make sure the data is correct
assertArrayEquals(randomData, client.getObject(containerName, filename));
// Make sure the data is correct as a stream
InputStream is = client.getObjectAsStream(containerName, filename);
byte otherData[] = new byte[NUMBER_RANDOM_BYTES];
is.read(otherData);
assertArrayEquals(randomData, otherData);
assertEquals(-1, is.read()); // Could hang if there's a bug on the other end
// Make sure the metadata is correct
FilesObjectMetaData metadata = client.getObjectMetaData(containerName, filename);
assertNotNull(metadata);
Map<String,String> serverMetadata = metadata.getMetaData();
assertEquals(meta.size(), serverMetadata.size());
for(String key : meta.keySet()) {
assertTrue(serverMetadata.containsKey(key));
assertEquals(meta.get(key), serverMetadata.get(key));
}
// Clean up
client.deleteObject(containerName, filename);
assertTrue(client.deleteContainer(containerName));
}
catch (FilesException e) {
e.printStackTrace();
fail(e.getHttpStatusMessage() + ":" + e.getMessage());