Skip to content

Commit

Permalink
PP-893: add postgres test container setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ubhaller committed Mar 11, 2021
1 parent f4b1b4f commit 172c2ce
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
6 changes: 6 additions & 0 deletions dpppt-additionalinfo-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.15.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles({"dev"})
@ActiveProfiles({"postgres-test"})
public abstract class BaseControllerTest {
@Autowired protected ObjectMapper objectMapper;
protected MockMvc mockMvc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ActiveProfiles;

@ComponentScan(basePackages = {"org.dpppt.additionalinfo.backend.ws.config"})
@SpringBootApplication
@ActiveProfiles("postgres")
public class TestApplication {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.dpppt.additionalinfo.backend.ws.config;

import javax.sql.DataSource;
import org.dpppt.additionalinfo.backend.ws.util.SingletonPostgresContainer;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("postgres-test")
public class PostgresDataConfig extends WSDevConfig {
@Bean
@Override
public DataSource dataSource() {
final SingletonPostgresContainer instance = SingletonPostgresContainer.getInstance();
instance.start();

return DataSourceBuilder.create()
.driverClassName(instance.getDriverClassName())
.url(instance.getJdbcUrl())
.username(instance.getUsername())
.password(instance.getPassword())
.build();
}

@Bean
public String dbType() {
return "pgsql";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.dpppt.additionalinfo.backend.ws.util;

import org.testcontainers.containers.PostgreSQLContainer;

// JVM handles shut down
public class SingletonPostgresContainer {

private static final String IMAGE_VERSION = "postgres:12";
private static final String DB_URL = "DB_URL";
private static final String DB_PORT = "DB_PORT";
private static final String DB_USERNAME = "DB_USERNAME";
private static final String DB_PASSWORD = "DB_PASSWORD";
private static SingletonPostgresContainer INSTANCE;

private PostgreSQLContainer<?> container;

private String jdbcUrl;
private String databaseName;
private String username;
private String password;

private SingletonPostgresContainer() {
this.databaseName = "test-db";
container =
new PostgreSQLContainer<>(IMAGE_VERSION)
.withUsername("test")
.withPassword("test")
.withDatabaseName(this.databaseName);
}

public static SingletonPostgresContainer getInstance() {
if (INSTANCE == null) {
INSTANCE = new SingletonPostgresContainer();
}
return INSTANCE;
}

public void start() {
String baseUrl = "jdbc:postgresql://%s:%s/test-db";
String dbUrl = System.getenv(DB_URL);
String dbPort = System.getenv(DB_PORT);
this.jdbcUrl = String.format(baseUrl, dbUrl, dbPort);
this.username = System.getenv(DB_USERNAME);
this.password = System.getenv(DB_PASSWORD);

// avoid start container if is already up
if (System.getenv("SKIP_POSTGRES_CONTAINER") == null) {
container.start();
this.jdbcUrl = container.getJdbcUrl();
this.username = container.getUsername();
this.password = container.getPassword();
}

System.setProperty(DB_URL, this.jdbcUrl);
System.setProperty(DB_USERNAME, this.username);
System.setProperty(DB_PASSWORD, this.password);
}

public String getDriverClassName() {
return container.getDriverClassName();
}

public String getJdbcUrl() {
return jdbcUrl;
}

public String getDatabaseName() {
return databaseName;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}
}

0 comments on commit 172c2ce

Please sign in to comment.