Skip to content

Commit

Permalink
chore: bumped ktor to 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MikAoJk committed Oct 11, 2024
1 parent 7f62cf8 commit 63a0bc2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 66 deletions.
14 changes: 13 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

group = "no.nav.syfo"
version = "1.0.0"

Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -123,6 +128,12 @@ dependencies {
testImplementation("io.zonky.test:embedded-postgres:$embeddedPostgresVersion")
}

kotlin {
compilerOptions {
jvmTarget.set(javaVersion)
}
}


tasks {

Expand All @@ -147,6 +158,7 @@ tasks {
useJUnitPlatform {}
testLogging {
events("skipped", "failed")
showStandardStreams = true
showStackTraces = true
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
Expand Down
21 changes: 3 additions & 18 deletions src/main/kotlin/no/nav/syfo/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/no/nav/syfo/plugins/LifecycleHooks.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
16 changes: 16 additions & 0 deletions src/main/kotlin/no/nav/syfo/plugins/Routing.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
98 changes: 51 additions & 47 deletions src/test/kotlin/no/nav/syfo/SelfTest.kt
Original file line number Diff line number Diff line change
@@ -1,82 +1,86 @@
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
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())
}
}
}

0 comments on commit 63a0bc2

Please sign in to comment.