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

Do not use deprecated methods from testcontainers-localstack. #512

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class DynamoDbTemplateIntegrationTest {
public static void createTable() {
DynamoDbClient dynamoDbClient = DynamoDbClient.builder()
.endpointOverride(localstack.getEndpointOverride(DYNAMODB)).region(Region.of(localstack.getRegion()))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("noop", "noop")))
.credentialsProvider(StaticCredentialsProvider
.create(AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey())))
.build();
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(dynamoDbClient).build();
dynamoDbTemplate = new DynamoDbTemplate(enhancedClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.amazonaws.auth.AWSCredentials;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -87,9 +86,8 @@ private static Stream<S3OutputStreamProvider> availableS3OutputStreamProviders()
static void beforeAll() {
// region and credentials are irrelevant for test, but must be added to make
// test work on environments without AWS cli configured
AWSCredentials localstackCredentials = localstack.getDefaultCredentialsProvider().getCredentials();
StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials
.create(localstackCredentials.getAWSAccessKeyId(), localstackCredentials.getAWSSecretKey()));
StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider
.create(AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey()));
client = S3Client.builder().region(Region.of(localstack.getRegion())).credentialsProvider(credentialsProvider)
.endpointOverride(localstack.getEndpointOverride(Service.S3)).build();
s3TransferManager = S3TransferManager.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNoException;

import com.amazonaws.auth.AWSCredentials;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -68,9 +67,8 @@ class S3TemplateIntegrationTests {
static void beforeAll() {
// region and credentials are irrelevant for test, but must be added to make
// test work on environments without AWS cli configured
AWSCredentials localstackCredentials = localstack.getDefaultCredentialsProvider().getCredentials();
StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials
.create(localstackCredentials.getAWSAccessKeyId(), localstackCredentials.getAWSSecretKey()));
StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider
.create(AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey()));
client = S3Client.builder().region(Region.of(localstack.getRegion())).credentialsProvider(credentialsProvider)
.endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3)).build();
}
Expand All @@ -81,7 +79,7 @@ void init() {
new DiskBufferingS3OutputStreamProvider(client, new PropertiesS3ObjectContentTypeResolver()),
new Jackson2JsonS3ObjectConverter(new ObjectMapper()));

client.createBucket(r -> r.bucket("test-bucket"));
client.createBucket(r -> r.bucket(BUCKET_NAME));
}

@AfterEach
Expand All @@ -95,51 +93,51 @@ void destroyBuckets() {

@Test
void createsBucket() {
String location = s3Template.createBucket("test-bucket");
String location = s3Template.createBucket(BUCKET_NAME);

assertThat(location).isNotNull();
assertThat(client.listBuckets()).satisfies(r -> this.bucketExists(r, "test-bucket"));
assertThat(client.listBuckets()).satisfies(r -> this.bucketExists(r, BUCKET_NAME));
}

@Test
void deletesBucket() {
client.createBucket(r -> r.bucket("test-bucket"));
assertThat(client.listBuckets()).satisfies(r -> this.bucketExists(r, "test-bucket"));
client.createBucket(r -> r.bucket(BUCKET_NAME));
assertThat(client.listBuckets()).satisfies(r -> this.bucketExists(r, BUCKET_NAME));

s3Template.deleteBucket("test-bucket");
s3Template.deleteBucket(BUCKET_NAME);

assertThat(client.listBuckets()).satisfies(r -> this.bucketDoesNotExist(r, "test-bucket"));
assertThat(client.listBuckets()).satisfies(r -> this.bucketDoesNotExist(r, BUCKET_NAME));
}

@Test
void deletesObject() {
client.createBucket(r -> r.bucket("test-bucket"));
client.putObject(r -> r.bucket("test-bucket").key("key.txt"), RequestBody.fromString("foo"));
assertThatNoException().isThrownBy(() -> client.headObject(r -> r.bucket("test-bucket").key("key.txt")));
client.createBucket(r -> r.bucket(BUCKET_NAME));
client.putObject(r -> r.bucket(BUCKET_NAME).key("key.txt"), RequestBody.fromString("foo"));
assertThatNoException().isThrownBy(() -> client.headObject(r -> r.bucket(BUCKET_NAME).key("key.txt")));

s3Template.deleteObject("test-bucket", "key.txt");
s3Template.deleteObject(BUCKET_NAME, "key.txt");

assertThatExceptionOfType(NoSuchKeyException.class)
.isThrownBy(() -> client.headObject(r -> r.bucket("test-bucket").key("key.txt")));
.isThrownBy(() -> client.headObject(r -> r.bucket(BUCKET_NAME).key("key.txt")));
}

@Test
void deletesObjectByS3Url() {
client.putObject(r -> r.bucket("test-bucket").key("key.txt"), RequestBody.fromString("foo"));
assertThatNoException().isThrownBy(() -> client.headObject(r -> r.bucket("test-bucket").key("key.txt")));
client.putObject(r -> r.bucket(BUCKET_NAME).key("key.txt"), RequestBody.fromString("foo"));
assertThatNoException().isThrownBy(() -> client.headObject(r -> r.bucket(BUCKET_NAME).key("key.txt")));

s3Template.deleteObject("s3://test-bucket/key.txt");

assertThatExceptionOfType(NoSuchKeyException.class)
.isThrownBy(() -> client.headObject(r -> r.bucket("test-bucket").key("key.txt")));
.isThrownBy(() -> client.headObject(r -> r.bucket(BUCKET_NAME).key("key.txt")));
}

@Test
void storesObject() throws IOException {
S3Resource storedObject = s3Template.store("test-bucket", "person.json", new Person("John", "Doe"));
S3Resource storedObject = s3Template.store(BUCKET_NAME, "person.json", new Person("John", "Doe"));

ResponseInputStream<GetObjectResponse> response = client
.getObject(r -> r.bucket("test-bucket").key("person.json"));
.getObject(r -> r.bucket(BUCKET_NAME).key("person.json"));
String result = StreamUtils.copyToString(response, StandardCharsets.UTF_8);

assertThat(storedObject).isNotNull();
Expand All @@ -149,10 +147,10 @@ void storesObject() throws IOException {

@Test
void readsObject() {
client.putObject(r -> r.bucket("test-bucket").key("person.json"),
client.putObject(r -> r.bucket(BUCKET_NAME).key("person.json"),
RequestBody.fromString("{\"firstName\":\"John\",\"lastName\":\"Doe\"}"));

Person person = s3Template.read("test-bucket", "person.json", Person.class);
Person person = s3Template.read(BUCKET_NAME, "person.json", Person.class);

assertThat(person.firstName).isEqualTo("John");
assertThat(person.lastName).isEqualTo("Doe");
Expand All @@ -161,22 +159,21 @@ void readsObject() {
@Test
void uploadsFile() throws IOException {
try (InputStream is = new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8))) {
S3Resource uploadedResource = s3Template.upload("test-bucket", "file.txt", is,
S3Resource uploadedResource = s3Template.upload(BUCKET_NAME, "file.txt", is,
ObjectMetadata.builder().contentType("text/plain").build());
assertThat(uploadedResource).isNotNull();
}

ResponseInputStream<GetObjectResponse> response = client
.getObject(r -> r.bucket("test-bucket").key("file.txt"));
ResponseInputStream<GetObjectResponse> response = client.getObject(r -> r.bucket(BUCKET_NAME).key("file.txt"));
String result = StreamUtils.copyToString(response, StandardCharsets.UTF_8);
assertThat(result).isEqualTo("hello");
assertThat(response.response().contentType()).isEqualTo("text/plain");
}

@Test
void downloadsFile() throws IOException {
client.putObject(r -> r.bucket("test-bucket").key("file.txt"), RequestBody.fromString("hello"));
S3Resource resource = (S3Resource) s3Template.download("test-bucket", "file.txt");
client.putObject(r -> r.bucket(BUCKET_NAME).key("file.txt"), RequestBody.fromString("hello"));
S3Resource resource = (S3Resource) s3Template.download(BUCKET_NAME, "file.txt");
assertThat(resource.contentLength()).isEqualTo(5);
assertThat(resource.getDescription()).isNotNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SQS;

import com.amazonaws.auth.AWSCredentials;
import io.awspring.cloud.sqs.CompletableFutures;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -60,9 +59,8 @@ abstract class BaseSqsIntegrationTest {
static synchronized void beforeAll() {
if (!localstack.isRunning()) {
localstack.start();
AWSCredentials localstackCredentials = localstack.getDefaultCredentialsProvider().getCredentials();
credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials
.create(localstackCredentials.getAWSAccessKeyId(), localstackCredentials.getAWSSecretKey()));
credentialsProvider = StaticCredentialsProvider
.create(AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey()));
}
}

Expand Down