All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.gc.aafc.dina.testsupport.PostgresTestContainerInitializer Maven / Gradle / Ivy

package ca.gc.aafc.dina.testsupport;

import java.util.Objects;
import java.util.Optional;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
 * Initializes the Postgres TestContainer if the "embedded.postgresql.enabled" property is true.
 * 
 * Use this initializer in integration tests by adding this annotation to your test class:
 * 
 * {@code @ContextConfiguration(initializers = { PostgresTestContainerInitializer.class })}
 * 
*/ @SuppressFBWarnings({"LI_LAZY_INIT_UPDATE_STATIC", "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD"}) public class PostgresTestContainerInitializer implements ApplicationContextInitializer { private static PostgreSQLContainer sqlContainer = null; @Override public void initialize(ConfigurableApplicationContext ctx) { ConfigurableEnvironment env = ctx.getEnvironment(); // If there is a postgres container enabled, point Spring's datasource properties to it: if (!Objects.equals(env.getProperty("embedded.postgresql.enabled"), "false")) { if (sqlContainer == null) { DockerImageName myImage = DockerImageName .parse(Optional.ofNullable(env.getProperty("embedded.postgresql.image")).orElse("postgres")) .asCompatibleSubstituteFor("postgres"); sqlContainer = new PostgreSQLContainer<>(myImage) .withDatabaseName( Optional.ofNullable(env.getProperty("embedded.postgresql.database")) .orElse("integration-tests-db")) .withUrlParam("currentSchema", Optional.ofNullable(env.getProperty("embedded.postgresql.schema")) .orElse("public")) .withUsername("sa") .withPassword("sa"); Optional.ofNullable(env.getProperty("embedded.postgresql.max_connection")) .ifPresent( max -> sqlContainer.setCommand("postgres", "-c", "max_connections=" + max)); sqlContainer.withInitScript(env.getProperty("embedded.postgresql.init-script-file")); sqlContainer.start(); } TestPropertyValues.of( "spring.datasource.url=" + sqlContainer.getJdbcUrl() ).applyTo(env); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy