-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from kdhrubo/feature/140_load_multiple_db_scr…
…ipts testcontainer DB loading multiple scripts
- Loading branch information
Showing
6 changed files
with
28,616 additions
and
28,579 deletions.
There are no files selected for viewing
40 changes: 30 additions & 10 deletions
40
src/test/java/com/homihq/db2rest/MySQLContainerConfiguration.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 |
---|---|---|
@@ -1,20 +1,40 @@ | ||
package com.homihq.db2rest; | ||
|
||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.context.annotation.Bean; | ||
import org.testcontainers.containers.MySQLContainer; | ||
import org.testcontainers.ext.ScriptUtils; | ||
import org.testcontainers.jdbc.JdbcDatabaseDelegate; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Set; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class MySQLContainerConfiguration { | ||
@Bean | ||
@ServiceConnection | ||
public MySQLContainer<?> mySQLContainer() { | ||
var mySQLContainer = new MySQLContainer("mysql:8.2"); | ||
mySQLContainer.withUsername("mysql") | ||
.withPassword("mysql") | ||
.withInitScript("mysql/mysql-sakila.sql") | ||
.withDatabaseName("sakila").withReuse(true); | ||
return mySQLContainer; | ||
|
||
private static final Set<String> mySqlScripts = Set.of("mysql/mysql-sakila.sql", | ||
"mysql/mysql-sakila-data.sql"); | ||
|
||
private static final MySQLContainer mySQLContainer = (MySQLContainer) new MySQLContainer("mysql:8.2") | ||
.withDatabaseName("sakila") | ||
.withUsername("mysql") | ||
.withPassword("mysql") | ||
.withReuse(true); | ||
|
||
static { | ||
mySQLContainer.start(); | ||
var containerDelegate = new JdbcDatabaseDelegate(mySQLContainer, ""); | ||
mySqlScripts.forEach(initScript -> ScriptUtils.runInitScript(containerDelegate, initScript)); | ||
} | ||
|
||
@Bean("mySQLDataSource") | ||
public DataSource dataSource() { | ||
var dataSourceBuilder = DataSourceBuilder.create(); | ||
dataSourceBuilder.driverClassName("com.mysql.jdbc.Driver"); | ||
dataSourceBuilder.url(mySQLContainer.getJdbcUrl()); | ||
dataSourceBuilder.username(mySQLContainer.getUsername()); | ||
dataSourceBuilder.password(mySQLContainer.getPassword()); | ||
return dataSourceBuilder.build(); | ||
} | ||
} |
39 changes: 29 additions & 10 deletions
39
src/test/java/com/homihq/db2rest/PostgreSQLContainerConfiguration.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 |
---|---|---|
@@ -1,22 +1,41 @@ | ||
package com.homihq.db2rest; | ||
|
||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.context.annotation.Bean; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.ext.ScriptUtils; | ||
import org.testcontainers.jdbc.JdbcDatabaseDelegate; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Set; | ||
|
||
@TestConfiguration(proxyBeanMethods = false) | ||
public class PostgreSQLContainerConfiguration { | ||
|
||
@Bean | ||
@ServiceConnection | ||
public PostgreSQLContainer<?> postgreSQLContainer() { | ||
PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:15.2-alpine"); | ||
postgreSQLContainer.withUsername("postgres") | ||
.withPassword("postgres") | ||
.withInitScript("pg/postgres-sakila.sql") | ||
.withDatabaseName("postgres").withReuse(true); | ||
return postgreSQLContainer; | ||
private static final Set<String> postgresScripts = Set.of("pg/postgres-sakila.sql", | ||
"pg/postgres-sakila-data.sql"); | ||
|
||
private static final PostgreSQLContainer testPostgres = (PostgreSQLContainer) new PostgreSQLContainer("postgres:15.2-alpine") | ||
.withDatabaseName("postgres") | ||
.withUsername("postgres") | ||
.withPassword("postgres") | ||
.withReuse(true); | ||
|
||
static { | ||
testPostgres.start(); | ||
var containerDelegate = new JdbcDatabaseDelegate(testPostgres, ""); | ||
postgresScripts.forEach(initScript -> ScriptUtils.runInitScript(containerDelegate, initScript)); | ||
} | ||
|
||
@Bean("postgresDataSource") | ||
public DataSource dataSource() { | ||
var dataSourceBuilder = DataSourceBuilder.create(); | ||
dataSourceBuilder.driverClassName("org.postgresql.Driver"); | ||
dataSourceBuilder.url(testPostgres.getJdbcUrl()); | ||
dataSourceBuilder.username(testPostgres.getUsername()); | ||
dataSourceBuilder.password(testPostgres.getPassword()); | ||
return dataSourceBuilder.build(); | ||
} | ||
|
||
} |
Oops, something went wrong.