diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 4217f0dc..2169159c 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -38,3 +38,8 @@ jobs: - name: Execute Gradle build run: ./gradlew build + + - name: Run Integration tests + run: ./gradlew integrationTest + env: + TESTCONTAINERS_CHECKS_DISABLE: true diff --git a/THIRD-PARTY b/THIRD-PARTY index 2b916044..b759cc14 100644 --- a/THIRD-PARTY +++ b/THIRD-PARTY @@ -414,3 +414,40 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +** Testcontainers Java -- https://testcontainers.com/ -- The MIT License (MIT) + +Copyright (c) 2015-2019 Richard North + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +** Localstack -- https://localstack.cloud/ -- Copyright (c) 2017+ LocalStack contributors +Copyright (c) 2016 Atlassian Pty Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/build.gradle b/build.gradle index 9f21c605..22e7d3ba 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ plugins { id 'signing' id 'maven-publish' id 'jacoco' + id 'jvm-test-suite' id 'com.github.johnrengelman.shadow' version '7.1.2' } @@ -48,11 +49,6 @@ sourceSets { dependencies { implementation 'io.reactivex.rxjava3:rxjava:3.1.8' - testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' - testImplementation 'org.mockito:mockito-core:5.5.0' - testImplementation 'org.assertj:assertj-core:3.24.2' - testImplementation 'org.mockito:mockito-junit-jupiter:5.5.0' - testImplementation 'com.github.stefanbirkner:system-lambda:1.2.1' implementation platform('software.amazon.awssdk:bom:2.20.158') @@ -121,9 +117,28 @@ javadoc { } } -test { - useJUnitPlatform() - finalizedBy jacocoTestReport +// JVM test suite plugin https://docs.gradle.org/7.3.3/userguide/jvm_test_suite_plugin.html +testing { + suites { + integrationTest(JvmTestSuite) { + dependencies { + implementation project + implementation 'org.testcontainers:junit-jupiter:1.19.1' + implementation 'org.testcontainers:localstack:1.19.1' + implementation 'org.testcontainers:testcontainers:1.19.1' + } + } + withType(JvmTestSuite).configureEach { + useJUnitJupiter('5.10.0') + dependencies { + implementation 'org.assertj:assertj-core:3.24.2' + implementation 'org.mockito:mockito-core:5.5.0' + implementation 'org.assertj:assertj-core:3.24.2' + implementation 'org.mockito:mockito-junit-jupiter:5.5.0' + implementation 'com.github.stefanbirkner:system-lambda:1.2.1' + } + } + } } jacocoTestReport { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2c072774..6c14747a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -5,6 +5,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/integrationTest/java/software/amazon/nio/spi/s3/Containers.java b/src/integrationTest/java/software/amazon/nio/spi/s3/Containers.java new file mode 100644 index 00000000..2a0987c7 --- /dev/null +++ b/src/integrationTest/java/software/amazon/nio/spi/s3/Containers.java @@ -0,0 +1,53 @@ +package software.amazon.nio.spi.s3; + +import org.testcontainers.containers.Container; +import org.testcontainers.containers.localstack.LocalStackContainer; +import org.testcontainers.utility.DockerImageName; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3; +import static software.amazon.nio.spi.s3.config.S3NioSpiConfiguration.S3_SPI_ENDPOINT_PROTOCOL_PROPERTY; + +abstract class Containers { + + static final LocalStackContainer LOCAL_STACK_CONTAINER; + + static { + LOCAL_STACK_CONTAINER = new LocalStackContainer( + DockerImageName.parse("localstack/localstack:2.3.2") + ).withServices(S3).withEnv("PROVIDER_OVERRIDE_S3", "v3"); + LOCAL_STACK_CONTAINER.start(); + System.setProperty(S3_SPI_ENDPOINT_PROTOCOL_PROPERTY, "http"); + } + + public static void createBucket(String name) { + assertThatCode(() -> { + Container.ExecResult execResult = LOCAL_STACK_CONTAINER.execInContainer(("awslocal s3api create-bucket --bucket " + name).split(" ")); + assertThat(execResult.getExitCode()).isZero(); + }).as("Failed to create bucket '%s'", name) + .doesNotThrowAnyException(); + } + + public static void putObject(String bucket, String key) { + assertThatCode(() -> { + Container.ExecResult execResult = LOCAL_STACK_CONTAINER.execInContainer(("awslocal s3api put-object --bucket " + bucket + " --key " + key).split(" ")); + assertThat(execResult.getExitCode()).isZero(); + }).as("Failed to put object '%s' in bucket '%s'", key, bucket) + .doesNotThrowAnyException(); + } + + public static String localStackConnectionEndpoint() { + return localStackConnectionEndpoint(null, null); + } + + public static String localStackConnectionEndpoint(String key, String secret) { + String accessKey = key != null ? key : LOCAL_STACK_CONTAINER.getAccessKey(); + String secretKey = secret != null ? secret : LOCAL_STACK_CONTAINER.getSecretKey(); + return String.format("s3x://%s:%s@%s", accessKey, secretKey, localStackHost()); + } + + private static String localStackHost() { + return LOCAL_STACK_CONTAINER.getEndpoint().getHost() + ":" + LOCAL_STACK_CONTAINER.getEndpoint().getPort(); + } +} diff --git a/src/integrationTest/java/software/amazon/nio/spi/s3/FilesExistsTest.java b/src/integrationTest/java/software/amazon/nio/spi/s3/FilesExistsTest.java new file mode 100644 index 00000000..80fa4bb4 --- /dev/null +++ b/src/integrationTest/java/software/amazon/nio/spi/s3/FilesExistsTest.java @@ -0,0 +1,64 @@ +package software.amazon.nio.spi.s3; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +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$exists()") +public class FilesExistsTest { + + @Nested + @DisplayName("should be false") + class FileDoesNotExist { + + @Test + @DisplayName("when bucket does not exist") + public void fileExistsShouldReturnFalseWhenBucketNotFound() { + final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/does-not-exist")); + then(Files.exists(path)).isFalse(); + } + + @Test + @DisplayName("when bucket exists but file doesn't") + public void fileExistsShouldReturnFalseWhenBucketExistsAndFileNotFound() { + Containers.createBucket("sink"); + final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/sink/missing-file")); + then(Files.exists(path)).isFalse(); + } + } + + @Nested + @DisplayName("should be true") + class FileExists { + + @BeforeEach + public void createBucket() { + Containers.createBucket("sink"); + } + + @Test + @DisplayName("when bucket and file exist") + public void fileExistsShouldReturnTrueWhenBucketExistsAndFileFound() { + putObject("sink", "sample-file.txt"); + final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/sink/sample-file.txt")); + then(Files.exists(path)).isTrue(); + } + + @Test + @DisplayName("for bucket path when it exists") + public void fileExistsShouldReturnTrueWhenBucketExists() { + final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/sink/")); + then(Files.exists(path)).isTrue(); + } + } +} diff --git a/src/integrationTest/resources/logback.xml b/src/integrationTest/resources/logback.xml new file mode 100644 index 00000000..51af58c0 --- /dev/null +++ b/src/integrationTest/resources/logback.xml @@ -0,0 +1,24 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + + \ No newline at end of file