-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test containers (postgresql) (#72)
* added test containers for postgres Signed-off-by: Alessio <[email protected]> * restored workflow Signed-off-by: Alessio <[email protected]> * Update maven.yaml * Update maven.yaml * Update maven.yaml * Update maven.yaml --------- Signed-off-by: Alessio <[email protected]> Co-authored-by: Alessio <[email protected]>
- Loading branch information
1 parent
719ded9
commit 2ae74c0
Showing
19 changed files
with
125 additions
and
29 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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
85 changes: 85 additions & 0 deletions
85
src/test/java/com/devpool/thothBot/util/AbstractIntegrationTest.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,85 @@ | ||
package com.devpool.thothBot.util; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.datasource.SingleConnectionDataSource; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@Testcontainers | ||
@SpringBootTest | ||
public abstract class AbstractIntegrationTest { | ||
private static final Logger LOG = LoggerFactory.getLogger(AbstractIntegrationTest.class); | ||
|
||
private static final List<String> DB_MIGRATION = List.of( | ||
"db-migration/thoth-v1.0.0-rc1.sql", | ||
"db-migration/alter-1.0.0-rc1-TO-1.5.0.sql", | ||
"db-migration/alter-1.5.0-TO-1.7.0.sql", | ||
"db-migration/alter-1.7.0-TO-1.8.0.sql", | ||
"db-migration/alter-1.8.0-TO-1.9.1.sql", | ||
"db-migration/alter-1.9.1-TO-1.9.2.sql" | ||
); | ||
|
||
@Container | ||
private final static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16.6") | ||
.withDatabaseName("testdb") | ||
.withUsername("postgres") | ||
.withPassword("testpass"); | ||
|
||
@BeforeAll | ||
public static void setupPostgresContainer() throws IOException { | ||
LOG.info("Starting postgresql..."); | ||
postgres.start(); | ||
assertTrue(postgres.isRunning()); | ||
LOG.info("Postgres started!"); | ||
migrateDb(); | ||
} | ||
|
||
private static void migrateDb() throws IOException { | ||
LOG.info("Migrating DB..."); | ||
// Create a JDBC DataSource | ||
var dataSource = new SingleConnectionDataSource( | ||
postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword(), true | ||
); | ||
var jdbcTemplate = new JdbcTemplate(dataSource); | ||
|
||
for (String sqlFile : DB_MIGRATION) { | ||
LOG.info("Executing {}", sqlFile); | ||
Path scriptPath = Paths.get( | ||
Objects.requireNonNull( | ||
AbstractIntegrationTest.class.getClassLoader() | ||
.getResource(sqlFile)).getPath()); | ||
String sql = Files.readString(scriptPath); | ||
jdbcTemplate.execute(sql); | ||
} | ||
} | ||
|
||
@DynamicPropertySource | ||
static void configureProperties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.datasource.url", postgres::getJdbcUrl); | ||
registry.add("spring.datasource.username", postgres::getUsername); | ||
registry.add("spring.datasource.password", postgres::getPassword); | ||
} | ||
|
||
@AfterAll | ||
public static void shutdownPostgresContainer() { | ||
if (postgres != null) | ||
postgres.stop(); | ||
} | ||
} |
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