Skip to content

Commit

Permalink
Oppgrader ktlint til 1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
androa committed Jul 23, 2024
1 parent 57da6fb commit a208f3d
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 71 deletions.
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/common.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ tasks.test {
tasks.withType<KotlinCompile>().configureEach {
dependsOn("ktlintFormat")
}

configure<org.jlleitschuh.gradle.ktlint.KtlintExtension> {
version.set("1.3.1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import mu.KotlinLogging
import no.nav.helse.rapids_rivers.RapidApplication
import no.nav.helse.rapids_rivers.RapidsConnection

internal class ApplicationBuilder(config: Map<String, String>) : RapidsConnection.StatusListener {
internal class ApplicationBuilder(
config: Map<String, String>,
) : RapidsConnection.StatusListener {
companion object {
private val logger = KotlinLogging.logger { }
}

private val rapidsConnection =
RapidApplication.Builder(
RapidApplication.RapidApplicationConfig.fromEnv(config),
).build()
RapidApplication
.Builder(
RapidApplication.RapidApplicationConfig.fromEnv(config),
).build()

private val inntektClient =
InntektHttpClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import java.util.UUID
private val logger = KotlinLogging.logger { }
private val sikkerlogg = KotlinLogging.logger("tjenestekall")

internal class InntektBehovløser(rapidsConnection: RapidsConnection, private val inntektClient: InntektHttpClient) :
River.PacketListener {
internal class InntektBehovløser(
rapidsConnection: RapidsConnection,
private val inntektClient: InntektHttpClient,
) : River.PacketListener {
companion object {
const val BEHOV_ID = "behovId"
const val INNTEKT = "inntektV1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ internal class InntektHttpClient(
beregningsDato: LocalDate,
dselsnummer: String?,
callId: String? = null,
): Inntekt {
return getInntekt(
): Inntekt =
getInntekt(
aktørId,
regelkontekst,
beregningsDato,
fødselsnummer,
url = "${inntektApiUrl}v2/inntekt/klassifisert",
callId,
)
}

suspend fun getKlassifisertInntekt(
inntektId: String,
callId: String,
): Inntekt {
return try {
httpKlient.get("${inntektApiUrl}v2/inntekt/klassifisert/$inntektId") {
header(HttpHeaders.Authorization, "Bearer ${tokenProvider.invoke()}")
header(HttpHeaders.XRequestId, callId)
accept(ContentType.Application.Json)
}.body<Inntekt>()
): Inntekt =
try {
httpKlient
.get("${inntektApiUrl}v2/inntekt/klassifisert/$inntektId") {
header(HttpHeaders.Authorization, "Bearer ${tokenProvider.invoke()}")
header(HttpHeaders.XRequestId, callId)
accept(ContentType.Application.Json)
}.body<Inntekt>()
} catch (error: ResponseException) {
val problem = mapTilHttpProblem(error)
throw InntektApiHttpClientException(
Expand All @@ -66,7 +66,6 @@ internal class InntektHttpClient(
error,
)
}
}

private suspend inline fun <reified T : Any> getInntekt(
aktørId: String,
Expand All @@ -85,13 +84,14 @@ internal class InntektHttpClient(
)

return try {
httpKlient.post(url) {
header("Content-Type", "application/json")
header(HttpHeaders.Authorization, "Bearer ${tokenProvider.invoke()}")
header(HttpHeaders.XRequestId, callId)
setBody(requestBody)
accept(ContentType.Application.Json)
}.body<T>()
httpKlient
.post(url) {
header("Content-Type", "application/json")
header(HttpHeaders.Authorization, "Bearer ${tokenProvider.invoke()}")
header(HttpHeaders.XRequestId, callId)
setBody(requestBody)
accept(ContentType.Application.Json)
}.body<T>()
} catch (error: ResponseException) {
val problem = mapTilHttpProblem(error)

Expand All @@ -108,7 +108,8 @@ internal class InntektHttpClient(
}

private suspend fun mapTilHttpProblem(error: ResponseException): Problem =
kotlin.runCatching { objectMapper.readValue(error.response.bodyAsText(), Problem::class.java) }
kotlin
.runCatching { objectMapper.readValue(error.response.bodyAsText(), Problem::class.java) }
.getOrDefault<Problem, Problem>(
Problem(
URI.create("urn:dp:error:inntektskomponenten"),
Expand All @@ -125,14 +126,17 @@ private data class InntektRequest(
val beregningsDato: LocalDate,
)

class InntektApiHttpClientException(override val message: String, val problem: Problem, override val cause: Throwable) :
RuntimeException(message, cause)
class InntektApiHttpClientException(
override val message: String,
val problem: Problem,
override val cause: Throwable,
) : RuntimeException(message, cause)

internal fun httpClient(
engine: HttpClientEngine = CIO.create { requestTimeout = Long.MAX_VALUE },
httpMetricsBasename: String? = null,
): HttpClient {
return HttpClient(engine) {
): HttpClient =
HttpClient(engine) {
expectSuccess = true
install(HttpTimeout) {
connectTimeoutMillis = Duration.ofSeconds(30).toMillis()
Expand All @@ -150,4 +154,3 @@ internal fun httpClient(
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package no.nav.dagpenger.inntekt.klassifiserer
import no.nav.dagpenger.inntekt.v1.Inntekt
import no.nav.dagpenger.inntekt.v1.KlassifisertInntektMåned

private fun List<KlassifisertInntektMåned>.toMap(): List<Map<String, Any>> {
return this.map { klassifisertInntektMåned ->
private fun List<KlassifisertInntektMåned>.toMap(): List<Map<String, Any>> =
this.map { klassifisertInntektMåned ->
val harAvvik: Boolean = klassifisertInntektMåned.harAvvik ?: false
mapOf(
"årMåned" to "${klassifisertInntektMåned.årMåned}",
Expand All @@ -18,7 +18,6 @@ private fun List<KlassifisertInntektMåned>.toMap(): List<Map<String, Any>> {
"harAvvik" to harAvvik,
)
}
}

internal fun Inntekt.toMap(): Map<String, Any> {
val manueltRedigert: Boolean = this.manueltRedigert ?: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package no.nav.dagpenger.inntekt.klassifiserer

internal fun Problem.toMap(): Map<String, Any?> {
return mapOf(
internal fun Problem.toMap(): Map<String, Any?> =
mapOf(
"type" to this.type.toString(),
"title" to this.title,
"status" to this.status,
"instance" to this.instance.toString(),
"detail" to this.detail,
)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package no.nav.dagpenger.inntekt.klassifiserer

data class RegelKontekst(val id: String, val type: String)
data class RegelKontekst(
val id: String,
val type: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,26 @@ class InntektHttpClientTest {
fun `fetch klassifisert inntekt on 200 ok with fødselsnummer`() {
val responseBodyJson =
InntektHttpClientTest::class.java
.getResource("/test-data/example-klassifisert-inntekt-payload.json")!!.readText()
.getResource("/test-data/example-klassifisert-inntekt-payload.json")!!
.readText()

WireMock.stubFor(
WireMock.post(WireMock.urlEqualTo("/v2/inntekt/klassifisert"))
WireMock
.post(WireMock.urlEqualTo("/v2/inntekt/klassifisert"))
.withHeader("Authorization", EqualToPattern("Bearer token"))
.withRequestBody(
matchingJsonPath("aktørId", equalTo("45456")),
)
.withRequestBody(
).withRequestBody(
matchingJsonPath("regelkontekst.id", equalTo("123")),
)
.withRequestBody(
).withRequestBody(
matchingJsonPath("regelkontekst.type", equalTo("vedtak")),
)
.withRequestBody(
).withRequestBody(
matchingJsonPath("beregningsDato", matching("^\\d{4}-\\d{2}-\\d{2}\$")),
)
.withRequestBody(
).withRequestBody(
matchingJsonPath("fødselsnummer", equalTo("12345678901")),
)
.willReturn(
WireMock.aResponse()
).willReturn(
WireMock
.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(responseBodyJson),
),
Expand Down Expand Up @@ -115,10 +113,12 @@ class InntektHttpClientTest {
}
""".trimIndent()
WireMock.stubFor(
WireMock.post(WireMock.urlEqualTo("/v2/inntekt/klassifisert"))
WireMock
.post(WireMock.urlEqualTo("/v2/inntekt/klassifisert"))
.withHeader("Authorization", EqualToPattern("Bearer token"))
.willReturn(
WireMock.serverError()
WireMock
.serverError()
.withHeader("Content-Type", "application/json")
.withBody(responseBodyJson),
),
Expand Down Expand Up @@ -148,13 +148,16 @@ class InntektHttpClientTest {
runBlocking {
val responseBodyJson =
InntektHttpClientTest::class.java
.getResource("/test-data/example-klassifisert-inntekt-payload.json")!!.readText()
.getResource("/test-data/example-klassifisert-inntekt-payload.json")!!
.readText()

WireMock.stubFor(
WireMock.get(WireMock.urlEqualTo("/v2/inntekt/klassifisert/12345"))
WireMock
.get(WireMock.urlEqualTo("/v2/inntekt/klassifisert/12345"))
.withHeader("Authorization", EqualToPattern("Bearer token"))
.willReturn(
WireMock.aResponse()
WireMock
.aResponse()
.withHeader("Content-Type", "application/json")
.withBody(responseBodyJson),
),
Expand All @@ -168,7 +171,8 @@ class InntektHttpClientTest {
@Test
fun `fetch spesifisert inntekt fails on error and no body`() {
WireMock.stubFor(
WireMock.post(WireMock.urlEqualTo("/v2/inntekt/klassifisert"))
WireMock
.post(WireMock.urlEqualTo("/v2/inntekt/klassifisert"))
.willReturn(
WireMock.serviceUnavailable(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,22 @@ class PacketParserTest {
}

private fun testMessageMedRequiredFelter(ekstraFelter: Map<String, Any> = emptyMap()) =
JsonMessage.newMessage(
mapOf(BEHOV_ID to "behovId") + ekstraFelter,
).toJson()

private class OnPacketTestListener(rapidsConnection: RapidsConnection) : River.PacketListener {
JsonMessage
.newMessage(
mapOf(BEHOV_ID to "behovId") + ekstraFelter,
).toJson()

private class OnPacketTestListener(
rapidsConnection: RapidsConnection,
) : River.PacketListener {
var problems: MessageProblems? = null
lateinit var packet: JsonMessage

init {
River(rapidsConnection).apply(
InntektBehovløser.rapidFilter,
).register(this)
River(rapidsConnection)
.apply(
InntektBehovløser.rapidFilter,
).register(this)
}

override fun onPacket(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,22 @@ class RapidFilterTest {
}

private fun testMessageMedRequiredFelter(ekstraFelter: Map<String, Any> = emptyMap()) =
JsonMessage.newMessage(
mapOf(BEHOV_ID to "behovId") + ekstraFelter,
).toJson()
JsonMessage
.newMessage(
mapOf(BEHOV_ID to "behovId") + ekstraFelter,
).toJson()

private class TestListener(rapidsConnection: RapidsConnection) : River.PacketListener {
private class TestListener(
rapidsConnection: RapidsConnection,
) : River.PacketListener {
var onPacketCalled = false
lateinit var jsonMessage: JsonMessage

init {
River(rapidsConnection).apply(
InntektBehovløser.rapidFilter,
).register(this)
River(rapidsConnection)
.apply(
InntektBehovløser.rapidFilter,
).register(this)
}

override fun onPacket(
Expand Down

0 comments on commit a208f3d

Please sign in to comment.