Skip to content

Commit

Permalink
Bruker naisful-test-app for å teste apiet. Bruker statuspages fra nai…
Browse files Browse the repository at this point in the history
…sful app og endret exceptiontypen en kaster fra APIet. Endret måten vi leser barn opp fra basen, leser som liste først og legger til BarnListe
  • Loading branch information
geiralund committed Nov 20, 2024
1 parent a8cc2bd commit 5c6c66e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 72 deletions.
1 change: 1 addition & 0 deletions mediator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
testImplementation(libs.mock.oauth2.server)
testImplementation(libs.bundles.postgres.test)
testImplementation("io.ktor:ktor-server-test-host-jvm:${libs.versions.ktor.get()}")
testImplementation("com.github.navikt.tbd-libs:naisful-test-app:2024.11.19-09.27-9c591574")
testImplementation("com.approvaltests:approvaltests:22.3.3")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ package no.nav.dagpenger.behandling.mediator

import com.github.navikt.tbd_libs.rapids_and_rivers.KafkaRapid
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.plugins.statuspages.StatusPagesConfig
import io.ktor.server.request.uri
import io.ktor.server.response.respond
import mu.KotlinLogging
import no.nav.dagpenger.behandling.api.models.HttpProblemDTO
import no.nav.dagpenger.behandling.db.PostgresDataSourceBuilder.clean
import no.nav.dagpenger.behandling.db.PostgresDataSourceBuilder.runMigration
import no.nav.dagpenger.behandling.konfigurasjon.Configuration.config
Expand All @@ -29,8 +23,6 @@ import no.nav.dagpenger.behandling.objectMapper
import no.nav.dagpenger.opplysning.Opplysningstype
import no.nav.dagpenger.regel.RegelverkDagpenger
import no.nav.helse.rapids_rivers.RapidApplication
import org.apache.kafka.common.errors.ResourceNotFoundException
import java.net.URI

internal class ApplicationBuilder(
config: Map<String, String>,
Expand All @@ -43,9 +35,7 @@ internal class ApplicationBuilder(
private val opplysningstyper: Set<Opplysningstype<*>> = RegelverkDagpenger.produserer

private val rapidsConnection: RapidsConnection =
RapidApplication.create(env = config, objectMapper = objectMapper, builder = {
withStatusPagesConfig(statusPages())
}) { engine, rapidsConnection: KafkaRapid ->
RapidApplication.create(env = config, objectMapper = objectMapper) { engine, rapidsConnection: KafkaRapid ->
val aktivitetsloggMediator = AktivitetsloggMediator()

// Logger bare oppgaver enn så lenge. Bør inn i HendelseMediator
Expand Down Expand Up @@ -78,43 +68,6 @@ internal class ApplicationBuilder(
)
}

private fun statusPages(): StatusPagesConfig.() -> Unit =
{
exception<IllegalArgumentException> { call: ApplicationCall, cause: IllegalArgumentException ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Ugyldig forespørsel",
status = HttpStatusCode.BadRequest.value,
detail = cause.message ?: "Ugyldig forespørsel",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.BadRequest, problem)
}
exception<ResourceNotFoundException> { call: ApplicationCall, cause: ResourceNotFoundException ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Fant ikke ressurs",
status = HttpStatusCode.NotFound.value,
detail = cause.message ?: "Fant ikke ressurs",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.NotFound, problem)
}
exception<Throwable> { call: ApplicationCall, cause: Throwable ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Noe gikk galt",
status = HttpStatusCode.InternalServerError.value,
detail = cause.message ?: "Noe gikk galt",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.InternalServerError, problem)
}
}

init {
rapidsConnection.register(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.createApplicationPlugin
import io.ktor.server.application.install
import io.ktor.server.auth.authenticate
import io.ktor.server.plugins.BadRequestException
import io.ktor.server.plugins.NotFoundException
import io.ktor.server.plugins.swagger.swaggerUI
import io.ktor.server.request.receive
import io.ktor.server.response.respond
Expand Down Expand Up @@ -48,7 +50,6 @@ import no.nav.dagpenger.opplysning.Penger
import no.nav.dagpenger.opplysning.Tekst
import no.nav.dagpenger.opplysning.ULID
import no.nav.dagpenger.uuid.UUIDv7
import org.apache.kafka.common.errors.ResourceNotFoundException
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.UUID
Expand Down Expand Up @@ -105,7 +106,7 @@ internal fun Application.behandlingApi(
val person =
personRepository.hent(
ident.tilPersonIdentfikator(),
) ?: throw ResourceNotFoundException("Person ikke funnet")
) ?: throw NotFoundException("Person ikke funnet")

auditlogg.les("Listet ut behandlinger", ident, call.saksbehandlerId())

Expand All @@ -115,9 +116,7 @@ internal fun Application.behandlingApi(
route("{behandlingId}") {
get {
val behandling =
personRepository.hentBehandling(
call.behandlingId,
) ?: throw ResourceNotFoundException("Behandling ikke funnet")
hentBehandling(personRepository, call.behandlingId)

auditlogg.les("Så en behandling", behandling.behandler.ident, call.saksbehandlerId())

Expand All @@ -126,9 +125,7 @@ internal fun Application.behandlingApi(

get("vedtak") {
val behandling =
personRepository.hentBehandling(
call.behandlingId,
) ?: throw ResourceNotFoundException("Behandling ikke funnet")
hentBehandling(personRepository, call.behandlingId)

auditlogg.les("Så en behandling", behandling.behandler.ident, call.saksbehandlerId())

Expand Down Expand Up @@ -178,10 +175,10 @@ internal fun Application.behandlingApi(
val opplysningId = call.opplysningId
val oppdaterOpplysningRequestDTO = call.receive<OppdaterOpplysningRequestDTO>()
val behandling =
personRepository.hentBehandling(behandlingId) ?: throw ResourceNotFoundException("Behandling ikke funnet")
hentBehandling(personRepository, behandlingId)

require(!behandling.harTilstand(Redigert)) {
"Kan ikke redigere opplysninger før forrige redigering er ferdig"
if (behandling.harTilstand(Redigert)) {
throw BadRequestException("Kan ikke redigere opplysninger før forrige redigering er ferdig")
}

val opplysning = behandling.opplysninger().finnOpplysning(opplysningId)
Expand Down Expand Up @@ -209,7 +206,7 @@ internal fun Application.behandlingApi(
get("avklaring") {
val behandlingId = call.behandlingId
val behandling =
personRepository.hentBehandling(behandlingId) ?: throw ResourceNotFoundException("Behandling ikke funnet")
hentBehandling(personRepository, behandlingId)
call.respond(HttpStatusCode.OK, behandling.avklaringer().map { it.tilAvklaringDTO() })
}

Expand All @@ -218,11 +215,11 @@ internal fun Application.behandlingApi(
val avklaringId = call.avklaringId
val kvitteringDTO = call.receive<KvitterAvklaringRequestDTO>()
val behandling =
personRepository.hentBehandling(behandlingId) ?: throw ResourceNotFoundException("Behandling ikke funnet")
hentBehandling(personRepository, behandlingId)

val avklaring =
behandling.avklaringer().singleOrNull { it.id == avklaringId }
?: throw ResourceNotFoundException("Avklaring ikke funnet")
?: throw NotFoundException("Avklaring ikke funnet")

if (!avklaring.kanKvitteres) {
call.respond(HttpStatusCode.BadRequest)
Expand Down Expand Up @@ -250,6 +247,11 @@ internal fun Application.behandlingApi(
}
}

private fun hentBehandling(
personRepository: PersonRepository,
behandlingId: UUID,
) = personRepository.hentBehandling(behandlingId) ?: throw NotFoundException("Behandling ikke funnet")

internal class ApiMessageContext(
val rapid: MessageContext,
val ident: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.dagpenger.behandling.mediator.repository

import com.fasterxml.jackson.core.type.TypeReference
import kotliquery.Row
import kotliquery.Session
import kotliquery.queryOf
Expand All @@ -26,6 +27,7 @@ import no.nav.dagpenger.opplysning.Tekst
import no.nav.dagpenger.opplysning.ULID
import no.nav.dagpenger.opplysning.Utledning
import no.nav.dagpenger.opplysning.id
import no.nav.dagpenger.opplysning.verdier.Barn
import no.nav.dagpenger.opplysning.verdier.BarnListe
import no.nav.dagpenger.opplysning.verdier.Beløp
import no.nav.dagpenger.opplysning.verdier.Inntekt
Expand Down Expand Up @@ -230,7 +232,7 @@ class OpplysningerRepositoryPostgres : OpplysningerRepository {
Heltall -> row.int("verdi_heltall")
ULID -> Ulid(row.string("verdi_string"))
Penger -> Beløp(row.string("verdi_string"))
BarnDatatype -> objectMapper.readValue(row.string("verdi_jsonb"), BarnListe::class.java) as T
BarnDatatype -> BarnListe(objectMapper.readValue(row.string("verdi_jsonb"), object : TypeReference<List<Barn>>() {})) as T
InntektDataType ->
Inntekt(
row.binaryStream("verdi_jsonb").use {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.navikt.tbd_libs.naisful.test.TestContext
import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers.test_support.TestRapid
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageProblems
Expand All @@ -18,7 +19,6 @@ import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.ApplicationTestBuilder
import io.mockk.mockk
import io.mockk.slot
import io.mockk.spyk
Expand Down Expand Up @@ -332,7 +332,6 @@ internal class BehandlingApiTest {
@Test
fun `saksbehandler kan kvittere ut avklaring`() {
medSikretBehandlingApi {
val messageMediator = mockk<IMessageMediator>(relaxed = true)
val kvitteringHendelse = slot<AvklaringKvittertHendelse>()

val behandlingId = person.behandlinger().first().behandlingId
Expand Down Expand Up @@ -363,7 +362,7 @@ internal class BehandlingApiTest {
private fun medSikretBehandlingApi(
personRepository: PersonRepository = this.personRepository,
hendelseMediator: HendelseMediator = this.hendelseMediator,
test: suspend ApplicationTestBuilder.() -> Unit,
test: suspend TestContext.() -> Unit,
) {
System.setProperty("Grupper.saksbehandler", "dagpenger-saksbehandler")
TestApplication.withMockAuthServerAndTestApplication(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.nav.dagpenger.behandling.mediator.api

import com.github.navikt.tbd_libs.naisful.test.TestContext
import com.github.navikt.tbd_libs.naisful.test.naisfulTestApp
import io.ktor.client.request.header
import io.ktor.client.request.request
import io.ktor.client.request.setBody
Expand All @@ -9,9 +11,10 @@ import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.content.TextContent
import io.ktor.server.application.Application
import io.ktor.server.testing.ApplicationTestBuilder
import io.ktor.server.testing.testApplication
import io.micrometer.prometheusmetrics.PrometheusConfig
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
import no.nav.dagpenger.behandling.konfigurasjon.Configuration
import no.nav.dagpenger.behandling.objectMapper
import no.nav.security.mock.oauth2.MockOAuth2Server

object TestApplication {
Expand All @@ -38,18 +41,23 @@ object TestApplication {

internal fun withMockAuthServerAndTestApplication(
moduleFunction: Application.() -> Unit,
test: suspend ApplicationTestBuilder.() -> Unit,
test: suspend TestContext.() -> Unit,
) {
System.setProperty("azure-app.client-id", CLIENT_ID)
System.setProperty("azure-app.well-known-url", "${mockOAuth2Server.wellKnownUrl(AZUREAD_ISSUER_ID)}")

return testApplication {
application(moduleFunction)
return naisfulTestApp(
{
apply { moduleFunction() }
},
objectMapper,
PrometheusMeterRegistry(PrometheusConfig.DEFAULT),
) {
test()
}
}

internal suspend fun ApplicationTestBuilder.autentisert(
internal suspend fun TestContext.autentisert(
endepunkt: String,
token: String =
testAzureAdToken(
Expand All @@ -62,5 +70,7 @@ object TestApplication {
this.method = httpMethod
body?.let { this.setBody(TextContent(it, ContentType.Application.Json)) }
this.header(HttpHeaders.Authorization, "Bearer $token")
this.header(HttpHeaders.Accept, ContentType.Application.Json.toString())
this.header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
}
}

0 comments on commit 5c6c66e

Please sign in to comment.