Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IS-2720: Ktor3 #565

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val jsonVersion = "20240303"
val jettyVersion = "9.4.56.v20240826"
val joseVersion = "0.9.4"
val kafkaVersion = "3.7.0"
val ktorVersion = "2.3.12"
val ktorVersion = "3.0.3"
val kluentVersion = "1.73"
val jaxbApiVersion = "2.3.1"
val jaxbRuntimeVersion = "2.3.6"
Expand Down
156 changes: 76 additions & 80 deletions src/main/kotlin/no/nav/syfo/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package no.nav.syfo
import com.typesafe.config.ConfigFactory
import io.ktor.server.application.ApplicationStarted
import io.ktor.server.config.HoconApplicationConfig
import io.ktor.server.engine.applicationEngineEnvironment
import io.ktor.server.engine.connector
import io.ktor.server.engine.embeddedServer
import io.ktor.server.engine.stop
import io.ktor.server.engine.*
import io.ktor.server.netty.Netty
import java.util.concurrent.TimeUnit
import no.nav.syfo.application.ApplicationState
Expand Down Expand Up @@ -149,13 +146,22 @@ fun main() {
lateinit var dialogmoterelasjonService: DialogmoterelasjonService
lateinit var dialogmotestatusService: DialogmotestatusService

val applicationEngineEnvironment = applicationEngineEnvironment {
val applicationEngineEnvironment = applicationEnvironment {
log = logger
config = HoconApplicationConfig(ConfigFactory.load())
connector {
port = applicationPort
}
module {
}
val server = embeddedServer(
factory = Netty,
environment = applicationEngineEnvironment,
configure = {
connector {
port = applicationPort
}
connectionGroupSize = 8
workerGroupSize = 8
callGroupSize = 16
},
module = {
databaseModule(
environment = environment
)
Expand Down Expand Up @@ -213,84 +219,74 @@ fun main() {
arbeidstakerVarselService = arbeidstakerVarselService,
moteStatusEndretRepository = moteStatusEndretRepository,
)
}
}

applicationEngineEnvironment.monitor.subscribe(ApplicationStarted) {
applicationState.ready = true
logger.info(
"Application is ready, running Java VM ${Runtime.version()} on this number of processors: ${
Runtime.getRuntime().availableProcessors()
}"
)
val dialogmeldingService = DialogmeldingService(
behandlerVarselService = behandlerVarselService,
)
val dialogmeldingConsumerService = DialogmeldingConsumerService(
kafkaConsumer = KafkaConsumer(kafkaDialogmeldingConsumerConfig(environment.kafka)),
applicationState = applicationState,
dialogmeldingService = dialogmeldingService
)
launchBackgroundTask(applicationState = applicationState) {
logger.info("Starting dialogmelding kafka consumer")
dialogmeldingConsumerService.startConsumer()
}
val identhendelseService = IdenthendelseService(
database = applicationDatabase,
pdlClient = pdlClient,
)
val identhendelseConsumerService = IdenthendelseConsumerService(
kafkaConsumer = KafkaConsumer(kafkaIdenthendelseConsumerConfig(environment.kafka)),
applicationState = applicationState,
identhendelseService = identhendelseService,
)
launchBackgroundTask(applicationState = applicationState) {
identhendelseConsumerService.startConsumer()
}
monitor.subscribe(ApplicationStarted) {
applicationState.ready = true
logger.info(
"Application is ready, running Java VM ${Runtime.version()} on this number of processors: ${
Runtime.getRuntime().availableProcessors()
}"
)
val dialogmeldingService = DialogmeldingService(
behandlerVarselService = behandlerVarselService,
)
val dialogmeldingConsumerService = DialogmeldingConsumerService(
kafkaConsumer = KafkaConsumer(kafkaDialogmeldingConsumerConfig(environment.kafka)),
applicationState = applicationState,
dialogmeldingService = dialogmeldingService
)
launchBackgroundTask(applicationState = applicationState) {
logger.info("Starting dialogmelding kafka consumer")
dialogmeldingConsumerService.startConsumer()
}
val identhendelseService = IdenthendelseService(
database = applicationDatabase,
pdlClient = pdlClient,
)
val identhendelseConsumerService = IdenthendelseConsumerService(
kafkaConsumer = KafkaConsumer(kafkaIdenthendelseConsumerConfig(environment.kafka)),
applicationState = applicationState,
identhendelseService = identhendelseService,
)
launchBackgroundTask(applicationState = applicationState) {
identhendelseConsumerService.startConsumer()
}

val janitorService = JanitorService(
database = applicationDatabase,
dialogmotestatusService = dialogmotestatusService,
dialogmoterelasjonService = dialogmoterelasjonService,
janitorEventStatusProducer = JanitorEventStatusProducer(
kafkaProducer = KafkaProducer(kafkaJanitorEventProducerConfig(environment.kafka)),
),
)
val janitorService = JanitorService(
database = applicationDatabase,
dialogmotestatusService = dialogmotestatusService,
dialogmoterelasjonService = dialogmoterelasjonService,
janitorEventStatusProducer = JanitorEventStatusProducer(
kafkaProducer = KafkaProducer(kafkaJanitorEventProducerConfig(environment.kafka)),
),
)

val janitorEventConsumer = JanitorEventConsumer(
kafkaConsumer = KafkaConsumer(kafkaJanitorEventConsumerConfig(environment.kafka)),
applicationState = applicationState,
janitorService = janitorService,
)
launchBackgroundTask(applicationState = applicationState) {
janitorEventConsumer.startConsumer()
}
val janitorEventConsumer = JanitorEventConsumer(
kafkaConsumer = KafkaConsumer(kafkaJanitorEventConsumerConfig(environment.kafka)),
applicationState = applicationState,
janitorService = janitorService,
)
launchBackgroundTask(applicationState = applicationState) {
janitorEventConsumer.startConsumer()
}

if (environment.isDevGcp()) {
val testdataResetService = TestdataResetService(
database = applicationDatabase,
)
if (environment.isDevGcp()) {
val testdataResetService = TestdataResetService(
database = applicationDatabase,
)

val testdataResetConsumer = TestdataResetConsumer(
kafkaConsumer = KafkaConsumer(kafkaTestdataResetConsumerConfig(environment.kafka)),
applicationState = applicationState,
testdataResetService = testdataResetService,
)
val testdataResetConsumer = TestdataResetConsumer(
kafkaConsumer = KafkaConsumer(kafkaTestdataResetConsumerConfig(environment.kafka)),
applicationState = applicationState,
testdataResetService = testdataResetService,
)

launchBackgroundTask(applicationState = applicationState) {
testdataResetConsumer.startConsumer()
launchBackgroundTask(applicationState = applicationState) {
testdataResetConsumer.startConsumer()
}
}
}
}
}

val server = embeddedServer(
factory = Netty,
environment = applicationEngineEnvironment,
) {
connectionGroupSize = 8
workerGroupSize = 8
callGroupSize = 16
}
)

Runtime.getRuntime().addShutdownHook(
Thread {
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/no/nav/syfo/util/PipelineUtil.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package no.nav.syfo.util

import io.ktor.server.application.*
import io.ktor.util.pipeline.*
import io.ktor.server.routing.*
import no.nav.syfo.application.api.authentication.ForbiddenAccessVeilederException
import no.nav.syfo.dialogmote.tilgang.DialogmoteTilgangService
import no.nav.syfo.domain.PersonIdent

suspend fun PipelineContext<out Unit, ApplicationCall>.validateVeilederAccess(
suspend fun RoutingContext.validateVeilederAccess(
dialogmoteTilgangService: DialogmoteTilgangService,
personIdentToAccess: PersonIdent,
action: String,
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/no/nav/syfo/util/RequestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package no.nav.syfo.util

import io.ktor.http.HttpHeaders.Authorization
import io.ktor.server.application.*
import io.ktor.util.pipeline.*
import io.ktor.server.routing.*
import net.logstash.logback.argument.StructuredArguments

const val NAV_PERSONIDENT_HEADER = "nav-personident"
const val NAV_VIRKSOMHETSNUMMER = "nav-virksomhetsnummer"
const val NAV_CALL_ID_HEADER = "Nav-Call-Id"
fun PipelineContext<out Unit, ApplicationCall>.getCallId(): String {
fun RoutingContext.getCallId(): String {
return this.call.getCallId()
}

Expand All @@ -31,10 +31,10 @@ fun ApplicationCall.getBearerHeader(): String? {
return getHeader(Authorization)?.removePrefix("Bearer ")
}

fun PipelineContext<out Unit, ApplicationCall>.getBearerHeader(): String? {
fun RoutingContext.getBearerHeader(): String? {
return this.call.getBearerHeader()
}

fun PipelineContext<out Unit, ApplicationCall>.getPersonIdentHeader(): String? {
fun RoutingContext.getPersonIdentHeader(): String? {
return this.call.getHeader(NAV_PERSONIDENT_HEADER)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import io.ktor.util.*
import io.ktor.utils.io.*
import io.mockk.*
import kotlinx.coroutines.runBlocking
import no.altinn.schemas.services.intermediary.receipt._2009._10.ReceiptExternal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.server.testing.*
import io.ktor.util.*
import io.ktor.utils.io.*
import io.mockk.*
import kotlinx.coroutines.runBlocking
import no.altinn.schemas.services.intermediary.receipt._2009._10.ReceiptExternal
Expand Down
42 changes: 15 additions & 27 deletions src/test/kotlin/no/nav/syfo/dialogmote/VarselServiceSpek.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package no.nav.syfo.dialogmote

import io.mockk.*
import kotlinx.coroutines.DelicateCoroutinesApi
import java.time.LocalDate
import java.util.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import no.nav.syfo.application.Unbounded
import no.nav.syfo.application.database.Database
import kotlinx.coroutines.runBlocking
import no.nav.syfo.brev.arbeidstaker.ArbeidstakerVarselService
import no.nav.syfo.brev.behandler.BehandlerVarselService
import no.nav.syfo.brev.narmesteleder.NarmesteLederVarselService
Expand All @@ -25,7 +18,9 @@ import no.nav.syfo.testhelper.generator.DIALOGMOTE_TIDSPUNKT_FIXTURE
import no.nav.syfo.testhelper.mock.narmesteLeder
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.*

object VarselServiceSpek : Spek({

Expand All @@ -36,7 +31,10 @@ object VarselServiceSpek : Spek({
val behandlerVarselService = mockk<BehandlerVarselService>()
val altinnClient = mockk<AltinnClient>()
val oppfolgingstilfelleClient = mockk<OppfolgingstilfelleClient>()
val database: Database = mockk()
val anyOppfolgingstilfelle = Oppfolgingstilfelle(
start = LocalDate.now().minusDays(10),
end = LocalDate.now().plusDays(10),
)

val varselService = VarselService(
arbeidstakerVarselService = arbeidstakerVarselService,
Expand All @@ -61,10 +59,7 @@ object VarselServiceSpek : Spek({
}

it("Send varsel to nærmeste leder") {
coEvery { oppfolgingstilfelleClient.oppfolgingstilfellePerson(any(), any(), any()) } returns Oppfolgingstilfelle(
start = LocalDate.MIN,
end = LocalDate.MAX,
)
coEvery { oppfolgingstilfelleClient.oppfolgingstilfellePerson(any(), any(), any()) } returns anyOppfolgingstilfelle
val virksomhetsbrevId = UUID.randomUUID()
val virksomhetsPdf = byteArrayOf(0x2E, 0x38)
val altinnMelding = createAltinnMelding(
Expand All @@ -77,8 +72,7 @@ object VarselServiceSpek : Spek({
true
)

@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(Dispatchers.Unbounded) {
runBlocking {
varselService.sendVarsel(
varselType = MotedeltakerVarselType.INNKALT,
isDigitalVarselEnabledForArbeidstaker = false,
Expand All @@ -96,7 +90,7 @@ object VarselServiceSpek : Spek({
behandlerbrevId = null,
behandlerbrevParentId = null,
behandlerInnkallingUuid = null,
motetidspunkt = LocalDateTime.now().plusDays(1L),
motetidspunkt = DIALOGMOTE_TIDSPUNKT_FIXTURE,
token = "token",
callId = "callId",
)
Expand All @@ -113,10 +107,7 @@ object VarselServiceSpek : Spek({
}

it("Send brev to Altinn when no nærmeste leder") {
coEvery { oppfolgingstilfelleClient.oppfolgingstilfellePerson(any(), any(), any()) } returns Oppfolgingstilfelle(
start = LocalDate.MIN,
end = LocalDate.MAX,
)
coEvery { oppfolgingstilfelleClient.oppfolgingstilfellePerson(any(), any(), any()) } returns anyOppfolgingstilfelle
val virksomhetsbrevId = UUID.randomUUID()
val virksomhetsPdf = byteArrayOf(0x2E, 0x38)
val altinnMelding = createAltinnMelding(
Expand All @@ -129,8 +120,7 @@ object VarselServiceSpek : Spek({
false
)

@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(Dispatchers.Unbounded) {
runBlocking {
varselService.sendVarsel(
varselType = MotedeltakerVarselType.INNKALT,
isDigitalVarselEnabledForArbeidstaker = false,
Expand Down Expand Up @@ -158,7 +148,7 @@ object VarselServiceSpek : Spek({
altinnMelding
)
}
verify(exactly = 0) { narmesteLederVarselService.sendVarsel(any(), any(), DIALOGMOTE_TIDSPUNKT_FIXTURE) }
verify(exactly = 0) { narmesteLederVarselService.sendVarsel(any(), any(), any()) }
}
}

Expand All @@ -179,8 +169,7 @@ object VarselServiceSpek : Spek({
false
)

@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(Dispatchers.Unbounded) {
runBlocking {
varselService.sendVarsel(
varselType = MotedeltakerVarselType.INNKALT,
isDigitalVarselEnabledForArbeidstaker = false,
Expand Down Expand Up @@ -226,8 +215,7 @@ object VarselServiceSpek : Spek({
false
)

@OptIn(DelicateCoroutinesApi::class)
GlobalScope.launch(Dispatchers.Unbounded) {
runBlocking {
varselService.sendVarsel(
varselType = MotedeltakerVarselType.INNKALT,
isDigitalVarselEnabledForArbeidstaker = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import no.nav.syfo.testhelper.UserConstants.ARBEIDSTAKER_VIRKSOMHET_NO_NARMESTEL
import no.nav.syfo.testhelper.UserConstants.ENHET_NR
import no.nav.syfo.testhelper.UserConstants.VEILEDER_IDENT
import no.nav.syfo.testhelper.generator.*
import no.nav.syfo.testhelper.generator.generateInkallingHendelse
import no.nav.syfo.testhelper.generator.generateNewDialogmoteDTO
import no.nav.syfo.testhelper.generator.generateNewDialogmoteDTOWithBehandler
import no.nav.syfo.testhelper.generator.generateNewDialogmoteDTOWithMissingValues
import no.nav.syfo.testhelper.mock.oppfolgingstilfellePersonDTO
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldBeNull
Expand Down
Loading