Skip to content

Commit

Permalink
brevforsendelse - pågående arbeid
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Kroken committed Nov 14, 2023
1 parent aef73ef commit 5c3d2c9
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.external

import com.fasterxml.jackson.annotation.JsonInclude
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.metrics.PENBrevMetrikker
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model.BrevClient
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model.BrevClientException
Expand All @@ -19,35 +20,45 @@ import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import pensjon.opptjening.azure.ad.client.TokenProvider
import java.time.Year
import java.util.*

@Component
private class PENBrevClient(
@Value("\${PEN_BASE_URL}") private val baseUrl: String,
@Qualifier("PENTokenProvider") private val tokenProvider: TokenProvider,
private val penBrevMetricsMåling: PENBrevMetrikker,
class PENBrevClient(
@Value("\${PEN_BASE_URL}")
private val baseUrl: String,
@Qualifier("PENTokenProvider")
private val tokenProvider: TokenProvider,
private val penBrevMetricsMåling: PENBrevMetrikker,

) : BrevClient {
) : BrevClient {
private val restTemplate = RestTemplateBuilder().build()

override fun sendBrev(
sakId: String,
fnr: String,
omsorgsår: Int
): Journalpost {
val url = "$baseUrl/api/bestillbrev/todo"
companion object {
fun sendBrevPath(sakId: String) : String { return "/sak/$sakId/PE_OMSORG_HJELPESTOENAD_AUTO" }
}


override fun sendBrev(sakId: String, fnr: String, omsorgsår: Year, språk: BrevSpraak?): Journalpost {
val url = baseUrl + sendBrevPath(sakId)
println("sendBrev: url=$url")
return try {
penBrevMetricsMåling.oppdater {
val brevRequest = serialize(SendBrevRequest(
omsorgsår,
UUID.randomUUID().toString(), // TODO: Fiks
språk
))
println("brevRequest: $brevRequest")
val response = restTemplate.exchange(
url,
HttpMethod.POST,
HttpEntity(
serialize(
SendBrevRequest(
sakId = sakId,
fnr = fnr,
omsorgsår = omsorgsår
)
),
serialize(SendBrevRequest(
omsorgsår,
UUID.randomUUID().toString(), // TODO: Fiks
språk
)),
HttpHeaders().apply {
add("Nav-Call-Id", Mdc.getCorrelationId())
add("Nav-Consumer-Id", "omsorgsopptjening-bestem-pensjonsopptjening")
Expand All @@ -70,13 +81,30 @@ private class PENBrevClient(
}
}

private data class SendBrevRequest(
val sakId: String,
val fnr: String,
val omsorgsår: Int
)

data class BrevData(val aarInvilgetOmsorgspoeng: Int)
data class Overstyr(val spraak: BrevSpraak)

data class SendBrevRequest(
// val omsorgsår: Year,
val brevdata: BrevData,
val eksternReferanseId: String,
@JsonInclude(JsonInclude.Include. NON_NULL)
val overstyr: Overstyr?
) {
constructor(omsorgsår: Year,eksternReferanseId: String,spraak: BrevSpraak? = null) :
this(BrevData(omsorgsår.value),eksternReferanseId,spraak?.let { språk -> Overstyr(språk) })
}


private data class SendBrevResponse(
val journalpostId: String
)

enum class BrevSpraak {
EN, NB, NN
}

class PENBrevKlientException(message: String, throwable: Throwable) : RuntimeException(message, throwable)

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model

import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.external.PENBrevClient
import java.time.Year

interface BrevClient {
fun sendBrev(sakId: String, fnr: String, omsorgsår: Int): Journalpost
fun sendBrev(sakId: String, fnr: String, omsorgsår: Year, språk: PENBrevClient.BrevSpraak? = null): Journalpost
}

data class BrevClientException(val msg: String, val throwable: Throwable?): RuntimeException(msg, throwable)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.support.TransactionTemplate
import java.time.Year

@Component
class BrevService(
Expand Down Expand Up @@ -62,7 +63,7 @@ class BrevService(
brevClient.sendBrev(
sakId = sak.sakId,
fnr = brev.omsorgsyter,
omsorgsår = brev.omsorgsår,
omsorgsår = Year.of(brev.omsorgsår),
).let { journalpost ->
brev.ferdig(journalpost).also {
brevRepository.updateStatus(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.br

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.stubbing.Scenario
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.external.PENBrevClient.Companion.sendBrevPath
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model.Brev
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model.BrevClient
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model.BrevClientException
Expand Down Expand Up @@ -87,7 +88,7 @@ class BrevProsesseringTest : SpringContextTest.NoKafka() {
wiremock.bestemSakOk()

wiremock.givenThat(
WireMock.post(WireMock.urlPathEqualTo(SEND_BREV_PATH))
WireMock.post(WireMock.urlPathEqualTo(sendBrevPath("42")))
.inScenario("retry")
.whenScenarioStateIs(Scenario.STARTED)
.willReturn(
Expand All @@ -97,7 +98,7 @@ class BrevProsesseringTest : SpringContextTest.NoKafka() {
)

wiremock.givenThat(
WireMock.post(WireMock.urlPathEqualTo(SEND_BREV_PATH))
WireMock.post(WireMock.urlPathEqualTo(sendBrevPath("42")))
.inScenario("retry")
.whenScenarioStateIs("feil 2")
.willReturn(
Expand All @@ -107,7 +108,7 @@ class BrevProsesseringTest : SpringContextTest.NoKafka() {
)

wiremock.givenThat(
WireMock.post(WireMock.urlPathEqualTo(SEND_BREV_PATH))
WireMock.post(WireMock.urlPathEqualTo(sendBrevPath("42")))
.inScenario("retry")
.whenScenarioStateIs("ok")
.willReturn(
Expand Down Expand Up @@ -216,7 +217,7 @@ class BrevProsesseringTest : SpringContextTest.NoKafka() {
wiremock.bestemSakOk()

wiremock.givenThat(
WireMock.post(WireMock.urlPathEqualTo(SEND_BREV_PATH))
WireMock.post(WireMock.urlPathEqualTo(sendBrevPath("42")))
.willReturn(
WireMock.forbidden()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.external

import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.*
import com.github.tomakehurst.wiremock.core.WireMockConfiguration
import com.github.tomakehurst.wiremock.junit5.WireMockExtension
import com.github.tomakehurst.wiremock.matching.UrlPattern
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model.BrevClient
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model.BrevClientException
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.model.Journalpost
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.common.SpringContextTest
import no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.utils.Mdc
import no.nav.pensjon.opptjening.omsorgsopptjening.felles.CorrelationId
import no.nav.pensjon.opptjening.omsorgsopptjening.felles.InnlesingId
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.RegisterExtension
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpHeaders
import java.time.LocalDate
import java.time.Year
import java.time.format.DateTimeFormatter
import kotlin.test.Test
import kotlin.test.assertEquals

class BrevKlientTest(
@Value("\${PEN_BASE_URL}")
private val baseUrl: String,
) : SpringContextTest.NoKafka() {

@Autowired
private lateinit var client: PENBrevClient

companion object {
@JvmField
@RegisterExtension
val wiremock = WireMockExtension.newInstance()
.options(WireMockConfiguration.wireMockConfig().port(WIREMOCK_PORT))
.build()!!
}

@Test
fun `kaster exception dersom kall ikke gikk bra`() {
Mdc.scopedMdc(CorrelationId.generate()) {
Mdc.scopedMdc(InnlesingId.generate()) {
val url = baseUrl + PENBrevClient.sendBrevPath("42")
println("Wiremock-path: ${url}")
wiremock.givenThat(
WireMock.post("*")
// WireMock.post(WireMock.urlPathEqualTo(url))
.willReturn(WireMock.serverError())
)

assertThrows<BrevClientException> {
client.sendBrev(
"42",
"12345678912",
Year.of(2020),
)
}
}
}
}

@Test
fun `returnerer id for opprettet brev hvis kall går bra`() {
val sakId = "42"
Mdc.scopedMdc(CorrelationId.generate()) { correlationId ->
Mdc.scopedMdc(InnlesingId.generate()) { innlesingId ->
wiremock.givenThat(
WireMock.post(UrlPattern.ANY)
// WireMock.post(WireMock.urlPathEqualTo(PENBrevClient.sendBrevPath(sakId)))
// .withHeader(HttpHeaders.AUTHORIZATION, equalTo("Bearer test.token.test"))
// .withHeader(HttpHeaders.ACCEPT, equalTo("application/json"))
// .withHeader(HttpHeaders.CONTENT_TYPE, equalTo("application/json"))
// .withHeader("x-correlation-id", equalTo(correlationId.toString()))
// .withHeader("X-Correlation-ID", equalTo(correlationId.toString()))
// .withHeader("x-innlesing-id", equalTo(innlesingId.toString()))
// .withRequestBody(
// equalToJson(
// """{"brevdata":{"aarInvilgetOmsorgspoeng":2020},"eksternReferanseId":"${'$'}{json-unit.any-string}"}"""
// )
// )
.willReturn(
WireMock.created()
.withBody("""{"journalpostId":"123"}""")
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
)
)

assertThat(client.sendBrev("42", "123451234512", Year.of(2020)))
.isEqualTo(Journalpost("123"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package no.nav.pensjon.opptjening.omsorgsopptjening.bestem.pensjonsopptjening.brev.external

import no.nav.pensjon.opptjening.omsorgsopptjening.felles.serialize
import org.junit.jupiter.api.Test
import java.time.Year

class PENBrevSerializationTest {

@Test
fun testSerializeRegular() {
val request = PENBrevClient.SendBrevRequest(omsorgsår = Year.of(2010), eksternReferanseId = "42")
val json = serialize(request)
println("2 $json")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ sealed class SpringContextTest {
const val PDL_PATH = "/graphql"
const val WIREMOCK_PORT = 9991
const val BESTEM_SAK_PATH = "/pen/api/bestemsak/v1"
const val SEND_BREV_PATH = "/pen/api/bestillbrev/todo"
const val OPPGAVE_PATH = "/api/v1/oppgaver"
const val POPP_OMSORG_PATH = "/api/omsorg"
const val POPP_PENSJONSPOENG_PATH = "/api/pensjonspoeng"
Expand Down

0 comments on commit 5c3d2c9

Please sign in to comment.