Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve MFileS3 test coverage #1310

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
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;
import org.junit.experimental.categories.Category;
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;

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand All @@ -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 {
Expand Down
Loading