Skip to content

Commit

Permalink
fix project
Browse files Browse the repository at this point in the history
  • Loading branch information
rieckpil committed Jan 22, 2024
1 parent d591b5e commit b0595cd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 45 deletions.
8 changes: 6 additions & 2 deletions spring-boot-aws-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<description>Spring Boot integration tests with AWS services</description>

<properties>
<awspring.version>2.3.2</awspring.version>
<awspring.version>3.1.0</awspring.version>
</properties>

<dependencies>
Expand All @@ -26,9 +26,13 @@
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
<artifactId>spring-cloud-aws-starter-sqs</artifactId>
</dependency>

<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-s3</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.rieckpil.blog;

import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.awspring.cloud.messaging.core.QueueMessagingTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
Expand All @@ -17,8 +15,4 @@ public MappingJackson2MessageConverter mappingJackson2MessageConverter(ObjectMap
return jackson2MessageConverter;
}

@Bean
public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSQS) {
return new QueueMessagingTemplate(amazonSQS);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package de.rieckpil.blog;

import com.amazonaws.services.s3.AmazonS3;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.awspring.cloud.messaging.listener.annotation.SqsListener;
import io.awspring.cloud.sqs.annotation.SqsListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

@Component
public class SimpleMessageListener {

private static final Logger LOG = LoggerFactory.getLogger(SimpleMessageListener.class);

private final AmazonS3 amazonS3;
private final S3Client amazonS3;
private final ObjectMapper objectMapper;
private final String orderEventBucket;

public SimpleMessageListener(
@Value("${event-processing.order-event-bucket}") String orderEventBucket,
AmazonS3 amazonS3,
S3Client amazonS3,
ObjectMapper objectMapper) {
this.amazonS3 = amazonS3;
this.objectMapper = objectMapper;
Expand All @@ -32,8 +34,7 @@ public SimpleMessageListener(
public void processMessage(@Payload OrderEvent orderEvent) throws JsonProcessingException {
LOG.info("Incoming order: '{}'", orderEvent);

amazonS3.putObject(orderEventBucket, orderEvent.getId(), objectMapper.writeValueAsString(orderEvent));

amazonS3.putObject(PutObjectRequest.builder().bucket(orderEventBucket).key(orderEvent.getId()).build(), RequestBody.fromBytes(objectMapper.writeValueAsString(orderEvent).getBytes()));
LOG.info("Successfully uploaded order to S3");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package de.rieckpil.blog;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.UUID;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import io.awspring.cloud.messaging.core.QueueMessagingTemplate;
import io.awspring.cloud.s3.S3Exception;
import io.awspring.cloud.sqs.operations.SqsTemplate;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -19,6 +16,8 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.given;
Expand Down Expand Up @@ -55,15 +54,15 @@ static void overrideConfiguration(DynamicPropertyRegistry registry) {
}

@Autowired
private AmazonS3 amazonS3;
private S3Client amazonS3;

@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
private SqsTemplate sqsTemplate;

@Test
void messageShouldBeUploadedToBucketOnceConsumedFromQueue() {

queueMessagingTemplate.send(QUEUE_NAME, new GenericMessage<>("""
sqsTemplate.send(QUEUE_NAME, new GenericMessage<>("""
{
"id": "42",
"message": "Please delivery ASAP",
Expand All @@ -74,9 +73,9 @@ void messageShouldBeUploadedToBucketOnceConsumedFromQueue() {
""", Map.of("contentType", "application/json")));

given()
.ignoreException(AmazonS3Exception.class)
.ignoreException(S3Exception.class)
.await()
.atMost(5, SECONDS)
.untilAsserted(() -> assertNotNull(amazonS3.getObject(BUCKET_NAME, "42")));
.untilAsserted(() -> assertNotNull(amazonS3.getObject(GetObjectRequest.builder().bucket(BUCKET_NAME).key("42").build())));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package de.rieckpil.blog;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder;
import io.awspring.cloud.messaging.core.QueueMessagingTemplate;
import io.awspring.cloud.s3.S3Exception;
import io.awspring.cloud.sqs.operations.SqsTemplate;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,6 +15,11 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.sqs.SqsClient;

import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -62,27 +60,27 @@ static void overrideConfiguration(DynamicPropertyRegistry registry) {
static class AwsTestConfig {

@Bean
public AmazonS3 amazonS3() {
return AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(localStack.getAccessKey(), localStack.getSecretKey())))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(localStack.getEndpointOverride(S3).toString(), "eu-central-1"))
public S3Client amazonS3() {
return S3Client.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(localStack.getAccessKey(), localStack.getSecretKey())))
.endpointOverride(localStack.getEndpointOverride(S3))
.build();
}

@Bean
public AmazonSQSAsync amazonSQS() {
return AmazonSQSAsyncClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(localStack.getAccessKey(), localStack.getSecretKey())))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(localStack.getEndpointOverride(SQS).toString(), "eu-central-1"))
public SqsClient amazonSQS() {
return SqsClient.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(localStack.getAccessKey(), localStack.getSecretKey())))
.endpointOverride(localStack.getEndpointOverride(SQS))
.build();
}
}

@Autowired
private AmazonS3 amazonS3;
private S3Client amazonS3;

@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
private SqsTemplate queueMessagingTemplate;

@Test
void messageShouldBeUploadedToBucketOnceConsumedFromQueue() {
Expand All @@ -98,9 +96,9 @@ void messageShouldBeUploadedToBucketOnceConsumedFromQueue() {
""", Map.of("contentType", "application/json")));

given()
.ignoreException(AmazonS3Exception.class)
.ignoreException(S3Exception.class)
.await()
.atMost(5, SECONDS)
.untilAsserted(() -> assertNotNull(amazonS3.getObject(BUCKET_NAME, "13")));
.untilAsserted(() -> assertNotNull(amazonS3.getObject(GetObjectRequest.builder().bucket(BUCKET_NAME).key("13").build())));
}
}

0 comments on commit b0595cd

Please sign in to comment.