Skip to content

Commit

Permalink
attempt to use OOTB service connection for S3
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored Dec 27, 2024
1 parent b0372ae commit 8f34249
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.learning.awspring;

import com.learning.awspring.common.ContainerConfig;
import com.learning.awspring.common.LocalStackContainerConfig;
import com.learning.awspring.common.SQLContainerConfig;
import org.springframework.boot.SpringApplication;

public class TestS3Application {

public static void main(String[] args) {
SpringApplication.from(S3Application::main).with(ContainerConfig.class).run(args);
SpringApplication.from(S3Application::main)
.with(SQLContainerConfig.class, LocalStackContainerConfig.class)
.run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.springframework.test.web.servlet.MockMvc;

@ActiveProfiles({PROFILE_TEST})
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = ContainerConfig.class)
@SpringBootTest(
webEnvironment = RANDOM_PORT,
classes = {SQLContainerConfig.class, LocalStackContainerConfig.class})
@AutoConfigureMockMvc
public abstract class AbstractIntegrationTest {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.learning.awspring.common;

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.localstack.LocalStackContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

@TestConfiguration(proxyBeanMethods = false)
public class LocalStackContainerConfig {

@Bean
@ServiceConnection
LocalStackContainer localstackContainer() {
return new LocalStackContainer(
DockerImageName.parse("localstack/localstack").withTag("4.0.3"))
.withCopyFileToContainer(
MountableFile.forHostPath(".localstack/"), "/etc/localstack/init/ready.d/")
.waitingFor(Wait.forLogMessage(".*LocalStack initialized successfully\n", 1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.learning.awspring.common;

import org.springframework.boot.devtools.restart.RestartScope;
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;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class SQLContainerConfig {

@Bean
@ServiceConnection
@RestartScope
PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:17.2-alpine"))
.withReuse(true);
}

// @Bean
// DynamicPropertyRegistrar dynamicPropertyRegistrar(LocalStackContainer localStackContainer) {
// return registry -> {
// 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.cloud.aws.endpoint", localStackContainer::getEndpoint);
// };
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.learning.awspring.common.SQLContainerConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
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.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

@DataJpaTest(
properties = {
"spring.jpa.hibernate.ddl-auto=validate",
"spring.test.database.replace=none",
"spring.datasource.url=jdbc:tc:postgresql:16.2-alpine:///db"
})
@DataJpaTest(properties = {"spring.jpa.hibernate.ddl-auto=validate"})
@AutoConfigureTestDatabase
@Import(SQLContainerConfig.class)
class SchemaValidationTest {

@Autowired private DataSource dataSource;
Expand Down

0 comments on commit 8f34249

Please sign in to comment.