Skip to content

Commit

Permalink
Ny måte å koble seg til db
Browse files Browse the repository at this point in the history
Co-authored-by: Giao The Cung <giaothe@gmail.com>
  • Loading branch information
MariusEriksen and gtcno committed May 22, 2024
1 parent 91fcbcc commit 7e76385
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import java.util.UUID

private val logger = KotlinLogging.logger { }

class InnsendingFerdigstiltMottak(rapidsConnection: RapidsConnection, private val journalpostRepository: JournalpostRepository) :
River.PacketListener {
class InnsendingFerdigstiltMottak(
rapidsConnection: RapidsConnection,
private val journalpostRepository: JournalpostRepository,
) : River.PacketListener {
init {
River(rapidsConnection).apply {
validate { it.demandValue("@event_name", "innsending_ferdigstilt") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ 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 mu.KotlinLogging
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"
const val DB_URL_KEY = "DB_URL"

private fun getOrThrow(key: String): String = getEnv(key) ?: getSystemProperty(key)

private val logger = KotlinLogging.logger {}
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))
jdbcUrl =
getOrThrow(DB_URL_KEY).ensurePrefix("jdbc:postgresql://").stripCredentials().also {
logger.info("Connecting to $it")
}
username = getOrThrow(DB_USERNAME_KEY)
password = getOrThrow(DB_PASSWORD_KEY)
maximumPoolSize = 10
minimumIdle = 1
idleTimeout = 10001
Expand Down Expand Up @@ -56,3 +56,12 @@ internal object PostgresDataSourceBuilder {
.migrations
.size
}

private fun String.stripCredentials() = this.replace(Regex("://.*:.*@"), "://")

private fun String.ensurePrefix(prefix: String) =
if (this.startsWith(prefix)) {
this
} else {
prefix + this.substringAfter("//")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 {
Expand All @@ -12,10 +11,10 @@ internal object Postgres {
}
}

fun withMigratedDb(block: (ds: DataSource) -> Unit) {
fun withMigratedDb(block: () -> Unit) {
withCleanDb {
PostgresDataSourceBuilder.runMigration()
block(PostgresDataSourceBuilder.dataSource)
block()
}
}

Expand All @@ -26,22 +25,16 @@ internal object Postgres {
}

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(ConfigUtils.CLEAN_DISABLED, "false")
System.setProperty(PostgresDataSourceBuilder.DB_URL_KEY, instance.jdbcUrl)
System.setProperty(PostgresDataSourceBuilder.DB_USERNAME_KEY, instance.username)
System.setProperty(PostgresDataSourceBuilder.DB_PASSWORD_KEY, instance.password)
}

fun tearDown() {
System.clearProperty(PostgresDataSourceBuilder.DB_PASSWORD_KEY)
System.clearProperty(PostgresDataSourceBuilder.DB_URL_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(PostgresDataSourceBuilder.DB_PASSWORD_KEY)
System.clearProperty(ConfigUtils.CLEAN_DISABLED)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package no.nav.dagpenger.oppslag.journalpost.id

import io.kotest.matchers.shouldBe
import no.nav.dagpenger.oppslag.journalpost.id.Postgres.withMigratedDb
import no.nav.dagpenger.oppslag.journalpost.id.PostgresDataSourceBuilder.dataSource
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)
withMigratedDb {
val journalpostRepository = PostgresJournalpostRepository(dataSource)
val søknadId = UUID.randomUUID()
val journalpostId = "123"
journalpostRepository.lagre(søknadId, journalpostId)
Expand Down

0 comments on commit 7e76385

Please sign in to comment.