Skip to content

Commit

Permalink
Merge pull request #102 from DataONEorg/feature-101-nmce-declare
Browse files Browse the repository at this point in the history
Feature-101: 'NonMatchingChecksumException' Declaration
  • Loading branch information
doulikecookiedough authored Aug 20, 2024
2 parents e54e48f + 8c865a8 commit d2044ef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dataone.hashstore.ObjectMetadata;
import org.dataone.hashstore.exceptions.NonMatchingChecksumException;
import org.dataone.hashstore.filehashstore.FileHashStoreUtility;

import java.io.IOException;
Expand Down Expand Up @@ -61,14 +62,16 @@ public HashStoreConverter(Properties hashstoreProperties)
* @param checksum Value of checksum
* @param checksumAlgorithm Ex. "SHA-256"
* @return ObjectMetadata for the given pid
* @throws IOException An issue with calculating checksums or storing sysmeta
* @throws NoSuchAlgorithmException An algorithm defined is not supported
* @throws InterruptedException Issue with synchronizing storing metadata
* @throws IOException An issue with calculating checksums or storing sysmeta
* @throws NoSuchAlgorithmException An algorithm defined is not supported
* @throws InterruptedException Issue with synchronizing storing metadata
* @throws NonMatchingChecksumException When the checksums calculated/given do not match
*/
public ObjectMetadata convert(
Path filePath, String pid, InputStream sysmetaStream, String checksum,
String checksumAlgorithm)
throws IOException, NoSuchAlgorithmException, InterruptedException {
throws IOException, NoSuchAlgorithmException, InterruptedException,
NonMatchingChecksumException {
logHashStoreConverter.info("Begin converting data object and sysmeta for pid: " + pid);
FileHashStoreUtility.ensureNotNull(sysmetaStream, "sysmetaStream");
FileHashStoreUtility.ensureNotNull(pid, "pid");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.dataone.hashstore.hashstoreconverter;

import org.dataone.hashstore.ObjectMetadata;
import org.dataone.hashstore.exceptions.NonMatchingChecksumException;
import org.dataone.hashstore.filehashstore.FileHashStore;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -16,8 +17,10 @@
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import org.dataone.hashstore.testdata.TestDataHarness;

/**
Expand Down Expand Up @@ -56,8 +59,7 @@ public void initializeFileHashStoreLinks() {
storeProperties.setProperty("storeWidth", "2");
storeProperties.setProperty("storeAlgorithm", "SHA-256");
storeProperties.setProperty(
"storeMetadataNamespace", "https://ns.dataone.org/service/types/v2.0#SystemMetadata"
);
"storeMetadataNamespace", "https://ns.dataone.org/service/types/v2.0#SystemMetadata");

try {
fileHashStoreLinks = new FileHashStoreLinks(storeProperties);
Expand Down Expand Up @@ -123,8 +125,7 @@ public void testExistingHashStoreConfiguration_sameConfig() throws Exception {
storeProperties.setProperty("storeWidth", "2");
storeProperties.setProperty("storeAlgorithm", "SHA-256");
storeProperties.setProperty(
"storeMetadataNamespace", "https://ns.dataone.org/service/types/v2.0#SystemMetadata"
);
"storeMetadataNamespace", "https://ns.dataone.org/service/types/v2.0#SystemMetadata");

new FileHashStore(storeProperties);
}
Expand Down Expand Up @@ -175,6 +176,23 @@ public void storeHardLink_alreadyExists() throws Exception {
}
}

/**
* Check that storeHardLink throws nonMatchingChecksumException when values do not match
*/
@Test
public void storeHardLink_nonMatchingChecksum() {
for (String pid : testData.pidList) {
String pidFormatted = pid.replace("/", "_");
Path testDataFile = testData.getTestFile(pidFormatted);
assertTrue(Files.exists(testDataFile));

assertThrows(NonMatchingChecksumException.class,
() -> fileHashStoreLinks.storeHardLink(testDataFile, pid, "badchecksum",
"SHA-256"));

}
}

/**
* Confirm that generateChecksums calculates checksums as expected
*/
Expand Down

0 comments on commit d2044ef

Please sign in to comment.