Skip to content

Commit

Permalink
oppgraderer Altinn client til å bruke maskinporten client
Browse files Browse the repository at this point in the history
  • Loading branch information
mettok committed Jul 17, 2024
1 parent bb091f9 commit d090565
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 74 deletions.
3 changes: 3 additions & 0 deletions altinn/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ dependencies {
val maskinportenClientVersion: String by project
implementation("no.nav.helsearbeidsgiver:altinn-client:$altinnClientVersion")
implementation("no.nav.helsearbeidsgiver:maskinporten-client:$maskinportenClientVersion")
testImplementation("com.nimbusds:nimbus-jose-jwt:9.40")
testImplementation("com.squareup.okhttp3:mockwebserver:5.0.0-alpha.14")

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun RapidsConnection.createAltinn(altinnClient: AltinnClient): RapidsConnection
}

private fun createAltinnClient(): AltinnClient {
val maskinportenClient: MaskinportenClient = createMaskinportenClient()
val maskinportenClient = createMaskinportenClient()
return AltinnClient(
url = Env.url,
serviceCode = Env.serviceCode,
Expand All @@ -42,11 +42,11 @@ private fun createAltinnClient(): AltinnClient {
private fun createMaskinportenClient(): MaskinportenClient =
MaskinportenClient(
MaskinportenClientConfig(
Env.Maskinporten.altinnScope,
Env.Maskinporten.endpoint,
Env.Maskinporten.clientJwk,
Env.Maskinporten.issuer,
Env.Maskinporten.clientId,
scope = Env.Maskinporten.altinnScope,
endpoint = Env.Maskinporten.endpoint,
clientJwk = Env.Maskinporten.clientJwk,
issuer = Env.Maskinporten.issuer,
clientId = Env.Maskinporten.clientId,
),
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,112 @@
package no.nav.helsearbeidsgiver.inntektsmelding.altinn

import com.nimbusds.jose.jwk.gen.RSAKeyGenerator
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.ints.shouldBeExactly
import io.kotest.matchers.maps.shouldContainExactly
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldStartWith
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.mockkStatic
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import no.nav.helse.rapids_rivers.RapidApplication
import no.nav.helse.rapids_rivers.testsupport.TestRapid
import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.fromEnv
import no.nav.helsearbeidsgiver.felles.json.toJson
import no.nav.helsearbeidsgiver.felles.json.toMap
import no.nav.helsearbeidsgiver.felles.test.rapidsrivers.firstMessage
import no.nav.helsearbeidsgiver.felles.test.rapidsrivers.sendJson
import no.nav.helsearbeidsgiver.inntektsmelding.altinn.Mock.altinnOrganisasjoner
import no.nav.helsearbeidsgiver.inntektsmelding.altinn.Mock.innkommendeMelding
import no.nav.helsearbeidsgiver.inntektsmelding.altinn.Mock.toMap
import no.nav.helsearbeidsgiver.maskinporten.TokenResponse
import no.nav.helsearbeidsgiver.utils.json.serializer.set
import no.nav.helsearbeidsgiver.utils.json.toJson
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.jupiter.api.assertDoesNotThrow

class AltinnAppTest :
FunSpec({
val testRapid = TestRapid()
lateinit var server: MockWebServer
beforeEach {
testRapid.reset()
clearAllMocks()
server = MockWebServer()
server.start()
val baseUrl = server.url("/").toString()
println("baseUrl== $baseUrl")
}
afterEach { clearAllMocks() }

test("main") {
test("tester at Altinn client og maskinporten kaller riktig endepunkt og sender riktig data") {
mockkObject(RapidApplication)
every { RapidApplication.create(any()) } returns testRapid
mockEnv()

val maskinportenToken = TokenResponse("test_token", "Bearer", 3600, "test:test1")
val tokenResponse = Json.encodeToString(TokenResponse.serializer(), maskinportenToken)
server.enqueue(
MockResponse()
.setBody(tokenResponse)
.addHeader("Content-Type", "application/json"),
)
val altinnResponse = Json.encodeToString(altinnOrganisasjoner)
val mockResponse =
MockResponse()
.setBody(altinnResponse)
.addHeader("Content-Type", "application/json")
server.enqueue(mockResponse)

mockEnv(server)

assertDoesNotThrow { main() }

val innkommendeMelding = innkommendeMelding()

testRapid.sendJson(innkommendeMelding.toMap())

testRapid.inspektør.size shouldBeExactly 1

val altinnOrgnr =
altinnOrganisasjoner
.mapNotNull { it.orgnr }
.toSet()

testRapid.firstMessage().toMap() shouldContainExactly
mapOf(
Key.EVENT_NAME to innkommendeMelding.eventName.toJson(),
Key.UUID to innkommendeMelding.transaksjonId.toJson(),
Key.DATA to
innkommendeMelding.data
.plus(
Key.ORG_RETTIGHETER to altinnOrgnr.toJson(String.serializer().set()),
).toJson(),
)
val tokenRequest = server.takeRequest()
tokenRequest.path shouldBe "/token"
tokenRequest.method shouldBe "POST"

val altinnRequest = server.takeRequest()
altinnRequest.path shouldStartWith "/altinn/reportees/"
altinnRequest.method shouldBe "GET"
}
})

private fun mockEnv() {
private fun mockEnv(server: MockWebServer) {
mockkStatic("no.nav.helsearbeidsgiver.felles.EnvUtilsKt")

every { "ALTINN_URL".fromEnv() } returns "mocked_value"
every { "ALTINN_SERVICE_CODE".fromEnv() } returns "mocked_value"
every { "MASKINPORTEN_TOKEN_ENDPOINT".fromEnv() } returns "mocked_value"
every { "MASKINPORTEN_ISSUER".fromEnv() } returns "mocked_value"
every { "MASKINPORTEN_CLIENT_JWK".fromEnv() } returns "mocked_value"
every { "MASKINPORTEN_CLIENT_ID".fromEnv() } returns "mocked_value"
every { "ALTINN_SCOPE".fromEnv() } returns "mocked_value"
every { "ALTINN_URL".fromEnv() } returns server.url("/altinn").toString()
every { "ALTINN_SERVICE_CODE".fromEnv() } returns "4936"
every { "MASKINPORTEN_TOKEN_ENDPOINT".fromEnv() } returns server.url("/token").toString()
every { "MASKINPORTEN_ISSUER".fromEnv() } returns "https://test.test.no/"
every { "MASKINPORTEN_CLIENT_JWK".fromEnv() } returns generateJWK()
every { "MASKINPORTEN_CLIENT_ID".fromEnv() } returns "TEST_CLIENT_ID"
every { "ALTINN_SCOPE".fromEnv() } returns "test:test/test"
every { "ALTINN_API_KEY".fromEnv() } returns "mocked_value"
every { "KAFKA_BROKERS".fromEnv() } returns "mocked_value"
}

private fun generateJWK() = RSAKeyGenerator(2048).keyID("test-key-id").generate().toString()
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ import io.mockk.coVerify
import io.mockk.coVerifySequence
import io.mockk.mockk
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import no.nav.helse.rapids_rivers.testsupport.TestRapid
import no.nav.helsearbeidsgiver.altinn.AltinnClient
import no.nav.helsearbeidsgiver.altinn.AltinnOrganisasjon
import no.nav.helsearbeidsgiver.felles.BehovType
import no.nav.helsearbeidsgiver.felles.EventName
import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.json.toJson
import no.nav.helsearbeidsgiver.felles.json.toMap
Expand All @@ -26,9 +22,6 @@ import no.nav.helsearbeidsgiver.felles.test.rapidsrivers.sendJson
import no.nav.helsearbeidsgiver.inntektsmelding.altinn.Mock.toMap
import no.nav.helsearbeidsgiver.utils.json.serializer.set
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 AltinnRiverTest :
FunSpec({
Expand Down Expand Up @@ -123,48 +116,3 @@ class AltinnRiverTest :
}
}
})

private object Mock {
fun innkommendeMelding(): Melding {
val fnr = Fnr.genererGyldig()

return Melding(
eventName = EventName.AKTIVE_ORGNR_REQUESTED,
behovType = BehovType.ARBEIDSGIVERE,
transaksjonId = UUID.randomUUID(),
data =
mapOf(
Key.ARBEIDSGIVER_FNR to fnr.toJson(),
),
fnr = fnr,
)
}

fun Melding.toMap(): Map<Key, JsonElement> =
mapOf(
Key.EVENT_NAME to eventName.toJson(),
Key.BEHOV to behovType.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
mapOf(
Key.ARBEIDSGIVER_FNR to fnr.toJson(),
).toJson(),
)

val fail =
Fail(
feilmelding = "One does not simply walk into Mordor.",
event = EventName.AKTIVE_ORGNR_REQUESTED,
transaksjonId = UUID.randomUUID(),
forespoerselId = null,
utloesendeMelding = JsonNull,
)

val altinnOrganisasjoner =
setOf(
AltinnOrganisasjon(
navn = "Pippin's Breakfast & Breakfast",
type = "gluttonous",
),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package no.nav.helsearbeidsgiver.inntektsmelding.altinn

import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonNull
import no.nav.helsearbeidsgiver.altinn.AltinnOrganisasjon
import no.nav.helsearbeidsgiver.felles.BehovType
import no.nav.helsearbeidsgiver.felles.EventName
import no.nav.helsearbeidsgiver.felles.Key
import no.nav.helsearbeidsgiver.felles.json.toJson
import no.nav.helsearbeidsgiver.felles.rapidsrivers.model.Fail
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

object Mock {
fun innkommendeMelding(): Melding {
val fnr = Fnr.genererGyldig()

return Melding(
eventName = EventName.AKTIVE_ORGNR_REQUESTED,
behovType = BehovType.ARBEIDSGIVERE,
transaksjonId = UUID.randomUUID(),
data =
mapOf(
Key.ARBEIDSGIVER_FNR to fnr.toJson(),
),
fnr = fnr,
)
}

fun Melding.toMap(): Map<Key, JsonElement> =
mapOf(
Key.EVENT_NAME to eventName.toJson(),
Key.BEHOV to behovType.toJson(),
Key.UUID to transaksjonId.toJson(),
Key.DATA to
mapOf(
Key.ARBEIDSGIVER_FNR to fnr.toJson(),
).toJson(),
)

val fail =
Fail(
feilmelding = "One does not simply walk into Mordor.",
event = EventName.AKTIVE_ORGNR_REQUESTED,
transaksjonId = UUID.randomUUID(),
forespoerselId = null,
utloesendeMelding = JsonNull,
)

val altinnOrganisasjoner =
setOf(
AltinnOrganisasjon(
navn = "Pippin's Breakfast & Breakfast",
type = "gluttonous",
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import no.nav.helsearbeidsgiver.utils.json.toJson
import no.nav.helsearbeidsgiver.utils.test.mock.mockStatic
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.util.UUID
import java.util.*

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ForespoerselMottattIT : EndToEndTest() {
Expand Down Expand Up @@ -59,10 +59,10 @@ class ForespoerselMottattIT : EndToEndTest() {
}

private object Mock {
const val FNR = "fnr-rebekka"
var FNR = "fnr-rebekka"
const val ORGNR = "orgnr-gås"

val forespoerselId: UUID = UUID.randomUUID()
val forespoerselId = UUID.randomUUID()
val transaksjonId: UUID = UUID.randomUUID()
}
}

0 comments on commit d090565

Please sign in to comment.