From 3f2afb47714744e353d04e496ae96b9b19a83b6e Mon Sep 17 00:00:00 2001 From: Nils Martin Sande Date: Mon, 28 Oct 2024 21:24:13 +0100 Subject: [PATCH] =?UTF-8?q?Endret=20til=20=C3=A5=20bare=20telle=20antall?= =?UTF-8?q?=20utest=C3=A5ende=20merges?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kafkakeygenerator/ktor/helse_endepunkt.kt | 2 +- .../kafkakeygenerator/merge/MergeDetector.kt | 19 ++--- .../merge/MergeDetectorKtTest.kt | 70 +++++++++++++++++++ 3 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 apps/kafka-key-generator/src/test/kotlin/no/nav/paw/kafkakeygenerator/merge/MergeDetectorKtTest.kt diff --git a/apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/ktor/helse_endepunkt.kt b/apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/ktor/helse_endepunkt.kt index d4ab9f81..1ebc0561 100644 --- a/apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/ktor/helse_endepunkt.kt +++ b/apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/ktor/helse_endepunkt.kt @@ -27,7 +27,7 @@ fun Routing.konfigurereHelse( call.respondText( mergeDetector .findMerges(900) - .map { "Number of pending merges: ${it.size } "} + .map { "Number of pending merges: $it "} .onRight { mergeLogger.info(it) } .onLeft { mergeLogger.error("Error: ${it.system}:${it.code}", it.exception) } .fold( { "Error: ${it.system}:${it.code}" }, { it } ) diff --git a/apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/merge/MergeDetector.kt b/apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/merge/MergeDetector.kt index a7358069..6e61519f 100644 --- a/apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/merge/MergeDetector.kt +++ b/apps/kafka-key-generator/src/main/kotlin/no/nav/paw/kafkakeygenerator/merge/MergeDetector.kt @@ -11,7 +11,7 @@ class MergeDetector( private val pdlIdentitesTjeneste: PdlIdentitesTjeneste, private val kafkaKeys: KafkaKeys ) { - suspend fun findMerges(batchSize: Int): Either> { + suspend fun findMerges(batchSize: Int): Either { require(batchSize > 0) { "Batch size must be greater than 0" } return kafkaKeys.hentSisteArbeidssoekerId() .map { it.value } @@ -20,7 +20,7 @@ class MergeDetector( stopAt = max, maxSize = batchSize, currentPos = 0L, - right(emptyList()) + right(0L) ) } } @@ -29,8 +29,8 @@ class MergeDetector( stopAt: Long, maxSize: Int, currentPos: Long, - results: Either> - ): Either> { + results: Either + ): Either { return when (results) { is Left -> { return results @@ -45,9 +45,10 @@ class MergeDetector( .suspendingFlatMap { pdlIdentitesTjeneste.hentIdenter(it.keys.toList()).map { res -> it to res } } - .map { (local, pdl) -> - detectMerges(local, pdl) - }.map(results.right::plus) + .map { (local, pdl) -> detectMerges(local, pdl) } + .map(Sequence::count) + .map(Int::toLong) + .map(results.right::plus) val newStart = storedData.fold({ -1L }, { it.values.maxOfOrNull(ArbeidssoekerId::value)?.plus(1) ?: -1 }) processRange(stopAt, maxSize, newStart, detected) @@ -60,7 +61,7 @@ class MergeDetector( fun detectMerges( local: Map, pdl: Map> -): List { +): Sequence { return pdl.asSequence() .mapNotNull { (searchedId, resultIds) -> val arbIds = resultIds @@ -78,7 +79,7 @@ fun detectMerges( } else { null } - }.toList() + } } suspend fun Either.suspendingFlatMap(f: suspend (R) -> Either): Either = diff --git a/apps/kafka-key-generator/src/test/kotlin/no/nav/paw/kafkakeygenerator/merge/MergeDetectorKtTest.kt b/apps/kafka-key-generator/src/test/kotlin/no/nav/paw/kafkakeygenerator/merge/MergeDetectorKtTest.kt new file mode 100644 index 00000000..5dee0b65 --- /dev/null +++ b/apps/kafka-key-generator/src/test/kotlin/no/nav/paw/kafkakeygenerator/merge/MergeDetectorKtTest.kt @@ -0,0 +1,70 @@ +package no.nav.paw.kafkakeygenerator.merge + +import io.kotest.core.spec.style.FreeSpec +import io.kotest.matchers.collections.shouldBeEmpty +import io.kotest.matchers.sequences.shouldBeEmpty +import io.kotest.matchers.should +import io.kotest.matchers.shouldBe +import no.nav.paw.kafkakeygenerator.vo.ArbeidssoekerId +import no.nav.paw.kafkakeygenerator.vo.Identitetsnummer +import no.nav.paw.pdl.graphql.generated.enums.IdentGruppe +import no.nav.paw.pdl.graphql.generated.hentidenter.IdentInformasjon +import org.junit.jupiter.api.Test + +import org.junit.jupiter.api.Assertions.* + +class MergeDetectorKtTest : FreeSpec({ + + "Når 2 identiteter er samme person i PDL, men har forskjellige arbeidssøkerId skal det slå ut som 'merge detected" { + val local = mapOf( + Identitetsnummer("1") to ArbeidssoekerId(11), + Identitetsnummer("2") to ArbeidssoekerId(12) + ) + val pdl = mapOf( + "1" to listOf( + IdentInformasjon( + ident = "1", + gruppe = IdentGruppe.FOLKEREGISTERIDENT, + historisk = false + ), + IdentInformasjon( + ident = "2", + gruppe = IdentGruppe.FOLKEREGISTERIDENT, + historisk = false + ) + ) + ) + detectMerges( + local = local, + pdl = pdl + ) should { merges -> + merges.toList().size shouldBe 1 + } + } + + "Når 2 identiteter er samme person i PDL, og har arbeidssøkerId skal det ikke slå ut som 'merge detected" { + val local = mapOf( + Identitetsnummer("1") to ArbeidssoekerId(11), + Identitetsnummer("2") to ArbeidssoekerId(11) + ) + val pdl = mapOf( + "1" to listOf( + IdentInformasjon( + ident = "1", + gruppe = IdentGruppe.FOLKEREGISTERIDENT, + historisk = false + ), + IdentInformasjon( + ident = "2", + gruppe = IdentGruppe.FOLKEREGISTERIDENT, + historisk = false + ) + ) + ) + detectMerges( + local = local, + pdl = pdl + ).shouldBeEmpty() + } + +}) \ No newline at end of file