-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-893: add postgres test container setup
- Loading branch information
Showing
5 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...-backend/src/test/java/org/dpppt/additionalinfo/backend/ws/config/PostgresDataConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...nd/src/test/java/org/dpppt/additionalinfo/backend/ws/util/SingletonPostgresContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |