From 87fd9e6b30ae5b36e98cae7ecd730362ebf53ea7 Mon Sep 17 00:00:00 2001 From: Dou Mok Date: Tue, 20 Aug 2024 16:09:38 -0700 Subject: [PATCH 1/3] Declare 'NonMatchingChecksumException' in HashStoreConverter's 'convert' method and add new junit test --- .../HashStoreConverter.java | 11 +++++--- .../FileHashStoreLinksTest.java | 26 ++++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/dataone/hashstore/hashstoreconverter/HashStoreConverter.java b/src/main/java/org/dataone/hashstore/hashstoreconverter/HashStoreConverter.java index 9df4f215..34ef6407 100644 --- a/src/main/java/org/dataone/hashstore/hashstoreconverter/HashStoreConverter.java +++ b/src/main/java/org/dataone/hashstore/hashstoreconverter/HashStoreConverter.java @@ -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; @@ -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"); diff --git a/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java b/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java index 7453fcb0..0aae09dd 100644 --- a/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java +++ b/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java @@ -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; @@ -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; /** @@ -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); @@ -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); } @@ -175,6 +176,23 @@ public void storeHardLink_alreadyExists() throws Exception { } } + /** + * Check that storeHardLink creates hard link and returns the correct ObjectMetadata cid + */ + @Test + public void storeHardLink_nonMatchingChecksum() throws Exception { + 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 */ From 6e4185194fbe88016f6e4597a4ca7675d5c46cea Mon Sep 17 00:00:00 2001 From: Dou Mok Date: Tue, 20 Aug 2024 16:13:43 -0700 Subject: [PATCH 2/3] Fix inaccurate javadoc for new junit test --- .../hashstore/hashstoreconverter/FileHashStoreLinksTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java b/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java index 0aae09dd..6472c6b7 100644 --- a/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java +++ b/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java @@ -177,7 +177,7 @@ public void storeHardLink_alreadyExists() throws Exception { } /** - * Check that storeHardLink creates hard link and returns the correct ObjectMetadata cid + * Check that storeHardLink throws nonMatchingChecksumException when values do not match */ @Test public void storeHardLink_nonMatchingChecksum() throws Exception { From 8c865a8a81eb70946fc9f9510241337613afcf8b Mon Sep 17 00:00:00 2001 From: Dou Mok Date: Tue, 20 Aug 2024 16:14:34 -0700 Subject: [PATCH 3/3] Cleanup 'FileHashStoreLinksTest' class --- .../hashstore/hashstoreconverter/FileHashStoreLinksTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java b/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java index 6472c6b7..94f9b1f4 100644 --- a/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java +++ b/src/test/java/org/dataone/hashstore/hashstoreconverter/FileHashStoreLinksTest.java @@ -180,7 +180,7 @@ public void storeHardLink_alreadyExists() throws Exception { * Check that storeHardLink throws nonMatchingChecksumException when values do not match */ @Test - public void storeHardLink_nonMatchingChecksum() throws Exception { + public void storeHardLink_nonMatchingChecksum() { for (String pid : testData.pidList) { String pidFormatted = pid.replace("/", "_"); Path testDataFile = testData.getTestFile(pidFormatted);