ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bos);
byte[] bytes;
CreateDataObjectOptions pCreateDataObjectOptions;
DataObject dataObject;
Map<String, String> dataObjectMetaDataOut;
Map<String, String> pContainerMetaDataIn = Maps.newHashMap();
Map<String, String> pDataObjectMetaDataIn = Maps.newLinkedHashMap();
pDataObjectMetaDataIn.put("dataObjectkey1", "value1");
pDataObjectMetaDataIn.put("dataObjectkey2", "value2");
pDataObjectMetaDataIn.put("dataObjectkey3", "value3");
CreateContainerOptions pCreateContainerOptions = CreateContainerOptions.Builder.metadata(pContainerMetaDataIn);
ContainerApi containerApi = cdmiContext.getApi().getApi();
DataApi dataApi = cdmiContext.getApi().getDataApiForContainer(containerName);
Logger.getAnonymousLogger().info("create: " + containerName);
Container container = containerApi.create(containerName, pCreateContainerOptions);
try {
assertNotNull(container);
System.out.println(container);
container = containerApi.get(containerName);
assertNotNull(container);
assertNotNull(container.getChildren());
assertEquals(container.getChildren().isEmpty(), true);
// exercise create data object with value mimetype and metadata
value = "Hello CDMI data object with value mimetype and metadata";
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(value).mimetype("text/plain")
.metadata(pDataObjectMetaDataIn);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getMimetype(), "text/plain");
assertEquals(dataObject.getValueAsString(), value);
dataObjectMetaDataOut = dataObject.getUserMetadata();
assertNotNull(dataObjectMetaDataOut);
for (Map.Entry<String, String> entry : pDataObjectMetaDataIn.entrySet()) {
String key = entry.getKey();
assertEquals(dataObjectMetaDataOut.containsKey(key), true);
assertEquals(dataObjectMetaDataOut.get(key), entry.getValue());
}
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// verify that options order does not matter
value = "Hello CDMI World3";
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.metadata(pDataObjectMetaDataIn)
.mimetype("text/plain").value(value);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getMimetype(), "text/plain");
assertEquals(dataObject.getValueAsString(), value);
dataObjectMetaDataOut = dataObject.getUserMetadata();
assertNotNull(dataObjectMetaDataOut);
for (Map.Entry<String, String> entry : pDataObjectMetaDataIn.entrySet()) {
String key = entry.getKey();
assertEquals(dataObjectMetaDataOut.containsKey(key), true);
assertEquals(dataObjectMetaDataOut.get(key), entry.getValue());
}
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with empty metadata
value = "Hello CDMI World4";
pDataObjectMetaDataIn.clear();
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(value).mimetype("text/plain")
.metadata(pDataObjectMetaDataIn);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getMimetype(), "text/plain");
assertEquals(dataObject.getValueAsString(), value);
dataObjectMetaDataOut = dataObject.getUserMetadata();
assertNotNull(dataObjectMetaDataOut);
assertEquals(dataObjectMetaDataOut.isEmpty(), true);
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with null metadata
value = "Hello CDMI World5";
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(value).mimetype("text/plain");
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getMimetype(), "text/plain");
assertEquals(dataObject.getValueAsString(), value);
dataObjectMetaDataOut = dataObject.getUserMetadata();
assertNotNull(dataObjectMetaDataOut);
assertEquals(true, dataObjectMetaDataOut.isEmpty());
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with only value
value = "Hello CDMI World6";
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(value);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), value);
dataObjectMetaDataOut = dataObject.getUserMetadata();
assertNotNull(dataObjectMetaDataOut);
assertEquals(dataObjectMetaDataOut.isEmpty(), true);
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with empty mimetype only
value = "";
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.mimetype(new String());
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
// assertEquals(dataObject.getMimetype(), "");
assertEquals(dataObject.getValueAsString(), "");
dataObjectMetaDataOut = dataObject.getUserMetadata();
assertNotNull(dataObjectMetaDataOut);
// assertEquals(dataObjectMetaDataOut.isEmpty(),true);
dataApi.delete(dataObjectNameIn);
// exercise create data object with no value
value = "";
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value();
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), "");
dataObjectMetaDataOut = dataObject.getUserMetadata();
assertNotNull(dataObjectMetaDataOut);
assertEquals(dataObjectMetaDataOut.isEmpty(), true);
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with byte array
value = "Hello CDMI World 7";
out.writeUTF(value);
out.close();
bytes = bos.toByteArray();
// String.getBytes causes an exception CreateDataObjectOptions need to investigate byte
// arrays
// bytes = value.getBytes(Charsets.UTF_8);
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(bytes);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), value);
assertEquals(new String(dataObject.getValueAsByteArray()), value);
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with an existing file
inFile = new File(System.getProperty("user.dir") + "/src/test/resources/container.json");
assertEquals(true, inFile.isFile());
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(inFile);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
tmpFileOut = dataObject.getValueAsFile(Files.createTempDir());
assertEquals(true, Files.equal(tmpFileOut, inFile));
tmpFileOut.delete();
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")),
Files.toString(inFile, Charsets.UTF_8).length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with a temporary file that we create
// on the fly
// with default Charset
value = "Hello CDMI World 10";
Files.write(value, tmpFileIn, Charsets.UTF_8);
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(tmpFileIn);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), value);
tmpFileOut = dataObject.getValueAsFile(Files.createTempDir());
assertEquals(true, Files.equal(tmpFileOut, tmpFileIn));
tmpFileOut.delete();
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with a temporary file that we create
// on the fly
// specify charset UTF_8
Files.write(value, tmpFileIn, Charsets.UTF_8);
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(tmpFileIn, Charsets.UTF_8);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), value);
tmpFileOut = dataObject.getValueAsFile(Files.createTempDir());
assertEquals(true, Files.equal(tmpFileOut, tmpFileIn));
tmpFileOut.delete();
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with a temporary file that we create
// on the fly
// specify charset US_ASCII
Files.write(value, tmpFileIn, Charsets.US_ASCII);
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(tmpFileIn, Charsets.US_ASCII);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), value);
tmpFileOut = dataObject.getValueAsFile(Files.createTempDir());
assertEquals(true, Files.equal(tmpFileOut, tmpFileIn));
tmpFileOut.delete();
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with a temporary file with multiple
// lines
// with default Charset
Files.write("line1", tmpFileIn, Charsets.UTF_8);
Files.append("\nline2", tmpFileIn, Charsets.UTF_8);
Files.append("\nline3", tmpFileIn, Charsets.UTF_8);
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(tmpFileIn);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), "line1\nline2\nline3");
tmpFileOut = dataObject.getValueAsFile(Files.createTempDir());
assertEquals(true, Files.equal(tmpFileOut, tmpFileIn));
tmpFileOut.delete();
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")),
Files.toString(tmpFileIn, Charsets.UTF_8).length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with a temporary file with multiple
// lines
// with Charset UTF_8
Files.write("line1", tmpFileIn, Charsets.UTF_8);
Files.append("\nline2", tmpFileIn, Charsets.UTF_8);
Files.append("\nline3", tmpFileIn, Charsets.UTF_8);
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(new FileInputStream(tmpFileIn));
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), "line1\nline2\nline3");
tmpFileOut = dataObject.getValueAsFile(Files.createTempDir());
assertEquals(true, Files.equal(tmpFileOut, tmpFileIn));
tmpFileOut.delete();
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")),
Files.toString(tmpFileIn, Charsets.UTF_8).length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with a temporary file with multiple
// lines
// with Charset ISO_8859_1
Files.write("line1", tmpFileIn, Charsets.ISO_8859_1);
Files.append("\nline2", tmpFileIn, Charsets.ISO_8859_1);
Files.append("\nline3", tmpFileIn, Charsets.ISO_8859_1);
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(new FileInputStream(tmpFileIn),
Charsets.ISO_8859_1);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), "line1\nline2\nline3");
tmpFileOut = dataObject.getValueAsFile(Files.createTempDir());
assertEquals(true, Files.equal(tmpFileOut, tmpFileIn));
tmpFileOut.delete();
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")),
Files.toString(tmpFileIn, Charsets.ISO_8859_1).length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
// exercise create data object with an inputstream
is = new ByteArrayInputStream(value.getBytes());
pCreateDataObjectOptions = CreateDataObjectOptions.Builder.value(is);
dataObject = dataApi.create(dataObjectNameIn, pCreateDataObjectOptions);
assertNotNull(dataObject);
dataObject = dataApi.get(dataObjectNameIn);
assertNotNull(dataObject);
System.out.println(dataObject);
System.out.println("value: " + dataObject.getValueAsString());
assertEquals(dataObject.getValueAsString(), value);
assertNotNull(dataObject.getValueAsInputSupplier());
assertEquals(CharStreams.toString(CharStreams.newReaderSupplier(
dataObject.getValueAsInputSupplier(Charsets.UTF_8), Charsets.UTF_8)), value);
assertEquals(Integer.parseInt(dataObject.getSystemMetadata().get("cdmi_size")), value.length());
assertEquals(dataObject.getObjectName(), dataObjectNameIn);
assertEquals(dataObject.getObjectType(), "application/cdmi-object");
assertEquals(dataObject.getParentURI(), "/" + containerName);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), true);
dataApi.delete(dataObjectNameIn);
assertEquals(containerApi.get(containerName).getChildren().contains(dataObjectNameIn), false);
} finally {
tmpFileIn.delete();