From eefcea508d84ad48446398cce3835412a949c77d Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Thu, 19 Sep 2024 10:55:25 -0600 Subject: [PATCH 1/3] Add parametrized enclosed class to s3 tests --- .../thredds/inventory/s3/TestMFileS3.java | 708 +++++++++--------- 1 file changed, 362 insertions(+), 346 deletions(-) diff --git a/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java b/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java index 6b1f75502d..e67ceed2b4 100644 --- a/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java +++ b/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java @@ -12,11 +12,17 @@ import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.List; + import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.services.s3.model.NoSuchKeyException; @@ -27,6 +33,7 @@ import ucar.unidata.io.s3.S3TestsCommon; import ucar.unidata.util.test.category.NotPullRequest; +@RunWith(Enclosed.class) public class TestMFileS3 { private static final Logger logger = LoggerFactory.getLogger(TestMFileS3.class); @@ -72,435 +79,444 @@ public static void setup() { System.setProperty(S3TestsCommon.AWS_REGION_PROP_NAME, S3TestsCommon.AWS_G16_REGION); } - ///////////////////////////////////////// - // CdmS3Uri for just the bucket (no key) - // - @Test - public void justBucketAws() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - String fullUri = S3TestsCommon.TOP_LEVEL_AWS_BUCKET + delimiter; - checkWithBucket(fullUri); - } + @AfterClass + public static void teardown() { + System.clearProperty(S3TestsCommon.AWS_REGION_PROP_NAME); } - @Test - public void justBucketGcs() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - String fullUri = S3TestsCommon.TOP_LEVEL_GCS_BUCKET + delimiter; - checkWithBucket(fullUri); + public static class TestMFileS3NonParameterized { + + ///////////////////////////////////////// + // CdmS3Uri for just the bucket (no key) + // + @Test + public void justBucketAws() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + String fullUri = S3TestsCommon.TOP_LEVEL_AWS_BUCKET + delimiter; + checkWithBucket(fullUri); + } } - } - @Test - public void justBucketOsdc() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - String fullUri = S3TestsCommon.TOP_LEVEL_OSDC_BUCKET + delimiter; - checkWithBucket(fullUri); + @Test + public void justBucketGcs() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + String fullUri = S3TestsCommon.TOP_LEVEL_GCS_BUCKET + delimiter; + checkWithBucket(fullUri); + } } - } - ////////////////////////////////////////// - // CdmS3Uri bucket and key (valid object) - // - @Test - public void bucketAndKeyAws() throws IOException { - checkWithBucketAndKey(AWS_G16_S3_OBJECT_1, G16_OBJECT_KEY_1, null); - checkWithBucketAndKey(AWS_G16_S3_OBJECT_1 + DELIMITER_FRAGMENT, G16_NAME_1, "/"); - } + @Test + public void justBucketOsdc() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + String fullUri = S3TestsCommon.TOP_LEVEL_OSDC_BUCKET + delimiter; + checkWithBucket(fullUri); + } + } - @Test - public void bucketAndKeyGcs() throws IOException { - checkWithBucketAndKey(GCS_G16_S3_OBJECT_1, G16_OBJECT_KEY_1, null); - checkWithBucketAndKey(GCS_G16_S3_OBJECT_1 + DELIMITER_FRAGMENT, G16_NAME_1, "/"); - } + ////////////////////////////////////////// + // CdmS3Uri bucket and key (valid object) + // + @Test + public void bucketAndKeyAws() throws IOException { + checkWithBucketAndKey(AWS_G16_S3_OBJECT_1, G16_OBJECT_KEY_1, null); + checkWithBucketAndKey(AWS_G16_S3_OBJECT_1 + DELIMITER_FRAGMENT, G16_NAME_1, "/"); + } - @Ignore("Failing due to expired certificate on OSDC") - @Test - @Category(NotPullRequest.class) - public void bucketAndKeyOsdc() throws IOException { - checkWithBucketAndKey(OSDC_G16_S3_OBJECT_1, OSDC_G16_OBJECT_KEY_1, null); - checkWithBucketAndKey(OSDC_G16_S3_OBJECT_1 + DELIMITER_FRAGMENT, G16_NAME_1, "/"); - } + @Test + public void bucketAndKeyGcs() throws IOException { + checkWithBucketAndKey(GCS_G16_S3_OBJECT_1, G16_OBJECT_KEY_1, null); + checkWithBucketAndKey(GCS_G16_S3_OBJECT_1 + DELIMITER_FRAGMENT, G16_NAME_1, "/"); + } - @Test - public void dirCheckAws() throws IOException { - dirCheckNoDelim(AWS_G16_S3_URI_DIR, G16_DIR); - dirCheckDelim(AWS_G16_S3_URI_DIR + DELIMITER_FRAGMENT); - dirCheckDelim(AWS_G16_S3_URI_DIR + "/" + DELIMITER_FRAGMENT); - } + @Ignore("Failing due to expired certificate on OSDC") + @Test + @Category(NotPullRequest.class) + public void bucketAndKeyOsdc() throws IOException { + checkWithBucketAndKey(OSDC_G16_S3_OBJECT_1, OSDC_G16_OBJECT_KEY_1, null); + checkWithBucketAndKey(OSDC_G16_S3_OBJECT_1 + DELIMITER_FRAGMENT, G16_NAME_1, "/"); + } - @Test - public void dirCheckGcs() throws IOException { - dirCheckNoDelim(GCS_G16_S3_URI_DIR, G16_DIR); - dirCheckDelim(GCS_G16_S3_URI_DIR + DELIMITER_FRAGMENT); - } + @Test + public void dirCheckAws() throws IOException { + dirCheckNoDelim(AWS_G16_S3_URI_DIR, G16_DIR); + dirCheckDelim(AWS_G16_S3_URI_DIR + DELIMITER_FRAGMENT); + dirCheckDelim(AWS_G16_S3_URI_DIR + "/" + DELIMITER_FRAGMENT); + } - @Test - public void dirCheckOsdc() throws IOException { - dirCheckNoDelim(OSDC_G16_S3_URI_DIR, OSDC_G16_DIR); - dirCheckDelim(OSDC_G16_S3_URI_DIR + DELIMITER_FRAGMENT); - } + @Test + public void dirCheckGcs() throws IOException { + dirCheckNoDelim(GCS_G16_S3_URI_DIR, G16_DIR); + dirCheckDelim(GCS_G16_S3_URI_DIR + DELIMITER_FRAGMENT); + } - @Test - public void shouldReturnTopLevelKeyName() throws IOException { - final MFileS3 fileWithoutDelimiter = new MFileS3(AWS_G16_S3_URI_TOP_DIR); - assertThat(fileWithoutDelimiter.getName()).isEqualTo(topLevelDir); + @Test + public void dirCheckOsdc() throws IOException { + dirCheckNoDelim(OSDC_G16_S3_URI_DIR, OSDC_G16_DIR); + dirCheckDelim(OSDC_G16_S3_URI_DIR + DELIMITER_FRAGMENT); + } - final MFileS3 fileWithDelimiter = new MFileS3(AWS_G16_S3_URI_TOP_DIR + DELIMITER_FRAGMENT); - assertThat(fileWithDelimiter.getName()).isEqualTo(topLevelDir); - } + @Test + public void shouldReturnTopLevelKeyName() throws IOException { + final MFileS3 fileWithoutDelimiter = new MFileS3(AWS_G16_S3_URI_TOP_DIR); + assertThat(fileWithoutDelimiter.getName()).isEqualTo(topLevelDir); - @Test - public void shouldCompareSameMFile() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); - assertThat(mFile.equals(mFile)).isTrue(); - assertThat(mFile.compareTo(mFile)).isEqualTo(0); - } + final MFileS3 fileWithDelimiter = new MFileS3(AWS_G16_S3_URI_TOP_DIR + DELIMITER_FRAGMENT); + assertThat(fileWithDelimiter.getName()).isEqualTo(topLevelDir); + } - @Test - public void shouldCompareToDifferentClass() throws IOException { - final MFile mFile1 = new MFileS3(AWS_G16_S3_OBJECT_1); - final MFile mFile2 = new MFileOS("test"); - assertThat(mFile1.equals(mFile2)).isFalse(); - } + @Test + public void shouldCompareSameMFile() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); + assertThat(mFile.equals(mFile)).isTrue(); + assertThat(mFile.compareTo(mFile)).isEqualTo(0); + } - @Test - public void compareMFilesAws() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - compareS3Mfiles(AWS_G16_S3_OBJECT_1 + delimiter, AWS_G16_S3_OBJECT_2 + delimiter); + @Test + public void shouldCompareToDifferentClass() throws IOException { + final MFile mFile1 = new MFileS3(AWS_G16_S3_OBJECT_1); + final MFile mFile2 = new MFileOS("test"); + assertThat(mFile1.equals(mFile2)).isFalse(); } - } - @Test - public void compareMFilesGcs() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - compareS3Mfiles(GCS_G16_S3_OBJECT_1 + delimiter, GCS_G16_S3_OBJECT_2 + delimiter); + @Test + public void compareMFilesAws() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + compareS3Mfiles(AWS_G16_S3_OBJECT_1 + delimiter, AWS_G16_S3_OBJECT_2 + delimiter); + } } - } - @Test - public void compareMFilesOsdc() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - compareS3Mfiles(OSDC_G16_S3_OBJECT_1 + delimiter, OSDC_G16_S3_OBJECT_2 + delimiter); + @Test + public void compareMFilesGcs() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + compareS3Mfiles(GCS_G16_S3_OBJECT_1 + delimiter, GCS_G16_S3_OBJECT_2 + delimiter); + } } - } - @Test - public void s3MFilesAuxInfoAws() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - checkS3MFilesAuxInfo(AWS_G16_S3_OBJECT_1 + delimiter); + @Test + public void compareMFilesOsdc() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + compareS3Mfiles(OSDC_G16_S3_OBJECT_1 + delimiter, OSDC_G16_S3_OBJECT_2 + delimiter); + } } - } - @Test - public void s3MFilesAuxInfoGsc() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - checkS3MFilesAuxInfo(GCS_G16_S3_OBJECT_1 + delimiter); + @Test + public void s3MFilesAuxInfoAws() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + checkS3MFilesAuxInfo(AWS_G16_S3_OBJECT_1 + delimiter); + } } - } - @Test - public void s3MFilesAuxInfoOsdc() throws IOException { - for (String delimiter : DELIMITER_FRAGMENTS) { - checkS3MFilesAuxInfo(OSDC_G16_S3_OBJECT_1 + delimiter); + @Test + public void s3MFilesAuxInfoGsc() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + checkS3MFilesAuxInfo(GCS_G16_S3_OBJECT_1 + delimiter); + } + } + + @Test + public void s3MFilesAuxInfoOsdc() throws IOException { + for (String delimiter : DELIMITER_FRAGMENTS) { + checkS3MFilesAuxInfo(OSDC_G16_S3_OBJECT_1 + delimiter); + } } - } - @Ignore("Failing due to expired certificate on OSDC") - @Test - public void shouldWriteObjectsToStream() throws IOException { - final String[] objects = {AWS_G16_S3_OBJECT_1, GCS_G16_S3_OBJECT_1, OSDC_G16_S3_OBJECT_1}; + @Ignore("Failing due to expired certificate on OSDC") + @Test + public void shouldWriteObjectsToStream() throws IOException { + final String[] objects = {AWS_G16_S3_OBJECT_1, GCS_G16_S3_OBJECT_1, OSDC_G16_S3_OBJECT_1}; - for (String object : objects) { - final MFile mFile = new MFileS3(object); - final long length = mFile.getLength(); + for (String object : objects) { + final MFile mFile = new MFileS3(object); + final long length = mFile.getLength(); - final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - mFile.writeToStream(outputStream); - assertThat(outputStream.size()).isEqualTo(length); + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + mFile.writeToStream(outputStream); + assertThat(outputStream.size()).isEqualTo(length); + } } - } - @Test - public void shouldWritePartialObjectToStream() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); - final long length = mFile.getLength(); + @Test + public void shouldWritePartialObjectToStream() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); + final long length = mFile.getLength(); - final long[][] testCases = {{0, 0}, {10, 10}, {0, length}, {0, 100}, {42, 100}}; + final long[][] testCases = {{0, 0}, {10, 10}, {0, length}, {0, 100}, {42, 100}}; - for (long[] testCase : testCases) { - final long offset = testCase[0]; - final long maxBytes = testCase[1]; + for (long[] testCase : testCases) { + final long offset = testCase[0]; + final long maxBytes = testCase[1]; - final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - mFile.writeToStream(outputStream, offset, maxBytes); + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + mFile.writeToStream(outputStream, offset, maxBytes); - final long bytesWritten = Math.min(maxBytes, length - offset); - assertThat(outputStream.size()).isEqualTo(bytesWritten); + final long bytesWritten = Math.min(maxBytes, length - offset); + assertThat(outputStream.size()).isEqualTo(bytesWritten); + } } - } - @Test - public void shouldNotWriteDirectoryToStream() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/" + DELIMITER_FRAGMENT); - assertThat(mFile.isDirectory()).isTrue(); + @Test + public void shouldNotWriteDirectoryToStream() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/" + DELIMITER_FRAGMENT); + assertThat(mFile.isDirectory()).isTrue(); - final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - assertThrows(NoSuchKeyException.class, () -> mFile.writeToStream(outputStream)); - } + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + assertThrows(NoSuchKeyException.class, () -> mFile.writeToStream(outputStream)); + } - @Test - public void shouldNotWriteNonExistingObjectToStream() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/NotARealKey"); + @Test + public void shouldNotWriteNonExistingObjectToStream() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/NotARealKey"); - final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - assertThrows(NoSuchKeyException.class, () -> mFile.writeToStream(outputStream)); - } + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + assertThrows(NoSuchKeyException.class, () -> mFile.writeToStream(outputStream)); + } - @Test - public void shouldReturnTrueForExistingFile() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); - assertThat(mFile.exists()).isEqualTo(true); - } + @Test + public void shouldReturnTrueForExistingFile() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); + assertThat(mFile.exists()).isEqualTo(true); + } - @Test - public void shouldReturnFalseForNonExistingFile() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/NotARealKey"); - assertThat(mFile.exists()).isEqualTo(false); - } + @Test + public void shouldReturnFalseForNonExistingFile() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/NotARealKey"); + assertThat(mFile.exists()).isEqualTo(false); + } - @Test - public void shouldCheckExistsForExistingDirectory() throws IOException { - final MFile withFragmentWithSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/" + DELIMITER_FRAGMENT); - assertThat(withFragmentWithSlash.exists()).isEqualTo(true); + @Test + public void shouldCheckExistsForExistingDirectory() throws IOException { + final MFile withFragmentWithSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/" + DELIMITER_FRAGMENT); + assertThat(withFragmentWithSlash.exists()).isEqualTo(true); - final MFile withoutFragmentWithSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/"); - assertThat(withoutFragmentWithSlash.exists()).isEqualTo(false); + final MFile withoutFragmentWithSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/"); + assertThat(withoutFragmentWithSlash.exists()).isEqualTo(false); - final MFile withFragmentWithoutSlash = new MFileS3(AWS_G16_S3_URI_DIR + DELIMITER_FRAGMENT); - assertThat(withFragmentWithoutSlash.exists()).isEqualTo(false); + final MFile withFragmentWithoutSlash = new MFileS3(AWS_G16_S3_URI_DIR + DELIMITER_FRAGMENT); + assertThat(withFragmentWithoutSlash.exists()).isEqualTo(false); - final MFile withoutFragmentWithoutSlash = new MFileS3(AWS_G16_S3_URI_DIR); - assertThat(withoutFragmentWithoutSlash.exists()).isEqualTo(false); - } + final MFile withoutFragmentWithoutSlash = new MFileS3(AWS_G16_S3_URI_DIR); + assertThat(withoutFragmentWithoutSlash.exists()).isEqualTo(false); + } - @Test - public void shouldReturnFalseForNonExistingDirectory() throws IOException { - final MFile withFragmentWithSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/notADirectory/" + DELIMITER_FRAGMENT); - assertThat(withFragmentWithSlash.exists()).isEqualTo(false); + @Test + public void shouldReturnFalseForNonExistingDirectory() throws IOException { + final MFile withFragmentWithSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/notADirectory/" + DELIMITER_FRAGMENT); + assertThat(withFragmentWithSlash.exists()).isEqualTo(false); - final MFile withoutFragmentWithSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/notADirectory/"); - assertThat(withoutFragmentWithSlash.exists()).isEqualTo(false); + final MFile withoutFragmentWithSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/notADirectory/"); + assertThat(withoutFragmentWithSlash.exists()).isEqualTo(false); - final MFile withFragmentWithoutSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/notADirectory" + DELIMITER_FRAGMENT); - assertThat(withFragmentWithoutSlash.exists()).isEqualTo(false); + final MFile withFragmentWithoutSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/notADirectory" + DELIMITER_FRAGMENT); + assertThat(withFragmentWithoutSlash.exists()).isEqualTo(false); - final MFile withoutFragmentWithoutSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/notADirectory"); - assertThat(withoutFragmentWithoutSlash.exists()).isEqualTo(false); - } + final MFile withoutFragmentWithoutSlash = new MFileS3(AWS_G16_S3_URI_DIR + "/notADirectory"); + assertThat(withoutFragmentWithoutSlash.exists()).isEqualTo(false); + } - @Test - public void shouldReturnFalseForKeyPrefixMatch() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1.substring(0, AWS_G16_S3_OBJECT_1.length() - 5)); - assertThat(mFile.exists()).isEqualTo(false); - } + @Test + public void shouldReturnFalseForKeyPrefixMatch() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1.substring(0, AWS_G16_S3_OBJECT_1.length() - 5)); + assertThat(mFile.exists()).isEqualTo(false); + } - @Test - public void shouldReturnTrueForBucket() throws IOException { - final MFile bucketWithDelimiter = new MFileS3(S3TestsCommon.TOP_LEVEL_AWS_BUCKET + DELIMITER_FRAGMENT); - assertThat(bucketWithDelimiter.exists()).isEqualTo(true); + @Test + public void shouldReturnTrueForBucket() throws IOException { + final MFile bucketWithDelimiter = new MFileS3(S3TestsCommon.TOP_LEVEL_AWS_BUCKET + DELIMITER_FRAGMENT); + assertThat(bucketWithDelimiter.exists()).isEqualTo(true); - final MFile bucketWithoutDelimiter = new MFileS3(S3TestsCommon.TOP_LEVEL_AWS_BUCKET); - assertThat(bucketWithoutDelimiter.exists()).isEqualTo(true); - } + final MFile bucketWithoutDelimiter = new MFileS3(S3TestsCommon.TOP_LEVEL_AWS_BUCKET); + assertThat(bucketWithoutDelimiter.exists()).isEqualTo(true); + } - @Test - public void shouldReturnFalseForNonExistentBucket() throws IOException { - final MFile bucketWithDelimiter = new MFileS3("cdms3:notABucket" + DELIMITER_FRAGMENT); - assertThat(bucketWithDelimiter.exists()).isEqualTo(false); + @Test + public void shouldReturnFalseForNonExistentBucket() throws IOException { + final MFile bucketWithDelimiter = new MFileS3("cdms3:notABucket" + DELIMITER_FRAGMENT); + assertThat(bucketWithDelimiter.exists()).isEqualTo(false); - final MFile bucketWithoutDelimiter = new MFileS3("cdms3:notABucket"); - assertThat(bucketWithoutDelimiter.exists()).isEqualTo(false); - } + final MFile bucketWithoutDelimiter = new MFileS3("cdms3:notABucket"); + assertThat(bucketWithoutDelimiter.exists()).isEqualTo(false); + } - @Test - public void shouldGetChildMFileFromBucket() throws IOException { - final MFileS3 withDelimiter = new MFileS3("cdms3:bucket" + DELIMITER_FRAGMENT); - final MFileS3 newMFileWithDelimiter = withDelimiter.getChild("newKey"); - assertThat(newMFileWithDelimiter).isNotNull(); - assertThat(newMFileWithDelimiter.getPath()).isEqualTo("cdms3:bucket?newKey" + DELIMITER_FRAGMENT); - - final MFileS3 withoutDelimiter = new MFileS3("cdms3:bucket"); - final MFileS3 newMFileWithoutDelimiter = withoutDelimiter.getChild("newKey"); - assertThat(newMFileWithoutDelimiter).isNotNull(); - assertThat(newMFileWithoutDelimiter.getPath()).isEqualTo("cdms3:bucket?newKey"); - } + @Test + public void shouldGetChildMFileFromBucket() throws IOException { + final MFileS3 withDelimiter = new MFileS3("cdms3:bucket" + DELIMITER_FRAGMENT); + final MFileS3 newMFileWithDelimiter = withDelimiter.getChild("newKey"); + assertThat(newMFileWithDelimiter).isNotNull(); + assertThat(newMFileWithDelimiter.getPath()).isEqualTo("cdms3:bucket?newKey" + DELIMITER_FRAGMENT); + + final MFileS3 withoutDelimiter = new MFileS3("cdms3:bucket"); + final MFileS3 newMFileWithoutDelimiter = withoutDelimiter.getChild("newKey"); + assertThat(newMFileWithoutDelimiter).isNotNull(); + assertThat(newMFileWithoutDelimiter.getPath()).isEqualTo("cdms3:bucket?newKey"); + } - @Test - public void shouldGetChildMFileFromBucketAndKey() throws IOException { - final MFileS3 withDelimiterWithSlash = new MFileS3("cdms3:bucket?key/" + DELIMITER_FRAGMENT); - final MFileS3 newMFileWithDelimiterWithSlash = withDelimiterWithSlash.getChild("newKey"); - assertThat(newMFileWithDelimiterWithSlash).isNotNull(); - assertThat(newMFileWithDelimiterWithSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey" + DELIMITER_FRAGMENT); - - final MFileS3 withoutDelimiterWithSlash = new MFileS3("cdms3:bucket?key/"); - final MFileS3 newMFileWithoutDelimiterWithSlash = withoutDelimiterWithSlash.getChild("newKey"); - assertThat(newMFileWithoutDelimiterWithSlash).isNotNull(); - assertThat(newMFileWithoutDelimiterWithSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey"); - - final MFileS3 withDelimiterWithoutSlash = new MFileS3("cdms3:bucket?key" + DELIMITER_FRAGMENT); - final MFileS3 newMFileWithDelimiterWithoutSlash = withDelimiterWithoutSlash.getChild("newKey"); - assertThat(newMFileWithDelimiterWithoutSlash).isNotNull(); - assertThat(newMFileWithDelimiterWithoutSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey" + DELIMITER_FRAGMENT); - - final MFileS3 withoutDelimiterWithoutSlash = new MFileS3("cdms3:bucket?key"); - final MFileS3 newMFileWithoutDelimiterWithoutSlash = withoutDelimiterWithoutSlash.getChild("newKey"); - assertThat(newMFileWithoutDelimiterWithoutSlash).isNotNull(); - assertThat(newMFileWithoutDelimiterWithoutSlash.getPath()).isEqualTo("cdms3:bucket?keynewKey"); - } + @Test + public void shouldGetChildMFileFromBucketAndKey() throws IOException { + final MFileS3 withDelimiterWithSlash = new MFileS3("cdms3:bucket?key/" + DELIMITER_FRAGMENT); + final MFileS3 newMFileWithDelimiterWithSlash = withDelimiterWithSlash.getChild("newKey"); + assertThat(newMFileWithDelimiterWithSlash).isNotNull(); + assertThat(newMFileWithDelimiterWithSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey" + DELIMITER_FRAGMENT); + + final MFileS3 withoutDelimiterWithSlash = new MFileS3("cdms3:bucket?key/"); + final MFileS3 newMFileWithoutDelimiterWithSlash = withoutDelimiterWithSlash.getChild("newKey"); + assertThat(newMFileWithoutDelimiterWithSlash).isNotNull(); + assertThat(newMFileWithoutDelimiterWithSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey"); + + final MFileS3 withDelimiterWithoutSlash = new MFileS3("cdms3:bucket?key" + DELIMITER_FRAGMENT); + final MFileS3 newMFileWithDelimiterWithoutSlash = withDelimiterWithoutSlash.getChild("newKey"); + assertThat(newMFileWithDelimiterWithoutSlash).isNotNull(); + assertThat(newMFileWithDelimiterWithoutSlash.getPath()).isEqualTo("cdms3:bucket?key/newKey" + DELIMITER_FRAGMENT); + + final MFileS3 withoutDelimiterWithoutSlash = new MFileS3("cdms3:bucket?key"); + final MFileS3 newMFileWithoutDelimiterWithoutSlash = withoutDelimiterWithoutSlash.getChild("newKey"); + assertThat(newMFileWithoutDelimiterWithoutSlash).isNotNull(); + assertThat(newMFileWithoutDelimiterWithoutSlash.getPath()).isEqualTo("cdms3:bucket?keynewKey"); + } - @Test - public void shouldGetInputStream() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); - try (final InputStream inputStream = mFile.getInputStream()) { - assertThat(inputStream.read()).isNotEqualTo(-1); + @Test + public void shouldGetInputStream() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); + try (final InputStream inputStream = mFile.getInputStream()) { + assertThat(inputStream.read()).isNotEqualTo(-1); + } } - } - @Test - public void shouldGetLastModifiedForExistingFile() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); - assertThat(mFile.getLastModified()).isGreaterThan(0); + @Test + public void shouldGetLastModifiedForExistingFile() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); + assertThat(mFile.getLastModified()).isGreaterThan(0); - final MFile mFile2 = new MFileS3(AWS_G16_S3_OBJECT_1, 0, -1); - assertThat(mFile2.getLastModified()).isGreaterThan(0); + final MFile mFile2 = new MFileS3(AWS_G16_S3_OBJECT_1, 0, -1); + assertThat(mFile2.getLastModified()).isGreaterThan(0); - final MFile mFile3 = new MFileS3(AWS_G16_S3_OBJECT_1, 0, 1); - assertThat(mFile3.getLastModified()).isEqualTo(1); - } + final MFile mFile3 = new MFileS3(AWS_G16_S3_OBJECT_1, 0, 1); + assertThat(mFile3.getLastModified()).isEqualTo(1); + } - @Test - public void shouldThrowForGetLastModifiedOnNonExistingFile() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/NotARealKey"); - assertThrows(NoSuchKeyException.class, mFile::getLastModified); - } + @Test + public void shouldThrowForGetLastModifiedOnNonExistingFile() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/NotARealKey"); + assertThrows(NoSuchKeyException.class, mFile::getLastModified); + } - @Test - public void shouldGetLengthForExistingFile() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); - assertThat(mFile.getLength()).isGreaterThan(0); + @Test + public void shouldGetLengthForExistingFile() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_OBJECT_1); + assertThat(mFile.getLength()).isGreaterThan(0); - final MFile mFile2 = new MFileS3(AWS_G16_S3_OBJECT_1, -1, 0); - assertThat(mFile2.getLength()).isGreaterThan(0); + final MFile mFile2 = new MFileS3(AWS_G16_S3_OBJECT_1, -1, 0); + assertThat(mFile2.getLength()).isGreaterThan(0); - final MFile mFile3 = new MFileS3(AWS_G16_S3_OBJECT_1, 1, 0); - assertThat(mFile3.getLength()).isEqualTo(1); - } + final MFile mFile3 = new MFileS3(AWS_G16_S3_OBJECT_1, 1, 0); + assertThat(mFile3.getLength()).isEqualTo(1); + } - @Test - public void shouldThrowForGetLengthOnNonExistingFile() throws IOException { - final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/NotARealKey"); - assertThrows(NoSuchKeyException.class, mFile::getLength); - } + @Test + public void shouldThrowForGetLengthOnNonExistingFile() throws IOException { + final MFile mFile = new MFileS3(AWS_G16_S3_URI_DIR + "/NotARealKey"); + assertThrows(NoSuchKeyException.class, mFile::getLength); + } - @Test - public void shouldGetProtocol() { - assertThat(new Provider().getProtocol()).isEqualTo("cdms3"); - } + @Test + public void shouldGetProtocol() { + assertThat(new Provider().getProtocol()).isEqualTo("cdms3"); + } - @Test - public void shouldCreateMFile() throws IOException { - final MFile mFile = new Provider().create(AWS_G16_S3_OBJECT_1); - assertThat(mFile.exists()).isTrue(); - } + @Test + public void shouldCreateMFile() throws IOException { + final MFile mFile = new Provider().create(AWS_G16_S3_OBJECT_1); + assertThat(mFile.exists()).isTrue(); + } - @Test - public void shouldCreateMFileUsingCdms3Uri() throws URISyntaxException { - final MFile mFile = new MFileS3(new CdmS3Uri(AWS_G16_S3_OBJECT_1)); - assertThat(mFile.exists()).isTrue(); - } + @Test + public void shouldCreateMFileUsingCdms3Uri() throws URISyntaxException { + final MFile mFile = new MFileS3(new CdmS3Uri(AWS_G16_S3_OBJECT_1)); + assertThat(mFile.exists()).isTrue(); + } - private void checkWithBucket(String cdmS3Uri) throws IOException { - logger.info("Checking {}", cdmS3Uri); - MFile mFile = new MFileS3(cdmS3Uri); - assertThat(mFile.getPath()).isEqualTo(cdmS3Uri); - // Without a delimiter, the name is equal to the key. In this case, there is no key, so the name is empty - assertThat(mFile.getName()).isEqualTo(""); - MFile parent = mFile.getParent(); - // Since we have do not have a delimiter, we should not have a parent. - assertThat(parent).isNull(); - } + private void checkWithBucket(String cdmS3Uri) throws IOException { + logger.info("Checking {}", cdmS3Uri); + MFile mFile = new MFileS3(cdmS3Uri); + assertThat(mFile.getPath()).isEqualTo(cdmS3Uri); + // Without a delimiter, the name is equal to the key. In this case, there is no key, so the name is empty + assertThat(mFile.getName()).isEqualTo(""); + MFile parent = mFile.getParent(); + // Since we have do not have a delimiter, we should not have a parent. + assertThat(parent).isNull(); + } - private void checkWithBucketAndKey(String cdmS3Uri, String expectedName, String delimiter) throws IOException { - logger.info("Checking {}", cdmS3Uri); - MFile mFile = new MFileS3(cdmS3Uri); - assertThat(mFile.getPath()).isEqualTo(cdmS3Uri); - assertThat(mFile.getName()).isEqualTo(expectedName); + private void checkWithBucketAndKey(String cdmS3Uri, String expectedName, String delimiter) throws IOException { + logger.info("Checking {}", cdmS3Uri); + MFile mFile = new MFileS3(cdmS3Uri); + assertThat(mFile.getPath()).isEqualTo(cdmS3Uri); + assertThat(mFile.getName()).isEqualTo(expectedName); + + if (delimiter != null) { + assertThat(mFile.getParent()).isNotNull(); + } else { + assertThat(mFile.getParent()).isNull(); + } + assertThat(mFile.isDirectory()).isFalse(); + assertThat(mFile.getLength()).isEqualTo(G16_OBJECT_1_SIZE); + } - if (delimiter != null) { - assertThat(mFile.getParent()).isNotNull(); - } else { + private void dirCheckNoDelim(String cdmS3Uri, String expectedName) throws IOException { + MFile mFile = new MFileS3(cdmS3Uri); + logger.info("Checking {}", cdmS3Uri); + // The path is always the full cdms3 uri. + assertThat(mFile.getPath()).isEqualTo(cdmS3Uri); + // Without a delimiter, the name is the key. + assertThat(mFile.getName()).isEqualTo(expectedName); + // Without a delimiter, there is no parent. assertThat(mFile.getParent()).isNull(); + // Without a delimiter, there is no concept of a directory. + assertThat(mFile.isDirectory()).isFalse(); } - assertThat(mFile.isDirectory()).isFalse(); - assertThat(mFile.getLength()).isEqualTo(G16_OBJECT_1_SIZE); - } - private void dirCheckNoDelim(String cdmS3Uri, String expectedName) throws IOException { - MFile mFile = new MFileS3(cdmS3Uri); - logger.info("Checking {}", cdmS3Uri); - // The path is always the full cdms3 uri. - assertThat(mFile.getPath()).isEqualTo(cdmS3Uri); - // Without a delimiter, the name is the key. - assertThat(mFile.getName()).isEqualTo(expectedName); - // Without a delimiter, there is no parent. - assertThat(mFile.getParent()).isNull(); - // Without a delimiter, there is no concept of a directory. - assertThat(mFile.isDirectory()).isFalse(); - } + private void dirCheckDelim(String cdmS3Uri) throws IOException { + logger.info("Checking {}", cdmS3Uri); + MFile mFile = new MFileS3(cdmS3Uri); + assertThat(mFile.getPath()).isEqualTo(cdmS3Uri); + // With a delimiter, the name is equal to the rightmost part of the path + assertThat(mFile.getName()).isEqualTo(dirName); + MFile parent = mFile.getParent(); + // Since we have a delimiter, and the object key contains the delimiter, we know this should not be null. + assertThat(parent).isNotNull(); + assertThat(parent.getPath()) + .isEqualTo(cdmS3Uri.replace("/" + dirName, "/").replace(parentDirName + "//", parentDirName + "/")); + assertThat(parent.getName()).isEqualTo(parentDirName); + assertThat(parent.isDirectory()).isTrue(); + } - private void dirCheckDelim(String cdmS3Uri) throws IOException { - logger.info("Checking {}", cdmS3Uri); - MFile mFile = new MFileS3(cdmS3Uri); - assertThat(mFile.getPath()).isEqualTo(cdmS3Uri); - // With a delimiter, the name is equal to the rightmost part of the path - assertThat(mFile.getName()).isEqualTo(dirName); - MFile parent = mFile.getParent(); - // Since we have a delimiter, and the object key contains the delimiter, we know this should not be null. - assertThat(parent).isNotNull(); - assertThat(parent.getPath()) - .isEqualTo(cdmS3Uri.replace("/" + dirName, "/").replace(parentDirName + "//", parentDirName + "/")); - assertThat(parent.getName()).isEqualTo(parentDirName); - assertThat(parent.isDirectory()).isTrue(); - } + private void compareS3Mfiles(String uri1, String uri2) throws IOException { + MFile mFile1 = new MFileS3(uri1); + MFile mFile2 = new MFileS3(uri1); + MFile mFile3 = new MFileS3(uri2); + assert mFile1.equals(mFile2); + assertThat(mFile1).isEqualTo(mFile2); + assertThat(mFile1.compareTo(mFile2)).isEqualTo(0); + assertThat(mFile1.hashCode()).isEqualTo(mFile2.hashCode()); + assertThat(uri1).ignoringCase().isNotEqualTo(uri2); + assertThat(mFile1).isNotEqualTo(mFile3); + assertThat(mFile1.compareTo(mFile3)).isNotEqualTo(0); + assertThat(mFile1.hashCode()).isNotEqualTo(mFile3.hashCode()); + } - private void compareS3Mfiles(String uri1, String uri2) throws IOException { - MFile mFile1 = new MFileS3(uri1); - MFile mFile2 = new MFileS3(uri1); - MFile mFile3 = new MFileS3(uri2); - assert mFile1.equals(mFile2); - assertThat(mFile1).isEqualTo(mFile2); - assertThat(mFile1.compareTo(mFile2)).isEqualTo(0); - assertThat(mFile1.hashCode()).isEqualTo(mFile2.hashCode()); - assertThat(uri1).ignoringCase().isNotEqualTo(uri2); - assertThat(mFile1).isNotEqualTo(mFile3); - assertThat(mFile1.compareTo(mFile3)).isNotEqualTo(0); - assertThat(mFile1.hashCode()).isNotEqualTo(mFile3.hashCode()); + private void checkS3MFilesAuxInfo(String uri) throws IOException { + MFile mFile = new MFileS3(uri); + mFile.setAuxInfo("Aux Info"); + Object auxInfo = mFile.getAuxInfo(); + assertThat(auxInfo.toString()).isEqualTo("Aux Info"); + assertThat(auxInfo.toString()).isNotEqualTo("aux info"); + assertThat(auxInfo.toString()).isNotEqualTo("Ox Info"); + } } - private void checkS3MFilesAuxInfo(String uri) throws IOException { - MFile mFile = new MFileS3(uri); - mFile.setAuxInfo("Aux Info"); - Object auxInfo = mFile.getAuxInfo(); - assertThat(auxInfo.toString()).isEqualTo("Aux Info"); - assertThat(auxInfo.toString()).isNotEqualTo("aux info"); - assertThat(auxInfo.toString()).isNotEqualTo("Ox Info"); - } + @RunWith(Parameterized.class) + public static class TestMFileS3Parameterized { + - @AfterClass - public static void teardown() { - System.clearProperty(S3TestsCommon.AWS_REGION_PROP_NAME); } } From 82b1fb3adb75079be0447ada44f24c959bba7e04 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Thu, 19 Sep 2024 11:31:01 -0600 Subject: [PATCH 2/3] Add MFile::relativize method --- .../main/java/thredds/filesystem/MFileOS.java | 8 ++++++ .../java/thredds/filesystem/MFileOS7.java | 8 ++++++ .../inventory/CollectionManagerCatalog.java | 5 ++++ .../main/java/thredds/inventory/MFile.java | 8 ++++++ .../java/thredds/inventory/s3/MFileS3.java | 26 +++++++++++++++++++ .../java/thredds/inventory/zarr/MFileZip.java | 5 ++++ .../ucar/nc2/grib/collection/GcMFile.java | 5 ++++ 7 files changed, 65 insertions(+) diff --git a/cdm/core/src/main/java/thredds/filesystem/MFileOS.java b/cdm/core/src/main/java/thredds/filesystem/MFileOS.java index 094a02f6ec..5525e1b025 100644 --- a/cdm/core/src/main/java/thredds/filesystem/MFileOS.java +++ b/cdm/core/src/main/java/thredds/filesystem/MFileOS.java @@ -146,4 +146,12 @@ public File getFile() { public MFileOS getChild(String newFilename) { return new MFileOS(new File(file, newFilename)); } + + @Override + public String relativize(MFile other) { + if (other instanceof MFileOS) { + return file.toPath().relativize(((MFileOS) other).getFile().toPath()).toString(); + } + throw new IllegalArgumentException("Cannot relativize " + other + " against " + this); + } } diff --git a/cdm/core/src/main/java/thredds/filesystem/MFileOS7.java b/cdm/core/src/main/java/thredds/filesystem/MFileOS7.java index 5a04a5ca85..4bacc297b9 100644 --- a/cdm/core/src/main/java/thredds/filesystem/MFileOS7.java +++ b/cdm/core/src/main/java/thredds/filesystem/MFileOS7.java @@ -149,4 +149,12 @@ public MFileOS7 getChild(String newFilename) { public Path getNioPath() { return path; } + + @Override + public String relativize(MFile other) { + if (other instanceof MFileOS7) { + return path.relativize(((MFileOS7) other).path).toString(); + } + throw new IllegalArgumentException("Cannot relativize " + other + " against " + this); + } } diff --git a/cdm/core/src/main/java/thredds/inventory/CollectionManagerCatalog.java b/cdm/core/src/main/java/thredds/inventory/CollectionManagerCatalog.java index 70c408db6e..6e157f9d19 100644 --- a/cdm/core/src/main/java/thredds/inventory/CollectionManagerCatalog.java +++ b/cdm/core/src/main/java/thredds/inventory/CollectionManagerCatalog.java @@ -173,6 +173,11 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes) public MFileRemote getChild(String newFilename) { throw new UnsupportedOperationException("MFileRemote::getChild not implemented. Filename: " + getName()); } + + @Override + public String relativize(MFile other) { + throw new UnsupportedOperationException("MFileRemote::relativize not implemented. Filename: " + getName()); + } } /////////////////////////////// diff --git a/cdm/core/src/main/java/thredds/inventory/MFile.java b/cdm/core/src/main/java/thredds/inventory/MFile.java index 38b73bc6cd..b84767ab3c 100644 --- a/cdm/core/src/main/java/thredds/inventory/MFile.java +++ b/cdm/core/src/main/java/thredds/inventory/MFile.java @@ -114,4 +114,12 @@ default boolean isReadable() { */ @Nullable MFile getChild(String newFileName); + + /** + * Construct a relative path between this MFile and a given MFile. + * + * @param other the MFile to relativize against this MFile's path + * @return the resulting relative path as a String, or an empty path if both paths are equal + */ + String relativize(MFile other); } diff --git a/cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java b/cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java index 48a26cab1e..d6f5e6ba07 100644 --- a/cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java +++ b/cdm/s3/src/main/java/thredds/inventory/s3/MFileS3.java @@ -7,6 +7,7 @@ import java.io.IOException; import java.io.OutputStream; import java.net.URISyntaxException; +import java.nio.file.Paths; import java.util.Objects; import java.util.function.Supplier; import javax.annotation.Nonnull; @@ -382,6 +383,31 @@ public MFileS3 getChild(String newFilename) { } } + /** + * Construct a relative key path (as if it were a file path) between this MFile and a given MFile. + * They must have same delimiter and bucket, otherwise the path returned is empty. + * + * @param other the MFile to relativize against this MFile's path + * @return the resulting relative path as a String, or an empty path if both paths are equal + */ + @Override + public String relativize(MFile other) { + if (!(other instanceof MFileS3)) { + throw new IllegalArgumentException("Cannot relativize " + other + " against " + this); + } + final MFileS3 otherS3 = (MFileS3) other; + + if (getDelimiter() != null && getDelimiter().equals("/") && getDelimiter().equals(otherS3.getDelimiter()) + && cdmS3Uri.getBucket().equals(otherS3.cdmS3Uri.getBucket())) { + final String key = getKey(); + final String otherKey = otherS3.getKey(); + return key == null || otherKey == null ? "" + : Paths.get("/" + key).relativize(Paths.get("/" + otherKey)).toString(); + } + + return ""; + } + public static class Provider implements MFileProvider { private static String protocol = CdmS3Uri.SCHEME_CDM_S3; diff --git a/cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java b/cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java index 061c10dad4..2b21ba7f25 100644 --- a/cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java +++ b/cdm/zarr/src/main/java/thredds/inventory/zarr/MFileZip.java @@ -184,6 +184,11 @@ public MFileZip getChild(String newFilename) { throw new UnsupportedOperationException("MFileZip::getChild not implemented. Filename: " + getName()); } + @Override + public String relativize(MFile other) { + throw new UnsupportedOperationException("MFileZip::relativize not implemented. Filename: " + getName()); + } + public Path getRootPath() { return rootPath; } diff --git a/grib/src/main/java/ucar/nc2/grib/collection/GcMFile.java b/grib/src/main/java/ucar/nc2/grib/collection/GcMFile.java index 0f7ffee20a..1976210268 100644 --- a/grib/src/main/java/ucar/nc2/grib/collection/GcMFile.java +++ b/grib/src/main/java/ucar/nc2/grib/collection/GcMFile.java @@ -141,4 +141,9 @@ public void writeToStream(OutputStream outputStream, long offset, long maxBytes) public GcMFile getChild(String newFilename) { throw new UnsupportedOperationException("GcMFile::getChild not implemented. Filename: " + getName()); } + + @Override + public String relativize(MFile other) { + throw new UnsupportedOperationException("GcMFile::relativize not implemented. Filename: " + getName()); + } } From 3fedc92c8f58c99adca0348c1d59b7cf17124f84 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Thu, 19 Sep 2024 11:32:20 -0600 Subject: [PATCH 3/3] Add tests for MFile::relativize --- .../java/thredds/filesystem/TestMFileOS.java | 8 +++++ .../java/thredds/filesystem/TestMFileOS7.java | 8 +++++ .../thredds/inventory/s3/TestMFileS3.java | 35 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/cdm/core/src/test/java/thredds/filesystem/TestMFileOS.java b/cdm/core/src/test/java/thredds/filesystem/TestMFileOS.java index d1a0af4d72..a12ae653fa 100644 --- a/cdm/core/src/test/java/thredds/filesystem/TestMFileOS.java +++ b/cdm/core/src/test/java/thredds/filesystem/TestMFileOS.java @@ -100,6 +100,14 @@ public void shouldGetChildMFile() { assertThat(newMFile.getParent().getPath()).isEqualTo(mFile.getPath()); assertThat(newMFile.getPath()).isEqualTo(Paths.get(mFile.getPath(), "newFile").toString()); } + + @Test + public void shouldGetRelativePath() { + final MFileOS mFile = new MFileOS("/an/absolute/path/a/"); + final MFileOS mFile2 = new MFileOS("/an/absolute/path/foo/bar/"); + assertThat(mFile.relativize(mFile2)).isEqualTo("../foo/bar"); + assertThat(mFile2.relativize(mFile)).isEqualTo("../../a"); + } } private static File createTemporaryFile(int size) throws IOException { diff --git a/cdm/core/src/test/java/thredds/filesystem/TestMFileOS7.java b/cdm/core/src/test/java/thredds/filesystem/TestMFileOS7.java index f2ec53a261..6d001ffe03 100644 --- a/cdm/core/src/test/java/thredds/filesystem/TestMFileOS7.java +++ b/cdm/core/src/test/java/thredds/filesystem/TestMFileOS7.java @@ -98,6 +98,14 @@ public void shouldGetInputStream() throws IOException { assertThat(inputStream.read()).isNotEqualTo(-1); } } + + @Test + public void shouldGetRelativePath() throws IOException { + final MFileOS7 mFile = new MFileOS7(Paths.get("/an/absolute/path"), null); + final MFileOS7 mFile2 = new MFileOS7(Paths.get("/an/absolute/path/foo"), null); + assertThat(mFile.relativize(mFile2)).isEqualTo("foo"); + assertThat(mFile2.relativize(mFile)).isEqualTo(".."); + } } private static File createTemporaryFile(int size) throws IOException { diff --git a/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java b/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java index e67ceed2b4..6334bdde53 100644 --- a/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java +++ b/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java @@ -516,7 +516,42 @@ private void checkS3MFilesAuxInfo(String uri) throws IOException { @RunWith(Parameterized.class) public static class TestMFileS3Parameterized { + @Parameterized.Parameters(name = "{0}, {1}, {2}") + public static List getTestParameters() { + return Arrays.asList(new Object[][] { + {"cdms3:bucket?a/long/key/", "cdms3:bucket?a/long/", ""}, + {"cdms3:bucket?a/long/key/#delimiter=/", "cdms3:otherBucket?a/long/#delimiter=/", ""}, + {"cdms3:bucket?a/long/#delimiter=/", "cdms3:bucket?a/long/key", ""}, + {"cdms3:bucket", "cdms3:bucket?a/long/key", ""}, {"cdms3:bucket?a/long/key", "cdms3:bucket", ""}, + + {"cdms3:bucket?a/long/#delimiter=/", "cdms3:bucket?a/long/key/#delimiter=/", "key"}, + {"cdms3:bucket?a/long/#delimiter=/", "cdms3:bucket?a/long/key#delimiter=/", "key"}, + {"cdms3:bucket?a/long#delimiter=/", "cdms3:bucket?a/long/key/#delimiter=/", "key"}, + {"cdms3:bucket?a/long#delimiter=/", "cdms3:bucket?a/long/key#delimiter=/", "key"}, + + {"cdms3:bucket?a/long/key/#delimiter=/", "cdms3:bucket?a/long/key/#delimiter=/", ""}, + {"cdms3:bucket?a/long/key#delimiter=/", "cdms3:bucket?a/long#delimiter=/", ".."}, + {"cdms3:bucket?a/long#delimiter=/", "cdms3:bucket?a/long_key#delimiter=/", "../long_key"}, + + }); + } + + @Parameterized.Parameter(value = 0) + public String uri1; + + @Parameterized.Parameter(value = 1) + public String uri2; + + @Parameterized.Parameter(value = 2) + public String expectedRelativePath; + + @Test + public void shouldRelativizeKey() throws URISyntaxException, IOException { + final MFile mFile = new MFileS3(uri1); + final MFile mFile2 = new MFileS3(uri2); + assertThat(mFile.relativize(mFile2)).isEqualTo(expectedRelativePath); + } } }