diff --git a/aws-secretmanager-project/localstack/init-aws.sh b/aws-secretmanager-project/localstack/init-aws.sh index b4b398e5..12b66d08 100755 --- a/aws-secretmanager-project/localstack/init-aws.sh +++ b/aws-secretmanager-project/localstack/init-aws.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash awslocal secretsmanager create-secret --name /spring/secret --secret-string '{"application.username":"appuser","application.password":"secret"}' --region us-east-1 echo "List of secretsmanager secrets:" diff --git a/aws-secretmanager-project/pom.xml b/aws-secretmanager-project/pom.xml index 78fec718..f0573aae 100644 --- a/aws-secretmanager-project/pom.xml +++ b/aws-secretmanager-project/pom.xml @@ -24,8 +24,6 @@ 2.15.0 3.0.3 - 1.18.3 - ${project.build.directory}/test-results 2.40.0 8.4.2 @@ -260,7 +258,7 @@ - 1.17.0 + 1.18.1 diff --git a/aws-secretmanager-project/src/test/java/com/example/awsspring/TestApplication.java b/aws-secretmanager-project/src/test/java/com/example/awsspring/TestApplication.java new file mode 100644 index 00000000..93e35472 --- /dev/null +++ b/aws-secretmanager-project/src/test/java/com/example/awsspring/TestApplication.java @@ -0,0 +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).with(TestApplication.class).run(args); + } +} diff --git a/aws-secretmanager-project/src/test/java/com/example/awsspring/common/AbstractIntegrationTest.java b/aws-secretmanager-project/src/test/java/com/example/awsspring/common/AbstractIntegrationTest.java index 806ccf29..adf24a68 100644 --- a/aws-secretmanager-project/src/test/java/com/example/awsspring/common/AbstractIntegrationTest.java +++ b/aws-secretmanager-project/src/test/java/com/example/awsspring/common/AbstractIntegrationTest.java @@ -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-secretsmanager:/spring/secret"}) -@ImportTestcontainers(DBTestContainer.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = TestApplication.class) @AutoConfigureMockMvc +@ContextConfiguration(initializers = ConfigDataApplicationContextInitializer.class) public abstract class AbstractIntegrationTest extends LocalStackContainerConfig { @Autowired protected MockMvc mockMvc; diff --git a/aws-secretmanager-project/src/test/java/com/example/awsspring/common/DBTestContainer.java b/aws-secretmanager-project/src/test/java/com/example/awsspring/common/DBTestContainer.java index 18d77bfb..17e8232f 100644 --- a/aws-secretmanager-project/src/test/java/com/example/awsspring/common/DBTestContainer.java +++ b/aws-secretmanager-project/src/test/java/com/example/awsspring/common/DBTestContainer.java @@ -7,5 +7,5 @@ public interface DBTestContainer { @Container @ServiceConnection - PostgreSQLContainer sqlContainer = new PostgreSQLContainer<>("postgres:15.3-alpine"); + PostgreSQLContainer sqlContainer = new PostgreSQLContainer<>("postgres:16.0-alpine"); } diff --git a/aws-secretmanager-project/src/test/java/com/example/awsspring/common/LocalStackContainerConfig.java b/aws-secretmanager-project/src/test/java/com/example/awsspring/common/LocalStackContainerConfig.java index 3bf6848f..3bf19223 100644 --- a/aws-secretmanager-project/src/test/java/com/example/awsspring/common/LocalStackContainerConfig.java +++ b/aws-secretmanager-project/src/test/java/com/example/awsspring/common/LocalStackContainerConfig.java @@ -1,12 +1,18 @@ 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( @@ -14,15 +20,13 @@ public class LocalStackContainerConfig { "/etc/localstack/init/ready.d/") .waitingFor(Wait.forLogMessage(".*LocalStack initialized successfully\n", 1)); - static { - localStackContainer.start(); - // Workaround to set value early - 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-secretsmanager:/spring/secret"); } } diff --git a/aws-secretmanager-project/src/test/java/com/example/awsspring/repositories/SchemaValidationIntegrationTest.java b/aws-secretmanager-project/src/test/java/com/example/awsspring/repositories/SchemaValidationIntegrationTest.java index 9d74f6c1..0f8b7acd 100644 --- a/aws-secretmanager-project/src/test/java/com/example/awsspring/repositories/SchemaValidationIntegrationTest.java +++ b/aws-secretmanager-project/src/test/java/com/example/awsspring/repositories/SchemaValidationIntegrationTest.java @@ -1,7 +1,11 @@ package com.example.awsspring.repositories; import com.example.awsspring.common.DBTestContainer; +import com.zaxxer.hikari.HikariDataSource; +import javax.sql.DataSource; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; @@ -12,6 +16,10 @@ @ImportTestcontainers(DBTestContainer.class) public class SchemaValidationIntegrationTest { + @Autowired private DataSource dataSource; + @Test - public void testSchemaValidity() {} + public void testSchemaValidity() { + Assertions.assertThat(dataSource).isInstanceOf(HikariDataSource.class); + } }