From 116f5c9f62aa6562c63f844a185320e4a39848f1 Mon Sep 17 00:00:00 2001 From: Tara Drwenski Date: Fri, 1 Mar 2024 14:28:04 -0700 Subject: [PATCH] Improve MFileS3 test coverage --- .../thredds/inventory/s3/TestMFileS3.java | 80 ++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) 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 cac3356b37..f207422266 100644 --- a/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java +++ b/cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java @@ -11,6 +11,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URISyntaxException; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -18,7 +19,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.services.s3.model.NoSuchKeyException; +import thredds.filesystem.MFileOS; import thredds.inventory.MFile; +import thredds.inventory.s3.MFileS3.Provider; +import ucar.unidata.io.s3.CdmS3Uri; import ucar.unidata.io.s3.S3TestsCommon; import ucar.unidata.util.test.category.NotPullRequest; @@ -120,6 +124,7 @@ public void bucketAndKeyOsdc() throws IOException { 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 @@ -143,6 +148,20 @@ public void shouldReturnTopLevelKeyName() throws IOException { assertThat(fileWithDelimiter.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); + } + + @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 compareMFilesAws() throws IOException { for (String delimiter : DELIMITER_FRAGMENTS) { @@ -345,6 +364,60 @@ public void shouldGetInputStream() throws IOException { } } + + @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 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 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 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 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 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); @@ -393,7 +466,8 @@ private void dirCheckDelim(String cdmS3Uri) throws IOException { 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, "/")); + assertThat(parent.getPath()) + .isEqualTo(cdmS3Uri.replace("/" + dirName, "/").replace(parentDirName + "//", parentDirName + "/")); assertThat(parent.getName()).isEqualTo(parentDirName); assertThat(parent.isDirectory()).isTrue(); } @@ -404,8 +478,12 @@ private void compareS3Mfiles(String uri1, String uri2) throws IOException { 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 {