Skip to content

Commit

Permalink
Bruk sterkt typet nøkkel i Redis-klasse (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjerga authored Nov 22, 2023
1 parent 5488222 commit 3038302
Show file tree
Hide file tree
Showing 30 changed files with 301 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package no.nav.helsearbeidsgiver.inntektsmelding.brospinn
import no.nav.helse.rapids_rivers.RapidApplication
import no.nav.helse.rapids_rivers.RapidsConnection
import no.nav.helsearbeidsgiver.felles.oauth2.OAuth2ClientConfig
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.IRedisStore
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisStore
import no.nav.helsearbeidsgiver.utils.log.logger

Expand All @@ -27,13 +26,13 @@ fun RapidsConnection.createEksternInntektsmeldingLoeser(spinnKlient: SpinnKlient
EksternInntektsmeldingLoeser(this, spinnKlient)
}

fun RapidsConnection.createSpinnService(redisStore: IRedisStore): RapidsConnection =
fun RapidsConnection.createSpinnService(redisStore: RedisStore): RapidsConnection =
also {
logger.info("Starter ${SpinnService::class.simpleName}...")
SpinnService(this, redisStore)
}

fun buildRedisStore(): IRedisStore {
fun buildRedisStore(): RedisStore {
return RedisStore(Env.redisUrl)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import no.nav.helsearbeidsgiver.felles.rapidsrivers.StatefullEventListener
import no.nav.helsearbeidsgiver.felles.rapidsrivers.composite.CompositeEventListener
import no.nav.helsearbeidsgiver.felles.rapidsrivers.composite.Transaction
import no.nav.helsearbeidsgiver.felles.rapidsrivers.publish
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.IRedisStore
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisKey
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisStore
import no.nav.helsearbeidsgiver.felles.rapidsrivers.toJsonMap
import no.nav.helsearbeidsgiver.felles.utils.Log
import no.nav.helsearbeidsgiver.felles.utils.randomUuid
Expand All @@ -34,7 +34,7 @@ private const val AVSENDER_NAV_NO = "NAV_NO"

class SpinnService(
private val rapid: RapidsConnection,
override val redisStore: IRedisStore
override val redisStore: RedisStore
) : CompositeEventListener(redisStore) {

private val logger = logger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.ints.shouldBeExactly
import io.kotest.matchers.shouldBe
import io.mockk.clearAllMocks
import io.mockk.verify
import no.nav.helse.rapids_rivers.testsupport.TestRapid
import no.nav.helsearbeidsgiver.felles.BehovType
import no.nav.helsearbeidsgiver.felles.DataFelt
Expand All @@ -13,7 +14,8 @@ import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.json.les
import no.nav.helsearbeidsgiver.felles.json.toJson
import no.nav.helsearbeidsgiver.felles.json.toMap
import no.nav.helsearbeidsgiver.felles.test.mock.MockRedisStore
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisKey
import no.nav.helsearbeidsgiver.felles.test.mock.MockRedis
import no.nav.helsearbeidsgiver.felles.test.rapidsrivers.firstMessage
import no.nav.helsearbeidsgiver.felles.test.rapidsrivers.sendJson
import no.nav.helsearbeidsgiver.felles.utils.randomUuid
Expand All @@ -25,13 +27,14 @@ import no.nav.helsearbeidsgiver.utils.test.date.januar
class SpinnServiceTest : FunSpec({
val testRapid = TestRapid()

val testRedis = MockRedisStore()
val mockRedis = MockRedis()

SpinnService(testRapid, testRedis)
SpinnService(testRapid, mockRedis.store)

beforeEach {
testRapid.reset()
clearAllMocks()
mockRedis.setup()
}

test("Publiser nytt behov med inntektsmeldingId ved nytt event") {
Expand All @@ -52,8 +55,6 @@ class SpinnServiceTest : FunSpec({

test("EksternInntektsmelding blir skrevet til redis") {

testRedis.set("${Mock.transaksjonsId}${DataFelt.FORESPOERSEL_ID.str}", Mock.forespoerselId.toString())

testRapid.sendJson(
Key.EVENT_NAME to EventName.EKSTERN_INNTEKTSMELDING_REQUESTED.toJson(),
Key.DATA to "".toJson(),
Expand All @@ -62,9 +63,13 @@ class SpinnServiceTest : FunSpec({
DataFelt.SPINN_INNTEKTSMELDING_ID to Mock.spinnInntektsmeldingId.toJson(),
DataFelt.EKSTERN_INNTEKTSMELDING to Mock.eksternInntektsmelding.toJson(EksternInntektsmelding.serializer())
)
testRedis.get(Mock.transaksjonsId.toString() + DataFelt.EKSTERN_INNTEKTSMELDING.str) shouldBe Mock.eksternInntektsmelding.toJsonStr(
EksternInntektsmelding.serializer()
)

verify {
mockRedis.store.set(
RedisKey.of(Mock.transaksjonsId, DataFelt.EKSTERN_INNTEKTSMELDING),
Mock.eksternInntektsmelding.toJsonStr(EksternInntektsmelding.serializer())
)
}
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package no.nav.helsearbeidsgiver.felles.test.mock

import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisKey
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisStore

class MockRedis {
val store = mockk<RedisStore>()

private val mockStorage = mutableMapOf<RedisKey, String>()

private val redisKey = slot<RedisKey>()
private val newValue = slot<String>()

// Fungerer som en capture slot for vararg
private val keysToCheck = mutableListOf<RedisKey>()

init {
setup()
}

fun setup() {
mockStorage.clear()

every { store.set(capture(redisKey), capture(newValue)) } answers {
mockStorage[redisKey.captured] = newValue.captured
}

every { store.get(capture(redisKey)) } answers {
mockStorage[redisKey.captured]
}

every { store.exist(*varargAll { keysToCheck.add(it) }) } answers {
val allKeys = mockStorage.keys.toSet()

val keysExist = keysToCheck.intersect(allKeys).size.toLong()

keysToCheck.clear()

keysExist
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ private val objectMapper = customObjectMapper()
fun JsonElement.toJsonNode(): JsonNode =
toString().let(objectMapper::readTree)

// TODO slett
object Jackson {
val objectMapper = customObjectMapper()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import no.nav.helsearbeidsgiver.felles.DataFelt
import no.nav.helsearbeidsgiver.felles.EventName
import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.createFail
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.IRedisStore
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisKey
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisStore
import no.nav.helsearbeidsgiver.utils.log.logger
import no.nav.helsearbeidsgiver.utils.log.sikkerLogger
import java.util.UUID
Expand All @@ -18,10 +18,12 @@ class StatefullDataKanal(
override val eventName: EventName,
private val mainListener: River.PacketListener,
rapidsConnection: RapidsConnection,
val redisStore: IRedisStore
val redisStore: RedisStore
) : DataKanal(
rapidsConnection
) {
private val logger = logger()
private val sikkerLogger = sikkerLogger()

override fun accept(): River.PacketValidation {
return River.PacketValidation {
Expand All @@ -35,21 +37,21 @@ class StatefullDataKanal(

override fun onData(packet: JsonMessage) {
if (packet[Key.FORESPOERSEL_ID.str].asText().isEmpty()) {
logger().warn("Mangler forespørselId!")
sikkerLogger().warn("Mangler forespørselId!")
logger.warn("Mangler forespørselId!")
sikkerLogger.warn("Mangler forespørselId!")
}
if (packet[Key.UUID.str].asText().isNullOrEmpty()) {
sikkerLogger().error("TransaksjonsID er ikke initialisert for\n${packet.toPretty()}")
sikkerLogger.error("TransaksjonsID er ikke initialisert for\n${packet.toPretty()}")
rapidsConnection.publish(
packet.createFail(
"TransaksjonsID / UUID kan ikke vare tom da man bruker Composite Service"
).toJsonMessage().toJson()
)
} else if (collectData(packet)) {
sikkerLogger().info("data collected for event ${eventName.name} med packet\n${packet.toPretty()}")
sikkerLogger.info("data collected for event ${eventName.name} med packet\n${packet.toPretty()}")
mainListener.onPacket(packet, rapidsConnection)
} else {
sikkerLogger().warn("Mangler data for ${packet.toPretty()}")
sikkerLogger.warn("Mangler data for ${packet.toPretty()}")
// @TODO fiks logging logger.warn("Unrecognized package with uuid:" + packet[Key.UUID.str])
}
}
Expand Down Expand Up @@ -81,13 +83,13 @@ class StatefullDataKanal(
return dataMap.isNotEmpty()
}

fun isAllDataCollected(key: RedisKey): Boolean {
val numKeysInRedis = redisStore.exist(*dataFelter.map { key.toString() + it }.toTypedArray())
logger().info("found " + numKeysInRedis)
fun isAllDataCollected(transaksjonId: UUID): Boolean {
val allKeys = dataFelter.map { RedisKey.of(transaksjonId, it) }.toTypedArray()
val numKeysInRedis = redisStore.exist(*allKeys)
logger.info("found " + numKeysInRedis)
return numKeysInRedis == dataFelter.size.toLong()
}
fun isDataCollected(vararg keys: RedisKey): Boolean {
val keysAsString = keys.map { it.toString() }.toTypedArray()
return redisStore.exist(*keysAsString) == keys.size.toLong()
return redisStore.exist(*keys) == keys.size.toLong()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ import no.nav.helsearbeidsgiver.felles.DataFelt
import no.nav.helsearbeidsgiver.felles.EventName
import no.nav.helsearbeidsgiver.felles.IKey
import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.IRedisStore
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisKey
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisStore
import no.nav.helsearbeidsgiver.felles.utils.randomUuid
import no.nav.helsearbeidsgiver.utils.log.sikkerLogger
import java.lang.IllegalStateException

class StatefullEventListener(
val redisStore: IRedisStore,
val redisStore: RedisStore,
override val event: EventName,
private val dataFelter: Array<IKey>,
private val mainListener: River.PacketListener,
rapidsConnection: RapidsConnection
) : EventListener(
rapidsConnection
) {
private val sikkerLogger = sikkerLogger()

override fun accept(): River.PacketValidation {
return River.PacketValidation {
it.interestedIn(*dataFelter)
Expand Down Expand Up @@ -55,7 +57,7 @@ class StatefullEventListener(
}

override fun onEvent(packet: JsonMessage) {
sikkerLogger().info("Statefull event listener for event ${event.name} med packet \n${packet.toPretty()}")
sikkerLogger.info("Statefull event listener for event ${event.name} med packet \n${packet.toPretty()}")
collectData(packet)
mainListener.onPacket(packet, rapidsConnection)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.rapidsrivers.EventListener
import no.nav.helsearbeidsgiver.felles.rapidsrivers.FailKanal
import no.nav.helsearbeidsgiver.felles.rapidsrivers.StatefullDataKanal
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.IRedisStore
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisKey
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisStore
import no.nav.helsearbeidsgiver.felles.rapidsrivers.toPretty
import no.nav.helsearbeidsgiver.felles.toFeilMessage
import no.nav.helsearbeidsgiver.utils.log.logger
import no.nav.helsearbeidsgiver.utils.log.sikkerLogger
import no.nav.helsearbeidsgiver.utils.pipe.orDefault
import java.util.UUID

abstract class CompositeEventListener(open val redisStore: IRedisStore) : River.PacketListener {
abstract class CompositeEventListener(open val redisStore: RedisStore) : River.PacketListener {

private val logger = logger()
private val sikkerLogger = sikkerLogger()
Expand Down Expand Up @@ -129,6 +129,6 @@ abstract class CompositeEventListener(open val redisStore: IRedisStore) : River.
return this
}

open fun isDataCollected(uuid: UUID): Boolean = dataKanal.isAllDataCollected(RedisKey.of(uuid))
open fun isDataCollected(uuid: UUID): Boolean = dataKanal.isAllDataCollected(uuid)
open fun isDataCollected(vararg keys: RedisKey): Boolean = dataKanal.isDataCollected(*keys)
}

This file was deleted.

Loading

0 comments on commit 3038302

Please sign in to comment.