Skip to content

Commit

Permalink
sett maxRetries til default 0 for raskere tester
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrele committed Oct 30, 2024
1 parent 79a5e49 commit 171e902
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ import java.util.concurrent.TimeUnit
const val VALP_BEHANDLINGSNUMMER: String = "B450"

class PdlClient(
private val baseUrl: String,
private val config: Config,
private val tokenProvider: TokenProvider,
clientEngine: HttpClientEngine = CIO.create(),
) {
data class Config(
val baseUrl: String,
val maxRetries: Int = 0,
)
private val log = LoggerFactory.getLogger(javaClass)

private val client = httpJsonClient(clientEngine).config {
install(HttpCache)
install(HttpRequestRetry) {
retryOnException(maxRetries = 3, retryOnTimeout = true)
retryOnException(maxRetries = config.maxRetries, retryOnTimeout = true)
exponentialDelay()
}
install(HttpTimeout) {
Expand Down Expand Up @@ -175,7 +179,7 @@ class PdlClient(
req: GraphqlRequest<T>,
accessType: AccessType,
): Either<PdlError, V> {
val response = client.post("$baseUrl/graphql") {
val response = client.post("${config.baseUrl}/graphql") {
bearerAuth(tokenProvider.exchange(accessType))
header(HttpHeaders.ContentType, ContentType.Application.Json)
header("Behandlingsnummer", VALP_BEHANDLINGSNUMMER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private fun services(appConfig: AppConfig) = module {
}
single {
PdlClient(
baseUrl = appConfig.pdl.url,
config = PdlClient.Config(appConfig.pdl.url, maxRetries = 3),
tokenProvider = cachedTokenProvider.withScope(appConfig.pdl.scope),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class HentPersonBolkPdlQueryTest : FunSpec({
val identer = nonEmptySetOf(PdlIdent("12345678910"), PdlIdent("12345678911"), PdlIdent("test"))

val pdl = PdlClient(
baseUrl = "https://pdl.no",
config = PdlClient.Config(baseUrl = "https://pdl.no"),
tokenProvider = { "token" },
clientEngine = createMockEngine(
"/graphql" to {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,119 +4,110 @@ import io.kotest.assertions.arrow.core.shouldBeLeft
import io.kotest.assertions.arrow.core.shouldBeRight
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
import io.kotest.matchers.shouldBe
import io.ktor.client.engine.mock.*
import no.nav.mulighetsrommet.ktor.createMockEngine
import no.nav.mulighetsrommet.ktor.respondJson
import no.nav.mulighetsrommet.tokenprovider.AccessType

class PdlClientTest : FunSpec({
test("Missing errors is parsed ok") {
val pdlClient = PdlClient(
baseUrl = "https://pdl.no",
tokenProvider = { "token" },
clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": { "hentIdenter": { "identer": [] } }
}
""".trimIndent(),
)
},
),
val clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": { "hentIdenter": { "identer": [] } }
}
""".trimIndent(),
)
},
)
val pdlClient = createPdlClient(clientEngine)

val request = GraphqlRequest.HentHistoriskeIdenter(ident = PdlIdent("12345678910"), grupper = listOf())

pdlClient.hentHistoriskeIdenter(request, AccessType.M2M).shouldBeRight(emptyList())
}

test("not_found gives NotFound") {
val pdlClient = PdlClient(
baseUrl = "https://pdl.no",
tokenProvider = { "token" },
clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": { "hentIdenter": null },
"errors": [
{ "extensions": { "code": "not_found" } },
{ "extensions": { "code": "bad_request" } }
]
}
""".trimIndent(),
)
},
),
val clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": { "hentIdenter": null },
"errors": [
{ "extensions": { "code": "not_found" } },
{ "extensions": { "code": "bad_request" } }
]
}
""".trimIndent(),
)
},
)
val pdlClient = createPdlClient(clientEngine)

val request = GraphqlRequest.HentHistoriskeIdenter(ident = PdlIdent("12345678910"), grupper = listOf())

pdlClient.hentHistoriskeIdenter(request, AccessType.M2M).shouldBeLeft(PdlError.NotFound)
}

test("håndterer errors og manglende data") {
val pdlClient = PdlClient(
baseUrl = "https://pdl.no",
tokenProvider = { "token" },
clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": null,
"errors": [
{ "extensions": { "code": "bad_request" } }
]
}
""".trimIndent(),
)
},
),
val clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": null,
"errors": [
{ "extensions": { "code": "bad_request" } }
]
}
""".trimIndent(),
)
},
)
val pdlClient = createPdlClient(clientEngine)

val request = GraphqlRequest.HentHistoriskeIdenter(ident = PdlIdent("12345678910"), grupper = listOf())

pdlClient.hentHistoriskeIdenter(request, AccessType.M2M).shouldBeLeft(PdlError.Error)
}

test("happy case hentIdenter") {
val pdlClient = PdlClient(
baseUrl = "https://pdl.no",
tokenProvider = { "token" },
clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": {
"hentIdenter": {
"identer": [
{
"ident": "12345678910",
"gruppe": "FOLKEREGISTERIDENT",
"historisk": false
},
{
"ident": "123",
"gruppe": "AKTORID",
"historisk": true
},
{
"ident": "99999999999",
"gruppe": "NPID",
"historisk": true
}
]
}
},
"errors": []
}
""".trimIndent(),
)
},
),
val clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": {
"hentIdenter": {
"identer": [
{
"ident": "12345678910",
"gruppe": "FOLKEREGISTERIDENT",
"historisk": false
},
{
"ident": "123",
"gruppe": "AKTORID",
"historisk": true
},
{
"ident": "99999999999",
"gruppe": "NPID",
"historisk": true
}
]
}
},
"errors": []
}
""".trimIndent(),
)
},
)
val pdlClient = createPdlClient(clientEngine)

val request = GraphqlRequest.HentHistoriskeIdenter(
ident = PdlIdent("12345678910"),
Expand All @@ -143,62 +134,63 @@ class PdlClientTest : FunSpec({
}

test("happy case hentPerson") {
val pdlClient = PdlClient(
baseUrl = "https://pdl.no",
tokenProvider = { "token" },
clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": {
"hentPerson": {
"navn": [
{
"fornavn": "Ola",
"mellomnavn": null,
"etternavn": "Normann"
}
]
}
val clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": {
"hentPerson": {
"navn": [
{
"fornavn": "Ola",
"mellomnavn": null,
"etternavn": "Normann"
}
]
}
}
""".trimIndent(),
)
},
),
}
""".trimIndent(),
)
},
)
val pdlClient = createPdlClient(clientEngine)

val person = pdlClient.hentPerson(PdlIdent("12345678910"), AccessType.M2M).shouldBeRight()
person shouldBe HentPersonResponse.Person(navn = listOf(PdlNavn(fornavn = "Ola", etternavn = "Normann")))
pdlClient
.hentPerson(PdlIdent("12345678910"), AccessType.M2M)
.shouldBeRight(HentPersonResponse.Person(navn = listOf(PdlNavn(fornavn = "Ola", etternavn = "Normann"))))
}

test("happy case hentGeografiskTilknytning") {
val pdlClient = PdlClient(
baseUrl = "https://pdl.no",
tokenProvider = { "token" },
clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": {
"hentGeografiskTilknytning":{
"gtType": "BYDEL",
"gtLand": null,
"gtKommune": null,
"gtBydel": "030102"
}
val clientEngine = createMockEngine(
"/graphql" to {
respondJson(
"""
{
"data": {
"hentGeografiskTilknytning":{
"gtType": "BYDEL",
"gtLand": null,
"gtKommune": null,
"gtBydel": "030102"
}
}
""".trimIndent(),
)
},
),
}
""".trimIndent(),
)
},
)
val pdlClient = createPdlClient(clientEngine)

val geografiskTilknytning =
pdlClient.hentGeografiskTilknytning(PdlIdent("12345678910"), AccessType.M2M).shouldBeRight()
geografiskTilknytning shouldBe GeografiskTilknytning.GtBydel(value = "030102")
pdlClient
.hentGeografiskTilknytning(PdlIdent("12345678910"), AccessType.M2M)
.shouldBeRight(GeografiskTilknytning.GtBydel(value = "030102"))
}
})

private fun createPdlClient(clientEngine: MockEngine) = PdlClient(
config = PdlClient.Config(baseUrl = "https://pdl.no"),
tokenProvider = { "token" },
clientEngine = clientEngine,
)

0 comments on commit 171e902

Please sign in to comment.