Skip to content

Commit

Permalink
feat: using ConfigDataApplicationContextInitializer instead of System…
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 6, 2023
1 parent 116af53 commit bf373dd
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 37 deletions.
18 changes: 9 additions & 9 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ This repository contains AWS samples that can be integrated with Spring boot, co

The following table list all sample codes related to the spring boot integrations with AWS capabilities

| **SERVICE** | **DESCRIPTION** |
|---------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
| [cloudwatch](./aws-cloudwatch-project) | This Sample demonstrates how to use the cloudwatch and see the metrics |
| [dynamodb](./aws-dynamodb-project) | This Sample demonstrates how to connect with dynamodb using AWS SDK v2 and perform CRUD Operations |
| [kinesis](./aws-kinesis-project) | This Sample demonstrates how to use Kinesis as a publisher and Subscriber using Spring Cloud Stream |
| [parameterstore](./aws-parameterstore-project) | This Sample demonstrates how to read the configuration values from parameter store and use it creating beans |
| [s3](./aws-s3-project) | This Sample demonstrated how to upload and download the files in S3 Bucket using AWS SDK V2 |
| **SERVICE** | **DESCRIPTION** |
|------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
| [cloudwatch](./aws-cloudwatch-project) | This Sample demonstrates how to use the cloudwatch and see the metrics |
| [dynamodb](./aws-dynamodb-project) | This Sample demonstrates how to connect with dynamodb using AWS SDK v2 and perform CRUD Operations |
| [kinesis](./aws-kinesis-project) | This Sample demonstrates how to use Kinesis as a publisher and Subscriber using Spring Cloud Stream |
| [parameterstore](./aws-parameterstore-project) | This Sample demonstrates how to read the configuration values from parameter store and use it creating beans |
| [s3](./aws-s3-project) | This Sample demonstrated how to upload and download the files in S3 Bucket using AWS SDK V2 |
| [secretmanager](./aws-secretmanager-project) | This Sample demonstrates how to read the configuration values from Secret Manager and use it creating beans |
| [ses](./aws-ses-project) | This Sample demonstrates how to send email using Simple Email Service with and without attachments using AWS SDK V2 |
| [sqs](./aws-sqs-project) | This Sample demonstrated how to send and receive Simple messages using AWS SDK V2 |
| [ses](./aws-ses-project) | This Sample demonstrates how to send email using Simple Email Service with and without attachments using AWS SDK V2 |
| [sqs](./aws-sqs-project) | This Sample demonstrated how to send and receive Simple messages using AWS SDK V2 |

2 changes: 1 addition & 1 deletion aws-parameterstore-project/localstack/init-aws.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

awslocal ssm put-parameter --name /spring/config/application.password --value secret --type String --region us-east-1
awslocal ssm put-parameter --name /spring/config/application.username --value appuser --type String --region us-east-1
Expand Down
2 changes: 1 addition & 1 deletion aws-parameterstore-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
<configuration>
<java>
<googleJavaFormat>
<version>1.15.0</version>
<version>1.18.1</version>
<style>AOSP</style>
</googleJavaFormat>
</java>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package com.example.awsspring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.PostgreSQLContainer;

@TestConfiguration(proxyBeanMethods = false)
public class TestApplication {

@Bean
@ServiceConnection
PostgreSQLContainer<?> sqlContainer() {
return new PostgreSQLContainer<>("postgres:16.0-alpine");
}

public static void main(String[] args) {
SpringApplication.from(Application::main).run(args);
SpringApplication.from(Application::main).with(TestApplication.class).run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
import static com.example.awsspring.utils.AppConstants.PROFILE_TEST;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import com.example.awsspring.TestApplication;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;

@ActiveProfiles({PROFILE_TEST})
@SpringBootTest(
webEnvironment = RANDOM_PORT,
properties = {"spring.config.import=aws-parameterstore:/spring/config/"})
@ImportTestcontainers(MyDBContainer.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = TestApplication.class)
@AutoConfigureMockMvc
@ContextConfiguration(initializers = ConfigDataApplicationContextInitializer.class)
public abstract class AbstractIntegrationTest extends LocalStackContainerConfig {

@Autowired protected MockMvc mockMvc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package com.example.awsspring.common;

import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

@Testcontainers
public class LocalStackContainerConfig {

@Container
private static final LocalStackContainer localStackContainer =
new LocalStackContainer(DockerImageName.parse("localstack/localstack").withTag("2.3.2"))
.withCopyFileToContainer(
MountableFile.forHostPath("localstack/"),
"/etc/localstack/init/ready.d/")
.waitingFor(Wait.forLogMessage(".*Initialized\\.\n", 1));

static {
localStackContainer.start();
System.setProperty(
"spring.cloud.aws.endpoint", localStackContainer.getEndpoint().toString());
System.setProperty(
"spring.cloud.aws.credentials.access-key", localStackContainer.getAccessKey());
System.setProperty(
"spring.cloud.aws.credentials.secret-key", localStackContainer.getSecretKey());
System.setProperty("spring.cloud.aws.region.static", localStackContainer.getRegion());
@DynamicPropertySource
static void properties(DynamicPropertyRegistry registry) {
registry.add(
"spring.cloud.aws.endpoint", () -> localStackContainer.getEndpoint().toString());
registry.add("spring.cloud.aws.credentials.access-key", localStackContainer::getAccessKey);
registry.add("spring.cloud.aws.credentials.secret-key", localStackContainer::getSecretKey);
registry.add("spring.cloud.aws.region.static", localStackContainer::getRegion);
registry.add("spring.config.import", () -> "aws-parameterstore:/spring/config/");
}
}

This file was deleted.

0 comments on commit bf373dd

Please sign in to comment.