-
Notifications
You must be signed in to change notification settings - Fork 4
/
LocalApp.kt
193 lines (172 loc) · 6.52 KB
/
LocalApp.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package no.nav.helse
import com.expediagroup.graphql.client.jackson.GraphQLClientJacksonSerializer
import com.expediagroup.graphql.client.serializer.GraphQLClientSerializer
import com.expediagroup.graphql.client.types.GraphQLClientResponse
import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import io.ktor.server.application.call
import io.ktor.server.cio.CIO
import io.ktor.server.engine.embeddedServer
import io.ktor.server.response.respond
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import no.nav.helse.bootstrap.Environment
import no.nav.helse.bootstrap.SpesialistApp
import no.nav.helse.rapids_rivers.testsupport.TestRapid
import no.nav.helse.spesialist.api.AzureConfig
import no.nav.helse.spesialist.api.bootstrap.Gruppe
import no.nav.helse.spesialist.api.bootstrap.Tilgangsgrupper
import no.nav.helse.spesialist.api.graphql.schema.Reservasjon
import no.nav.helse.spesialist.api.reservasjon.ReservasjonClient
import no.nav.helse.spesialist.api.snapshot.ISnapshotClient
import no.nav.helse.spleis.graphql.HentSnapshot
import no.nav.security.mock.oauth2.MockOAuth2Server
import org.flywaydb.core.Flyway
import org.testcontainers.containers.PostgreSQLContainer
import java.util.UUID
fun main() {
LocalApp.start()
}
object LocalApp {
// Simulerer RapidApp, der SpesialistApp (som trenger RapidsConnection) først gis
// til RapidApplicationBuildern, og deretter får ut en RapidsConnection, dvs. at RapidsConnection er en
// lateinit var
@Suppress("JoinDeclarationAndAssignment")
private lateinit var testRapid: TestRapid
private val mockOAuth2Server = MockOAuth2Server()
private val clientId = "en-client-id"
private val issuerId = "LocalTestIssuer"
private val token =
mockOAuth2Server.issueToken(
issuerId = issuerId,
audience = clientId,
claims =
mapOf(
"preferred_username" to "[email protected]",
"oid" to "${UUID.randomUUID()}",
"name" to "En Saksbehandler",
"NAVident" to "X123456",
),
).serialize().also {
println("OAuth2-token:")
println(it)
}
private val azureConfig =
AzureConfig(
clientId = clientId,
issuerUrl = mockOAuth2Server.issuerUrl(issuerId).toString(),
jwkProviderUri = mockOAuth2Server.jwksUrl(issuerId).toString(),
tokenEndpoint = mockOAuth2Server.tokenEndpointUrl(issuerId).toString(),
)
private val spesialistApp =
SpesialistApp(
env = Environment(database.envvars + mapOf("LOKAL_UTVIKLING" to "true")),
gruppekontroll = gruppekontroll,
snapshotClient = snapshotClient,
azureConfig = azureConfig,
tilgangsgrupper = tilgangsgrupper,
reservasjonClient = reservasjonClient,
versjonAvKode = "versjon_1",
) {
testRapid
}
private val server =
embeddedServer(CIO, port = 4321) {
spesialistApp.ktorApp(this)
routing {
get("/local-token") {
return@get call.respond(message = token)
}
}
}
init {
testRapid = TestRapid()
}
fun start() {
spesialistApp.start()
server.start(wait = true)
}
}
private val snapshotClient =
object : ISnapshotClient {
private val serializer: GraphQLClientSerializer =
GraphQLClientJacksonSerializer(
jacksonObjectMapper().disable(
FAIL_ON_UNKNOWN_PROPERTIES,
),
)
override fun hentSnapshot(fnr: String): GraphQLClientResponse<HentSnapshot.Result> {
return serializer.deserialize(
rawResponse = "",
responseType = HentSnapshot(variables = HentSnapshot.Variables(fnr = fnr)).responseType(),
)
}
}
private val reservasjonClient =
object : ReservasjonClient {
override suspend fun hentReservasjonsstatus(fnr: String): Reservasjon? {
return null
}
}
private val tilgangsgrupper =
object : Tilgangsgrupper {
override val kode7GruppeId: UUID = UUID.randomUUID()
override val beslutterGruppeId: UUID = UUID.randomUUID()
override val skjermedePersonerGruppeId: UUID = UUID.randomUUID()
override val stikkprøveGruppeId: UUID = UUID.randomUUID()
override val spesialsakGruppeId: UUID = UUID.randomUUID()
override fun gruppeId(gruppe: Gruppe): UUID {
return when (gruppe) {
Gruppe.KODE7 -> kode7GruppeId
Gruppe.BESLUTTER -> beslutterGruppeId
Gruppe.SKJERMEDE -> skjermedePersonerGruppeId
Gruppe.STIKKPRØVE -> stikkprøveGruppeId
Gruppe.SPESIALSAK -> spesialsakGruppeId
}
}
}
private val gruppekontroll =
object : Gruppekontroll {
override suspend fun erIGrupper(
oid: UUID,
gruppeIder: List<UUID>,
): Boolean {
return true
}
}
private val database =
object {
private val postgres =
PostgreSQLContainer<Nothing>("postgres:14").apply {
withReuse(true)
withLabel("app-navn", "spesialist")
start()
println("Database: jdbc:postgresql://localhost:$firstMappedPort/test startet opp, credentials: test og test")
}
val envvars =
mapOf(
"DATABASE_HOST" to "localhost",
"DATABASE_PORT" to "${postgres.firstMappedPort}",
"DATABASE_DATABASE" to "test",
"DATABASE_USERNAME" to "test",
"DATABASE_PASSWORD" to "test",
)
protected val dataSource =
HikariDataSource(HikariConfig().apply {
jdbcUrl = postgres.jdbcUrl
username = postgres.username
password = postgres.password
maximumPoolSize = 100
connectionTimeout = 500
initializationFailTimeout = 5000
})
init {
Flyway.configure()
.dataSource(dataSource)
.placeholders(mapOf("spesialist_oid" to UUID.randomUUID().toString()))
.load()
.migrate()
}
}