|
| 1 | +package no.nav.dagpenger.inntekt.db |
| 2 | + |
| 3 | +import de.huxhorn.sulky.ulid.ULID |
| 4 | +import io.kotest.matchers.collections.shouldContainAll |
| 5 | +import io.kotest.matchers.ints.shouldBeExactly |
| 6 | +import java.time.LocalDate |
| 7 | +import java.time.ZonedDateTime |
| 8 | +import kotlin.random.Random |
| 9 | +import kotliquery.LoanPattern.using |
| 10 | +import kotliquery.queryOf |
| 11 | +import kotliquery.sessionOf |
| 12 | +import no.nav.dagpenger.inntekt.DataSource as TestDataSource |
| 13 | +import no.nav.dagpenger.inntekt.inntektskomponenten.v1.Aktoer |
| 14 | +import no.nav.dagpenger.inntekt.inntektskomponenten.v1.AktoerType |
| 15 | +import no.nav.dagpenger.inntekt.inntektskomponenten.v1.InntektkomponentResponse |
| 16 | +import no.nav.dagpenger.inntekt.withMigratedDb |
| 17 | +import org.junit.jupiter.api.Test |
| 18 | +import org.postgresql.util.PGobject |
| 19 | + |
| 20 | +internal class ArenaMappingMigratorTest { |
| 21 | + |
| 22 | + companion object { |
| 23 | + |
| 24 | + private val hentInntektListeResponse = InntektkomponentResponse( |
| 25 | + emptyList(), |
| 26 | + Aktoer(AktoerType.AKTOER_ID, "1234") |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `Skal migrere data fra arena mapping til person tabell `() { |
| 32 | + withMigratedDb { |
| 33 | + val numberOfInserts = 100 |
| 34 | + val commands = (1..numberOfInserts).toList().map { InntektId(ULID().nextULID()) to StoreInntektCommand( |
| 35 | + inntektparametre = Inntektparametre(aktørId = Random.nextInt().toString(), vedtakId = Random.nextLong().toString(), beregningsdato = LocalDate.now()), |
| 36 | + inntekt = hentInntektListeResponse |
| 37 | + ) } |
| 38 | + fillArenaMappingTable(TestDataSource.instance, commands) |
| 39 | + val arenaMappingMigrator = ArenaMappingMigrator(TestDataSource.instance) |
| 40 | + val rowsMigrated = arenaMappingMigrator.migrate() |
| 41 | + |
| 42 | + val inntektStore = PostgresInntektStore(TestDataSource.instance) |
| 43 | + |
| 44 | + val ids = commands.mapNotNull { (_, command) -> |
| 45 | + inntektStore.getInntektId( |
| 46 | + inntektparametre = command.inntektparametre |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + rowsMigrated shouldBeExactly numberOfInserts |
| 51 | + ids.size shouldBeExactly numberOfInserts |
| 52 | + ids shouldContainAll commands.map { it.first } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `Skal migrere data fra arena mapping til person tabell med samme vedtak id og aktør id men forskjellig beregningsdato`() { |
| 58 | + withMigratedDb { |
| 59 | + val numberOfInserts = 4 |
| 60 | + val commandsWithSameVedtakId: List<Pair<InntektId, StoreInntektCommand>> = (1..4).toList().map { InntektId(ULID().nextULID()) to StoreInntektCommand( |
| 61 | + inntektparametre = Inntektparametre(aktørId = "1234", vedtakId = "5678", beregningsdato = LocalDate.now().minusDays(Random.nextInt(365).toLong())), |
| 62 | + inntekt = hentInntektListeResponse |
| 63 | + ) } |
| 64 | + fillArenaMappingTable(TestDataSource.instance, commandsWithSameVedtakId) |
| 65 | + val arenaMappingMigrator = ArenaMappingMigrator(TestDataSource.instance) |
| 66 | + val rowsMigrated = arenaMappingMigrator.migrate() |
| 67 | + |
| 68 | + val inntektStore = PostgresInntektStore(TestDataSource.instance) |
| 69 | + |
| 70 | + val ids = commandsWithSameVedtakId.mapNotNull { (_, command) -> |
| 71 | + inntektStore.getInntektId( |
| 72 | + inntektparametre = command.inntektparametre |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + rowsMigrated shouldBeExactly numberOfInserts |
| 77 | + ids.size shouldBeExactly numberOfInserts |
| 78 | + ids shouldContainAll commandsWithSameVedtakId.map { it.first } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun ` Migrere kan skje flere ganger `() { |
| 84 | + |
| 85 | + withMigratedDb { |
| 86 | + val numberOfInserts = 100 |
| 87 | + val commands = (1..numberOfInserts).toList().map { |
| 88 | + InntektId(ULID().nextULID()) to StoreInntektCommand( |
| 89 | + inntektparametre = Inntektparametre( |
| 90 | + aktørId = Random.nextInt().toString(), |
| 91 | + vedtakId = Random.nextLong().toString(), |
| 92 | + beregningsdato = LocalDate.now() |
| 93 | + ), |
| 94 | + inntekt = hentInntektListeResponse |
| 95 | + ) |
| 96 | + } |
| 97 | + fillArenaMappingTable(TestDataSource.instance, commands) |
| 98 | + val arenaMappingMigrator = ArenaMappingMigrator(TestDataSource.instance) |
| 99 | + val rowsMigrated = arenaMappingMigrator.migrate() |
| 100 | + val rowsMigratedSecondTime = arenaMappingMigrator.migrate() |
| 101 | + |
| 102 | + val inntektStore = PostgresInntektStore(TestDataSource.instance) |
| 103 | + |
| 104 | + val ids = commands.mapNotNull { (_, command) -> |
| 105 | + inntektStore.getInntektId( |
| 106 | + inntektparametre = command.inntektparametre |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + rowsMigrated shouldBeExactly numberOfInserts |
| 111 | + rowsMigratedSecondTime shouldBeExactly 0 |
| 112 | + ids.size shouldBeExactly numberOfInserts |
| 113 | + ids shouldContainAll commands.map { it.first } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private fun fillArenaMappingTable(dataSource: javax.sql.DataSource, commands: List<Pair<InntektId, StoreInntektCommand>>) { |
| 118 | + commands.forEach { (id, command) -> |
| 119 | + using(sessionOf(dataSource)) { session -> |
| 120 | + session.transaction { tx -> |
| 121 | + tx.run( |
| 122 | + queryOf( |
| 123 | + "INSERT INTO inntekt_V1 (id, inntekt, manuelt_redigert, timestamp) VALUES (:id, :data, :manuelt, :created)", |
| 124 | + mapOf( |
| 125 | + "id" to id.id, |
| 126 | + "created" to ZonedDateTime.now(), |
| 127 | + "data" to PGobject().apply { |
| 128 | + type = "jsonb" |
| 129 | + value = PostgresInntektStore.adapter.toJson(command.inntekt) |
| 130 | + }, |
| 131 | + when (command.manueltRedigert) { |
| 132 | + null -> "manuelt" to false |
| 133 | + else -> "manuelt" to true |
| 134 | + } |
| 135 | + ) |
| 136 | + |
| 137 | + ).asUpdate |
| 138 | + ) |
| 139 | + tx.run( |
| 140 | + queryOf( |
| 141 | + "INSERT INTO inntekt_V1_arena_mapping VALUES (:inntektId, :aktorId, :vedtakId, :beregningsDato)", |
| 142 | + mapOf( |
| 143 | + "inntektId" to id.id, |
| 144 | + "aktorId" to command.inntektparametre.aktørId, |
| 145 | + "vedtakId" to command.inntektparametre.vedtakId.toLong(), |
| 146 | + "beregningsDato" to command.inntektparametre.beregningsdato |
| 147 | + ) |
| 148 | + ).asUpdate |
| 149 | + ) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments