From 63a0bc2d16b8869dda9686841fbf084ef69591c2 Mon Sep 17 00:00:00 2001 From: Joakim Taule Kartveit Date: Fri, 11 Oct 2024 12:49:35 +0200 Subject: [PATCH] chore: bumped ktor to 3.0.0 --- build.gradle.kts | 14 ++- src/main/kotlin/no/nav/syfo/Application.kt | 21 +--- .../no/nav/syfo/plugins/LifecycleHooks.kt | 13 +++ .../kotlin/no/nav/syfo/plugins/Routing.kt | 16 +++ src/test/kotlin/no/nav/syfo/SelfTest.kt | 98 ++++++++++--------- 5 files changed, 96 insertions(+), 66 deletions(-) create mode 100644 src/main/kotlin/no/nav/syfo/plugins/LifecycleHooks.kt create mode 100644 src/main/kotlin/no/nav/syfo/plugins/Routing.kt diff --git a/build.gradle.kts b/build.gradle.kts index 8e2ddeda..838ca628 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,5 @@ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget + group = "no.nav.syfo" version = "1.0.0" @@ -8,7 +10,7 @@ val javaxActivationVersion = "1.1.1" val jacksonVersion = "2.17.2" val jaxbApiVersion = "2.4.0-b180830.0359" val kafkaVersion = "3.8.0" -val ktorVersion = "2.3.12" +val ktorVersion = "3.0.0" val logbackVersion = "1.5.7" val logstashEncoderVersion = "8.0" val prometheusVersion = "0.16.0" @@ -32,6 +34,9 @@ val snappyJavaVersion = "1.1.10.6" val opentelemetryVersion = "2.3.0" val diagnosekoderVersion = "1.2024.0" +val javaVersion = JvmTarget.JVM_21 + + plugins { id("application") kotlin("jvm") version "2.0.20" @@ -123,6 +128,12 @@ dependencies { testImplementation("io.zonky.test:embedded-postgres:$embeddedPostgresVersion") } +kotlin { + compilerOptions { + jvmTarget.set(javaVersion) + } +} + tasks { @@ -147,6 +158,7 @@ tasks { useJUnitPlatform {} testLogging { events("skipped", "failed") + showStandardStreams = true showStackTraces = true exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL } diff --git a/src/main/kotlin/no/nav/syfo/Application.kt b/src/main/kotlin/no/nav/syfo/Application.kt index 89ccbc4c..a1046c88 100644 --- a/src/main/kotlin/no/nav/syfo/Application.kt +++ b/src/main/kotlin/no/nav/syfo/Application.kt @@ -41,10 +41,9 @@ import no.nav.syfo.mq.MqTlsUtils import no.nav.syfo.mq.connectionFactory import no.nav.syfo.mq.consumerForQueue import no.nav.syfo.mq.producerForQueue -import no.nav.syfo.nais.isalive.naisIsAliveRoute -import no.nav.syfo.nais.isready.naisIsReadyRoute -import no.nav.syfo.nais.prometheus.naisPrometheusRoute import no.nav.syfo.pdl.service.PdlPersonService +import no.nav.syfo.plugins.configureLifecycleHooks +import no.nav.syfo.plugins.configureRouting import no.nav.syfo.service.DuplicationService import no.nav.syfo.service.VirusScanService import no.nav.syfo.util.LoggingMeta @@ -94,15 +93,9 @@ fun Application.module() { DefaultExports.initialize() - environment.monitor.subscribe(ApplicationStopped) { - applicationState.ready = false - applicationState.alive = false - } - + configureLifecycleHooks(applicationState = applicationState) configureRouting(applicationState = applicationState) - DefaultExports.initialize() - val httpClients = HttpClients(environmentVariables) val kafkaClients = KafkaClients(environmentVariables) @@ -298,14 +291,6 @@ fun sendReceivedSykmelding( } } -fun Application.configureRouting(applicationState: ApplicationState) { - routing { - naisIsAliveRoute(applicationState) - naisIsReadyRoute(applicationState) - naisPrometheusRoute() - } -} - data class ApplicationState( var alive: Boolean = true, var ready: Boolean = true, diff --git a/src/main/kotlin/no/nav/syfo/plugins/LifecycleHooks.kt b/src/main/kotlin/no/nav/syfo/plugins/LifecycleHooks.kt new file mode 100644 index 00000000..7f4aeba9 --- /dev/null +++ b/src/main/kotlin/no/nav/syfo/plugins/LifecycleHooks.kt @@ -0,0 +1,13 @@ +package no.nav.syfo.plugins + +import io.ktor.server.application.* +import no.nav.syfo.ApplicationState + +fun Application.configureLifecycleHooks(applicationState: ApplicationState) { + + this.monitor.subscribe(ApplicationStarted) { applicationState.ready = true } + this.monitor.subscribe(ApplicationStopped) { + applicationState.ready = false + applicationState.alive = false + } +} diff --git a/src/main/kotlin/no/nav/syfo/plugins/Routing.kt b/src/main/kotlin/no/nav/syfo/plugins/Routing.kt new file mode 100644 index 00000000..2bfd8bf2 --- /dev/null +++ b/src/main/kotlin/no/nav/syfo/plugins/Routing.kt @@ -0,0 +1,16 @@ +package no.nav.syfo.plugins + +import io.ktor.server.application.* +import io.ktor.server.routing.* +import no.nav.syfo.ApplicationState +import no.nav.syfo.nais.isalive.naisIsAliveRoute +import no.nav.syfo.nais.isready.naisIsReadyRoute +import no.nav.syfo.nais.prometheus.naisPrometheusRoute + +fun Application.configureRouting(applicationState: ApplicationState) { + routing { + naisIsAliveRoute(applicationState) + naisIsReadyRoute(applicationState) + naisPrometheusRoute() + } +} diff --git a/src/test/kotlin/no/nav/syfo/SelfTest.kt b/src/test/kotlin/no/nav/syfo/SelfTest.kt index 1a176c2f..be8a9117 100644 --- a/src/test/kotlin/no/nav/syfo/SelfTest.kt +++ b/src/test/kotlin/no/nav/syfo/SelfTest.kt @@ -1,11 +1,10 @@ package no.nav.syfo -import io.ktor.http.HttpMethod +import io.ktor.client.request.* +import io.ktor.client.statement.* import io.ktor.http.HttpStatusCode import io.ktor.server.routing.routing -import io.ktor.server.testing.TestApplicationEngine -import io.ktor.server.testing.handleRequest -import io.ktor.util.InternalAPI +import io.ktor.server.testing.* import no.nav.syfo.nais.isalive.naisIsAliveRoute import no.nav.syfo.nais.isready.naisIsReadyRoute import org.junit.jupiter.api.Assertions.assertEquals @@ -13,70 +12,75 @@ import org.junit.jupiter.api.Test internal class SelfTest { - @InternalAPI @Test internal fun `Returns ok on is_alive`() { - with(TestApplicationEngine()) { - start() - val applicationState = ApplicationState() - applicationState.ready = true - applicationState.alive = true - application.routing { naisIsAliveRoute(applicationState) } - - with(handleRequest(HttpMethod.Get, "/internal/is_alive")) { - assertEquals(HttpStatusCode.OK, response.status()) - assertEquals("I'm alive! :)", response.content) + testApplication { + application { + routing { + val applicationState = ApplicationState() + applicationState.ready = true + applicationState.alive = true + naisIsAliveRoute(applicationState) + } } + val response = client.get("/internal/is_alive") + + assertEquals(HttpStatusCode.OK, response.status) + assertEquals("I'm alive! :)", response.bodyAsText()) } } - @InternalAPI @Test internal fun `Returns ok in is_ready`() { - with(TestApplicationEngine()) { - start() - val applicationState = ApplicationState() - applicationState.ready = true - applicationState.alive = true - application.routing { naisIsReadyRoute(applicationState) } - - with(handleRequest(HttpMethod.Get, "/internal/is_ready")) { - assertEquals(HttpStatusCode.OK, response.status()) - assertEquals("I'm ready! :)", response.content) + testApplication { + application { + routing { + val applicationState = ApplicationState() + applicationState.ready = true + applicationState.alive = true + naisIsReadyRoute(applicationState) + } } + val response = client.get("/internal/is_ready") + + assertEquals(HttpStatusCode.OK, response.status) + assertEquals("I'm ready! :)", response.bodyAsText()) } } - @InternalAPI @Test internal fun `Returns internal server error when liveness check fails`() { - with(TestApplicationEngine()) { - start() - val applicationState = ApplicationState() - applicationState.ready = false - applicationState.alive = false - application.routing { naisIsAliveRoute(applicationState) } - - with(handleRequest(HttpMethod.Get, "/internal/is_alive")) { - assertEquals(HttpStatusCode.InternalServerError, response.status()) - assertEquals("I'm dead x_x", response.content) + testApplication { + application { + routing { + val applicationState = ApplicationState() + applicationState.ready = false + applicationState.alive = false + naisIsAliveRoute(applicationState) + } } + val response = client.get("/internal/is_alive") + + assertEquals(HttpStatusCode.InternalServerError, response.status) + assertEquals("I'm dead x_x", response.bodyAsText()) } } - @InternalAPI @Test internal fun `Returns internal server error when readyness check fails`() { - with(TestApplicationEngine()) { - start() - val applicationState = ApplicationState() - applicationState.ready = false - applicationState.alive = false - application.routing { naisIsReadyRoute(applicationState) } - with(handleRequest(HttpMethod.Get, "/internal/is_ready")) { - assertEquals(HttpStatusCode.InternalServerError, response.status()) - assertEquals("Please wait! I'm not ready :(", response.content) + testApplication { + application { + routing { + val applicationState = ApplicationState() + applicationState.ready = false + applicationState.alive = false + naisIsReadyRoute(applicationState) + } } + val response = client.get("/internal/is_ready") + + assertEquals(HttpStatusCode.InternalServerError, response.status) + assertEquals("Please wait! I'm not ready :(", response.bodyAsText()) } } }