Skip to content

Commit

Permalink
Add missing exception catch blocks in 'storeMetadata()', add new juni…
Browse files Browse the repository at this point in the history
…t tests for 'move()' and clean up code (fix typos, update javadocs, etc.)
  • Loading branch information
doulikecookiedough committed Jun 27, 2023
1 parent b920585 commit b012841
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ public HashAddress storeObject(InputStream object, String pid, String additional

@Override
public String storeMetadata(InputStream metadata, String pid, String formatId)
throws IOException, IllegalArgumentException, NoSuchAlgorithmException, InterruptedException {
throws IOException, FileNotFoundException, IllegalArgumentException, InterruptedException,
NoSuchAlgorithmException {
logFileHashStore.debug("FileHashStore.storeMetadata - Called to store metadata for pid: " + pid
+ ", with formatId: " + formatId);

Expand Down Expand Up @@ -497,10 +498,18 @@ public String storeMetadata(InputStream metadata, String pid, String formatId)
+ ". Metadata Content Identifier (metadataCid): " + metadataCid);
return metadataCid;

} catch (Exception e) {
String errMsg = "Placeholder";
} catch (IOException ioe) {
// Covers FileNotFoundException
String errMsg = "FileHashStore.storeMetadata - Unable to store metadata, IOException encountered: "
+ ioe.getMessage();
logFileHashStore.error(errMsg);
throw ioe;

} catch (NoSuchAlgorithmException nsae) {
String errMsg = "FileHashStore.storeMetadata - Unable to store metadata, algorithm to calculate"
+ " permanent address is not supported: " + nsae.getMessage();
logFileHashStore.error(errMsg);
throw e;
throw nsae;

} finally {
// Release lock
Expand Down Expand Up @@ -1015,10 +1024,24 @@ protected Map<String, String> writeToTmpFileAndGenerateChecksums(File tmpFile, I
*/
protected boolean move(File source, File target, String entity)
throws IOException, SecurityException, AtomicMoveNotSupportedException, FileAlreadyExistsException {
if (entity.equals("object") && target.exists()) {
logFileHashStore.debug("FileHashStore.move - called to move entity type: " + entity + ", from source: "
+ source + ", to target: " + target);
// Validate input parameters
if (entity == null) {
String errMsg = "FileHashStore.move - entity cannot be null, must be 'object' for storeObject() or 'metadata' for storeMetadata()";
logFileHashStore.debug(errMsg);
throw new NullPointerException(errMsg);

} else if (entity.trim().isEmpty()) {
String errMsg = "FileHashStore.move - entity cannot be empty, must be 'object' for storeObject()";
logFileHashStore.debug(errMsg);
throw new IllegalArgumentException(errMsg);

} else if (entity.equals("object") && target.exists()) {
String errMsg = "FileHashStore.move - File already exists for target: " + target;
logFileHashStore.debug(errMsg);
throw new FileAlreadyExistsException(errMsg);

}

File destinationDirectory = new File(target.getParent());
Expand Down Expand Up @@ -1060,7 +1083,7 @@ protected boolean move(File source, File target, String entity)
* @param metadata Inputstream to metadata
* @param pid Authority-based identifier
* @param formatId Metadata formatId or namespace
* @return
* @return Metadata content identifier
* @throws NoSuchAlgorithmException When the algorithm used to calculate
* permanent address is not supported
* @throws IOException I/O error when writing to tmp file
Expand Down Expand Up @@ -1098,7 +1121,7 @@ protected String putMetadata(InputStream metadata, String pid, String formatId)
checkedFormatId = formatId;
}

// Get permanent address of the given metadata document
// Get permanent address for the given metadata document
String metadataCid = this.getPidHexDigest(pid + checkedFormatId, this.OBJECT_STORE_ALGORITHM);
String metadataCidShardString = this.getHierarchicalPathString(this.DIRECTORY_DEPTH, this.DIRECTORY_WIDTH,
metadataCid);
Expand All @@ -1108,8 +1131,11 @@ protected String putMetadata(InputStream metadata, String pid, String formatId)
File tmpMetadataFile = this.generateTmpFile("tmp", this.METADATA_TMP_FILE_DIRECTORY);
boolean tmpMetadataWritten = this.writeToTmpMetadataFile(tmpMetadataFile, metadata, checkedFormatId);
if (tmpMetadataWritten) {
File permMeadataFile = metadataCidAbsPath.toFile();
this.move(tmpMetadataFile, permMeadataFile, "metadata");
logFileHashStore.debug(
"FileHashStore.putObject - tmp metadata file has been written, moving to permanent location: "
+ metadataCidAbsPath);
File permMetadataFile = metadataCidAbsPath.toFile();
this.move(tmpMetadataFile, permMetadataFile, "metadata");
}
logFileHashStore
.info("FileHashStore.putObject - Move metadata success, permanent address: " + metadataCidAbsPath);
Expand All @@ -1124,7 +1150,7 @@ protected String putMetadata(InputStream metadata, String pid, String formatId)
* @param metadataStream Stream of metadata content
* @param formatId Namespace/format of metadata
*
* @return
* @return True if file is written successfully
* @throws IOException When an I/O error occurs
* @throws FileNotFoundException When given file to write into is not found
*/
Expand All @@ -1133,10 +1159,10 @@ protected boolean writeToTmpMetadataFile(File tmpFile, InputStream metadataStrea
FileOutputStream os = new FileOutputStream(tmpFile);

try {
// Write formatId and null character (header)
// Write formatId
byte[] metadataHeaderBytes = formatId.getBytes(StandardCharsets.UTF_8);
os.write(metadataHeaderBytes);
// Write null character
// Followed by null character
os.write('\u0000');

// Write metadata content (body)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,39 @@ public void testMove_targetExists() throws Exception {
this.fileHashStore.move(newTmpFileTwo, targetFile, "object");
}

/**
* Confirm that NullPointerException is thrown when entity is null
*/
@Test(expected = NullPointerException.class)
public void testMove_entityNull() throws Exception {
File newTmpFile = generateTemporaryFile();
String targetString = tempFolder.getRoot().toString() + "/testmove/test_tmp_object.tmp";
File targetFile = new File(targetString);
this.fileHashStore.move(newTmpFile, targetFile, null);
}

/**
* Confirm that FileAlreadyExistsException is thrown entity is empty
*/
@Test(expected = IllegalArgumentException.class)
public void testMove_entityEmpty() throws Exception {
File newTmpFile = generateTemporaryFile();
String targetString = tempFolder.getRoot().toString() + "/testmove/test_tmp_object.tmp";
File targetFile = new File(targetString);
this.fileHashStore.move(newTmpFile, targetFile, "");
}

/**
* Confirm that FileAlreadyExistsException is thrown when entity is empty spaces
*/
@Test(expected = IllegalArgumentException.class)
public void testMove_entityEmptySpaces() throws Exception {
File newTmpFile = generateTemporaryFile();
String targetString = tempFolder.getRoot().toString() + "/testmove/test_tmp_object.tmp";
File targetFile = new File(targetString);
this.fileHashStore.move(newTmpFile, targetFile, " ");
}

/**
* Test putMetadata stores metadata as expected
*/
Expand Down

0 comments on commit b012841

Please sign in to comment.