-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Legg til og bruk redis store med prefiks i service rivers (#544)
* Legg til og bruk redis store med prefiks i service rivers * Gjør funksjonsnavn konsekvent engelsk * Fiks navn * Fjern utdatert hjelpefunksjon
- Loading branch information
Showing
22 changed files
with
653 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
api/src/test/kotlin/no/nav/helsearbeidsgiver/inntektsmelding/api/RedisPollerMedMockClient.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
felles-test/src/main/kotlin/no/nav/helsearbeidsgiver/felles/test/mock/MockRedisClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package no.nav.helsearbeidsgiver.felles.test.mock | ||
|
||
import io.lettuce.core.KeyValue | ||
import io.lettuce.core.RedisClient | ||
import io.lettuce.core.api.StatefulRedisConnection | ||
import io.lettuce.core.api.sync.RedisCommands | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.slot | ||
import no.nav.helsearbeidsgiver.felles.rapidsrivers.redis.RedisConnection | ||
import no.nav.helsearbeidsgiver.utils.test.mock.mockStatic | ||
|
||
fun redisWithMockRedisClient(mockStorageInit: Map<String, String?>): RedisConnection { | ||
val mockCommands = mockk<RedisCommands<String, String>>().setupMock(mockStorageInit) | ||
return mockStatic(RedisClient::class) { | ||
every { RedisClient.create(any<String>()) } returns mockRedisClient(mockCommands) | ||
RedisConnection("") | ||
} | ||
} | ||
|
||
fun mockRedisClient(commands: RedisCommands<String, String>): RedisClient { | ||
val connection = mockk<StatefulRedisConnection<String, String>>(relaxed = true) { | ||
every { sync() } returns commands | ||
} | ||
|
||
return mockk<RedisClient> { | ||
every { connect() } returns connection | ||
} | ||
} | ||
|
||
fun RedisCommands<String, String>.setupMock(mockStorageInit: Map<String, String?>): RedisCommands<String, String> = | ||
also { | ||
val mockStorage = mockStorageInit.toMutableMap() | ||
|
||
val key = slot<String>() | ||
val value = slot<String>() | ||
|
||
// Fungerer som en capture slot for vararg | ||
val varargKeys = mutableListOf<String>() | ||
|
||
every { this@setupMock.get(capture(key)) } answers { | ||
mockStorage[key.captured] | ||
} | ||
|
||
every { mget(*varargAll { varargKeys.add(it) }) } answers { | ||
val keyValuePairs = varargKeys.associateWith { mockStorage[it] } | ||
.map { KeyValue.fromNullable(it.key, it.value) } | ||
|
||
varargKeys.clear() | ||
|
||
keyValuePairs | ||
} | ||
|
||
every { setex(capture(key), any(), capture(value)) } answers { | ||
mockStorage[key.captured] = value.captured | ||
"OK" | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
felles/src/main/kotlin/no/nav/helsearbeidsgiver/felles/rapidsrivers/redis/RedisConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package no.nav.helsearbeidsgiver.felles.rapidsrivers.redis | ||
|
||
import io.lettuce.core.RedisClient | ||
import io.lettuce.core.api.StatefulRedisConnection | ||
import io.lettuce.core.api.sync.RedisCommands | ||
import no.nav.helsearbeidsgiver.utils.collection.mapValuesNotNull | ||
|
||
class RedisConnection( | ||
redisUrl: String | ||
) { | ||
private val client: RedisClient = RedisClient.create(redisUrl) | ||
private val connection: StatefulRedisConnection<String, String> = client.connect() | ||
private val syncCommands: RedisCommands<String, String> = connection.sync() | ||
|
||
fun get(key: String): String? = | ||
syncCommands.get(key) | ||
|
||
internal fun getAll(vararg keys: String): Map<String, String> = | ||
syncCommands.mget(*keys) | ||
.associate { it.key to it.getValueOrElse(null) } | ||
.mapValuesNotNull { it } | ||
|
||
internal fun set(key: String, value: String) { | ||
syncCommands.setex(key, 60L, value) | ||
} | ||
|
||
fun close() { | ||
connection.close() | ||
client.shutdown() | ||
} | ||
} |
Oops, something went wrong.