Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
AudunSorheim committed Apr 10, 2024
1 parent 3b6921c commit 6e5013a
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 89 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ val prometheusVersion = "0.16.0"
val micrometerVersion = "1.12.5"
val kotestVersion = "5.8.1"
val kotestExtensionsVersion = "2.0.0"
val h2Version = "2.2.224"
val mockkVersion = "1.13.10"
val slf4jVersion = "2.0.12"
val logbackVersion = "1.5.4"
Expand All @@ -19,7 +20,6 @@ val hikariVersion = "5.1.0"
val flywayVersion = "10.10.0"
val vaultJdbcVersion = "1.3.9"
val jacksonVersion = "2.17.0"
val postgresEmbeddedVersion = "1.0.3"
val kafkaVersion = "3.6.0"
val avroVersion = "1.11.3"
val confluentVersion = "7.6.0"
Expand Down Expand Up @@ -126,13 +126,13 @@ dependencies {
testImplementation(kotlin("test"))
testImplementation("org.amshove.kluent:kluent:$kluentVersion")
testImplementation("io.mockk:mockk:$mockkVersion")
testImplementation("com.opentable.components:otj-pg-embedded:$postgresEmbeddedVersion")
testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion")
testImplementation("io.ktor:ktor-server-test-host-jvm:$ktorVersion")
testImplementation("io.kotest:kotest-runner-junit5-jvm:$kotestVersion")
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion")
testImplementation("io.kotest:kotest-property:$kotestVersion")
testImplementation("io.kotest.extensions:kotest-assertions-ktor:$kotestExtensionsVersion")
testImplementation("com.h2database:h2:$h2Version")

constraints {
implementation("org.apache.zookeeper:zookeeper") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
ALTER TABLE UTSENDT_VARSEL
ADD COLUMN narmesteleder_fnr varchar(11),
ADD COLUMN orgnummer varchar(9),
ADD COLUMN kanal varchar(50);

ALTER TABLE UTSENDT_VARSEL
ALTER COLUMN aktor_id DROP NOT NULL;
ALTER TABLE UTSENDT_VARSEL ADD COLUMN narmesteleder_fnr varchar(11);
ALTER TABLE UTSENDT_VARSEL ADD COLUMN orgnummer varchar(9);
ALTER TABLE UTSENDT_VARSEL ADD COLUMN kanal varchar(50);
ALTER TABLE UTSENDT_VARSEL ALTER COLUMN aktor_id DROP NOT NULL;
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
Removed to support H2 database
DO $$
BEGIN
CREATE USER "esyfo-analyse";
Expand All @@ -7,3 +10,4 @@ END
$$;
GRANT SELECT ON ALL TABLES IN SCHEMA PUBLIC TO "esyfo-analyse";
*/
6 changes: 1 addition & 5 deletions src/test/kotlin/no/nav/syfo/db/FodselsdatoDAOSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import org.amshove.kluent.*

class FodselsdatoDAOSpek : DescribeSpec({
describe("FodselsdatoDAOSpek") {
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()
beforeTest {
embeddedDatabase.connection.dropData()
}

afterTest {
embeddedDatabase.stop()
}

it("Should fetch birthdate") {
val fodselsdato = "1988-01-01"
embeddedDatabase.storeFodselsdato(arbeidstakerFnr1, fodselsdato)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ import java.time.LocalDate

class MikrofrontendSynlighetDAOSpek : DescribeSpec({
describe("MikrofrontendSynlighetDAOSpek") {
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()

afterTest {
beforeTest {
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}

val mikrofrontendSynlighet1 = MikrofrontendSynlighet(
arbeidstakerFnr1,
Tjeneste.DIALOGMOTE,
Expand Down
8 changes: 2 additions & 6 deletions src/test/kotlin/no/nav/syfo/db/PlanlagtVarselDAOSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ import java.time.LocalDate
class PlanlagtVarselDAOSpek : DescribeSpec({
describe("PlanlagtVarselDAOSpek") {

val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()

afterTest {
beforeTest {
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}

it("Store and fetch PlanlagtVarsel") {
val planlagtVarselToStore1 = PlanlagtVarsel(
arbeidstakerFnr1,
Expand Down
8 changes: 2 additions & 6 deletions src/test/kotlin/no/nav/syfo/db/UtbetalingerDAOSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ import java.util.*

class UtbetalingerDAOSpek : DescribeSpec({
describe("UtbetalingerDAOSpek") {
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()
val nowPlus1Day = now().plusDays(1)
val nowPlus2Days = now().plusDays(2)
val nowMinus1Day = now().minusDays(1)
val nowMinus2Days = now().minusDays(2)

afterTest {
beforeTest {
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}

it("Should include utbetaling with max date 14 days in the future, and gjenstående sykedager < 91") {
val spleisUtbetaling =
spleisUtbetaling(forelopigBeregnetSluttPaSykepenger = now().plusDays(14), gjenstaendeSykedager = 90)
Expand Down
8 changes: 2 additions & 6 deletions src/test/kotlin/no/nav/syfo/db/UtsendtVarselFeiletDAOSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ import java.util.*

class UtsendtVarselFeiletDAOSpek : DescribeSpec({
describe("UtsendtVarselFeiletDAOSpek") {
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()

afterTest {
beforeTest {
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}

it("Store ikke-utsendt varsel til NL i database") {
val fnr = "12121212121"
embeddedDatabase.storeUtsendtVarselFeilet(ikkeUtsendtVarsel(fnr))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DialogmoteInnkallingVarselServiceSpek : DescribeSpec({
val brukernotifikasjonerService = mockk<BrukernotifikasjonerService>()
val arbeidsgiverNotifikasjonService = mockk<ArbeidsgiverNotifikasjonService>()
val fysiskBrevUtsendingService = mockk<FysiskBrevUtsendingService>()
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()
val fakeDialogmoterUrl = "http://localhost/dialogmoter"
val journalpostUuid = "97b886fe-6beb-40df-af2b-04e504bc340c"
val journalpostId = "1"
Expand Down Expand Up @@ -59,14 +59,10 @@ class DialogmoteInnkallingVarselServiceSpek : DescribeSpec({
justRun { dittSykefravaerMeldingKafkaProducer.ferdigstillMelding(any(), any()) }
coJustRun { fysiskBrevUtsendingService.sendBrev(any(), any(), DistibusjonsType.ANNET) }

afterTest {
beforeTest {
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}

it("Non-reserved users should be notified externally") {
coEvery { accessControlService.getUserAccessStatus(fnr1) } returns
UserAccessStatus(fnr1, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.util.*

class MerVeiledningVarselFinderSpek : DescribeSpec({

val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()
val sykmeldingerConsumerMock: SykmeldingerConsumer = mockk(relaxed = true)
val sykmeldingServiceMockk = SykmeldingService(sykmeldingerConsumerMock)
val pdlConsumerMockk: PdlConsumer = mockk(relaxed = true)
Expand Down Expand Up @@ -65,15 +65,11 @@ class MerVeiledningVarselFinderSpek : DescribeSpec({
)

describe("MerVeiledningVarselFinderSpek") {
afterTest {
beforeTest {
clearAllMocks()
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}

it("Should send MER_VEILEDNING when it was not sent during past 3 months") {
coEvery { pdlConsumerMockk.isBrukerYngreEnnGittMaxAlder(any(), any()) } returns true
coEvery { sykmeldingServiceMockk.isPersonSykmeldtPaDato(LocalDate.now(), arbeidstakerFnr1) } returns true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class MikrofrontendServiceSpek : DescribeSpec({
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()
val minSideMicrofrontendKafkaProducer: MinSideMicrofrontendKafkaProducer = mockk(relaxed = true)
val mikrofrontendDialogmoteService = MikrofrontendDialogmoteService(embeddedDatabase)
val mikrofrontendAktivitetskravService = MikrofrontendAktivitetskravService(embeddedDatabase)
Expand All @@ -37,15 +37,11 @@ class MikrofrontendServiceSpek : DescribeSpec({
embeddedDatabase
)

afterTest {
beforeTest {
clearAllMocks()
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}

describe("MikrofrontendServiceSpek") {
justRun { minSideMicrofrontendKafkaProducer.sendRecordToMinSideTopic(any()) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class OppfolgingsplanVarselServiceSpek : DescribeSpec({
val brukernotifikasjonerService = mockk<BrukernotifikasjonerService>()
val arbeidsgiverNotifikasjonService = mockk<ArbeidsgiverNotifikasjonService>()
val fysiskBrevUtsendingService = mockk<FysiskBrevUtsendingService>()
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()
val fakeOppfolgingsplanerUrl = "http://localhost/oppfolgingsplaner"

val senderFacade = SenderFacade(
Expand Down
9 changes: 2 additions & 7 deletions src/test/kotlin/no/nav/syfo/service/SenderFacadeSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.util.*

class SenderFacadeSpek : DescribeSpec({
describe("SenderFacadeSpek") {
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()

val arbeidsgiverNotifikasjonService: ArbeidsgiverNotifikasjonService = mockk(relaxed = true)
val dineSykmeldteHendelseKafkaProducer: DineSykmeldteHendelseKafkaProducer = mockk(relaxed = true)
Expand Down Expand Up @@ -86,16 +86,11 @@ class SenderFacadeSpek : DescribeSpec({
eksternReferanse = eksternRefDittSykefravaer,
)

afterTest {
beforeTest {
clearAllMocks()
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}


it("Complete notifications for user") {

embeddedDatabase.storeUtsendtVarsel(arbeidsgivernotifikasjonUtsendtVarsel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.test.assertEquals

class SykepengerMaxDateServiceSpek : DescribeSpec({
describe("SykepengerMaxDateService") {
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()
val pdlConsumer = mockk<PdlConsumer>(relaxed = true)
val sykepengerMaxDateService = SykepengerMaxDateService(embeddedDatabase, pdlConsumer)
coEvery { pdlConsumer.hentPerson(any()) } returns PdlHentPerson(
Expand All @@ -27,14 +27,10 @@ class SykepengerMaxDateServiceSpek : DescribeSpec({
foedsel = listOf(PdlFoedsel("1986-01-01"))
)
)
afterTest {
beforeTest {
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}

it("Should store spleis utbetaling") {
val utbetalingUtbetalt = UtbetalingSpleis(
fødselsnummer = "123",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.util.*

class TestdataResetServiceSpek : DescribeSpec({
describe("TestdataResetServiceSpek") {
val embeddedDatabase by lazy { EmbeddedDatabase() }
val embeddedDatabase = EmbeddedDatabase()
val mikrofrontendService: MikrofrontendService = mockk(relaxed = true)
val senderFacade: SenderFacade = mockk(relaxed = true)
val testdataResetService = TestdataResetService(embeddedDatabase, mikrofrontendService, senderFacade)
Expand Down Expand Up @@ -83,15 +83,10 @@ class TestdataResetServiceSpek : DescribeSpec({
val mikrofrontendSynlighet =
MikrofrontendSynlighet(arbeidstakerFnr1, Tjeneste.DIALOGMOTE, LocalDate.now().plusWeeks(1))

afterTest {
beforeTest {
embeddedDatabase.connection.dropData()
}

afterSpec {
embeddedDatabase.stop()
}


it("Reset all testdata") {
embeddedDatabase.storePlanlagtVarsel(planlagtVarsel)
embeddedDatabase.storeUtsendtVarsel(utsendtVarsel)
Expand Down
34 changes: 22 additions & 12 deletions src/test/kotlin/no/nav/syfo/testutil/EmbeddedDatabase.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
package no.nav.syfo.testutil

import com.opentable.db.postgres.embedded.EmbeddedPostgres
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import no.nav.syfo.db.DatabaseInterface
import org.flywaydb.core.Flyway
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.sql.Connection

class EmbeddedDatabase : DatabaseInterface {
private val pg: EmbeddedPostgres

override val connection: Connection
get() = pg.postgresDatabase.connection.apply { autoCommit = false }
override val log: Logger = LoggerFactory.getLogger(EmbeddedDatabase::class.qualifiedName)
private val dataSource: HikariDataSource

init {
pg = EmbeddedPostgres.start()
val config = HikariConfig().apply {
jdbcUrl = "jdbc:h2:mem:lps-db;MODE=PostgreSQL;DB_CLOSE_DELAY=-1;"
username = "sa"
password = ""
maximumPoolSize = 2
minimumIdle = 1
isAutoCommit = false
transactionIsolation = "TRANSACTION_REPEATABLE_READ"
validate()
}

Flyway.configure().connectRetries(3).run {
dataSource(pg.postgresDatabase).load().migrate()
dataSource = HikariDataSource(config)

Flyway.configure().dataSource(dataSource).load().apply {
migrate()
validate()
}
}

fun stop() {
pg.close()
}
override val connection: Connection
get() = dataSource.connection.apply { autoCommit = false }
override val log: Logger
get() = LoggerFactory.getLogger(EmbeddedDatabase::class.qualifiedName)
}

fun Connection.dropData() {
Expand Down

0 comments on commit 6e5013a

Please sign in to comment.