BAEL-1173 - Add spring-cloud-aws module (#3046)

This commit is contained in:
Vivek Kumar
2017-11-20 21:54:03 +05:30
committed by Grzegorz Piwowarek
parent 6211154062
commit dba132f7de
18 changed files with 832 additions and 0 deletions
@@ -0,0 +1,73 @@
package com.baeldung.spring.cloud.aws;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.junit.BeforeClass;
/**
* This class is needed only for testing. This is because we need to
* create AWS resources before Spring's Application context is created
* in a {@link BeforeClass} method. Since Autowired dependencies don't
* work in static context, we will use this class for AWS clients.
*/
public class SpringCloudAwsTestUtil {
private static String awsAccessKey;
private static String awsSecretKey;
private static String defaultRegion;
static {
try {
InputStream is = SpringCloudAwsTestUtil.class.getResourceAsStream("/application.properties");
Properties properties = new Properties();
properties.load(is);
awsAccessKey = properties.getProperty("cloud.aws.credentials.accessKey");
awsSecretKey = properties.getProperty("cloud.aws.credentials.secretKey");
defaultRegion = properties.getProperty("cloud.aws.region.static");
} catch (IOException e) {
e.printStackTrace();
}
}
public static AWSCredentials awsCredentials() {
return new BasicAWSCredentials(awsAccessKey, awsSecretKey);
}
public static AWSCredentialsProvider awsCredentialsProvider() {
return new AWSStaticCredentialsProvider(awsCredentials());
}
public static AmazonS3 amazonS3() {
return AmazonS3ClientBuilder.standard()
.withCredentials(awsCredentialsProvider())
.withRegion(defaultRegion)
.build();
}
public static AmazonSNS amazonSNS() {
return AmazonSNSClientBuilder.standard()
.withCredentials(awsCredentialsProvider())
.withRegion(defaultRegion)
.build();
}
public static AmazonSQS amazonSQS() {
return AmazonSQSClientBuilder.standard()
.withCredentials(awsCredentialsProvider())
.withRegion(defaultRegion)
.build();
}
}
@@ -0,0 +1,46 @@
package com.baeldung.spring.cloud.aws.rds;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringCloudRDSIntegrationTest {
@Autowired
DataSource dataSource;
@Test
public void whenDataSourceCreated_thenSuccess() {
assertThat(dataSource).isNotNull();
}
@Test
public void givenDataSource_whenConnectionCreated_thenSuccess() throws SQLException {
Connection connection = dataSource.getConnection();
assertThat(connection).isNotNull();
}
@Test
public void givenConnection_whenQueryExecuted_thenSuccess() throws SQLException {
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT 1");
while (resultSet.next()) {
int result = resultSet.getInt(1);
assertThat(result).isEqualTo(1);
}
connection.close();
}
}
@@ -0,0 +1,101 @@
package com.baeldung.spring.cloud.aws.s3;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.baeldung.spring.cloud.aws.SpringCloudAwsTestUtil;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:application-test.properties")
public class SpringCloudS3IntegrationTest {
@Autowired
private SpringCloudS3 springCloudS3;
private static String bucketName;
private static String testFileToDownload;
private static String testFileToUpload;
private static String[] filesWithSimilarName;
private static List<File> similarNameFiles;
@BeforeClass
public static void setupResources() throws IOException {
bucketName = UUID.randomUUID().toString();
testFileToDownload = "test-file-download.txt";
testFileToUpload = "test-file-upload.txt";
filesWithSimilarName = new String[] { "foo/hello-apple.txt", "foo/hello-orange.txt", "bar/hello-grapes.txt", };
similarNameFiles = new ArrayList<>();
for (String name : filesWithSimilarName) {
similarNameFiles.add(new File(name.substring(0, name.lastIndexOf("/") + 1)));
}
Files.write(Paths.get(testFileToUpload), "Hello World Uploaded!".getBytes());
AmazonS3 amazonS3 = SpringCloudAwsTestUtil.amazonS3();
amazonS3.createBucket(bucketName);
amazonS3.putObject(bucketName, testFileToDownload, "Hello World");
for (String s3Key : filesWithSimilarName) {
amazonS3.putObject(bucketName, s3Key, "Hello World");
}
}
@Test
public void whenS3ObjectDownloaded_thenSuccess() throws IOException {
String s3Url = "s3://" + bucketName + "/" + testFileToDownload;
springCloudS3.downloadS3Object(s3Url);
assertThat(new File(testFileToDownload)).exists();
}
@Test
public void whenS3ObjectUploaded_thenSuccess() throws IOException {
String s3Url = "s3://" + bucketName + "/" + testFileToUpload;
File file = new File(testFileToUpload);
springCloudS3.uploadFileToS3(file, s3Url);
}
@Test
public void whenMultipleS3ObjectsDownloaded_thenSuccess() throws IOException {
String s3Url = "s3://" + bucketName + "/**/hello-*.txt";
springCloudS3.downloadMultipleS3Objects(s3Url);
similarNameFiles.forEach(f -> assertThat(f).exists());
}
@AfterClass
public static void cleanUpResources() {
AmazonS3 amazonS3 = SpringCloudAwsTestUtil.amazonS3();
ListObjectsV2Result listObjectsV2Result = amazonS3.listObjectsV2(bucketName);
for (S3ObjectSummary objectSummary : listObjectsV2Result.getObjectSummaries()) {
amazonS3.deleteObject(bucketName, objectSummary.getKey());
}
amazonS3.deleteBucket(bucketName);
new File(testFileToDownload).delete();
new File(testFileToUpload).delete();
similarNameFiles.forEach(File::delete);
}
}
@@ -0,0 +1,38 @@
package com.baeldung.spring.cloud.aws.sns;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.aws.messaging.endpoint.NotificationStatus;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
public class SNSEndpointControllerUnitTest {
SNSEndpointController snsEndpointController;
@Before
public void setUp() {
snsEndpointController = new SNSEndpointController();
}
@Test
public void whenReceivedNotificationInvoked_thenSuccess() {
snsEndpointController.receiveNotification("Message", "Subject");
}
@Test
public void whenConfirmUnsubscribeReturned_thenSuccess() {
NotificationStatus notificationStatus = mock(NotificationStatus.class);
doNothing().when(notificationStatus).confirmSubscription();
snsEndpointController.confirmUnsubscribeMessage(notificationStatus);
}
@Test
public void whenConfirmSubscriptionReturned_thenSuccess() {
NotificationStatus notificationStatus = mock(NotificationStatus.class);
doNothing().when(notificationStatus).confirmSubscription();
snsEndpointController.confirmSubscriptionMessage(notificationStatus);
}
}
@@ -0,0 +1,61 @@
package com.baeldung.spring.cloud.aws.sns;
import java.util.UUID;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.model.CreateTopicResult;
import com.baeldung.spring.cloud.aws.SpringCloudAwsTestUtil;
import com.baeldung.spring.cloud.aws.sqs.Greeting;
@SpringBootTest
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:application-test.properties")
public class SpringCloudSNSIntegrationTest {
@Autowired
private SNSMessageSender snsMessageSender;
private static String topicName;
private static String topicArn;
@BeforeClass
public static void setupAwsResources() {
topicName = UUID.randomUUID().toString();
AmazonSNS amazonSNS = SpringCloudAwsTestUtil.amazonSNS();
CreateTopicResult result = amazonSNS.createTopic(topicName);
topicArn = result.getTopicArn();
}
@Test
public void whenMessagePublished_thenSuccess() {
String subject = "Test Message";
String message = "Hello World";
snsMessageSender.send(topicName, message, subject);
}
@Test
public void whenConvertedMessagePublished_thenSuccess() {
String subject = "Test Message";
Greeting message = new Greeting("Helo", "World");
snsMessageSender.send(topicName, message, subject);
}
@AfterClass
public static void cleanupAwsResources() {
AmazonSNS amazonSNS = SpringCloudAwsTestUtil.amazonSNS();
amazonSNS.deleteTopic(topicArn);
}
}
@@ -0,0 +1,63 @@
package com.baeldung.spring.cloud.aws.sqs;
public class Greeting {
private String message;
private String name;
public Greeting() {
}
public Greeting(String mesage, String name) {
this.message = mesage;
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((message == null) ? 0 : message.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Greeting other = (Greeting) obj;
if (message == null) {
if (other.message != null)
return false;
} else if (!message.equals(other.message))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
@@ -0,0 +1,135 @@
package com.baeldung.spring.cloud.aws.sqs;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.CreateQueueResult;
import com.amazonaws.services.sqs.model.PurgeQueueRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import com.baeldung.spring.cloud.aws.SpringCloudAwsTestUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Lazy;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:application-test.properties")
public class SpringCloudSQSIntegrationTest {
private static final Logger logger = LoggerFactory.getLogger(SpringCloudSQSIntegrationTest.class);
@Autowired
@Lazy
private SpringCloudSQS springCloudSQS;
private static String receiveQueueName;
private static String receiveQueueUrl;
private static String sendQueueName;
private static String sendQueueURl;
@BeforeClass
public static void setupAwsResources() {
sendQueueName = UUID.randomUUID().toString();
receiveQueueName = SpringCloudSQS.QUEUE_NAME;
AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();
CreateQueueResult receiveQueue = amazonSQS.createQueue(receiveQueueName);
receiveQueueUrl = receiveQueue.getQueueUrl();
CreateQueueResult sendQueue = amazonSQS.createQueue(sendQueueName);
sendQueueURl = sendQueue.getQueueUrl();
}
@Test
public void whenMessageSentAndVerified_thenSuccess() throws InterruptedException {
String message = "Hello World";
springCloudSQS.send(sendQueueName, message);
AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();
ReceiveMessageRequest request = new ReceiveMessageRequest(sendQueueURl);
request.setMaxNumberOfMessages(1);
ReceiveMessageResult result = null;
do {
result = amazonSQS.receiveMessage(request);
if (result.getMessages().size() == 0) {
logger.info("Message not received at first time, waiting for 1 second");
}
} while (result.getMessages().size() == 0);
assertThat(result.getMessages().get(0).getBody()).isEqualTo(message);
// Delete message so that it doen't interfere with other test
amazonSQS.deleteMessage(sendQueueURl, result.getMessages().get(0).getReceiptHandle());
}
@Test
public void whenConvertedMessageSentAndVerified_thenSuccess() throws InterruptedException, IOException {
Greeting message = new Greeting("Hello", "World");
springCloudSQS.send(sendQueueName, message);
AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();
ReceiveMessageRequest request = new ReceiveMessageRequest(sendQueueURl);
request.setMaxNumberOfMessages(1);
ReceiveMessageResult result = null;
do {
result = amazonSQS.receiveMessage(request);
if (result.getMessages().size() == 0) {
logger.info("Message not received at first time, waiting for 1 second");
}
} while (result.getMessages().size() == 0);
assertThat(new ObjectMapper().readValue(result.getMessages().get(0).getBody(), Greeting.class)).isEqualTo(message);
// Delete message so that it doen't interfere with other test
amazonSQS.deleteMessage(sendQueueURl, result.getMessages().get(0).getReceiptHandle());
}
@Test
public void givenMessageSent_whenMessageReceived_thenSuccess() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(5);
springCloudSQS.setCountDownLatch(countDownLatch);
AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();
for (int i = 0; i < 5; i++) {
amazonSQS.sendMessage(receiveQueueUrl, "Hello World " + i);
logger.info("Sent message {}, waiting for 1 second", i + 1);
Thread.sleep(1000L);
}
countDownLatch.await();
}
@AfterClass
public static void cleanupAwsResources() {
AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();
PurgeQueueRequest receiveQueuePurge = new PurgeQueueRequest(receiveQueueUrl);
amazonSQS.purgeQueue(receiveQueuePurge);
amazonSQS.deleteQueue(receiveQueueUrl);
PurgeQueueRequest sendQueuePurge = new PurgeQueueRequest(sendQueueURl);
amazonSQS.purgeQueue(sendQueuePurge);
amazonSQS.deleteQueue(sendQueueURl);
}
}
@@ -0,0 +1,4 @@
# Don't try to create DataSouce when running tests which don't need a DataSource
spring.autoconfigure.exclude=\
org.springframework.cloud.aws.autoconfigure.jdbc.AmazonRdsDatabaseAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration