Skip to content

Commit

Permalink
test(Files): Add integration tests for Files.copy / read* from issue #…
Browse files Browse the repository at this point in the history
…236 [skip ci]
  • Loading branch information
guicamest committed Nov 3, 2023
1 parent 558d2ae commit 432d640
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ public static void putObject(String bucket, String key) {
.doesNotThrowAnyException();
}

public static void putObject(String bucket, String key, String content) {
assertThatCode(() -> {
Container.ExecResult execResultCreateFile = LOCAL_STACK_CONTAINER.execInContainer("sh", "-c", "echo -n '" + content + "' > " + key);
Container.ExecResult execResultPut = LOCAL_STACK_CONTAINER.execInContainer(("awslocal s3api put-object --bucket " + bucket + " --key " + key + " --body " + key).split(" "));

assertThat(execResultCreateFile.getExitCode()).isZero();
assertThat(execResultPut.getExitCode()).withFailMessage("Failed put: %s ", execResultPut.getStderr()).isZero();
}).as("Failed to put object '%s' in bucket '%s'", key, bucket)
.doesNotThrowAnyException();
}

public static String localStackConnectionEndpoint() {
return localStackConnectionEndpoint(null, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package software.amazon.nio.spi.s3;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;
import static software.amazon.nio.spi.s3.Containers.localStackConnectionEndpoint;
import static software.amazon.nio.spi.s3.Containers.putObject;

@DisplayName("Files$copy should load file contents from localstack")
public class FilesCopyTest
{
@TempDir
Path tempDir;

@Test
@DisplayName("when doing copy of existing file")
public void fileCopyShouldCopyFileWhenFileFound() throws IOException {
Containers.createBucket("sink");
putObject("sink", "files-copy.txt", "some content");
final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/sink/files-copy.txt"));
Path copiedFile = Files.copy(path, tempDir.resolve("sample-file-local.txt"));
assertThat(copiedFile).hasContent("some content");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package software.amazon.nio.spi.s3;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.assertj.core.api.BDDAssertions.then;
import static software.amazon.nio.spi.s3.Containers.localStackConnectionEndpoint;
import static software.amazon.nio.spi.s3.Containers.putObject;

@DisplayName("Files$read* should load file contents from localstack")
public class FilesReadTest
{
private final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/sink/files-read.txt"));

@BeforeAll
public static void createBucketAndFile(){
Containers.createBucket("sink");
putObject("sink", "files-read.txt", "some content");
}

@Test
@DisplayName("when doing readAllBytes from existing file in s3")
public void fileReadAllBytesShouldReturnFileContentsWhenFileFound() throws IOException {
then(Files.readAllBytes(path)).isEqualTo("some content".getBytes());
}

@Test
@DisplayName("when doing readAllLines from existing file in s3")
public void fileReadAllLinesShouldReturnFileContentWhenFileFound() throws IOException {
then(String.join("", Files.readAllLines(path))).isEqualTo("some content");
}

}

0 comments on commit 432d640

Please sign in to comment.