Skip to content

Commit

Permalink
feat : saperate concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored Dec 27, 2024
1 parent 6dc56a3 commit 04d7150
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +14,7 @@
@ActiveProfiles({PROFILE_TEST})
@SpringBootTest(
webEnvironment = RANDOM_PORT,
classes = {TestKinesisConsumerApplication.class})
classes = {ContainersConfig.class, ProducerConfig.class})
@AutoConfigureWebTestClient
public abstract class AbstractIntegrationTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 04d7150

Please sign in to comment.