From 583d665c02ca3f89948ada155fb2e5df5f4b4e08 Mon Sep 17 00:00:00 2001 From: Runar Skagestad Date: Wed, 9 Aug 2023 10:11:30 +0200 Subject: [PATCH 01/21] mob start [ci-skip] [ci skip] [skip ci] From 1534c3a464caac9842effce8e4876d59c8bf8dc4 Mon Sep 17 00:00:00 2001 From: Runar Skagestad Date: Wed, 9 Aug 2023 14:58:40 +0200 Subject: [PATCH 02/21] WIP Oppretta tabell import_eksport_status --- pom.xml | 1 - .../database/ImportEksportStatusRepository.kt | 55 +++++++++++++++++++ .../db/migration/V26__jobbstatus.sql | 10 ++++ .../ImportEksportStatusRepositoryTest.kt | 31 +++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt create mode 100644 src/main/resources/db/migration/V26__jobbstatus.sql create mode 100644 src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt diff --git a/pom.xml b/pom.xml index 2b5e318d5..d1f3bb964 100644 --- a/pom.xml +++ b/pom.xml @@ -196,7 +196,6 @@ ${net.minidev.json-smart.version} - no.nav.arbeidsgiver altinn-rettigheter-proxy-klient diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt new file mode 100644 index 000000000..65f3934ee --- /dev/null +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt @@ -0,0 +1,55 @@ +package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur.database + +import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ÅrstallOgKvartal +import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate +import org.springframework.stereotype.Repository + +@Repository +open class ImportEksportStatusRepository( + @param:Qualifier("sykefravarsstatistikkJdbcTemplate") private val namedParameterJdbcTemplate: NamedParameterJdbcTemplate +) { + + + fun initStatus(årstallOgKvartal: ÅrstallOgKvartal) { + namedParameterJdbcTemplate.update( + "insert into import_eksport_status (aarstall, kvartal, importert_statistikk, importert_virksomhetsdata, forberedt_neste_eksport, eksportert_paa_kafka)" + + " values (:aarstall, :kvartal, :importertStatistikk, :importertVirksomhetsdata, :forberedtNesteEksport, :eksportertPåKafka)", + mapOf( + "aarstall" to årstallOgKvartal.årstall.toString(), + "kvartal" to årstallOgKvartal.kvartal.toString(), + "importertStatistikk" to false, + "importertVirksomhetsdata" to false, + "forberedtNesteEksport" to false, + "eksportertPåKafka" to false + ) + ) + } + + fun hentImportEksportStatus( + årstallOgKvartal: ÅrstallOgKvartal + ): List { + return namedParameterJdbcTemplate.query( + "select * from import_eksport_status", + //mapOf("aarstall" to årstallOgKvartal.årstall.toString(), "kvartal" to årstallOgKvartal.kvartal.toString()) + ) { row, _ -> + ImportEksportStatusDao( + årstall = row.getString("aarstall"), + kvartal = row.getString("kvartal"), + importertStatistikk = row.getBoolean("importert_statistikk"), + importertVirksomhetsdata = row.getBoolean("importert_virksomhetsdata"), + forberedtNesteEksport = row.getBoolean("forberedt_neste_eksport"), + eksportertPåKafka = row.getBoolean("eksportert_paa_kafka") + ) + } + } +} + +data class ImportEksportStatusDao( + val årstall: String, + val kvartal: String, + val importertStatistikk: Boolean, + val importertVirksomhetsdata: Boolean, + val forberedtNesteEksport: Boolean, + val eksportertPåKafka: Boolean, +) diff --git a/src/main/resources/db/migration/V26__jobbstatus.sql b/src/main/resources/db/migration/V26__jobbstatus.sql new file mode 100644 index 000000000..1b8b6aada --- /dev/null +++ b/src/main/resources/db/migration/V26__jobbstatus.sql @@ -0,0 +1,10 @@ +create table import_eksport_status ( + aarstall varchar(4) not null, + kvartal varchar(1) not null, + importert_statistikk bool default false, + importert_virksomhetsdata bool default false, + forberedt_neste_eksport bool default false, + eksportert_paa_kafka bool default false, + created timestamp default current_timestamp, + primary key (aarstall, kvartal) +); \ No newline at end of file diff --git a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt new file mode 100644 index 000000000..3b5b00b09 --- /dev/null +++ b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt @@ -0,0 +1,31 @@ +package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur.database + +import io.kotest.core.extensions.Extension +import io.kotest.core.spec.style.FunSpec +import io.kotest.extensions.spring.SpringExtension +import io.kotest.matchers.collections.shouldContain +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe +import no.nav.arbeidsgiver.sykefravarsstatistikk.api.AppConfigForJdbcTesterConfig +import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ÅrstallOgKvartal +import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest +import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration +import org.springframework.test.context.ActiveProfiles +import org.springframework.test.context.ContextConfiguration + +@DataJdbcTest(excludeAutoConfiguration = [TestDatabaseAutoConfiguration::class]) +@ContextConfiguration(classes = [AppConfigForJdbcTesterConfig::class]) +@ActiveProfiles("db-test") +open class ImportEksportStatusRepositoryTest(repo: ImportEksportStatusRepository) : FunSpec({ + test("init burde inite") { + val årstallOgKvartal = ÅrstallOgKvartal(2023, 3) + repo.initStatus(årstallOgKvartal) + + val resultat = repo.hentImportEksportStatus(årstallOgKvartal) + + resultat shouldNotBe null + resultat shouldContain årstallOgKvartal.årstall.toString() + } +}) { + override fun extensions(): List = listOf(SpringExtension) +} \ No newline at end of file From 0ff1ef981b2f645745ad5fcf646665f0a9519f7e Mon Sep 17 00:00:00 2001 From: Runar Skagestad Date: Wed, 9 Aug 2023 14:58:45 +0200 Subject: [PATCH 03/21] Changes by oddgunnarfatland --- pom.xml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pom.xml b/pom.xml index d1f3bb964..d2ef372f1 100644 --- a/pom.xml +++ b/pom.xml @@ -264,6 +264,27 @@ ${kotlin.version} test + + + io.kotest + kotest-runner-junit5-jvm + 5.6.2 + test + + + + io.kotest + kotest-assertions-core-jvm + 5.6.2 + test + + + + io.kotest.extensions + kotest-extensions-spring + 1.1.3 + + net.javacrumbs.json-unit json-unit-assertj From 08eeed783b219deb09daf0a894692f568217f9d3 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Thu, 10 Aug 2023 14:17:16 +0200 Subject: [PATCH 04/21] Rename .java to .kt --- ...{ApplikasjonDBConfigLocal.java => ApplikasjonDBConfigLocal.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/{ApplikasjonDBConfigLocal.java => ApplikasjonDBConfigLocal.kt} (100%) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.java b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt similarity index 100% rename from src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.java rename to src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt From cea323bbece059aeacf0c13b1ee0549292b345a2 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Thu, 10 Aug 2023 14:17:20 +0200 Subject: [PATCH 05/21] Ta i bruk exposed --- pom.xml | 17 +++ .../api/config/ApplikasjonDBConfigLocal.kt | 141 +++++++++--------- .../database/ImportEksportStatusRepository.kt | 58 +++---- 3 files changed, 115 insertions(+), 101 deletions(-) diff --git a/pom.xml b/pom.xml index d2ef372f1..9d66a9ce3 100644 --- a/pom.xml +++ b/pom.xml @@ -41,6 +41,7 @@ 19.3.0.0 2.2.220 3.0.0-alpha1 + 0.42.0 @@ -125,6 +126,22 @@ ${no.nav.vault-jdbc.version} + + org.jetbrains.exposed + exposed-core + ${jetbrainsExposedVersion} + + + org.jetbrains.exposed + exposed-jdbc + ${jetbrainsExposedVersion} + + + org.jetbrains.exposed + exposed-java-time + ${jetbrainsExposedVersion} + + org.springdoc springdoc-openapi-starter-webmvc-ui diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt index e7fd5919d..f9c1f67ec 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt @@ -1,84 +1,79 @@ -package no.nav.arbeidsgiver.sykefravarsstatistikk.api.config; +package no.nav.arbeidsgiver.sykefravarsstatistikk.api.config -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; -import org.flywaydb.core.Flyway; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.Profile; -import org.springframework.core.env.Environment; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; - -import javax.sql.DataSource; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource +import org.flywaydb.core.Flyway +import org.jetbrains.exposed.sql.Database +import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Primary +import org.springframework.context.annotation.Profile +import org.springframework.core.env.Environment +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate +import javax.sql.DataSource @Configuration -@Profile({"local", "mvc-test", "db-test"}) -public class ApplikasjonDBConfigLocal { - - @Value("${applikasjon.datasource.url}") - private String databaseUrl; - - @Value("${applikasjon.datasource.username}") - private String username; - - @Value("${applikasjon.datasource.password}") - private String password; - - @Value("${applikasjon.datasource.driver-class-name}") - private String driverClassName; +@Profile("local", "mvc-test", "db-test") +open class ApplikasjonDBConfigLocal(private val environment: Environment) { + @Value("\${applikasjon.datasource.url}") + private val databaseUrl: String? = null - private final Environment environment; + @Value("\${applikasjon.datasource.username}") + private val username: String? = null - public ApplikasjonDBConfigLocal(Environment environment) { - this.environment = environment; - } + @Value("\${applikasjon.datasource.password}") + private val password: String? = null - @Primary - @Bean(name = "sykefravarsstatistikkDataSource") - public DataSource sykefravarsstatistikkDataSource() { - HikariConfig config = new HikariConfig(); - config.setPoolName("Sykefraværsstatistikk-connection-pool-local"); - config.setJdbcUrl(databaseUrl); - config.setUsername(username); - config.setPassword(password); - config.setMaximumPoolSize(2); - config.setDriverClassName(driverClassName); - - return new HikariDataSource(config); - } - - @Primary - @Bean(name = "sykefravarsstatistikkJdbcTemplate") - public NamedParameterJdbcTemplate sykefravarsstatistikkJdbcTemplate( - @Qualifier("sykefravarsstatistikkDataSource") DataSource dataSource) { - return new NamedParameterJdbcTemplate(dataSource); - } + @Value("\${applikasjon.datasource.driver-class-name}") + private val driverClassName: String? = null + @Primary + @Bean(name = ["sykefravarsstatistikkDataSource"]) + open fun sykefravarsstatistikkDataSource(): DataSource { + val config = HikariConfig() + config.poolName = "Sykefraværsstatistikk-connection-pool-local" + config.jdbcUrl = databaseUrl + config.username = username + config.password = password + config.maximumPoolSize = 2 + config.driverClassName = driverClassName + return HikariDataSource(config) + } - @Bean - public FlywayMigrationStrategy flywayMigrationStrategy() { - List locations = new ArrayList<>(); - locations.add("/db/migration"); - locations.add("/db/test-datavarehus"); + @Primary + @Bean(name = ["sykefravarsstatistikkJdbcTemplate"]) + open fun sykefravarsstatistikkJdbcTemplate( + @Qualifier("sykefravarsstatistikkDataSource") dataSource: DataSource + ): NamedParameterJdbcTemplate { + return NamedParameterJdbcTemplate(dataSource) + } - String[] profiles = environment.getActiveProfiles(); - if (Arrays.asList(profiles).contains("mvc-test") || Arrays.asList(profiles).contains("local")) { - locations.add("/db/test-lokaldb-data"); + @Bean + open fun database( + @Qualifier("sykefravarsstatistikkDataSource") dataSource: DataSource, + ): Database { + return Database.connect(dataSource) } - return flyway -> { - Flyway.configure() - .dataSource(sykefravarsstatistikkDataSource()) - .locations(locations.toArray(new String[0])) - .load() - .migrate(); - }; - } + @Bean + open fun flywayMigrationStrategy(): FlywayMigrationStrategy { + val locations: MutableList = ArrayList() + locations.add("/db/migration") + locations.add("/db/test-datavarehus") + val profiles = environment.activeProfiles + if (listOf(*profiles).contains("mvc-test") || listOf(*profiles) + .contains("local") + ) { + locations.add("/db/test-lokaldb-data") + } + return FlywayMigrationStrategy { + Flyway.configure() + .dataSource(sykefravarsstatistikkDataSource()) + .locations(*locations.toTypedArray()) + .load() + .migrate() + } + } } diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt index 65f3934ee..9c7719ab9 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt @@ -1,45 +1,47 @@ package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur.database import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ÅrstallOgKvartal -import org.springframework.beans.factory.annotation.Qualifier -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate +import org.jetbrains.exposed.sql.Table +import org.jetbrains.exposed.sql.select +import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.upsert import org.springframework.stereotype.Repository @Repository -open class ImportEksportStatusRepository( - @param:Qualifier("sykefravarsstatistikkJdbcTemplate") private val namedParameterJdbcTemplate: NamedParameterJdbcTemplate -) { - +object ImportEksportStatusRepository : Table("import_eksport_status") { + val årstall = varchar("aarstall", 4) + val kvartal = varchar("kvartal", 1) + val importertStatistikk = bool("importert_statistikk").default(false) + val importertVirksomhetsdata = bool("importert_virksomhetsdata").default(false) + val forberedtNesteEksport = bool("forberedt_neste_eksport").default(false) + val eksportertPåKafka = bool("eksportert_paa_kafka").default(false) + override val primaryKey: PrimaryKey = PrimaryKey(årstall, kvartal) fun initStatus(årstallOgKvartal: ÅrstallOgKvartal) { - namedParameterJdbcTemplate.update( - "insert into import_eksport_status (aarstall, kvartal, importert_statistikk, importert_virksomhetsdata, forberedt_neste_eksport, eksportert_paa_kafka)" + - " values (:aarstall, :kvartal, :importertStatistikk, :importertVirksomhetsdata, :forberedtNesteEksport, :eksportertPåKafka)", - mapOf( - "aarstall" to årstallOgKvartal.årstall.toString(), - "kvartal" to årstallOgKvartal.kvartal.toString(), - "importertStatistikk" to false, - "importertVirksomhetsdata" to false, - "forberedtNesteEksport" to false, - "eksportertPåKafka" to false - ) - ) + transaction { + upsert { + it[årstall] = årstallOgKvartal.årstall.toString() + it[kvartal] = årstallOgKvartal.kvartal.toString() + } + } } fun hentImportEksportStatus( årstallOgKvartal: ÅrstallOgKvartal ): List { - return namedParameterJdbcTemplate.query( - "select * from import_eksport_status", - //mapOf("aarstall" to årstallOgKvartal.årstall.toString(), "kvartal" to årstallOgKvartal.kvartal.toString()) - ) { row, _ -> + return transaction { + select { + årstall eq årstallOgKvartal.årstall.toString() + kvartal eq årstallOgKvartal.kvartal.toString() + } + }.map { ImportEksportStatusDao( - årstall = row.getString("aarstall"), - kvartal = row.getString("kvartal"), - importertStatistikk = row.getBoolean("importert_statistikk"), - importertVirksomhetsdata = row.getBoolean("importert_virksomhetsdata"), - forberedtNesteEksport = row.getBoolean("forberedt_neste_eksport"), - eksportertPåKafka = row.getBoolean("eksportert_paa_kafka") + årstall = it[årstall], + kvartal = it[kvartal], + importertStatistikk = it[importertStatistikk], + importertVirksomhetsdata = it[importertVirksomhetsdata], + forberedtNesteEksport = it[forberedtNesteEksport], + eksportertPåKafka = it[eksportertPåKafka], ) } } From 6415159d9df85c134ebe494fb56916f9794e0ff0 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Thu, 10 Aug 2023 14:49:40 +0200 Subject: [PATCH 06/21] Implementer sett status --- .../domenemodeller/ImportEksportStatus.kt | 9 ++++ .../database/ImportEksportStatusRepository.kt | 43 +++++++++++-------- .../ImportEksportStatusRepositoryTest.kt | 34 +++++++++++---- 3 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/applikasjon/domenemodeller/ImportEksportStatus.kt diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/applikasjon/domenemodeller/ImportEksportStatus.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/applikasjon/domenemodeller/ImportEksportStatus.kt new file mode 100644 index 000000000..56f6a3deb --- /dev/null +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/applikasjon/domenemodeller/ImportEksportStatus.kt @@ -0,0 +1,9 @@ +package no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller + +interface ImportEksportStatus { + val årstallOgKvartal: ÅrstallOgKvartal + val importertStatistikk: Boolean + val importertVirksomhetsdata: Boolean + val forberedtNesteEksport: Boolean + val eksportertPåKafka: Boolean +} \ No newline at end of file diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt index 9c7719ab9..93dfb07af 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt @@ -1,5 +1,6 @@ package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur.database +import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ImportEksportStatus import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ÅrstallOgKvartal import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.select @@ -17,32 +18,36 @@ object ImportEksportStatusRepository : Table("import_eksport_status") { val eksportertPåKafka = bool("eksportert_paa_kafka").default(false) override val primaryKey: PrimaryKey = PrimaryKey(årstall, kvartal) - fun initStatus(årstallOgKvartal: ÅrstallOgKvartal) { + fun settImportEksportStatus(importEksportStatus: ImportEksportStatus) { transaction { upsert { - it[årstall] = årstallOgKvartal.årstall.toString() - it[kvartal] = årstallOgKvartal.kvartal.toString() + it[årstall] = importEksportStatus.årstallOgKvartal.årstall.toString() + it[kvartal] = importEksportStatus.årstallOgKvartal.kvartal.toString() + it[importertStatistikk] = importEksportStatus.importertStatistikk + it[importertVirksomhetsdata] = importEksportStatus.importertVirksomhetsdata + it[forberedtNesteEksport] = importEksportStatus.forberedtNesteEksport + it[eksportertPåKafka] = importEksportStatus.eksportertPåKafka } } } fun hentImportEksportStatus( årstallOgKvartal: ÅrstallOgKvartal - ): List { + ): List { return transaction { select { årstall eq årstallOgKvartal.årstall.toString() kvartal eq årstallOgKvartal.kvartal.toString() + }.map { + ImportEksportStatusDao( + årstall = it[årstall], + kvartal = it[kvartal], + importertStatistikk = it[importertStatistikk], + importertVirksomhetsdata = it[importertVirksomhetsdata], + forberedtNesteEksport = it[forberedtNesteEksport], + eksportertPåKafka = it[eksportertPåKafka], + ) } - }.map { - ImportEksportStatusDao( - årstall = it[årstall], - kvartal = it[kvartal], - importertStatistikk = it[importertStatistikk], - importertVirksomhetsdata = it[importertVirksomhetsdata], - forberedtNesteEksport = it[forberedtNesteEksport], - eksportertPåKafka = it[eksportertPåKafka], - ) } } } @@ -50,8 +55,10 @@ object ImportEksportStatusRepository : Table("import_eksport_status") { data class ImportEksportStatusDao( val årstall: String, val kvartal: String, - val importertStatistikk: Boolean, - val importertVirksomhetsdata: Boolean, - val forberedtNesteEksport: Boolean, - val eksportertPåKafka: Boolean, -) + override val importertStatistikk: Boolean, + override val importertVirksomhetsdata: Boolean, + override val forberedtNesteEksport: Boolean, + override val eksportertPåKafka: Boolean, +) : ImportEksportStatus { + override val årstallOgKvartal: ÅrstallOgKvartal = ÅrstallOgKvartal(årstall.toInt(), kvartal.toInt()) +} diff --git a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt index 3b5b00b09..473d4fb4d 100644 --- a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt +++ b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt @@ -3,9 +3,8 @@ package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur.database import io.kotest.core.extensions.Extension import io.kotest.core.spec.style.FunSpec import io.kotest.extensions.spring.SpringExtension -import io.kotest.matchers.collections.shouldContain -import io.kotest.matchers.shouldBe -import io.kotest.matchers.shouldNotBe +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.collections.shouldHaveSize import no.nav.arbeidsgiver.sykefravarsstatistikk.api.AppConfigForJdbcTesterConfig import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ÅrstallOgKvartal import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest @@ -17,14 +16,31 @@ import org.springframework.test.context.ContextConfiguration @ContextConfiguration(classes = [AppConfigForJdbcTesterConfig::class]) @ActiveProfiles("db-test") open class ImportEksportStatusRepositoryTest(repo: ImportEksportStatusRepository) : FunSpec({ - test("init burde inite") { - val årstallOgKvartal = ÅrstallOgKvartal(2023, 3) - repo.initStatus(årstallOgKvartal) + val årstallOgKvartal = ÅrstallOgKvartal(2023, 3) + val importEksportStatus = ImportEksportStatusDao( + årstall = årstallOgKvartal.årstall.toString(), + kvartal = årstallOgKvartal.kvartal.toString(), + importertStatistikk = true, + importertVirksomhetsdata = false, + eksportertPåKafka = false, + forberedtNesteEksport = false, + ) - val resultat = repo.hentImportEksportStatus(årstallOgKvartal) + test("sett import eksport status burde sette ny status") { + repo.hentImportEksportStatus(årstallOgKvartal) shouldHaveSize 0 - resultat shouldNotBe null - resultat shouldContain årstallOgKvartal.årstall.toString() + repo.settImportEksportStatus(importEksportStatus) + + repo.hentImportEksportStatus(årstallOgKvartal) shouldContainExactly listOf(importEksportStatus) + } + + test("sett import eksport status burde oppdatere eksisterende status") { + val oppdatertImportEksportStatus = importEksportStatus.copy(importertVirksomhetsdata = true) + + repo.settImportEksportStatus(importEksportStatus) + repo.settImportEksportStatus(oppdatertImportEksportStatus) + + repo.hentImportEksportStatus(årstallOgKvartal) shouldContainExactly listOf(oppdatertImportEksportStatus) } }) { override fun extensions(): List = listOf(SpringExtension) From 324b11e0890160f17f53fdfbe84a466c2ee9c1b6 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Thu, 10 Aug 2023 15:30:20 +0200 Subject: [PATCH 07/21] Legg til kommentar --- .../api/applikasjon/domenemodeller/ImportEksportStatus.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/applikasjon/domenemodeller/ImportEksportStatus.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/applikasjon/domenemodeller/ImportEksportStatus.kt index 56f6a3deb..7ffabf059 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/applikasjon/domenemodeller/ImportEksportStatus.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/applikasjon/domenemodeller/ImportEksportStatus.kt @@ -1,5 +1,9 @@ package no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller +/** + * Viser status på alle stegene som gjøres ved automatisk + * import og eksport av sykefraværsstatistikk. + */ interface ImportEksportStatus { val årstallOgKvartal: ÅrstallOgKvartal val importertStatistikk: Boolean From bc4b89209fe3b301dccfd5fd2523ac15b9147c5c Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 12:08:24 +0200 Subject: [PATCH 08/21] Rename .java to .kt --- .../api/infrastruktur/{MockServer.java => MockServer.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/{MockServer.java => MockServer.kt} (100%) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/MockServer.java b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/MockServer.kt similarity index 100% rename from src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/MockServer.java rename to src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/MockServer.kt From aa03b62e22e1a7b6c681d676029abe5c2f2c16b3 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 12:08:27 +0200 Subject: [PATCH 09/21] Bruk eksisterende JUnit istedenfor Kotest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Var mye trøbbel med å håndtere Spring context når noen av testene brukte Kotest og resten brukte JUnit. --- pom.xml | 19 - .../api/infrastruktur/MockServer.kt | 337 +++++++++--------- .../ImportEksportStatusRepositoryTest.kt | 43 ++- 3 files changed, 197 insertions(+), 202 deletions(-) diff --git a/pom.xml b/pom.xml index 9d66a9ce3..90436486e 100644 --- a/pom.xml +++ b/pom.xml @@ -275,19 +275,6 @@ kotlinx-coroutines-core-jvm 1.6.4 - - org.jetbrains.kotlin - kotlin-test - ${kotlin.version} - test - - - - io.kotest - kotest-runner-junit5-jvm - 5.6.2 - test - io.kotest @@ -296,12 +283,6 @@ test - - io.kotest.extensions - kotest-extensions-spring - 1.1.3 - - net.javacrumbs.json-unit json-unit-assertj diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/MockServer.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/MockServer.kt index 77d8116fd..854a6e5cf 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/MockServer.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/MockServer.kt @@ -1,184 +1,179 @@ -package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur; +package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur + +import com.github.tomakehurst.wiremock.WireMockServer +import com.github.tomakehurst.wiremock.client.WireMock +import com.github.tomakehurst.wiremock.common.ConsoleNotifier +import com.github.tomakehurst.wiremock.core.WireMockConfiguration +import com.github.tomakehurst.wiremock.matching.AnythingPattern +import com.github.tomakehurst.wiremock.matching.ContainsPattern +import com.github.tomakehurst.wiremock.matching.StringValuePattern +import com.github.tomakehurst.wiremock.matching.UrlPathPattern +import io.micrometer.core.instrument.util.IOUtils +import org.apache.commons.lang3.StringUtils +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Value +import org.springframework.context.annotation.Profile +import org.springframework.core.env.Environment +import org.springframework.http.HttpStatus +import org.springframework.stereotype.Component +import java.net.URL +import java.nio.charset.StandardCharsets -import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; -import static java.nio.charset.StandardCharsets.UTF_8; - -import com.github.tomakehurst.wiremock.WireMockServer; -import com.github.tomakehurst.wiremock.client.WireMock; -import com.github.tomakehurst.wiremock.common.ConsoleNotifier; -import com.github.tomakehurst.wiremock.core.WireMockConfiguration; -import com.github.tomakehurst.wiremock.matching.AnythingPattern; -import com.github.tomakehurst.wiremock.matching.ContainsPattern; -import com.github.tomakehurst.wiremock.matching.StringValuePattern; -import com.github.tomakehurst.wiremock.matching.UrlPathPattern; -import com.github.tomakehurst.wiremock.matching.UrlPattern; -import io.micrometer.core.instrument.util.IOUtils; -import java.net.URL; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; -import org.jetbrains.annotations.NotNull; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Profile; -import org.springframework.core.env.Environment; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Component; - -@Slf4j @Component -@Profile({"local", "mvc-test"}) -public class MockServer { - public static final boolean AKTIVER_VERBOSE_LOGGING_I_KONSOLEN = false; - private final WireMockServer server; - - public MockServer( - @Value("${wiremock.mock.port}") Integer port, - @Value("${altinn.url}") String altinnUrl, - @Value("${altinn.proxy.url}") String altinnProxyUrl, - @Value("${enhetsregisteret.url}") String enhetsregisteretUrl, - Environment environment) { - log.info("Starter mock-server på port " + port); - - this.server = - new WireMockServer( +@Profile("local", "mvc-test") +class MockServer( + @Value("\${wiremock.mock.port}") port: Int, + @Value("\${altinn.proxy.url}") altinnProxyUrl: String, + @Value("\${enhetsregisteret.url}") enhetsregisteretUrl: String?, + environment: Environment +) { + private val server: WireMockServer + private val log = LoggerFactory.getLogger(this::class.java) + + init { + log.info("Starter mock-server på port $port") + server = WireMockServer( WireMockConfiguration.wireMockConfig() .port(port) - .notifier(new ConsoleNotifier(AKTIVER_VERBOSE_LOGGING_I_KONSOLEN))); - - if (Arrays.asList(environment.getActiveProfiles()).contains("local") - || Arrays.asList(environment.getActiveProfiles()).contains("mvc-test")) { - log.info("Mocker kall fra Altinn"); - mockKall(altinnProxyUrl + "v2/organisasjoner", HttpStatus.NOT_FOUND); - mockKallUtenParametereFraFil(altinnProxyUrl + "v2/organisasjoner", "altinnReportees.json"); - mockKallMedParametereFraFil( - altinnProxyUrl + "v2/organisasjoner", "altinnReporteesMedIARettigheter.json"); + .notifier(ConsoleNotifier(AKTIVER_VERBOSE_LOGGING_I_KONSOLEN)) + ) + if (listOf(*environment.activeProfiles).contains("local") + || listOf(*environment.activeProfiles).contains("mvc-test") + ) { + log.info("Mocker kall fra Altinn") + mockKall(altinnProxyUrl + "v2/organisasjoner", HttpStatus.NOT_FOUND) + mockKallUtenParametereFraFil(altinnProxyUrl + "v2/organisasjoner", "altinnReportees.json") + mockKallMedParametereFraFil( + altinnProxyUrl + "v2/organisasjoner", "altinnReporteesMedIARettigheter.json" + ) + } + log.info("Mocker kall fra Enhetsregisteret") + mockKallFraEnhetsregisteret(StringUtils.removeEnd(enhetsregisteretUrl, "/")) + server.start() } - log.info("Mocker kall fra Enhetsregisteret"); - mockKallFraEnhetsregisteret(StringUtils.removeEnd(enhetsregisteretUrl, "/")); - - server.start(); - } - - @SneakyThrows - private void mockKallFraEnhetsregisteret(String enhetsregisteretUrl) { - String path = new URL(enhetsregisteretUrl).getPath(); - mockKall( - WireMock.urlPathMatching(path + "/underenheter/[0-9]{9}"), - lesFilSomString("enhetsregisteretUnderenhet.json")); - mockKall( - WireMock.urlPathMatching(path + "/underenheter/444444444"), - lesFilSomString("enhetsregisteretUnderenhet_444444444.json")); - mockKall( - WireMock.urlPathMatching(path + "/underenheter/555555555"), - lesFilSomString("enhetsregisteretUnderenhet_555555555.json")); - mockKall( - WireMock.urlPathMatching(path + "/underenheter/910562452"), - lesFilSomString("dev_enhetsregisteretUnderenhet_910562452.json")); - mockKall( - WireMock.urlPathMatching(path + "/underenheter/910825518"), - lesFilSomString("dev_enhetsregisteretUnderenhet_910825518.json")); - mockKall( - WireMock.urlPathMatching(path + "/underenheter/910562436"), - lesFilSomString("dev_enhetsregisteretUnderenhet_910562436.json")); - mockKall( - WireMock.urlPathMatching(path + "/underenheter/311874411"), - lesFilSomString("dev_enhetsregisteretUnderenhet_311874411.json")); - mockKall( - WireMock.urlPathMatching(path + "/underenheter/315829062"), - lesFilSomString("dev_enhetsregisteretUnderenhet_315829062.json")); - mockKall( - WireMock.urlPathMatching(path + "/enheter/[0-9]{9}"), - lesFilSomString("enhetsregisteretEnhet.json")); - mockKall( - WireMock.urlPathMatching(path + "/enheter/910562223"), - lesFilSomString("dev_enhetsregisteretEnhet.json")); - mockKall( - WireMock.urlPathMatching(path + "/enheter/310529915"), - lesFilSomString("dev_enhetsregisteretEnhet_310529915.json")); - mockKall( - WireMock.urlPathMatching(path + "/enheter/313068420"), - lesFilSomString("dev_enhetsregisteretEnhet_313068420.json")); - } - - private void mockKallFraFil(String url, String filnavn) { - mockKall(url, lesFilSomString(filnavn)); - } - - @SneakyThrows - private void mockKall(String url, String body) { - String path = new URL(url).getPath(); - mockKall(urlPathEqualTo(path), body); - } - - @SneakyThrows - private void mockKall(UrlPathPattern urlPathPattern, String body) { - server.stubFor( - WireMock.get(urlPathPattern) - .willReturn( - WireMock.aResponse() - .withHeader("Content-Type", "application/json") - .withStatus(HttpStatus.OK.value()) - .withBody(body))); - } - - @SneakyThrows - private void mockKallMedParametereFraFil(String url, String filnavn) { - String body = lesFilSomString(filnavn); - String path = new URL(url).getPath(); - Map params = getParams(); - params.put("serviceCode", new ContainsPattern("3403")); - params.put("serviceEdition", new AnythingPattern()); + private fun mockKallFraEnhetsregisteret(enhetsregisteretUrl: String) { + val path = URL(enhetsregisteretUrl).path + mockKall( + WireMock.urlPathMatching("$path/underenheter/[0-9]{9}"), + lesFilSomString("enhetsregisteretUnderenhet.json") + ) + mockKall( + WireMock.urlPathMatching("$path/underenheter/444444444"), + lesFilSomString("enhetsregisteretUnderenhet_444444444.json") + ) + mockKall( + WireMock.urlPathMatching("$path/underenheter/555555555"), + lesFilSomString("enhetsregisteretUnderenhet_555555555.json") + ) + mockKall( + WireMock.urlPathMatching("$path/underenheter/910562452"), + lesFilSomString("dev_enhetsregisteretUnderenhet_910562452.json") + ) + mockKall( + WireMock.urlPathMatching("$path/underenheter/910825518"), + lesFilSomString("dev_enhetsregisteretUnderenhet_910825518.json") + ) + mockKall( + WireMock.urlPathMatching("$path/underenheter/910562436"), + lesFilSomString("dev_enhetsregisteretUnderenhet_910562436.json") + ) + mockKall( + WireMock.urlPathMatching("$path/underenheter/311874411"), + lesFilSomString("dev_enhetsregisteretUnderenhet_311874411.json") + ) + mockKall( + WireMock.urlPathMatching("$path/underenheter/315829062"), + lesFilSomString("dev_enhetsregisteretUnderenhet_315829062.json") + ) + mockKall( + WireMock.urlPathMatching("$path/enheter/[0-9]{9}"), + lesFilSomString("enhetsregisteretEnhet.json") + ) + mockKall( + WireMock.urlPathMatching("$path/enheter/910562223"), + lesFilSomString("dev_enhetsregisteretEnhet.json") + ) + mockKall( + WireMock.urlPathMatching("$path/enheter/310529915"), + lesFilSomString("dev_enhetsregisteretEnhet_310529915.json") + ) + mockKall( + WireMock.urlPathMatching("$path/enheter/313068420"), + lesFilSomString("dev_enhetsregisteretEnhet_313068420.json") + ) + } - server.stubFor( - WireMock.get(urlPathEqualTo(path)) - .withQueryParams(params) - .willReturn( - WireMock.aResponse() - .withHeader("Content-Type", "application/json") - .withStatus(HttpStatus.OK.value()) - .withBody(body))); - } + private fun mockKall(urlPathPattern: UrlPathPattern, body: String) { + server.stubFor( + WireMock.get(urlPathPattern) + .willReturn( + WireMock.aResponse() + .withHeader("Content-Type", "application/json") + .withStatus(HttpStatus.OK.value()) + .withBody(body) + ) + ) + } - @SneakyThrows - private void mockKallUtenParametereFraFil(String url, String filnavn) { - String body = lesFilSomString(filnavn); - String path = new URL(url).getPath(); - Map params = getParams(); + private fun mockKallMedParametereFraFil(url: String, filnavn: String) { + val body = lesFilSomString(filnavn) + val path = URL(url).path + val params = params + params["serviceCode"] = ContainsPattern("3403") + params["serviceEdition"] = AnythingPattern() + server.stubFor( + WireMock.get(WireMock.urlPathEqualTo(path)) + .withQueryParams(params) + .willReturn( + WireMock.aResponse() + .withHeader("Content-Type", "application/json") + .withStatus(HttpStatus.OK.value()) + .withBody(body) + ) + ) + } - server.stubFor( - WireMock.get(urlPathEqualTo(path)) - .withQueryParams(params) - .willReturn( - WireMock.aResponse() - .withHeader("Content-Type", "application/json") - .withStatus(HttpStatus.OK.value()) - .withBody(body))); - } + private fun mockKallUtenParametereFraFil(url: String, filnavn: String) { + val body = lesFilSomString(filnavn) + val path = URL(url).path + val params: Map = params + server.stubFor( + WireMock.get(WireMock.urlPathEqualTo(path)) + .withQueryParams(params) + .willReturn( + WireMock.aResponse() + .withHeader("Content-Type", "application/json") + .withStatus(HttpStatus.OK.value()) + .withBody(body) + ) + ) + } - @NotNull - private Map getParams() { - Map params = new HashMap<>(); - params.put("filter", new AnythingPattern()); - params.put("top", new AnythingPattern()); - params.put("skip", new AnythingPattern()); - return params; - } + private val params: MutableMap + get() { + val params: MutableMap = HashMap() + params["filter"] = AnythingPattern() + params["top"] = AnythingPattern() + params["skip"] = AnythingPattern() + return params + } + + private fun mockKall(url: String, status: HttpStatus) { + val path = URL(url).path + val urlMatching = WireMock.urlMatching(".*$path.*") + server.stubFor( + WireMock.get(urlMatching).willReturn(WireMock.aResponse().withStatus(status.value())) + ) + } - @SneakyThrows - private void mockKall(String url, HttpStatus status) { - String path = new URL(url).getPath(); - UrlPattern urlMatching = WireMock.urlMatching(".*" + path + ".*"); - server.stubFor( - WireMock.get(urlMatching).willReturn(WireMock.aResponse().withStatus(status.value()))); - } + private fun lesFilSomString(filnavn: String): String { + return IOUtils.toString( + this.javaClass.getClassLoader().getResourceAsStream("mock/$filnavn"), StandardCharsets.UTF_8 + ) + } - @SneakyThrows - private String lesFilSomString(String filnavn) { - return IOUtils.toString( - this.getClass().getClassLoader().getResourceAsStream("mock/" + filnavn), UTF_8); - } + companion object { + const val AKTIVER_VERBOSE_LOGGING_I_KONSOLEN = false + } } diff --git a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt index 473d4fb4d..26302a0f0 100644 --- a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt +++ b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt @@ -1,23 +1,31 @@ package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur.database -import io.kotest.core.extensions.Extension -import io.kotest.core.spec.style.FunSpec -import io.kotest.extensions.spring.SpringExtension import io.kotest.matchers.collections.shouldContainExactly import io.kotest.matchers.collections.shouldHaveSize import no.nav.arbeidsgiver.sykefravarsstatistikk.api.AppConfigForJdbcTesterConfig import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ÅrstallOgKvartal +import org.jetbrains.exposed.sql.deleteAll +import org.jetbrains.exposed.sql.transactions.transaction +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration import org.springframework.test.context.ActiveProfiles import org.springframework.test.context.ContextConfiguration -@DataJdbcTest(excludeAutoConfiguration = [TestDatabaseAutoConfiguration::class]) -@ContextConfiguration(classes = [AppConfigForJdbcTesterConfig::class]) @ActiveProfiles("db-test") -open class ImportEksportStatusRepositoryTest(repo: ImportEksportStatusRepository) : FunSpec({ - val årstallOgKvartal = ÅrstallOgKvartal(2023, 3) - val importEksportStatus = ImportEksportStatusDao( +@ExtendWith(org.springframework.test.context.junit.jupiter.SpringExtension::class) +@ContextConfiguration(classes = [AppConfigForJdbcTesterConfig::class]) +@DataJdbcTest(excludeAutoConfiguration = [TestDatabaseAutoConfiguration::class]) +open class ImportEksportStatusRepositoryTest { + @Autowired + private lateinit var repo: ImportEksportStatusRepository + + + private val årstallOgKvartal = ÅrstallOgKvartal(2023, 3) + private val importEksportStatus = ImportEksportStatusDao( årstall = årstallOgKvartal.årstall.toString(), kvartal = årstallOgKvartal.kvartal.toString(), importertStatistikk = true, @@ -26,7 +34,13 @@ open class ImportEksportStatusRepositoryTest(repo: ImportEksportStatusRepository forberedtNesteEksport = false, ) - test("sett import eksport status burde sette ny status") { + @BeforeEach + fun beforeEach() { + repo.slettAlt() + } + + @Test + fun `sett import eksport status burde sette ny status`() { repo.hentImportEksportStatus(årstallOgKvartal) shouldHaveSize 0 repo.settImportEksportStatus(importEksportStatus) @@ -34,7 +48,8 @@ open class ImportEksportStatusRepositoryTest(repo: ImportEksportStatusRepository repo.hentImportEksportStatus(årstallOgKvartal) shouldContainExactly listOf(importEksportStatus) } - test("sett import eksport status burde oppdatere eksisterende status") { + @Test + fun `sett import eksport status burde oppdatere eksisterende status`() { val oppdatertImportEksportStatus = importEksportStatus.copy(importertVirksomhetsdata = true) repo.settImportEksportStatus(importEksportStatus) @@ -42,6 +57,10 @@ open class ImportEksportStatusRepositoryTest(repo: ImportEksportStatusRepository repo.hentImportEksportStatus(årstallOgKvartal) shouldContainExactly listOf(oppdatertImportEksportStatus) } -}) { - override fun extensions(): List = listOf(SpringExtension) + + private fun ImportEksportStatusRepository.slettAlt() { + transaction { + deleteAll() + } + } } \ No newline at end of file From 660a305488f49e60cea9ae86faeb2263b3fb4694 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 12:19:22 +0200 Subject: [PATCH 10/21] =?UTF-8?q?Fix=20b=C3=B8nne?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/infrastruktur/database/ImportEksportStatusRepository.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt index 93dfb07af..e5af7772f 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt @@ -9,7 +9,7 @@ import org.jetbrains.exposed.sql.upsert import org.springframework.stereotype.Repository @Repository -object ImportEksportStatusRepository : Table("import_eksport_status") { +open class ImportEksportStatusRepository : Table("import_eksport_status") { val årstall = varchar("aarstall", 4) val kvartal = varchar("kvartal", 1) val importertStatistikk = bool("importert_statistikk").default(false) From 96df2d6e81a73cc4ad2a0ee44e466b4bf56cc61a Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 12:51:01 +0200 Subject: [PATCH 11/21] Rename .java to .kt --- .../config/{ApplikasjonDBConfig.java => ApplikasjonDBConfig.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/{ApplikasjonDBConfig.java => ApplikasjonDBConfig.kt} (100%) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfig.java b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfig.kt similarity index 100% rename from src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfig.java rename to src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfig.kt From 52e3fe8e89e41ded767841a0fca941db47368954 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 12:51:01 +0200 Subject: [PATCH 12/21] Sett default database i exposed transaction manager --- .../api/config/ApplikasjonDBConfig.kt | 150 +++++++++--------- .../api/config/ApplikasjonDBConfigLocal.kt | 5 +- 2 files changed, 82 insertions(+), 73 deletions(-) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfig.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfig.kt index f6553c12e..a7fe6afba 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfig.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfig.kt @@ -1,85 +1,91 @@ -package no.nav.arbeidsgiver.sykefravarsstatistikk.api.config; +package no.nav.arbeidsgiver.sykefravarsstatistikk.api.config -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; -import no.nav.vault.jdbc.hikaricp.HikariCPVaultUtil; -import no.nav.vault.jdbc.hikaricp.VaultError; -import org.flywaydb.core.Flyway; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.Profile; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; - -import javax.sql.DataSource; - -import static java.lang.String.format; +import com.zaxxer.hikari.HikariConfig +import com.zaxxer.hikari.HikariDataSource +import no.nav.vault.jdbc.hikaricp.HikariCPVaultUtil +import no.nav.vault.jdbc.hikaricp.VaultError +import org.flywaydb.core.Flyway +import org.jetbrains.exposed.sql.Database +import org.jetbrains.exposed.sql.transactions.TransactionManager +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.beans.factory.annotation.Value +import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Primary +import org.springframework.context.annotation.Profile +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate +import javax.sql.DataSource @Configuration -@Profile({"dev", "prod"}) -public class ApplikasjonDBConfig { - - // URL hentes i Vault - @Value("${spring.datasource.url}") - private String databaseUrl; +@Profile("dev", "prod") +open class ApplikasjonDBConfig { + // URL hentes i Vault + @Value("\${spring.datasource.url}") + private val databaseUrl: String? = null - @Value("${database.navn}") - private String databaseNavn; + @Value("\${database.navn}") + private val databaseNavn: String? = null - @Value("${vault.mount-path}") - private String mountPath; - - private static Logger logger = LoggerFactory.getLogger(ApplikasjonDBConfig.class); - - @Primary - @Bean(name = "sykefravarsstatistikkDataSource") - public DataSource userDataSource() { - return dataSource("admin"); - } + @Value("\${vault.mount-path}") + private val mountPath: String? = null + @Primary + @Bean(name = ["sykefravarsstatistikkDataSource"]) + open fun userDataSource(): DataSource? { + return dataSource("admin") + } - @Primary - @Bean(name = "sykefravarsstatistikkJdbcTemplate") - public NamedParameterJdbcTemplate sykefravarsstatistikkJdbcTemplate( - @Qualifier("sykefravarsstatistikkDataSource") DataSource dataSource) { + @Primary + @Bean(name = ["sykefravarsstatistikkJdbcTemplate"]) + open fun sykefravarsstatistikkJdbcTemplate( + @Qualifier("sykefravarsstatistikkDataSource") dataSource: DataSource + ): NamedParameterJdbcTemplate { + return NamedParameterJdbcTemplate(dataSource) + } - return new NamedParameterJdbcTemplate(dataSource); - } + @Bean + open fun database( + @Qualifier("sykefravarsstatistikkDataSource") dataSource: DataSource, + ): Database { + val db = Database.connect(dataSource) + TransactionManager.defaultDatabase = db + return db + } - private HikariDataSource dataSource(String user) { - HikariConfig config = new HikariConfig(); - config.setPoolName("Sykefraværsstatistikk-connection-pool"); - config.setJdbcUrl(databaseUrl); - config.setMaximumPoolSize(8); - config.setMinimumIdle(1); - HikariDataSource hikariDataSourceWithVaultIntegration = null; - try { - hikariDataSourceWithVaultIntegration = - HikariCPVaultUtil.createHikariDataSourceWithVaultIntegration( - config, mountPath, dbRole(user)); - } catch (VaultError vaultError) { - logger.warn("[GCP-migrering] Kunne ikke opprette DS. Returnerer null. ", vaultError); - return null; + private fun dataSource(user: String): HikariDataSource? { + val config = HikariConfig() + config.poolName = "Sykefraværsstatistikk-connection-pool" + config.jdbcUrl = databaseUrl + config.maximumPoolSize = 8 + config.minimumIdle = 1 + val hikariDataSourceWithVaultIntegration: HikariDataSource? = try { + HikariCPVaultUtil.createHikariDataSourceWithVaultIntegration( + config, mountPath, dbRole(user) + ) + } catch (vaultError: VaultError) { + logger.warn("[GCP-migrering] Kunne ikke opprette DS. Returnerer null. ", vaultError) + return null + } + return hikariDataSourceWithVaultIntegration } - return hikariDataSourceWithVaultIntegration; - } + @Bean + open fun flywayMigrationStrategy(): FlywayMigrationStrategy { + return FlywayMigrationStrategy { flyway: Flyway? -> + Flyway.configure() + .dataSource(dataSource("admin")) + .initSql(String.format("SET ROLE \"%s\"", dbRole("admin"))) + .load() + .migrate() + } + } - @Bean - public FlywayMigrationStrategy flywayMigrationStrategy() { - return flyway -> - Flyway.configure() - .dataSource(dataSource("admin")) - .initSql(format("SET ROLE \"%s\"", dbRole("admin"))) - .load() - .migrate(); - } + private fun dbRole(role: String): String { + return java.lang.String.join("-", databaseNavn, role) + } - private String dbRole(String role) { - return String.join("-", databaseNavn, role); - } + companion object { + private val logger = LoggerFactory.getLogger(ApplikasjonDBConfig::class.java) + } } diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt index f9c1f67ec..7cb72f32d 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt @@ -4,6 +4,7 @@ import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import org.flywaydb.core.Flyway import org.jetbrains.exposed.sql.Database +import org.jetbrains.exposed.sql.transactions.TransactionManager import org.springframework.beans.factory.annotation.Qualifier import org.springframework.beans.factory.annotation.Value import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy @@ -54,7 +55,9 @@ open class ApplikasjonDBConfigLocal(private val environment: Environment) { open fun database( @Qualifier("sykefravarsstatistikkDataSource") dataSource: DataSource, ): Database { - return Database.connect(dataSource) + val db = Database.connect(dataSource) + TransactionManager.defaultDatabase = db + return db } @Bean From 27bb0131a994e9dba512a11dc37877b6ccdcfb40 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 13:02:23 +0200 Subject: [PATCH 13/21] =?UTF-8?q?Pr=C3=B8v=20after=20each=20istedenfor=20b?= =?UTF-8?q?efore=20each?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../database/ImportEksportStatusRepositoryTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt index 26302a0f0..ff6d63cd9 100644 --- a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt +++ b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt @@ -6,7 +6,7 @@ import no.nav.arbeidsgiver.sykefravarsstatistikk.api.AppConfigForJdbcTesterConfi import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ÅrstallOgKvartal import org.jetbrains.exposed.sql.deleteAll import org.jetbrains.exposed.sql.transactions.transaction -import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import org.springframework.beans.factory.annotation.Autowired @@ -34,8 +34,8 @@ open class ImportEksportStatusRepositoryTest { forberedtNesteEksport = false, ) - @BeforeEach - fun beforeEach() { + @AfterEach + fun afterEach() { repo.slettAlt() } From fcf540685b60a946eb2d62679cf96c3bcf9e7335 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 13:26:03 +0200 Subject: [PATCH 14/21] =?UTF-8?q?Sett=20loggeniv=C3=A5=20under=20testing?= =?UTF-8?q?=20til=20debug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/config/ApplikasjonDBConfigLocal.kt | 1 + src/test/resources/logback-test.xml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt index 7cb72f32d..116d134be 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt @@ -40,6 +40,7 @@ open class ApplikasjonDBConfigLocal(private val environment: Environment) { config.password = password config.maximumPoolSize = 2 config.driverClassName = driverClassName + config.connectionTestQuery = "SELECT 1" return HikariDataSource(config) } diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index 2c21071bc..d9be38c31 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -15,7 +15,7 @@ - + From 48d840f2ce1a86c1b9ef1ec2666f93fb42fef09b Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 13:43:48 +0200 Subject: [PATCH 15/21] Repository -> Component --- .../infrastruktur/database/ImportEksportStatusRepository.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt index e5af7772f..c5634db62 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt @@ -6,9 +6,9 @@ import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.upsert -import org.springframework.stereotype.Repository +import org.springframework.stereotype.Component -@Repository +@Component open class ImportEksportStatusRepository : Table("import_eksport_status") { val årstall = varchar("aarstall", 4) val kvartal = varchar("kvartal", 1) From 8f369756b9b31894490a94966afc730ec9a6297c Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Fri, 11 Aug 2023 13:46:02 +0200 Subject: [PATCH 16/21] Rename --- .github/workflows/build-deploy.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build-deploy.yaml b/.github/workflows/build-deploy.yaml index ce817c7d1..3c9ee6819 100644 --- a/.github/workflows/build-deploy.yaml +++ b/.github/workflows/build-deploy.yaml @@ -36,9 +36,9 @@ jobs: image: ${{ steps.docker-push.outputs.image }} deploy-to-dev: - name: Deploy to dev-gcp + name: Deploy to dev-fss needs: docker-build-push - if: github.ref == 'refs/heads/arkitektur' + if: github.ref == 'refs/heads/tabell' runs-on: ubuntu-latest timeout-minutes: 5 steps: @@ -51,7 +51,7 @@ jobs: RESOURCE: nais/dev.yaml deploy-to-prod: - name: Deploy to dev-gcp + name: Deploy to prod-fss needs: docker-build-push if: github.ref == 'refs/heads/master' runs-on: ubuntu-latest @@ -71,7 +71,7 @@ jobs: if: github.ref == 'refs/heads/use_gar_distroless' steps: - uses: actions/checkout@v3 - - name: Deploy topics to dev-gcp + - name: Deploy topics to dev-fss uses: nais/deploy/actions/deploy@v1 env: APIKEY: ${{ secrets.NAIS_DEPLOY_APIKEY }} @@ -92,7 +92,7 @@ jobs: if: github.ref == 'refs/heads/master' steps: - uses: actions/checkout@v3 - - name: Deploy topics to prod-gcp + - name: Deploy topics to prod-fss uses: nais/deploy/actions/deploy@v1 env: APIKEY: ${{ secrets.NAIS_DEPLOY_APIKEY }} From f4a657776356fa1bb96d2c6263e28749fac1436c Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Mon, 14 Aug 2023 09:33:09 +0200 Subject: [PATCH 17/21] =?UTF-8?q?Skru=20p=C3=A5=20logging=20p=C3=A5=20Hika?= =?UTF-8?q?ri?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/config/ApplikasjonDBConfigLocal.kt | 2 +- src/main/resources/application-local.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt index 116d134be..67b5301b8 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt @@ -34,7 +34,7 @@ open class ApplikasjonDBConfigLocal(private val environment: Environment) { @Bean(name = ["sykefravarsstatistikkDataSource"]) open fun sykefravarsstatistikkDataSource(): DataSource { val config = HikariConfig() - config.poolName = "Sykefraværsstatistikk-connection-pool-local" + config.poolName = "sykefraværsstatistikk-connection-pool-local" config.jdbcUrl = databaseUrl config.username = username config.password = password diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index 40314e381..a46164cc2 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -25,7 +25,7 @@ statistikk.importering.aktivert: true statistikk.eksportering.aktivert: true applikasjon.datasource: - url: jdbc:h2:mem:db-local;MODE=PostgreSQL;DB_CLOSE_DELAY=-1 + url: jdbc:h2:mem:db-local;MODE=PostgreSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=3 username: SA password: driver-class-name: org.h2.Driver From e0dc48b82c9b643e187d50c7e5153bb86cda3ef6 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Mon, 14 Aug 2023 09:53:20 +0200 Subject: [PATCH 18/21] =?UTF-8?q?Sett=20eksplisitt=20connection=20timeout?= =?UTF-8?q?=20p=C3=A5=20connection=20pool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/config/ApplikasjonDBConfigLocal.kt | 2 +- src/main/resources/application-local.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt index 67b5301b8..d362fc67c 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/config/ApplikasjonDBConfigLocal.kt @@ -40,7 +40,7 @@ open class ApplikasjonDBConfigLocal(private val environment: Environment) { config.password = password config.maximumPoolSize = 2 config.driverClassName = driverClassName - config.connectionTestQuery = "SELECT 1" + config.connectionTimeout = 3000 return HikariDataSource(config) } diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index a46164cc2..40314e381 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -25,7 +25,7 @@ statistikk.importering.aktivert: true statistikk.eksportering.aktivert: true applikasjon.datasource: - url: jdbc:h2:mem:db-local;MODE=PostgreSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=3 + url: jdbc:h2:mem:db-local;MODE=PostgreSQL;DB_CLOSE_DELAY=-1 username: SA password: driver-class-name: org.h2.Driver From 7e9dd75fe7057aa64ef8082d3a5d85086b519612 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Mon, 14 Aug 2023 09:58:25 +0200 Subject: [PATCH 19/21] =?UTF-8?q?Sett=20loggniv=C3=A5=20tilbake=20til=20er?= =?UTF-8?q?ror?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/resources/logback-test.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index d9be38c31..2c21071bc 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -15,7 +15,7 @@ - + From 01329d27fef8db323ea3cfbc5dd8d25e7c474c9a Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Mon, 14 Aug 2023 10:37:03 +0200 Subject: [PATCH 20/21] Eksplisitt sett database --- .../database/ImportEksportStatusRepository.kt | 12 +++++++++--- .../database/ImportEksportStatusRepositoryTest.kt | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt index c5634db62..56161022f 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt @@ -2,14 +2,20 @@ package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur.database import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ImportEksportStatus import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller.ÅrstallOgKvartal +import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.upsert +import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate import org.springframework.stereotype.Component @Component -open class ImportEksportStatusRepository : Table("import_eksport_status") { +open class ImportEksportStatusRepository( + @Qualifier("sykefravarsstatistikkJdbcTemplate") private val namedParameterJdbcTemplate: NamedParameterJdbcTemplate, + val database: Database, +) : Table("import_eksport_status") { val årstall = varchar("aarstall", 4) val kvartal = varchar("kvartal", 1) val importertStatistikk = bool("importert_statistikk").default(false) @@ -19,7 +25,7 @@ open class ImportEksportStatusRepository : Table("import_eksport_status") { override val primaryKey: PrimaryKey = PrimaryKey(årstall, kvartal) fun settImportEksportStatus(importEksportStatus: ImportEksportStatus) { - transaction { + transaction(database) { upsert { it[årstall] = importEksportStatus.årstallOgKvartal.årstall.toString() it[kvartal] = importEksportStatus.årstallOgKvartal.kvartal.toString() @@ -34,7 +40,7 @@ open class ImportEksportStatusRepository : Table("import_eksport_status") { fun hentImportEksportStatus( årstallOgKvartal: ÅrstallOgKvartal ): List { - return transaction { + return transaction(database) { select { årstall eq årstallOgKvartal.årstall.toString() kvartal eq årstallOgKvartal.kvartal.toString() diff --git a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt index ff6d63cd9..1b2626d2b 100644 --- a/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt +++ b/src/test/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepositoryTest.kt @@ -59,7 +59,7 @@ open class ImportEksportStatusRepositoryTest { } private fun ImportEksportStatusRepository.slettAlt() { - transaction { + transaction(database) { deleteAll() } } From 3f76aa8adf6d00b67c43debd0c35f1a6a92e0121 Mon Sep 17 00:00:00 2001 From: Odd Gunnar Fatland Date: Mon, 14 Aug 2023 10:59:11 +0200 Subject: [PATCH 21/21] Lag interface for Exposed --- .../database/ImportEksportStatusRepository.kt | 12 ++++-------- .../api/infrastruktur/database/UsingExposed.kt | 11 +++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/UsingExposed.kt diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt index 56161022f..f3cdbc093 100644 --- a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/ImportEksportStatusRepository.kt @@ -5,17 +5,13 @@ import no.nav.arbeidsgiver.sykefravarsstatistikk.api.applikasjon.domenemodeller. import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.select -import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.upsert -import org.springframework.beans.factory.annotation.Qualifier -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate import org.springframework.stereotype.Component @Component open class ImportEksportStatusRepository( - @Qualifier("sykefravarsstatistikkJdbcTemplate") private val namedParameterJdbcTemplate: NamedParameterJdbcTemplate, - val database: Database, -) : Table("import_eksport_status") { + override val database: Database, +) : Table("import_eksport_status"), UsingExposed { val årstall = varchar("aarstall", 4) val kvartal = varchar("kvartal", 1) val importertStatistikk = bool("importert_statistikk").default(false) @@ -25,7 +21,7 @@ open class ImportEksportStatusRepository( override val primaryKey: PrimaryKey = PrimaryKey(årstall, kvartal) fun settImportEksportStatus(importEksportStatus: ImportEksportStatus) { - transaction(database) { + transaction { upsert { it[årstall] = importEksportStatus.årstallOgKvartal.årstall.toString() it[kvartal] = importEksportStatus.årstallOgKvartal.kvartal.toString() @@ -40,7 +36,7 @@ open class ImportEksportStatusRepository( fun hentImportEksportStatus( årstallOgKvartal: ÅrstallOgKvartal ): List { - return transaction(database) { + return transaction { select { årstall eq årstallOgKvartal.årstall.toString() kvartal eq årstallOgKvartal.kvartal.toString() diff --git a/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/UsingExposed.kt b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/UsingExposed.kt new file mode 100644 index 000000000..500ea6dac --- /dev/null +++ b/src/main/java/no/nav/arbeidsgiver/sykefravarsstatistikk/api/infrastruktur/database/UsingExposed.kt @@ -0,0 +1,11 @@ +package no.nav.arbeidsgiver.sykefravarsstatistikk.api.infrastruktur.database + +import org.jetbrains.exposed.sql.Database +import org.jetbrains.exposed.sql.Transaction +import org.jetbrains.exposed.sql.transactions.transaction + +interface UsingExposed { + val database: Database + + fun transaction(statement: Transaction.() -> T): T = transaction(database, statement) +}