implement demo code for how-to-mock-aws-s3-integration-tests
This commit is contained in:
parent
72cd365b26
commit
c977f8f738
|
@ -20,6 +20,12 @@
|
||||||
<artifactId>s3</artifactId>
|
<artifactId>s3</artifactId>
|
||||||
<version>${aws.java.sdk.version}</version>
|
<version>${aws.java.sdk.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>software.amazon.awssdk</groupId>
|
||||||
|
<artifactId>url-connection-client</artifactId>
|
||||||
|
<version>${aws.java.sdk.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
|
@ -37,12 +43,34 @@
|
||||||
<artifactId>commons-codec</artifactId>
|
<artifactId>commons-codec</artifactId>
|
||||||
<version>${commons-codec-version}</version>
|
<version>${commons-codec-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- adobe s3mock -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.adobe.testing</groupId>
|
||||||
|
<artifactId>s3mock</artifactId>
|
||||||
|
<version>${com.adobe.testing.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.adobe.testing</groupId>
|
||||||
|
<artifactId>s3mock-testcontainers</artifactId>
|
||||||
|
<version>${com.adobe.testing.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>${org.testcontainers.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<aws.java.sdk.version>2.20.52</aws.java.sdk.version>
|
<aws.java.sdk.version>2.20.52</aws.java.sdk.version>
|
||||||
<commons-codec-version>1.10.L001</commons-codec-version>
|
<commons-codec-version>1.10.L001</commons-codec-version>
|
||||||
<jets3t-version>0.9.4.0006L</jets3t-version>
|
<jets3t-version>0.9.4.0006L</jets3t-version>
|
||||||
|
<com.adobe.testing.version>3.3.0</com.adobe.testing.version>
|
||||||
|
<org.testcontainers.version>1.19.4</org.testcontainers.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,65 @@
|
||||||
|
package com.baeldung.s3;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import software.amazon.awssdk.core.ResponseBytes;
|
||||||
|
import software.amazon.awssdk.core.sync.RequestBody;
|
||||||
|
import software.amazon.awssdk.services.s3.S3Client;
|
||||||
|
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
|
||||||
|
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
|
||||||
|
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
|
||||||
|
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
|
||||||
|
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||||
|
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||||
|
|
||||||
|
public class S3CrudService {
|
||||||
|
|
||||||
|
private final S3Client s3Client;
|
||||||
|
|
||||||
|
public S3CrudService(S3Client s3Client) {
|
||||||
|
this.s3Client = s3Client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createBucket(String bucketName) {
|
||||||
|
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
|
||||||
|
.bucket(bucketName)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
s3Client.createBucket(bucketRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createObject(String bucketName, File inMemoryObject) {
|
||||||
|
PutObjectRequest request = PutObjectRequest.builder()
|
||||||
|
.bucket(bucketName)
|
||||||
|
.key(inMemoryObject.getName())
|
||||||
|
.build();
|
||||||
|
s3Client.putObject(request, RequestBody.fromByteBuffer(inMemoryObject.getContent()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<byte[]> getObject(String bucketName, String objectKey) {
|
||||||
|
try {
|
||||||
|
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
|
||||||
|
.bucket(bucketName)
|
||||||
|
.key(objectKey)
|
||||||
|
.build();
|
||||||
|
ResponseBytes<GetObjectResponse> responseResponseBytes = s3Client.getObjectAsBytes(getObjectRequest);
|
||||||
|
return Optional.of(responseResponseBytes.asByteArray());
|
||||||
|
} catch (S3Exception e) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean deleteObject(String bucketName, String objectKey) {
|
||||||
|
try {
|
||||||
|
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
|
||||||
|
.bucket(bucketName)
|
||||||
|
.key(objectKey)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
s3Client.deleteObject(deleteObjectRequest);
|
||||||
|
return true;
|
||||||
|
} catch (S3Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,82 @@
|
||||||
|
package com.baeldung.s3;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.testcontainers.junit.jupiter.Container;
|
||||||
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||||
|
|
||||||
|
import com.adobe.testing.s3mock.testcontainers.S3MockContainer;
|
||||||
|
|
||||||
|
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||||
|
import software.amazon.awssdk.services.s3.S3Client;
|
||||||
|
import software.amazon.awssdk.services.s3.S3Configuration;
|
||||||
|
import software.amazon.awssdk.utils.AttributeMap;
|
||||||
|
|
||||||
|
// This live test needs a running Docker instance so that a S3Mock Container can be started
|
||||||
|
|
||||||
|
@Testcontainers
|
||||||
|
public class S3CrudServiceIntegrationTest {
|
||||||
|
|
||||||
|
private static final String TEST_BUCKET_NAME = "test-bucket";
|
||||||
|
|
||||||
|
@Container
|
||||||
|
private final S3MockContainer s3Mock = new S3MockContainer("latest");
|
||||||
|
private S3Client s3Client;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
var endpoint = s3Mock.getHttpsEndpoint();
|
||||||
|
var serviceConfig = S3Configuration.builder()
|
||||||
|
.pathStyleAccessEnabled(true)
|
||||||
|
.build();
|
||||||
|
var httpClient = UrlConnectionHttpClient.builder()
|
||||||
|
.buildWithDefaults(AttributeMap.builder()
|
||||||
|
.put(TRUST_ALL_CERTIFICATES, Boolean.TRUE)
|
||||||
|
.build());
|
||||||
|
s3Client = S3Client.builder()
|
||||||
|
.endpointOverride(URI.create(endpoint))
|
||||||
|
.serviceConfiguration(serviceConfig)
|
||||||
|
.httpClient(httpClient)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenVerifyingCreationOfS3Bucket_thenCorrect() {
|
||||||
|
var s3CrudService = new S3CrudService(s3Client);
|
||||||
|
s3CrudService.createBucket(TEST_BUCKET_NAME);
|
||||||
|
|
||||||
|
var createdBucketName = s3Client.listBuckets()
|
||||||
|
.buckets()
|
||||||
|
.get(0)
|
||||||
|
.name();
|
||||||
|
assertThat(TEST_BUCKET_NAME).isEqualTo(createdBucketName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenCreatingAnObjectOnS3Bucket_thenSameObjectIsRetrived() {
|
||||||
|
var s3CrudService = new S3CrudService(s3Client);
|
||||||
|
s3CrudService.createBucket(TEST_BUCKET_NAME);
|
||||||
|
|
||||||
|
var fileToSave = FileGenerator.generateFiles(1, 100)
|
||||||
|
.get(0);
|
||||||
|
s3CrudService.createObject(TEST_BUCKET_NAME, fileToSave);
|
||||||
|
|
||||||
|
var savedFileContent = s3CrudService.getObject(TEST_BUCKET_NAME, fileToSave.getName());
|
||||||
|
|
||||||
|
assertThat(Arrays.equals(fileToSave.getContent()
|
||||||
|
.array(), savedFileContent.orElse(new byte[]{}))).isTrue();
|
||||||
|
|
||||||
|
s3CrudService.deleteObject(TEST_BUCKET_NAME,fileToSave.getName());
|
||||||
|
|
||||||
|
var deletedFileContent = s3CrudService.getObject(TEST_BUCKET_NAME, fileToSave.getName());
|
||||||
|
assertThat(deletedFileContent).isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue