Skip to content

Commit

Permalink
T - Enklel intagrasjonstest for uthenting av landkoder via kontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
dskarpas committed Aug 15, 2024
1 parent 250eb67 commit 62f2d6e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ dependencies {
exclude module: "mockito-core"
exclude module: 'junit-vintage-engine'
}

testImplementation("org.mock-server:mockserver-netty:5.15.0") {
exclude module: 'junit'
}
}

test {
systemProperty 'docker.host', 'unix:///var/run/docker.sock'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package no.nav.eessi.pensjon.api.geo

import com.ninjasquad.springmockk.MockkBean
import com.ninjasquad.springmockk.MockkBeans
import no.nav.eessi.pensjon.UnsecuredWebMvcTestLauncher
import no.nav.eessi.pensjon.gcp.GcpStorageService
import no.nav.eessi.pensjon.integrationtest.IntegrasjonsTestConfig
import no.nav.eessi.pensjon.pensjonsinformasjon.clients.PensjonsinformasjonClient
import no.nav.eessi.pensjon.personoppslag.pdl.PersonService
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.mockserver.client.MockServerClient
import org.mockserver.integration.ClientAndServer
import org.mockserver.model.HttpRequest
import org.mockserver.model.HttpResponse
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.kafka.test.context.EmbeddedKafka
import org.springframework.test.context.ActiveProfiles
import org.springframework.web.client.RestTemplate

@SpringBootTest(
classes = [IntegrasjonsTestConfig::class, UnsecuredWebMvcTestLauncher::class],
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
)
@ActiveProfiles(profiles = ["unsecured-webmvctest"])
@AutoConfigureMockMvc
@EmbeddedKafka
@MockkBeans(
MockkBean(name = "personService", classes = [PersonService::class]),
MockkBean(name = "pdlRestTemplate", classes = [RestTemplate::class]),
MockkBean(name = "restEuxTemplate", classes = [RestTemplate::class]),
MockkBean(name = "kodeverkRestTemplate", classes = [RestTemplate::class]),
MockkBean(name = "prefillOAuthTemplate", classes = [RestTemplate::class]),
MockkBean(name = "euxSystemRestTemplate", classes = [RestTemplate::class]),
MockkBean(name = "gcpStorageService", classes = [GcpStorageService::class]),
MockkBean(name = "euxNavIdentRestTemplate", classes = [RestTemplate::class]),
MockkBean(name = "safRestOidcRestTemplate", classes = [RestTemplate::class]),
MockkBean(name = "safGraphQlOidcRestTemplate", classes = [RestTemplate::class]),
MockkBean(name = "pensjonsinformasjonClient", classes = [PensjonsinformasjonClient::class])
)
class LandkoderControllerIntegrationTest {

@Autowired
lateinit var restTemplate: TestRestTemplate
companion object {
private var port = 1080

private var clientAndServer = ClientAndServer.startClientAndServer(port)

@JvmStatic
@BeforeAll
fun setUp() {
MockServerClient("localhost", port).apply {
`when`(
HttpRequest.request()
.withMethod("GET")
.withPath("/landkoder/rina")
.withQueryStringParameter("format", "iso2")
).respond(
HttpResponse.response()
.withStatusCode(200)
.withBody("""{"result": "iso2 format response"}""")
)

`when`(
HttpRequest.request()
.withMethod("GET")
.withPath("/landkoder/rina")
).respond(
HttpResponse.response()
.withStatusCode(200)
.withBody("""{"result": "default format response"}""")
)
}
}

@JvmStatic
@AfterAll
fun close() {
clientAndServer.close()
}
}


@Test
fun `landkoderAkseptertAvRina med format`() {
val format = "iso2"
val response: ResponseEntity<String> = restTemplate.getForEntity(
"http://localhost:$port/landkoder/rina?format=$format",
String::class.java
)

assertEquals(HttpStatus.OK, response.statusCode)
assertEquals("""{"result": "iso2 format response"}""", response.body)
}

@Test
fun `landkoderAkseptertAvRina uten format`() {
val response: ResponseEntity<String> = restTemplate.getForEntity(
"http://localhost:$port/landkoder/rina",
String::class.java
)

assertEquals(HttpStatus.OK, response.statusCode)
assertEquals("""{"result": "default format response"}""", response.body)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.eessi.pensjon.integrationtest

import io.mockk.mockk
import no.nav.eessi.pensjon.api.geo.KodeverkService
import no.nav.eessi.pensjon.eux.klient.EuxKlientAsSystemUser
import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.clients.producer.ProducerConfig
Expand Down Expand Up @@ -63,5 +64,7 @@ class IntegrasjonsTestConfig(
@Bean
fun euxKlient(): EuxKlientAsSystemUser = EuxKlientAsSystemUser(euxNavIdentRestTemplate, euxSystemRestTemplate)

@Bean
fun kodeverkService(): KodeverkService = KodeverkService(euxNavIdentRestTemplate)

}

0 comments on commit 62f2d6e

Please sign in to comment.