-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hver enkelt test får i praksis sin egen database slik at ulike tester ikke går i beina på hverandre
- Loading branch information
1 parent
18939e1
commit a7fa94b
Showing
4 changed files
with
100 additions
and
58 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
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
91 changes: 64 additions & 27 deletions
91
sykepenger-mediators/src/test/kotlin/no/nav/helse/spleis/mediator/e2e/PostgresDatabase.kt
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,51 +1,88 @@ | ||
package no.nav.helse.spleis.mediator.e2e | ||
|
||
import com.zaxxer.hikari.HikariConfig | ||
import com.zaxxer.hikari.HikariDataSource | ||
import java.time.Duration | ||
import kotliquery.queryOf | ||
import kotliquery.sessionOf | ||
import no.nav.helse.spleis.mediator.e2e.SpleisDataSource.migratedDb | ||
import org.flywaydb.core.Flyway | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
|
||
object PostgresContainer { | ||
val instance by lazy { | ||
private val instance by lazy { | ||
PostgreSQLContainer<Nothing>("postgres:15").apply { | ||
withCreateContainerCmdModifier { command -> command.withName("spleis-mediators") } | ||
withReuse(true) | ||
withLabel("app-navn", "spleis-mediators") | ||
start() | ||
} | ||
} | ||
} | ||
|
||
object SpleisDataSource { | ||
private val instance: HikariDataSource by lazy { | ||
HikariDataSource().apply { | ||
initializationFailTimeout = 5000 | ||
username = PostgresContainer.instance.username | ||
password = PostgresContainer.instance.password | ||
jdbcUrl = PostgresContainer.instance.jdbcUrl | ||
connectionTimeout = 1000L | ||
} | ||
private val systemtilkobling by lazy { instance.createConnection("") } | ||
|
||
fun nyTilkobling(dbnavn: String = "spleis_${kotlin.random.Random.nextInt(from = 0, until = 999999)}"): SpleisDataSource { | ||
opprettDatabase(dbnavn) | ||
instance.withDatabaseName(dbnavn) | ||
return SpleisDataSource(dbnavn, HikariConfig().apply { | ||
username = instance.username | ||
password = instance.password | ||
jdbcUrl = instance.jdbcUrl | ||
}) | ||
} | ||
|
||
val migratedDb = instance.also { migrate(it) } | ||
} | ||
private fun opprettDatabase(dbnavn: String) { | ||
println("Oppretter databasen $dbnavn") | ||
systemtilkobling.createStatement().execute("create database $dbnavn") | ||
} | ||
|
||
private val tabeller = listOf("person", "person_alias", "melding") | ||
fun resetDatabase() { | ||
//migrate(migratedDb) | ||
sessionOf(migratedDb).use { session -> | ||
tabeller.forEach { table -> | ||
session.run(queryOf("truncate table $table cascade").asExecute) | ||
fun droppTilkobling(spleisDataSource: SpleisDataSource) { | ||
spleisDataSource.teardown { dbnavn -> | ||
println("Dropper databasen $dbnavn") | ||
systemtilkobling.createStatement().execute("drop database $dbnavn") | ||
} | ||
} | ||
} | ||
|
||
private fun migrate(dataSource: HikariDataSource) = | ||
Flyway.configure() | ||
.dataSource(dataSource) | ||
.cleanDisabled(false) | ||
.load() | ||
.also { it.clean() } | ||
.migrate() | ||
class SpleisDataSource( | ||
private val dbnavn: String, | ||
config: HikariConfig | ||
) { | ||
private val migrationConfig = HikariConfig() | ||
private val spleisConfig = HikariConfig() | ||
private val dataSource: HikariDataSource by lazy { HikariDataSource(spleisConfig) } | ||
private val migrationDataSource: HikariDataSource by lazy { HikariDataSource(migrationConfig) } | ||
|
||
private val flyway by lazy { | ||
Flyway.configure() | ||
.dataSource(migrationDataSource) | ||
.validateMigrationNaming(true) | ||
.load() | ||
} | ||
|
||
init { | ||
println("Oppretter datasource med dbnavn=$dbnavn") | ||
config.copyStateTo(migrationConfig) | ||
config.copyStateTo(spleisConfig) | ||
|
||
migrationConfig.maximumPoolSize = 2 // flyway klarer seg ikke med én connection visstnok | ||
migrationConfig.initializationFailTimeout = Duration.ofSeconds(10).toMillis() | ||
|
||
spleisConfig.maximumPoolSize = 2 // spleis bruker to connections ved håndtering av en melding (replay av IM tar en tilkobling) | ||
} | ||
|
||
val ds: HikariDataSource by lazy { | ||
migrate() | ||
dataSource | ||
} | ||
|
||
private fun migrate() { | ||
println("Migrerer dbnavn=$dbnavn") | ||
flyway.migrate() | ||
migrationDataSource.close() | ||
} | ||
|
||
fun teardown(dropDatabase: (String) -> Unit) { | ||
dataSource.close() | ||
dropDatabase(dbnavn) | ||
} | ||
} |