Skip to content

Commit

Permalink
feat : using ConfigDataApplicationContextInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 6, 2023
1 parent bf373dd commit 30f2e9e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 21 deletions.
2 changes: 1 addition & 1 deletion aws-secretmanager-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 secretsmanager create-secret --name /spring/secret --secret-string '{"application.username":"appuser","application.password":"secret"}' --region us-east-1
echo "List of secretsmanager secrets:"
Expand Down
4 changes: 1 addition & 3 deletions aws-secretmanager-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
<commons-io.version>2.15.0</commons-io.version>
<spring-cloud-aws.version>3.0.3</spring-cloud-aws.version>

<testcontainers.version>1.18.3</testcontainers.version>

<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
<spotless.version>2.40.0</spotless.version>
<dependency-check-maven.version>8.4.2</dependency-check-maven.version>
Expand Down Expand Up @@ -260,7 +258,7 @@
<configuration>
<java>
<googleJavaFormat>
<version>1.17.0</version>
<version>1.18.1</version>
<style>AOSP</style>
</googleJavaFormat>
<formatAnnotations />
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
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-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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Original file line number Diff line number Diff line change
@@ -1,28 +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(".*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");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}

0 comments on commit 30f2e9e

Please sign in to comment.