Skip to content

Commit 444e4bb

Browse files
IS-2720: Ktor3 (#565)
* IS-2720: Migrate tests to testApplication * IS-2720: Ktor 3 * Fix tests * Fix compile * Fix tests after merge
1 parent 59572e4 commit 444e4bb

File tree

8 files changed

+104
-115
lines changed

8 files changed

+104
-115
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ val jsonVersion = "20240303"
1515
val jettyVersion = "9.4.56.v20240826"
1616
val joseVersion = "0.9.4"
1717
val kafkaVersion = "3.7.0"
18-
val ktorVersion = "2.3.12"
18+
val ktorVersion = "3.0.3"
1919
val kluentVersion = "1.73"
2020
val jaxbApiVersion = "2.3.1"
2121
val jaxbRuntimeVersion = "2.3.6"

src/main/kotlin/no/nav/syfo/App.kt

+76-80
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package no.nav.syfo
33
import com.typesafe.config.ConfigFactory
44
import io.ktor.server.application.ApplicationStarted
55
import io.ktor.server.config.HoconApplicationConfig
6-
import io.ktor.server.engine.applicationEngineEnvironment
7-
import io.ktor.server.engine.connector
8-
import io.ktor.server.engine.embeddedServer
9-
import io.ktor.server.engine.stop
6+
import io.ktor.server.engine.*
107
import io.ktor.server.netty.Netty
118
import java.util.concurrent.TimeUnit
129
import no.nav.syfo.application.ApplicationState
@@ -149,13 +146,22 @@ fun main() {
149146
lateinit var dialogmoterelasjonService: DialogmoterelasjonService
150147
lateinit var dialogmotestatusService: DialogmotestatusService
151148

152-
val applicationEngineEnvironment = applicationEngineEnvironment {
149+
val applicationEngineEnvironment = applicationEnvironment {
153150
log = logger
154151
config = HoconApplicationConfig(ConfigFactory.load())
155-
connector {
156-
port = applicationPort
157-
}
158-
module {
152+
}
153+
val server = embeddedServer(
154+
factory = Netty,
155+
environment = applicationEngineEnvironment,
156+
configure = {
157+
connector {
158+
port = applicationPort
159+
}
160+
connectionGroupSize = 8
161+
workerGroupSize = 8
162+
callGroupSize = 16
163+
},
164+
module = {
159165
databaseModule(
160166
environment = environment
161167
)
@@ -213,84 +219,74 @@ fun main() {
213219
arbeidstakerVarselService = arbeidstakerVarselService,
214220
moteStatusEndretRepository = moteStatusEndretRepository,
215221
)
216-
}
217-
}
218-
219-
applicationEngineEnvironment.monitor.subscribe(ApplicationStarted) {
220-
applicationState.ready = true
221-
logger.info(
222-
"Application is ready, running Java VM ${Runtime.version()} on this number of processors: ${
223-
Runtime.getRuntime().availableProcessors()
224-
}"
225-
)
226-
val dialogmeldingService = DialogmeldingService(
227-
behandlerVarselService = behandlerVarselService,
228-
)
229-
val dialogmeldingConsumerService = DialogmeldingConsumerService(
230-
kafkaConsumer = KafkaConsumer(kafkaDialogmeldingConsumerConfig(environment.kafka)),
231-
applicationState = applicationState,
232-
dialogmeldingService = dialogmeldingService
233-
)
234-
launchBackgroundTask(applicationState = applicationState) {
235-
logger.info("Starting dialogmelding kafka consumer")
236-
dialogmeldingConsumerService.startConsumer()
237-
}
238-
val identhendelseService = IdenthendelseService(
239-
database = applicationDatabase,
240-
pdlClient = pdlClient,
241-
)
242-
val identhendelseConsumerService = IdenthendelseConsumerService(
243-
kafkaConsumer = KafkaConsumer(kafkaIdenthendelseConsumerConfig(environment.kafka)),
244-
applicationState = applicationState,
245-
identhendelseService = identhendelseService,
246-
)
247-
launchBackgroundTask(applicationState = applicationState) {
248-
identhendelseConsumerService.startConsumer()
249-
}
222+
monitor.subscribe(ApplicationStarted) {
223+
applicationState.ready = true
224+
logger.info(
225+
"Application is ready, running Java VM ${Runtime.version()} on this number of processors: ${
226+
Runtime.getRuntime().availableProcessors()
227+
}"
228+
)
229+
val dialogmeldingService = DialogmeldingService(
230+
behandlerVarselService = behandlerVarselService,
231+
)
232+
val dialogmeldingConsumerService = DialogmeldingConsumerService(
233+
kafkaConsumer = KafkaConsumer(kafkaDialogmeldingConsumerConfig(environment.kafka)),
234+
applicationState = applicationState,
235+
dialogmeldingService = dialogmeldingService
236+
)
237+
launchBackgroundTask(applicationState = applicationState) {
238+
logger.info("Starting dialogmelding kafka consumer")
239+
dialogmeldingConsumerService.startConsumer()
240+
}
241+
val identhendelseService = IdenthendelseService(
242+
database = applicationDatabase,
243+
pdlClient = pdlClient,
244+
)
245+
val identhendelseConsumerService = IdenthendelseConsumerService(
246+
kafkaConsumer = KafkaConsumer(kafkaIdenthendelseConsumerConfig(environment.kafka)),
247+
applicationState = applicationState,
248+
identhendelseService = identhendelseService,
249+
)
250+
launchBackgroundTask(applicationState = applicationState) {
251+
identhendelseConsumerService.startConsumer()
252+
}
250253

251-
val janitorService = JanitorService(
252-
database = applicationDatabase,
253-
dialogmotestatusService = dialogmotestatusService,
254-
dialogmoterelasjonService = dialogmoterelasjonService,
255-
janitorEventStatusProducer = JanitorEventStatusProducer(
256-
kafkaProducer = KafkaProducer(kafkaJanitorEventProducerConfig(environment.kafka)),
257-
),
258-
)
254+
val janitorService = JanitorService(
255+
database = applicationDatabase,
256+
dialogmotestatusService = dialogmotestatusService,
257+
dialogmoterelasjonService = dialogmoterelasjonService,
258+
janitorEventStatusProducer = JanitorEventStatusProducer(
259+
kafkaProducer = KafkaProducer(kafkaJanitorEventProducerConfig(environment.kafka)),
260+
),
261+
)
259262

260-
val janitorEventConsumer = JanitorEventConsumer(
261-
kafkaConsumer = KafkaConsumer(kafkaJanitorEventConsumerConfig(environment.kafka)),
262-
applicationState = applicationState,
263-
janitorService = janitorService,
264-
)
265-
launchBackgroundTask(applicationState = applicationState) {
266-
janitorEventConsumer.startConsumer()
267-
}
263+
val janitorEventConsumer = JanitorEventConsumer(
264+
kafkaConsumer = KafkaConsumer(kafkaJanitorEventConsumerConfig(environment.kafka)),
265+
applicationState = applicationState,
266+
janitorService = janitorService,
267+
)
268+
launchBackgroundTask(applicationState = applicationState) {
269+
janitorEventConsumer.startConsumer()
270+
}
268271

269-
if (environment.isDevGcp()) {
270-
val testdataResetService = TestdataResetService(
271-
database = applicationDatabase,
272-
)
272+
if (environment.isDevGcp()) {
273+
val testdataResetService = TestdataResetService(
274+
database = applicationDatabase,
275+
)
273276

274-
val testdataResetConsumer = TestdataResetConsumer(
275-
kafkaConsumer = KafkaConsumer(kafkaTestdataResetConsumerConfig(environment.kafka)),
276-
applicationState = applicationState,
277-
testdataResetService = testdataResetService,
278-
)
277+
val testdataResetConsumer = TestdataResetConsumer(
278+
kafkaConsumer = KafkaConsumer(kafkaTestdataResetConsumerConfig(environment.kafka)),
279+
applicationState = applicationState,
280+
testdataResetService = testdataResetService,
281+
)
279282

280-
launchBackgroundTask(applicationState = applicationState) {
281-
testdataResetConsumer.startConsumer()
283+
launchBackgroundTask(applicationState = applicationState) {
284+
testdataResetConsumer.startConsumer()
285+
}
286+
}
282287
}
283288
}
284-
}
285-
286-
val server = embeddedServer(
287-
factory = Netty,
288-
environment = applicationEngineEnvironment,
289-
) {
290-
connectionGroupSize = 8
291-
workerGroupSize = 8
292-
callGroupSize = 16
293-
}
289+
)
294290

295291
Runtime.getRuntime().addShutdownHook(
296292
Thread {

src/main/kotlin/no/nav/syfo/util/PipelineUtil.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package no.nav.syfo.util
22

3-
import io.ktor.server.application.*
4-
import io.ktor.util.pipeline.*
3+
import io.ktor.server.routing.*
54
import no.nav.syfo.application.api.authentication.ForbiddenAccessVeilederException
65
import no.nav.syfo.dialogmote.tilgang.DialogmoteTilgangService
76
import no.nav.syfo.domain.PersonIdent
87

9-
suspend fun PipelineContext<out Unit, ApplicationCall>.validateVeilederAccess(
8+
suspend fun RoutingContext.validateVeilederAccess(
109
dialogmoteTilgangService: DialogmoteTilgangService,
1110
personIdentToAccess: PersonIdent,
1211
action: String,

src/main/kotlin/no/nav/syfo/util/RequestUtil.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package no.nav.syfo.util
22

33
import io.ktor.http.HttpHeaders.Authorization
44
import io.ktor.server.application.*
5-
import io.ktor.util.pipeline.*
5+
import io.ktor.server.routing.*
66
import net.logstash.logback.argument.StructuredArguments
77

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

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

34-
fun PipelineContext<out Unit, ApplicationCall>.getBearerHeader(): String? {
34+
fun RoutingContext.getBearerHeader(): String? {
3535
return this.call.getBearerHeader()
3636
}
3737

38-
fun PipelineContext<out Unit, ApplicationCall>.getPersonIdentHeader(): String? {
38+
fun RoutingContext.getPersonIdentHeader(): String? {
3939
return this.call.getHeader(NAV_PERSONIDENT_HEADER)
4040
}

src/test/kotlin/no/nav/syfo/brev/arbeidstaker/ArbeidstakerBrevApiSpek.kt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.ktor.client.statement.*
66
import io.ktor.http.*
77
import io.ktor.server.testing.*
88
import io.ktor.util.*
9+
import io.ktor.utils.io.*
910
import io.mockk.*
1011
import kotlinx.coroutines.runBlocking
1112
import no.altinn.schemas.services.intermediary.receipt._2009._10.ReceiptExternal

src/test/kotlin/no/nav/syfo/brev/narmesteleder/NarmesteLederBrevSpek.kt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.ktor.client.statement.*
66
import io.ktor.http.*
77
import io.ktor.server.testing.*
88
import io.ktor.util.*
9+
import io.ktor.utils.io.*
910
import io.mockk.*
1011
import kotlinx.coroutines.runBlocking
1112
import no.altinn.schemas.services.intermediary.receipt._2009._10.ReceiptExternal

src/test/kotlin/no/nav/syfo/dialogmote/VarselServiceSpek.kt

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package no.nav.syfo.dialogmote
22

33
import io.mockk.*
4-
import kotlinx.coroutines.DelicateCoroutinesApi
5-
import java.time.LocalDate
6-
import java.util.*
7-
import kotlinx.coroutines.Dispatchers
8-
import kotlinx.coroutines.GlobalScope
9-
import kotlinx.coroutines.launch
10-
import no.nav.syfo.application.Unbounded
11-
import no.nav.syfo.application.database.Database
4+
import kotlinx.coroutines.runBlocking
125
import no.nav.syfo.brev.arbeidstaker.ArbeidstakerVarselService
136
import no.nav.syfo.brev.behandler.BehandlerVarselService
147
import no.nav.syfo.brev.narmesteleder.NarmesteLederVarselService
@@ -25,7 +18,9 @@ import no.nav.syfo.testhelper.generator.DIALOGMOTE_TIDSPUNKT_FIXTURE
2518
import no.nav.syfo.testhelper.mock.narmesteLeder
2619
import org.spekframework.spek2.Spek
2720
import org.spekframework.spek2.style.specification.describe
21+
import java.time.LocalDate
2822
import java.time.LocalDateTime
23+
import java.util.*
2924

3025
object VarselServiceSpek : Spek({
3126

@@ -36,7 +31,10 @@ object VarselServiceSpek : Spek({
3631
val behandlerVarselService = mockk<BehandlerVarselService>()
3732
val altinnClient = mockk<AltinnClient>()
3833
val oppfolgingstilfelleClient = mockk<OppfolgingstilfelleClient>()
39-
val database: Database = mockk()
34+
val anyOppfolgingstilfelle = Oppfolgingstilfelle(
35+
start = LocalDate.now().minusDays(10),
36+
end = LocalDate.now().plusDays(10),
37+
)
4038

4139
val varselService = VarselService(
4240
arbeidstakerVarselService = arbeidstakerVarselService,
@@ -61,10 +59,7 @@ object VarselServiceSpek : Spek({
6159
}
6260

6361
it("Send varsel to nærmeste leder") {
64-
coEvery { oppfolgingstilfelleClient.oppfolgingstilfellePerson(any(), any(), any()) } returns Oppfolgingstilfelle(
65-
start = LocalDate.MIN,
66-
end = LocalDate.MAX,
67-
)
62+
coEvery { oppfolgingstilfelleClient.oppfolgingstilfellePerson(any(), any(), any()) } returns anyOppfolgingstilfelle
6863
val virksomhetsbrevId = UUID.randomUUID()
6964
val virksomhetsPdf = byteArrayOf(0x2E, 0x38)
7065
val altinnMelding = createAltinnMelding(
@@ -77,8 +72,7 @@ object VarselServiceSpek : Spek({
7772
true
7873
)
7974

80-
@OptIn(DelicateCoroutinesApi::class)
81-
GlobalScope.launch(Dispatchers.Unbounded) {
75+
runBlocking {
8276
varselService.sendVarsel(
8377
varselType = MotedeltakerVarselType.INNKALT,
8478
isDigitalVarselEnabledForArbeidstaker = false,
@@ -96,7 +90,7 @@ object VarselServiceSpek : Spek({
9690
behandlerbrevId = null,
9791
behandlerbrevParentId = null,
9892
behandlerInnkallingUuid = null,
99-
motetidspunkt = LocalDateTime.now().plusDays(1L),
93+
motetidspunkt = DIALOGMOTE_TIDSPUNKT_FIXTURE,
10094
token = "token",
10195
callId = "callId",
10296
)
@@ -113,10 +107,7 @@ object VarselServiceSpek : Spek({
113107
}
114108

115109
it("Send brev to Altinn when no nærmeste leder") {
116-
coEvery { oppfolgingstilfelleClient.oppfolgingstilfellePerson(any(), any(), any()) } returns Oppfolgingstilfelle(
117-
start = LocalDate.MIN,
118-
end = LocalDate.MAX,
119-
)
110+
coEvery { oppfolgingstilfelleClient.oppfolgingstilfellePerson(any(), any(), any()) } returns anyOppfolgingstilfelle
120111
val virksomhetsbrevId = UUID.randomUUID()
121112
val virksomhetsPdf = byteArrayOf(0x2E, 0x38)
122113
val altinnMelding = createAltinnMelding(
@@ -129,8 +120,7 @@ object VarselServiceSpek : Spek({
129120
false
130121
)
131122

132-
@OptIn(DelicateCoroutinesApi::class)
133-
GlobalScope.launch(Dispatchers.Unbounded) {
123+
runBlocking {
134124
varselService.sendVarsel(
135125
varselType = MotedeltakerVarselType.INNKALT,
136126
isDigitalVarselEnabledForArbeidstaker = false,
@@ -158,7 +148,7 @@ object VarselServiceSpek : Spek({
158148
altinnMelding
159149
)
160150
}
161-
verify(exactly = 0) { narmesteLederVarselService.sendVarsel(any(), any(), DIALOGMOTE_TIDSPUNKT_FIXTURE) }
151+
verify(exactly = 0) { narmesteLederVarselService.sendVarsel(any(), any(), any()) }
162152
}
163153
}
164154

@@ -179,8 +169,7 @@ object VarselServiceSpek : Spek({
179169
false
180170
)
181171

182-
@OptIn(DelicateCoroutinesApi::class)
183-
GlobalScope.launch(Dispatchers.Unbounded) {
172+
runBlocking {
184173
varselService.sendVarsel(
185174
varselType = MotedeltakerVarselType.INNKALT,
186175
isDigitalVarselEnabledForArbeidstaker = false,
@@ -226,8 +215,7 @@ object VarselServiceSpek : Spek({
226215
false
227216
)
228217

229-
@OptIn(DelicateCoroutinesApi::class)
230-
GlobalScope.launch(Dispatchers.Unbounded) {
218+
runBlocking {
231219
varselService.sendVarsel(
232220
varselType = MotedeltakerVarselType.INNKALT,
233221
isDigitalVarselEnabledForArbeidstaker = false,

src/test/kotlin/no/nav/syfo/dialogmote/api/v2/PostDialogmoteApiV2Spek.kt

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import no.nav.syfo.testhelper.UserConstants.ARBEIDSTAKER_VIRKSOMHET_NO_NARMESTEL
2626
import no.nav.syfo.testhelper.UserConstants.ENHET_NR
2727
import no.nav.syfo.testhelper.UserConstants.VEILEDER_IDENT
2828
import no.nav.syfo.testhelper.generator.*
29+
import no.nav.syfo.testhelper.generator.generateInkallingHendelse
30+
import no.nav.syfo.testhelper.generator.generateNewDialogmoteDTO
31+
import no.nav.syfo.testhelper.generator.generateNewDialogmoteDTOWithBehandler
32+
import no.nav.syfo.testhelper.generator.generateNewDialogmoteDTOWithMissingValues
2933
import no.nav.syfo.testhelper.mock.oppfolgingstilfellePersonDTO
3034
import org.amshove.kluent.shouldBeEqualTo
3135
import org.amshove.kluent.shouldBeNull

0 commit comments

Comments
 (0)