-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(test): use testcontainers for postgresql tests
- Loading branch information
Showing
25 changed files
with
330 additions
and
333 deletions.
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
86 changes: 86 additions & 0 deletions
86
...s/src/testFixtures/java/org/eclipse/edc/sql/testfixtures/PostgresqlEndToEndExtension.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,86 @@ | ||
/* | ||
* Copyright (c) 2025 Cofinity-X | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Cofinity-X - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.sql.testfixtures; | ||
|
||
import org.eclipse.edc.spi.system.configuration.Config; | ||
import org.eclipse.edc.spi.system.configuration.ConfigFactory; | ||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
|
||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Map; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* Extension to be used in end-to-end tests with Postgresql persistence | ||
*/ | ||
public class PostgresqlEndToEndExtension implements BeforeAllCallback, AfterAllCallback { | ||
|
||
private final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17.3"); | ||
|
||
public PostgresqlEndToEndExtension() { | ||
} | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) { | ||
postgres.start(); | ||
} | ||
|
||
@Override | ||
public void afterAll(ExtensionContext context) { | ||
postgres.stop(); | ||
postgres.close(); | ||
} | ||
|
||
/** | ||
* Return config suitable for EDC runtime for default database. | ||
* | ||
* @return the config. | ||
*/ | ||
public Config config() { | ||
return configFor(postgres.getDatabaseName()); | ||
} | ||
|
||
/** | ||
* Return config suitable for EDC runtime giving the database name. | ||
* | ||
* @param databaseName the database name; | ||
* @return the config. | ||
*/ | ||
public Config configFor(String databaseName) { | ||
var settings = Map.of( | ||
"edc.datasource.default.url", postgres.getJdbcUrl() + databaseName, | ||
"edc.datasource.default.user", postgres.getUsername(), | ||
"edc.datasource.default.password", postgres.getPassword(), | ||
"edc.sql.schema.autocreate", "true" | ||
); | ||
|
||
return ConfigFactory.fromMap(settings); | ||
} | ||
|
||
public void createDatabase(String name) { | ||
var jdbcUrl = postgres.getJdbcUrl() + postgres.getDatabaseName(); | ||
try (var connection = DriverManager.getConnection(jdbcUrl, postgres.getUsername(), postgres.getPassword())) { | ||
connection.createStatement().execute(format("create database %s;", name)); | ||
} catch (SQLException e) { | ||
// database could already exist | ||
} | ||
} | ||
|
||
} |
49 changes: 0 additions & 49 deletions
49
...es/src/testFixtures/java/org/eclipse/edc/sql/testfixtures/PostgresqlEndToEndInstance.java
This file was deleted.
Oops, something went wrong.
62 changes: 0 additions & 62 deletions
62
...tures/src/testFixtures/java/org/eclipse/edc/sql/testfixtures/PostgresqlLocalInstance.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.