diff --git a/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/TestKinesisConsumerApplication.java b/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/TestKinesisConsumerApplication.java index c87797b0..23cd860c 100644 --- a/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/TestKinesisConsumerApplication.java +++ b/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/TestKinesisConsumerApplication.java @@ -1,27 +1,13 @@ package com.learning.aws.spring; -import com.learning.aws.spring.common.LocalStackConfig; +import com.learning.aws.spring.common.ContainersConfig; 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.springframework.context.annotation.Import; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.utility.DockerImageName; -@TestConfiguration(proxyBeanMethods = false) -@Import(LocalStackConfig.class) public class TestKinesisConsumerApplication { - @Bean - @ServiceConnection - PostgreSQLContainer postgreSQLContainer() { - return new PostgreSQLContainer<>(DockerImageName.parse("postgres:17.2-alpine")); - } - public static void main(String[] args) { SpringApplication.from(KinesisConsumerApplication::main) - .with(TestKinesisConsumerApplication.class) + .with(ContainersConfig.class) .run(args); } } diff --git a/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/AbstractIntegrationTest.java b/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/AbstractIntegrationTest.java index 1a1a372a..e43eb7d0 100644 --- a/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/AbstractIntegrationTest.java +++ b/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/AbstractIntegrationTest.java @@ -4,7 +4,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import com.fasterxml.jackson.databind.ObjectMapper; -import com.learning.aws.spring.TestKinesisConsumerApplication; import com.learning.aws.spring.repository.IpAddressEventRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; @@ -15,7 +14,7 @@ @ActiveProfiles({PROFILE_TEST}) @SpringBootTest( webEnvironment = RANDOM_PORT, - classes = {TestKinesisConsumerApplication.class}) + classes = {ContainersConfig.class, ProducerConfig.class}) @AutoConfigureWebTestClient public abstract class AbstractIntegrationTest { diff --git a/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/ContainersConfig.java b/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/ContainersConfig.java new file mode 100644 index 00000000..e8fee15d --- /dev/null +++ b/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/ContainersConfig.java @@ -0,0 +1,37 @@ +package com.learning.aws.spring.common; + +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.DynamicPropertyRegistrar; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.containers.localstack.LocalStackContainer; +import org.testcontainers.utility.DockerImageName; + +@TestConfiguration(proxyBeanMethods = false) +public class ContainersConfig { + + @Bean + LocalStackContainer localStackContainer() { + return new LocalStackContainer( + DockerImageName.parse("localstack/localstack").withTag("4.0.3")); + } + + @Bean + @ServiceConnection + PostgreSQLContainer postgreSQLContainer() { + return new PostgreSQLContainer<>(DockerImageName.parse("postgres:17.2-alpine")); + } + + @Bean + DynamicPropertyRegistrar dynamicPropertyRegistrar(LocalStackContainer localStackContainer) { + return registry -> { + registry.add("spring.cloud.aws.endpoint", localStackContainer::getEndpoint); + registry.add("spring.cloud.aws.region.static", localStackContainer::getRegion); + registry.add( + "spring.cloud.aws.credentials.access-key", localStackContainer::getAccessKey); + registry.add( + "spring.cloud.aws.credentials.secret-key", localStackContainer::getSecretKey); + }; + } +} diff --git a/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/LocalStackConfig.java b/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/ProducerConfig.java similarity index 51% rename from aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/LocalStackConfig.java rename to aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/ProducerConfig.java index 3d7d9df9..b3d00279 100644 --- a/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/LocalStackConfig.java +++ b/aws-kinesis-project/consumer/src/test/java/com/learning/aws/spring/common/ProducerConfig.java @@ -10,28 +10,11 @@ import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.Scheduled; -import org.testcontainers.containers.localstack.LocalStackContainer; -import org.testcontainers.utility.DockerImageName; @TestConfiguration(proxyBeanMethods = false) -public class LocalStackConfig { +public class ProducerConfig { - private static final Logger log = LoggerFactory.getLogger(LocalStackConfig.class); - - static LocalStackContainer localStackContainer = - new LocalStackContainer( - DockerImageName.parse("localstack/localstack").withTag("4.0.3")); - - static { - localStackContainer.start(); - System.setProperty( - "spring.cloud.aws.endpoint", localStackContainer.getEndpoint().toString()); - System.setProperty("spring.cloud.aws.region.static", localStackContainer.getRegion()); - System.setProperty( - "spring.cloud.aws.credentials.access-key", localStackContainer.getAccessKey()); - System.setProperty( - "spring.cloud.aws.credentials.secret-key", localStackContainer.getSecretKey()); - } + private static final Logger log = LoggerFactory.getLogger(ProducerConfig.class); @Bean @Scheduled(fixedRate = 6_000_000L)