generated from navikt/dp-service-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Legg til postgres database, og faktisk lagring og henting av journalp…
…ostIder. Co-authored-by: Giao The Cung <giaothe@gmail.com>
- Loading branch information
1 parent
88aa94b
commit b27bb67
Showing
14 changed files
with
263 additions
and
18 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
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
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/no/nav/dagpenger/oppslag/journalpost/id/PostgresDataSourceBuilder.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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package no.nav.dagpenger.oppslag.journalpost.id | ||
|
||
import ch.qos.logback.core.util.OptionHelper.getEnv | ||
import ch.qos.logback.core.util.OptionHelper.getSystemProperty | ||
import com.zaxxer.hikari.HikariDataSource | ||
import org.flywaydb.core.Flyway | ||
import org.flywaydb.core.api.configuration.FluentConfiguration | ||
|
||
// Understands how to create a data source from environment variables | ||
internal object PostgresDataSourceBuilder { | ||
const val DB_USERNAME_KEY = "DB_USERNAME" | ||
const val DB_PASSWORD_KEY = "DB_PASSWORD" | ||
const val DB_DATABASE_KEY = "DB_DATABASE" | ||
const val DB_HOST_KEY = "DB_HOST" | ||
const val DB_PORT_KEY = "DB_PORT" | ||
|
||
private fun getOrThrow(key: String): String = getEnv(key) ?: getSystemProperty(key) | ||
|
||
val dataSource by lazy { | ||
HikariDataSource().apply { | ||
dataSourceClassName = "org.postgresql.ds.PGSimpleDataSource" | ||
addDataSourceProperty("serverName", getOrThrow(DB_HOST_KEY)) | ||
addDataSourceProperty("portNumber", getOrThrow(DB_PORT_KEY)) | ||
addDataSourceProperty("databaseName", getOrThrow(DB_DATABASE_KEY)) | ||
addDataSourceProperty("user", getOrThrow(DB_USERNAME_KEY)) | ||
addDataSourceProperty("password", getOrThrow(DB_PASSWORD_KEY)) | ||
maximumPoolSize = 10 | ||
minimumIdle = 1 | ||
idleTimeout = 10001 | ||
connectionTimeout = 1000 | ||
maxLifetime = 30001 | ||
} | ||
} | ||
|
||
private fun flyWayBuilder() = Flyway.configure().connectRetries(10) | ||
|
||
private val flyWayBuilder: FluentConfiguration = Flyway.configure().connectRetries(10) | ||
|
||
fun clean() = flyWayBuilder.cleanDisabled(false).dataSource(dataSource).load().clean() | ||
|
||
internal fun runMigration(initSql: String? = null): Int = | ||
flyWayBuilder | ||
.dataSource(dataSource) | ||
.initSql(initSql) | ||
.load() | ||
.migrate() | ||
.migrations | ||
.size | ||
|
||
internal fun runMigrationTo(target: String): Int = | ||
flyWayBuilder() | ||
.dataSource(dataSource) | ||
.target(target) | ||
.load() | ||
.migrate() | ||
.migrations | ||
.size | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/no/nav/dagpenger/oppslag/journalpost/id/PostgresJournalpostRepository.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package no.nav.dagpenger.oppslag.journalpost.id | ||
|
||
import kotliquery.queryOf | ||
import kotliquery.sessionOf | ||
import java.util.UUID | ||
import javax.sql.DataSource | ||
|
||
class PostgresJournalpostRepository(private val dataSource: DataSource) : JournalpostRepository { | ||
override fun lagre( | ||
søknadId: UUID, | ||
journalpostId: String, | ||
) { | ||
sessionOf(dataSource).use { session -> | ||
session.run( | ||
queryOf( | ||
//language=PostgreSQL | ||
statement = | ||
""" | ||
INSERT INTO soknad_id_journalpost_id_mapping_v1 | ||
(soknad_id, journalpost_id) | ||
VALUES | ||
(:soknad_id, :journalpost_id) | ||
ON CONFLICT (soknad_id) DO NOTHING | ||
""".trimIndent(), | ||
paramMap = | ||
mapOf( | ||
"soknad_id" to søknadId, | ||
"journalpost_id" to journalpostId, | ||
), | ||
).asUpdate, | ||
) | ||
} | ||
} | ||
|
||
override fun hent(søknadId: UUID): String { | ||
sessionOf(dataSource).use { session -> | ||
return session.run( | ||
queryOf( | ||
//language=PostgreSQL | ||
statement = | ||
""" | ||
SELECT journalpost_id | ||
FROM soknad_id_journalpost_id_mapping_v1 | ||
WHERE soknad_id = :soknad_id | ||
""".trimIndent(), | ||
paramMap = mapOf("soknad_id" to søknadId), | ||
).map { row -> | ||
row.string("journalpost_id") | ||
}.asSingle, | ||
) ?: throw JournalpostRepository.JournalpostIkkeFunnet("Fant ikke journapostId for søknadId: $søknadId") | ||
} | ||
} | ||
} |
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,6 @@ | ||
CREATE TABLE IF NOT EXISTS soknad_id_journalpost_id_mapping_v1 | ||
( | ||
soknad_id UUID PRIMARY KEY, | ||
journalpost_id TEXT NOT NULL, | ||
registrert_tidspunkt TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); |
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
72 changes: 72 additions & 0 deletions
72
src/test/kotlin/no/nav/dagpenger/oppslag/journalpost/id/Postgres.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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package no.nav.dagpenger.oppslag.journalpost.id | ||
|
||
import com.zaxxer.hikari.HikariDataSource | ||
import org.flywaydb.core.internal.configuration.ConfigUtils | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
import javax.sql.DataSource | ||
|
||
internal object Postgres { | ||
val instance by lazy { | ||
PostgreSQLContainer<Nothing>("postgres:15.5").apply { | ||
start() | ||
} | ||
} | ||
|
||
fun withMigratedDb(block: (ds: DataSource) -> Unit) { | ||
withCleanDb { | ||
PostgresDataSourceBuilder.runMigration() | ||
block(PostgresDataSourceBuilder.dataSource) | ||
} | ||
} | ||
|
||
fun withMigratedDb(): HikariDataSource { | ||
setup() | ||
PostgresDataSourceBuilder.runMigration() | ||
return PostgresDataSourceBuilder.dataSource | ||
} | ||
|
||
fun setup() { | ||
System.setProperty(PostgresDataSourceBuilder.DB_HOST_KEY, instance.host) | ||
System.setProperty( | ||
PostgresDataSourceBuilder.DB_PORT_KEY, | ||
instance.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT).toString(), | ||
) | ||
System.setProperty(PostgresDataSourceBuilder.DB_DATABASE_KEY, instance.databaseName) | ||
System.setProperty(PostgresDataSourceBuilder.DB_PASSWORD_KEY, instance.password) | ||
System.setProperty(PostgresDataSourceBuilder.DB_USERNAME_KEY, instance.username) | ||
} | ||
|
||
fun tearDown() { | ||
System.clearProperty(PostgresDataSourceBuilder.DB_PASSWORD_KEY) | ||
System.clearProperty(PostgresDataSourceBuilder.DB_USERNAME_KEY) | ||
System.clearProperty(PostgresDataSourceBuilder.DB_HOST_KEY) | ||
System.clearProperty(PostgresDataSourceBuilder.DB_PORT_KEY) | ||
System.clearProperty(PostgresDataSourceBuilder.DB_DATABASE_KEY) | ||
System.clearProperty(ConfigUtils.CLEAN_DISABLED) | ||
} | ||
|
||
fun withCleanDb(block: () -> Unit) { | ||
setup() | ||
PostgresDataSourceBuilder.clean().run { | ||
block() | ||
}.also { | ||
tearDown() | ||
} | ||
} | ||
|
||
fun withCleanDb( | ||
target: String, | ||
setup: () -> Unit, | ||
test: () -> Unit, | ||
) { | ||
this.setup() | ||
PostgresDataSourceBuilder.clean().run { | ||
PostgresDataSourceBuilder.runMigrationTo(target) | ||
setup() | ||
PostgresDataSourceBuilder.runMigration() | ||
test() | ||
}.also { | ||
tearDown() | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/kotlin/no/nav/dagpenger/oppslag/journalpost/id/PostgresJournalpostRepositoryTest.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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package no.nav.dagpenger.oppslag.journalpost.id | ||
|
||
import io.kotest.matchers.shouldBe | ||
import no.nav.dagpenger.oppslag.journalpost.id.Postgres.withMigratedDb | ||
import org.junit.jupiter.api.Test | ||
import java.util.UUID | ||
|
||
class PostgresJournalpostRepositoryTest { | ||
@Test | ||
fun `Skal kunne lagre og hente journalpostId basert på søknadId`() = | ||
withMigratedDb { ds -> | ||
val journalpostRepository = PostgresJournalpostRepository(dataSource = ds) | ||
val søknadId = UUID.randomUUID() | ||
val journalpostId = "123" | ||
journalpostRepository.lagre(søknadId, journalpostId) | ||
journalpostRepository.hent(søknadId) shouldBe journalpostId | ||
} | ||
} |
Oops, something went wrong.