Skip to content

Commit

Permalink
Add 'findobject' api option to HashStoreClient and new junit test
Browse files Browse the repository at this point in the history
  • Loading branch information
doulikecookiedough committed Jan 17, 2024
1 parent ce85fed commit 48a26eb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/main/java/org/dataone/hashstore/HashStoreClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ public static void main(String[] args) throws Exception {
String hexDigest = hashStore.getHexDigest(pid, algo);
System.out.println(hexDigest);

} else if (cmd.hasOption("findobject")) {
String pid = cmd.getOptionValue("pid");
FileHashStoreUtility.ensureNotNull(pid, "-pid", "HashStoreClient");

String cid = hashStore.findObject(pid);
System.out.println(cid);

} else if (cmd.hasOption("storeobject")) {
System.out.println("Storing object");
String pid = cmd.getOptionValue("pid");
Expand Down Expand Up @@ -274,6 +281,10 @@ private static Options addHashStoreClientOptions() {
"getchecksum", "client_getchecksum", false,
"Flag to get the hex digest of a data object in a HashStore."
);
options.addOption(
"findobject", "client_findobject", false,
"Flag to get the hex digest of a data object in a HashStore."
);
options.addOption(
"storeobject", "client_storeobject", false, "Flag to store objs to a HashStore."
);
Expand Down
40 changes: 39 additions & 1 deletion src/test/java/org/dataone/hashstore/HashStoreClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public void client_deleteMetadata() throws Exception {
}

/**
* Test hashStore client returns hex digest of object.
* Test hashStore client calculates the hex digest of object.
*/
@Test
public void client_getHexDigest() throws Exception {
Expand Down Expand Up @@ -427,4 +427,42 @@ public void client_getHexDigest() throws Exception {
assertEquals(testDataChecksum, pidStdOut.trim());
}
}

/**
* Test hashStore client returns the content identifier (cid) of an object
*/
@Test
public void client_findObject() throws Exception {
for (String pid : testData.pidList) {
// Redirect stdout to capture output
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(outputStream);
PrintStream old = System.out;
System.setOut(ps);

String pidFormatted = pid.replace("/", "_");
Path testDataFile = testData.getTestFile(pidFormatted);
InputStream dataStream = Files.newInputStream(testDataFile);
hashStore.storeObject(dataStream, pid, null, null, null, -1);

// Call client
String optFindObject = "-findobject";
String optStore = "-store";
String optStorePath = hsProperties.getProperty("storePath");
String optPid = "-pid";
String optPidValue = pid;
String[] args = {optFindObject, optStore, optStorePath, optPid, optPidValue};
HashStoreClient.main(args);

String contentIdentifier = testData.pidData.get(pid).get("sha256");

// Put things back
System.out.flush();
System.setOut(old);

// Confirm correct content identifier has been saved
String pidStdOut = outputStream.toString();
assertEquals(contentIdentifier, pidStdOut.trim());
}
}
}

0 comments on commit 48a26eb

Please sign in to comment.