Skip to content

Commit

Permalink
Feature/utvid statistikk hendelse (#412)
Browse files Browse the repository at this point in the history
* Renamet grunnlagstype barnetilsyn med stønad

* Utvidet ForskuddHendelse med saksnr og barnets fødselsdato
  • Loading branch information
rinnan17 authored Jan 16, 2025
1 parent dd0b7ae commit 858cb44
Show file tree
Hide file tree
Showing 100 changed files with 488 additions and 570 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import java.net.URI
import java.util.Stack
import kotlin.collections.HashMap

class HttpHeaderTestRestTemplate(val testRestTemplate: TestRestTemplate) {
class HttpHeaderTestRestTemplate(
val testRestTemplate: TestRestTemplate,
) {
private val headersForSingleCallbacks = Stack<Pair<String, String>>()
private val valueGenerators: MutableMap<String, ValueGenerator> = HashMap()

Expand All @@ -20,104 +22,76 @@ class HttpHeaderTestRestTemplate(val testRestTemplate: TestRestTemplate) {
httpMethod: HttpMethod?,
httpEntity: HttpEntity<*>,
responseClass: Class<T>?,
): ResponseEntity<T> {
return testRestTemplate.exchange(url, httpMethod, newEntityWithAddedHeaders(httpEntity), responseClass)
}
): ResponseEntity<T> = testRestTemplate.exchange(url, httpMethod, newEntityWithAddedHeaders(httpEntity), responseClass)

fun <T> exchange(
url: String?,
httpMethod: HttpMethod?,
httpEntity: HttpEntity<*>?,
typeReference: ParameterizedTypeReference<T>?,
): ResponseEntity<T> {
return testRestTemplate.exchange(url, httpMethod, newEntityWithAddedHeaders(httpEntity), typeReference)
}
): ResponseEntity<T> = testRestTemplate.exchange(url, httpMethod, newEntityWithAddedHeaders(httpEntity), typeReference)

fun <T> postForEntity(
url: String?,
httpEntity: HttpEntity<*>,
responseClass: Class<T>?,
): ResponseEntity<T> {
return testRestTemplate.postForEntity(url, newEntityWithAddedHeaders(httpEntity), responseClass)
}
): ResponseEntity<T> = testRestTemplate.postForEntity(url, newEntityWithAddedHeaders(httpEntity), responseClass)

inline fun <reified T : Any> getForEntity(
uri: URI,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.GET, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.GET, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> putForEntity(
uri: URI,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.PUT, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.PUT, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> postForEntity(
uri: URI,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.POST, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.POST, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> patchForEntity(
uri: URI,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.PATCH, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.PATCH, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> optionsForEntity(
uri: URI,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.OPTIONS, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.OPTIONS, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> delete(uri: URI): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.DELETE, newEntityWithAddedHeaders())
}
inline fun <reified T : Any> delete(uri: URI): ResponseEntity<T> =
testRestTemplate.exchange(uri, HttpMethod.DELETE, newEntityWithAddedHeaders())

inline fun <reified T : Any> getForEntity(
uri: String,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.GET, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.GET, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> putForEntity(
uri: String,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.PUT, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.PUT, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> postForEntity(
uri: String,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.POST, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.POST, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> patchForEntity(
uri: String,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.PATCH, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.PATCH, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> optionsForEntity(
uri: String,
request: Any? = null,
): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.OPTIONS, newEntityWithAddedHeaders(request))
}
): ResponseEntity<T> = testRestTemplate.exchange(uri, HttpMethod.OPTIONS, newEntityWithAddedHeaders(request))

inline fun <reified T : Any> delete(uri: String): ResponseEntity<T> {
return testRestTemplate.exchange(uri, HttpMethod.DELETE, newEntityWithAddedHeaders())
}
inline fun <reified T : Any> delete(uri: String): ResponseEntity<T> =
testRestTemplate.exchange(uri, HttpMethod.DELETE, newEntityWithAddedHeaders())

fun newEntityWithAddedHeaders(request: Any? = null): HttpEntity<*> {
val tempHeaders =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package no.nav.bidrag.commons

class CorrelationId private constructor(private val idValue: String) {
class CorrelationId private constructor(
private val idValue: String,
) {
init {
CORRELATION_ID_VALUE.set(idValue)
}

fun get(): String {
return idValue
}
fun get(): String = idValue

companion object {
const val CORRELATION_ID_HEADER = "X-Correlation-ID"
private val CORRELATION_ID_VALUE = ThreadLocal<String>()

fun fetchCorrelationIdForThread(): String {
return CORRELATION_ID_VALUE.get() ?: generateTimestamped("UNKNOWN").get()
}
fun fetchCorrelationIdForThread(): String = CORRELATION_ID_VALUE.get() ?: generateTimestamped("UNKNOWN").get()

fun existing(value: String?): CorrelationId {
return if (value.isNullOrBlank()) {
fun existing(value: String?): CorrelationId =
if (value.isNullOrBlank()) {
generateTimestamped("correlationId")
} else {
CorrelationId(value)
}
}

fun generateTimestamped(value: String): CorrelationId {
val currentTimeAsString = java.lang.Long.toHexString(System.currentTimeMillis())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package no.nav.bidrag.commons
import org.slf4j.LoggerFactory
import org.springframework.web.client.HttpStatusCodeException

class ExceptionLogger(private val application: String, vararg doNotLogClasses: Class<*>) {
class ExceptionLogger(
private val application: String,
vararg doNotLogClasses: Class<*>,
) {
private val logger = LoggerFactory.getLogger(this::class.java)

private val doNotLogClasses = doNotLogClasses.map { it.name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ package no.nav.bidrag.commons.cache

import org.apache.commons.lang3.builder.HashCodeBuilder

class BrukerCacheNøkkel(private val userId: String, private val key: Any) {
class BrukerCacheNøkkel(
private val userId: String,
private val key: Any,
) {
override fun equals(other: Any?): Boolean {
if (other is BrukerCacheNøkkel) {
return userId == other.userId && key == other.key
}
return false
}

override fun hashCode(): Int {
return HashCodeBuilder()
override fun hashCode(): Int =
HashCodeBuilder()
.append(userId)
.append(key)
.toHashCode()
}

override fun toString(): String {
return StringBuilder()
override fun toString(): String =
StringBuilder()
.append(userId)
.append(" - ")
.append(key)
.toString()
}

companion object {
const val GENERATOR_BØNNE = "BrukerCacheNøkkelGenerator"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import no.nav.bidrag.commons.security.service.OidcTokenManager
import org.springframework.cache.interceptor.SimpleKeyGenerator
import java.lang.reflect.Method

class BrukerCacheNøkkelGenerator(private val oidcTokenManager: OidcTokenManager = OidcTokenManager()) : SimpleKeyGenerator() {
class BrukerCacheNøkkelGenerator(
private val oidcTokenManager: OidcTokenManager = OidcTokenManager(),
) : SimpleKeyGenerator() {
companion object {
const val SYSTEMBRUKER_ID = "SYSTEM"
}
Expand All @@ -13,9 +15,7 @@ class BrukerCacheNøkkelGenerator(private val oidcTokenManager: OidcTokenManager
target: Any,
method: Method,
vararg params: Any,
): Any {
return tilBrukerCacheKey(super.generate(target, method, *params))
}
): Any = tilBrukerCacheKey(super.generate(target, method, *params))

private fun tilBrukerCacheKey(key: Any): BrukerCacheNøkkel {
val userId = if (oidcTokenManager.erApplikasjonBruker()) SYSTEMBRUKER_ID else oidcTokenManager.hentSaksbehandlerIdentFraToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class InvaliderCacheFørStartenAvArbeidsdag : Expiry<Any, Any> {
currentTime: Long,
): Long {
val expireAt =
LocalDateTime.now()
LocalDateTime
.now()
.plusDays(1)
.withHour(6)
.withMinute(0)
Expand All @@ -25,16 +26,12 @@ class InvaliderCacheFørStartenAvArbeidsdag : Expiry<Any, Any> {
o2: Any,
currentTime: Long,
currentDuration: Long,
): Long {
return currentDuration
}
): Long = currentDuration

override fun expireAfterRead(
p0: Any,
p1: Any,
currentTime: Long,
currentDuration: Long,
): Long {
return currentDuration
}
): Long = currentDuration
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class SensitiveLogMasker : ValueMasker {
override fun mask(
p0: JsonStreamContext?,
p1: Any?,
): Any? {
return (
): Any? =
(
if (p1 is CharSequence) {
maskLogMessage(p1)
} else {
p1
}
)
}

fun maskLogMessage(logMessage: CharSequence?): String {
val sb = StringBuilder(logMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ class AuditAdvice(
private fun finnSporingsdataForNavngittFeltIRequestBody(
requestBody: Any,
feltnavn: String,
): Sporingsdata {
return finnSporingsdataForFeltIRequestBody(requestBody, feltnavn)
}
): Sporingsdata = finnSporingsdataForFeltIRequestBody(requestBody, feltnavn)

private fun finnSporingsdataForFørsteKonstruktørparameterIRequestBody(requestBody: Any): Sporingsdata {
val feltnavn = Feltekstraherer.finnNavnPåFørsteKonstruktørParameter(requestBody)
Expand Down Expand Up @@ -89,19 +87,15 @@ class AuditAdvice(
}
}

private fun finnSporingsdataForString(s: String): Sporingsdata {
return when {
private fun finnSporingsdataForString(s: String): Sporingsdata =
when {
Saksnummer(s).gyldig() -> tilgangClient.hentSporingsdataSak(s)
Personident(s).gyldig() -> tilgangClient.hentSporingsdataPerson(s)
else -> error("Type på oppslagsfelt ikke støttet av audit-log")
}
}

private fun finnSporingsdataForPersonIdent(personIdent: Personident): Sporingsdata {
return tilgangClient.hentSporingsdataPerson(personIdent.verdi)
}
private fun finnSporingsdataForPersonIdent(personIdent: Personident): Sporingsdata =
tilgangClient.hentSporingsdataPerson(personIdent.verdi)

private fun finnSporingsdataForSaksnummer(saksnummer: Saksnummer): Sporingsdata {
return tilgangClient.hentSporingsdataSak(saksnummer.verdi)
}
private fun finnSporingsdataForSaksnummer(saksnummer: Saksnummer): Sporingsdata = tilgangClient.hentSporingsdataSak(saksnummer.verdi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ annotation class AuditLog(
val oppslagsparameter: String = "",
)

enum class AuditLoggerEvent(val type: String) {
enum class AuditLoggerEvent(
val type: String,
) {
CREATE("create"),
UPDATE("update"),
DELETE("delete"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class AuditLogger(
}
}

private fun getRequest(): HttpServletRequest? {
return RequestContextHolder.getRequestAttributes()
private fun getRequest(): HttpServletRequest? =
RequestContextHolder
.getRequestAttributes()
?.takeIf { it is ServletRequestAttributes }
?.let { it as ServletRequestAttributes }
?.request
}

private fun createAuditLogString(
event: AuditLoggerEvent,
Expand All @@ -64,17 +64,15 @@ class AuditLogger(
"flexStringLabel1=decision flexString1=${if (data.tilgang) "permit" else "deny"}"
}

private fun createCustomString(data: Sporingsdata): String {
return listOfNotNull(
private fun createCustomString(data: Sporingsdata): String =
listOfNotNull(
data.ekstrafelter.getOrNull(0)?.let { "cs3Label=${it.first} cs3=${it.second}" },
data.ekstrafelter.getOrNull(1)?.let { "cs5Label=${it.first} cs5=${it.second}" },
data.ekstrafelter.getOrNull(2)?.let { "cs6Label=${it.first} cs6=${it.second}" },
).joinToString(" ")
}

private fun getCallId(): String {
return MDC.get(CorrelationIdFilter.CORRELATION_ID_MDC)
private fun getCallId(): String =
MDC.get(CorrelationIdFilter.CORRELATION_ID_MDC)
?: MDC.get(MdcConstants.MDC_CALL_ID)
?: throw IllegalStateException("Mangler correlationId/callId")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package no.nav.bidrag.commons.security
import no.nav.security.token.support.spring.SpringTokenValidationContextHolder

object ContextService {
fun hentPåloggetSaksbehandler(): String {
return Result.runCatching { SpringTokenValidationContextHolder().tokenValidationContext }
fun hentPåloggetSaksbehandler(): String =
Result
.runCatching { SpringTokenValidationContextHolder().tokenValidationContext }
.fold(
onSuccess = { it.getClaims("aad")?.get("NAVident")?.toString() ?: error("Finner ikke NAVident i token") },
onFailure = { error("Finner ikke NAVident i token") },
)
}

fun erMaskinTilMaskinToken(): Boolean {
val claims = SpringTokenValidationContextHolder().tokenValidationContext.getClaims("aad")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ object SikkerhetsKontekst {
}
}

fun erIApplikasjonKontekst(): Boolean {
return ER_I_APPLIKASJONSKONTEKST.get() ?: false
}
fun erIApplikasjonKontekst(): Boolean = ER_I_APPLIKASJONSKONTEKST.get() ?: false
}
Loading

0 comments on commit 858cb44

Please sign in to comment.