Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publiser meldinger med Kafka-key i API-app #782

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import no.nav.helse.rapids_rivers.RapidApplication
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisConnection
import no.nav.helsearbeidsgiver.felles.rapidsrivers.registerShutdownLifecycle
import no.nav.helsearbeidsgiver.inntektsmelding.api.aktiveorgnr.aktiveOrgnrRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.auth.TilgangProducer
import no.nav.helsearbeidsgiver.inntektsmelding.api.auth.Tilgangskontroll
import no.nav.helsearbeidsgiver.inntektsmelding.api.hentforespoersel.hentForespoersel
import no.nav.helsearbeidsgiver.inntektsmelding.api.hentforespoerselIdListe.hentForespoerselIdListe
Expand All @@ -29,7 +30,6 @@ import no.nav.helsearbeidsgiver.inntektsmelding.api.inntekt.inntektRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.inntektselvbestemt.inntektSelvbestemtRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.kvittering.kvittering
import no.nav.helsearbeidsgiver.inntektsmelding.api.lagreselvbestemtim.lagreSelvbestemtImRoute
import no.nav.helsearbeidsgiver.inntektsmelding.api.tilgang.TilgangProducer
import no.nav.helsearbeidsgiver.inntektsmelding.api.tilgangorgnr.tilgangOrgnrRoute
import no.nav.helsearbeidsgiver.utils.cache.LocalCache
import no.nav.helsearbeidsgiver.utils.json.jsonConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AktiveOrgnrProducer(
) {
rapid
.publish(
key = arbeidstagerFnr,
Key.EVENT_NAME to EventName.AKTIVE_ORGNR_REQUESTED.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.helsearbeidsgiver.inntektsmelding.api.tilgang
package no.nav.helsearbeidsgiver.inntektsmelding.api.auth
bjerga marked this conversation as resolved.
Show resolved Hide resolved

import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import kotlinx.serialization.json.JsonElement
Expand All @@ -15,6 +15,7 @@ import no.nav.helsearbeidsgiver.utils.log.MdcUtils
import no.nav.helsearbeidsgiver.utils.wrapper.Fnr
import java.util.UUID

// TODO test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skal denne være med?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tok den med som en påminnelse til fremtiden når jeg tilfeldigvis så at den klassen manglet tester.

class TilgangProducer(
private val rapid: RapidsConnection,
) {
Expand All @@ -29,7 +30,7 @@ class TilgangProducer(
) = publish(
EventName.TILGANG_FORESPOERSEL_REQUESTED,
transaksjonId,
Key.FNR to fnr.toJson(),
fnr,
Key.FORESPOERSEL_ID to forespoerselId.toJson(),
)

Expand All @@ -40,14 +41,15 @@ class TilgangProducer(
) = publish(
EventName.TILGANG_ORG_REQUESTED,
transaksjonId,
Key.FNR to fnr.toJson(),
fnr,
Key.ORGNRUNDERENHET to orgnr.toJson(),
)

private fun publish(
eventName: EventName,
transaksjonId: UUID,
vararg messageFields: Pair<Key, JsonElement>,
fnr: Fnr,
dataField: Pair<Key, JsonElement>,
) {
MdcUtils.withLogFields(
Log.klasse(this),
Expand All @@ -56,9 +58,14 @@ class TilgangProducer(
) {
rapid
.publish(
key = fnr,
Key.EVENT_NAME to eventName.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to messageFields.toMap().toJson(),
Key.DATA to
mapOf(
Key.FNR to fnr.toJson(),
dataField,
).toJson(),
).also { json ->
"Publiserte request om tilgang.".let {
logger.info(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisPrefix
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisStore
import no.nav.helsearbeidsgiver.inntektsmelding.api.RedisPoller
import no.nav.helsearbeidsgiver.inntektsmelding.api.logger
import no.nav.helsearbeidsgiver.inntektsmelding.api.tilgang.TilgangProducer
import no.nav.helsearbeidsgiver.utils.cache.LocalCache
import no.nav.helsearbeidsgiver.utils.json.fromJson
import no.nav.helsearbeidsgiver.utils.wrapper.Fnr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class HentForespoerselProducer(
) {
rapid
.publish(
key = request.uuid,
Key.EVENT_NAME to EventName.TRENGER_REQUESTED.toJson(EventName.serializer()),
Key.UUID to transaksjonId.toString().toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class HentForespoerslerProducer(
) {
rapid
.publish(
key = UUID.randomUUID(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Her har vi ingen naturlig nøkkel, men vi det er heller ikke så farlig når vi kun skal lese data.

Key.EVENT_NAME to EventName.FORESPOERSLER_REQUESTED.toJson(EventName.serializer()),
Key.UUID to transaksjonId.toString().toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class HentSelvbestemtImProducer(
) {
rapid
.publish(
key = selvbestemtId,
Key.EVENT_NAME to EventName.SELVBESTEMT_IM_REQUESTED.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class InnsendingProducer(
) {
rapid
.publish(
key = skjemaInntektsmelding.forespoerselId,
Key.EVENT_NAME to EventName.INSENDING_STARTED.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class InntektProducer(
) {
rapid
.publish(
key = request.forespoerselId,
Key.EVENT_NAME to EventName.INNTEKT_REQUESTED.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class InntektSelvbestemtProducer(
) {
rapid
.publish(
key = request.sykmeldtFnr,
Key.EVENT_NAME to EventName.INNTEKT_SELVBESTEMT_REQUESTED.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class KvitteringProducer(
) {
rapid
.publish(
key = forespoerselId,
Key.EVENT_NAME to EventName.KVITTERING_REQUESTED.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class LagreSelvbestemtImProducer(
) {
rapid
.publish(
key = skjema.sykmeldtFnr,
Key.EVENT_NAME to EventName.SELVBESTEMT_IM_MOTTATT.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,25 @@ import kotlinx.serialization.json.JsonNull
import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.utils.json.parseJson
import no.nav.helsearbeidsgiver.utils.json.toJson
import no.nav.helsearbeidsgiver.utils.wrapper.Fnr
import java.util.UUID

fun MessageContext.publish(vararg messageFields: Pair<Key, JsonElement>): JsonElement = publish(messageFields.toMap())
fun MessageContext.publish(vararg messageFields: Pair<Key, JsonElement>): JsonElement = publish(null, messageFields.toMap())

fun MessageContext.publish(messageFields: Map<Key, JsonElement>): JsonElement =
fun MessageContext.publish(
key: Fnr,
vararg messageFields: Pair<Key, JsonElement>,
): JsonElement = publish(key.verdi, messageFields.toMap())

fun MessageContext.publish(
key: UUID,
vararg messageFields: Pair<Key, JsonElement>,
): JsonElement = publish(key.toString(), messageFields.toMap())

internal fun MessageContext.publish(
key: String?,
messageFields: Map<Key, JsonElement>,
): JsonElement =
messageFields
.mapKeys { (key, _) -> key.toString() }
.filterValues { it !is JsonNull }
Expand All @@ -27,5 +42,10 @@ fun MessageContext.publish(messageFields: Map<Key, JsonElement>): JsonElement =
randomIdGenerator = null,
)
}.toJson()
.also(::publish)
.parseJson()
.also {
if (key == null) {
publish(it)
} else {
publish(key, it)
}
}.parseJson()
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ internal class OpenRiver(
.toJson()
.parseJson()
.haandterMelding()
?.also(context::publish)
?.also { context.publish(null, it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import no.nav.helsearbeidsgiver.felles.json.toMap
import no.nav.helsearbeidsgiver.felles.test.mock.mockInntektsmeldingV1
import no.nav.helsearbeidsgiver.utils.json.parseJson
import no.nav.helsearbeidsgiver.utils.json.toJson
import no.nav.helsearbeidsgiver.utils.test.wrapper.genererGyldig
import no.nav.helsearbeidsgiver.utils.wrapper.Fnr
import java.util.UUID

class RiverUtilsKtTest :
Expand All @@ -28,7 +30,7 @@ class RiverUtilsKtTest :

context("publish") {

test("vararg pairs") {
test("vararg pairs (uten key)") {
val melding =
arrayOf(
Key.FORESPOERSEL_ID to UUID.randomUUID().toJson(),
Expand All @@ -47,18 +49,81 @@ class RiverUtilsKtTest :
}
}

test("vararg pairs (fnr-key)") {
val key = Fnr.genererGyldig()
val melding =
arrayOf(
Key.FORESPOERSEL_ID to UUID.randomUUID().toJson(),
Key.INNTEKTSMELDING to mockInntektsmeldingV1().toJson(Inntektsmelding.serializer()),
Key.FNR_LISTE to setOf("111", "333", "555").toJson(String.serializer()),
)

testRapid.publish(key, *melding)

verifySequence {
testRapid.publish(
key.toString(),
withArg<String> {
it.parseJson().toMap() shouldContainExactly melding.toMap()
},
)
}
}

test("vararg pairs (UUID-key)") {
val key = UUID.randomUUID()
val melding =
arrayOf(
Key.FORESPOERSEL_ID to UUID.randomUUID().toJson(),
Key.INNTEKTSMELDING to mockInntektsmeldingV1().toJson(Inntektsmelding.serializer()),
Key.FNR_LISTE to setOf("555", "333", "111").toJson(String.serializer()),
)

testRapid.publish(key, *melding)

verifySequence {
testRapid.publish(
key.toString(),
withArg<String> {
it.parseJson().toMap() shouldContainExactly melding.toMap()
},
)
}
}

test("map (uten key)") {
val melding =
mapOf(
Key.FORESPOERSEL_ID to UUID.randomUUID().toJson(),
Key.INNTEKTSMELDING to mockInntektsmeldingV1().toJson(Inntektsmelding.serializer()),
Key.ORGNR_UNDERENHETER to setOf("666", "444", "222").toJson(String.serializer()),
)

testRapid.publish(null, melding)

verifySequence {
testRapid.publish(
withArg<String> {
it.parseJson().toMap() shouldContainExactly melding
},
)
}
}

test("map") {
val key = UUID.randomUUID()
val melding =
mapOf(
Key.FORESPOERSEL_ID to UUID.randomUUID().toJson(),
Key.INNTEKTSMELDING to mockInntektsmeldingV1().toJson(Inntektsmelding.serializer()),
Key.ORGNR_UNDERENHETER to setOf("222", "444", "666").toJson(String.serializer()),
)

testRapid.publish(melding)
testRapid.publish(key.toString(), melding)

verifySequence {
testRapid.publish(
key.toString(),
withArg<String> {
it.parseJson().toMap() shouldContainExactly melding
},
Expand All @@ -67,9 +132,11 @@ class RiverUtilsKtTest :
}

test("filtrerer ut JsonNull") {
val key = UUID.randomUUID()
val selvbestemtId = UUID.randomUUID()

testRapid.publish(
key.toString(),
mapOf(
Key.SELVBESTEMT_ID to selvbestemtId.toJson(),
Key.FORESPOERSEL_SVAR to JsonNull,
Expand All @@ -78,6 +145,7 @@ class RiverUtilsKtTest :

verifySequence {
testRapid.publish(
key.toString(),
withArg<String> {
it.parseJson().toMap() shouldContainExactly mapOf(Key.SELVBESTEMT_ID to selvbestemtId.toJson())
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import no.nav.helsearbeidsgiver.inntekt.InntektKlient
import no.nav.helsearbeidsgiver.inntektsmelding.aareg.createAaregRiver
import no.nav.helsearbeidsgiver.inntektsmelding.aktiveorgnrservice.createAktiveOrgnrService
import no.nav.helsearbeidsgiver.inntektsmelding.altinn.createAltinn
import no.nav.helsearbeidsgiver.inntektsmelding.api.tilgang.TilgangProducer
import no.nav.helsearbeidsgiver.inntektsmelding.api.auth.TilgangProducer
import no.nav.helsearbeidsgiver.inntektsmelding.berikinntektsmeldingservice.createBerikInntektsmeldingService
import no.nav.helsearbeidsgiver.inntektsmelding.brospinn.SpinnKlient
import no.nav.helsearbeidsgiver.inntektsmelding.brospinn.createHentEksternImRiver
Expand Down Expand Up @@ -253,7 +253,7 @@ abstract class EndToEndTest : ContainerTest() {

fun publish(vararg messageFields: Pair<Key, JsonElement>) {
println("Publiserer melding med felt: ${messageFields.toMap()}")
imTestRapid.publish(messageFields.toMap())
imTestRapid.publish(UUID.randomUUID(), *messageFields)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nøkkelen har ikke betydning her, da imTestRapid ikke er en ekte rapid.

}

fun publish(vararg messageFields: Pair<Pri.Key, JsonElement>): JsonElement {
Expand Down