Skip to content

Commit

Permalink
Linterendringer og fikset postgresql feil i tester som følge av oppgr…
Browse files Browse the repository at this point in the history
…adering til spring 3.3.2
  • Loading branch information
olekvernberg committed Aug 2, 2024
1 parent 7f474e6 commit 971b713
Show file tree
Hide file tree
Showing 30 changed files with 228 additions and 252 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
<version>1.20.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
<version>10.15.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions prosessering-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@
<artifactId>flyway-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
<version>10.15.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ data class TaskFeil(
)

@Throws(IOException::class)
fun writeValueAsString(): String {
return objectWriter!!.writeValueAsString(this)
}
fun writeValueAsString(): String = objectWriter!!.writeValueAsString(this)

companion object {
private var objectWriter: ObjectWriter? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import org.springframework.data.convert.WritingConverter

@ReadingConverter
class StringTilPropertiesWrapperConverter : Converter<String, PropertiesWrapper> {
override fun convert(p0: String): PropertiesWrapper? {
return PropertiesWrapper(p0.asProperties())
}
override fun convert(p0: String): PropertiesWrapper? = PropertiesWrapper(p0.asProperties())
}

@WritingConverter
class PropertiesWrapperTilStringConverter : Converter<PropertiesWrapper, String> {
override fun convert(taskPropertiesWrapper: PropertiesWrapper): String? {
return taskPropertiesWrapper.properties.asString()
}
override fun convert(taskPropertiesWrapper: PropertiesWrapper): String? = taskPropertiesWrapper.properties.asString()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ fun Properties.asString(): String {
}
val stringWriter = StringWriter(512)
// custom i stedet for Properties.store slik at vi ikke får med default timestamp
this.forEach { key, value -> stringWriter.append(key as String).append('=').append(value as String).append('\n') }
this.forEach { key, value ->
stringWriter
.append(key as String)
.append('=')
.append(value as String)
.append('\n')
}
return stringWriter.toString()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package no.nav.familie.prosessering.domene

import java.util.Properties

class PropertiesWrapper(val properties: Properties = Properties())
class PropertiesWrapper(
val properties: Properties = Properties(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ data class Task(
),
)

fun medTriggerTid(triggerTid: LocalDateTime): Task {
return this.copy(triggerTid = triggerTid)
}
fun medTriggerTid(triggerTid: LocalDateTime): Task = this.copy(triggerTid = triggerTid)

override fun toString(): String {
return "Task(id=$id, status=$status, opprettetTid=$opprettetTid, triggerTid=$triggerTid, type='$type', versjon=$versjon)"
}
override fun toString(): String =
"Task(id=$id, status=$status, opprettetTid=$opprettetTid, triggerTid=$triggerTid, type='$type', versjon=$versjon)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ data class TaskLogg(
val melding: String? = null,
val opprettetTid: LocalDateTime = LocalDateTime.now(),
) {
override fun toString(): String {
return "TaskLogg(id=$id, type=$type, opprettetTid=$opprettetTid)"
}
override fun toString(): String = "TaskLogg(id=$id, type=$type, opprettetTid=$opprettetTid)"

companion object {
const val BRUKERNAVN_NÅR_SIKKERHETSKONTEKST_IKKE_FINNES = "VL"

private fun hentNodeNavn(): String {
return System.getenv("HOSTNAME") ?: "node1"
}
private fun hentNodeNavn(): String = System.getenv("HOSTNAME") ?: "node1"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import org.springframework.data.repository.PagingAndSortingRepository
import org.springframework.stereotype.Repository

@Repository
internal interface TaskLoggRepository : PagingAndSortingRepository<TaskLogg, Long>, CrudRepository<TaskLogg, Long> {
internal interface TaskLoggRepository :
PagingAndSortingRepository<TaskLogg, Long>,
CrudRepository<TaskLogg, Long> {
@Query
fun findByTaskId(taskId: Long): List<TaskLogg>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import org.springframework.stereotype.Repository
import java.time.LocalDateTime

@Repository
internal interface TaskRepository : PagingAndSortingRepository<Task, Long>, CrudRepository<Task, Long> {
internal interface TaskRepository :
PagingAndSortingRepository<Task, Long>,
CrudRepository<Task, Long> {
fun findByStatusInAndTriggerTidBeforeOrderByOpprettetTid(
status: List<Status>,
triggerTid: LocalDateTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package no.nav.familie.prosessering.error

import java.time.LocalDateTime

data class RekjørSenereException(val årsak: String, val triggerTid: LocalDateTime) :
RuntimeException("Rekjører senere - triggerTid=$triggerTid")
data class RekjørSenereException(
val årsak: String,
val triggerTid: LocalDateTime,
) : RuntimeException("Rekjører senere - triggerTid=$triggerTid")

data class MaxAntallRekjøringerException(val maxAntallRekjøring: Int) :
RuntimeException("Nådd max antall rekjøring - $maxAntallRekjøring")
data class MaxAntallRekjøringerException(
val maxAntallRekjøring: Int,
) : RuntimeException("Nådd max antall rekjøring - $maxAntallRekjøring")
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package no.nav.familie.prosessering.error

class TaskExceptionUtenStackTrace(melding: String) : RuntimeException(melding)
class TaskExceptionUtenStackTrace(
melding: String,
) : RuntimeException(melding)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import java.util.regex.Pattern
* [MDC] backet parameter som tillater en semi-colon separert liste av sub-keys.
* Kan dermed legge til og fjerne ekstra-kontekst data dynamisk.
*/
class MdcExtendedLogContext private constructor(private val paramName: String) {
class MdcExtendedLogContext private constructor(
private val paramName: String,
) {
fun add(
key: String,
value: String,
Expand Down Expand Up @@ -43,9 +45,12 @@ class MdcExtendedLogContext private constructor(private val paramName: String) {
.toMutableMap()
}

private fun toParamValue(elements: Map<String, String>): String {
return paramName + "[" + elements.map { it.key + '=' + it.value }.joinToString(";") + "]"
}
private fun toParamValue(elements: Map<String, String>): String =
paramName + "[" +
elements
.map {
it.key + '=' + it.value
}.joinToString(";") + "]"

fun clear() {
MDC.remove(paramName)
Expand All @@ -54,8 +59,6 @@ class MdcExtendedLogContext private constructor(private val paramName: String) {
companion object {
private val ILLEGAL_CHARS = Pattern.compile("[\\[\\];=]")

fun getContext(kontekstParamNavn: String): MdcExtendedLogContext {
return MdcExtendedLogContext(kontekstParamNavn)
}
fun getContext(kontekstParamNavn: String): MdcExtendedLogContext = MdcExtendedLogContext(kontekstParamNavn)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ class TaskService internal constructor(
private val logger = LoggerFactory.getLogger(javaClass)
private val secureLog = LoggerFactory.getLogger("secureLogger")

fun findById(id: Long): Task {
return taskRepository.findByIdOrNull(id) ?: error("Task med id: $id ikke funnet.")
}
fun findById(id: Long): Task = taskRepository.findByIdOrNull(id) ?: error("Task med id: $id ikke funnet.")

/**
* Brukes for å opprette task
Expand All @@ -49,55 +47,46 @@ class TaskService internal constructor(
*/
@Suppress("unused") // brukes av klienter
@Transactional
fun saveAll(tasks: Collection<Task>): List<Task> {
return tasks.map { save(it) }
}
fun saveAll(tasks: Collection<Task>): List<Task> = tasks.map { save(it) }

private fun validerTask(task: Task) {
if ((task.versjon == 0L && task.id != 0L) || (task.id == 0L && task.versjon != 0L)) {
error("Når man oppretter en ny task må task og versjon være satte til 0")
}
}

fun finnAlleTasksKlareForProsessering(page: Pageable): List<Task> {
return taskRepository.findByStatusInAndTriggerTidBeforeOrderByOpprettetTid(
fun finnAlleTasksKlareForProsessering(page: Pageable): List<Task> =
taskRepository.findByStatusInAndTriggerTidBeforeOrderByOpprettetTid(
listOf(
Status.KLAR_TIL_PLUKK,
Status.UBEHANDLET,
),
LocalDateTime.now(),
page,
)
}

fun finnAlleFeiledeTasks(): List<Task> {
return taskRepository.findByStatus(Status.FEILET)
}
fun finnAlleFeiledeTasks(): List<Task> = taskRepository.findByStatus(Status.FEILET)

internal fun finnAllePlukkedeTasks(tid: LocalDateTime): Pair<Long, List<Task>> {
return taskRepository.countByStatusIn(listOf(Status.PLUKKET)) to
internal fun finnAllePlukkedeTasks(tid: LocalDateTime): Pair<Long, List<Task>> =
taskRepository.countByStatusIn(listOf(Status.PLUKKET)) to
taskRepository.findAllByStatusAndLastProcessed(Status.PLUKKET, tid)
}

@Deprecated("Bruk finnTasksMedStatus", ReplaceWith("finnTasksMedStatus(status, type, page)"))
fun finnTasksMedStatus(
status: List<Status>,
page: Pageable,
): List<Task> {
return taskRepository.findByStatusIn(status, page)
}
): List<Task> = taskRepository.findByStatusIn(status, page)

fun finnTasksMedStatus(
status: List<Status>,
type: String? = null,
page: Pageable = Pageable.unpaged(),
): List<Task> {
return if (type == null) {
): List<Task> =
if (type == null) {
taskRepository.findByStatusIn(status, page)
} else {
taskRepository.findByStatusInAndType(status, type, page)
}
}

@Transactional
internal fun slettTasks(
Expand All @@ -121,33 +110,25 @@ class TaskService internal constructor(
return taskLoggRepository.finnTaskLoggMetadata(taskIds).associateBy { it.taskId }
}

fun findTaskLoggByTaskId(taskId: Long): List<TaskLogg> {
return taskLoggRepository.findByTaskId(taskId)
}
fun findTaskLoggByTaskId(taskId: Long): List<TaskLogg> = taskLoggRepository.findByTaskId(taskId)

@Suppress("unused") // brukes av klienter
fun finnTaskMedPayloadOgType(
payload: String,
type: String,
): Task? {
return taskRepository.findByPayloadAndType(payload, type)
}
): Task? = taskRepository.findByPayloadAndType(payload, type)

@Suppress("unused") // brukes av klienter
fun finnAlleTaskerMedPayloadOgType(
payload: String,
type: String,
): List<Task> {
return taskRepository.findAllByPayloadAndType(payload, type)
}
): List<Task> = taskRepository.findAllByPayloadAndType(payload, type)

/**
* Då taskRepository er internal så kan denne fortsatt være fin å bruke fra tests
*/
@Suppress("unused") // brukes av klienter
fun findAll(): List<Task> {
return taskRepository.findAll().toList()
}
fun findAll(): List<Task> = taskRepository.findAll().toList()

@Transactional
fun delete(task: Task) {
Expand All @@ -163,28 +144,19 @@ class TaskService internal constructor(
taskRepository.deleteAll(tasks)
}

internal fun antallTaskerTilOppfølging(): Long {
return taskRepository.countByStatusIn(listOf(Status.MANUELL_OPPFØLGING, Status.FEILET))
}
internal fun antallTaskerTilOppfølging(): Long = taskRepository.countByStatusIn(listOf(Status.MANUELL_OPPFØLGING, Status.FEILET))

fun antallTaskerMedStatusFeiletOgManuellOppfølging(): TaskerMedStatusFeiletOgManuellOppfølging {
return TaskerMedStatusFeiletOgManuellOppfølging(
fun antallTaskerMedStatusFeiletOgManuellOppfølging(): TaskerMedStatusFeiletOgManuellOppfølging =
TaskerMedStatusFeiletOgManuellOppfølging(
taskRepository.countByStatusIn(listOf(Status.FEILET)),
taskRepository.countByStatusIn(listOf(Status.MANUELL_OPPFØLGING)),
)
}

internal fun tellAntallÅpneTasker(): List<AntallÅpneTask> {
return taskRepository.countOpenTasks()
}
internal fun tellAntallÅpneTasker(): List<AntallÅpneTask> = taskRepository.countOpenTasks()

fun antallFeil(taskId: Long): Int {
return taskLoggRepository.countByTaskIdAndType(taskId, Loggtype.FEILET)
}
fun antallFeil(taskId: Long): Int = taskLoggRepository.countByTaskIdAndType(taskId, Loggtype.FEILET)

fun antallGangerPlukket(taskId: Long): Int {
return taskLoggRepository.countByTaskIdAndType(taskId, Loggtype.PLUKKET)
}
fun antallGangerPlukket(taskId: Long): Int = taskLoggRepository.countByTaskIdAndType(taskId, Loggtype.PLUKKET)

@Transactional
internal fun avvikshåndter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,18 @@ class TaskWorker(
secureLog.info("Feilhåndtering lagret ok {}", task)
}

private fun finnTriggerTidVedFeil(taskType: String): Long {
return triggerTidVedFeilMap[taskType] ?: error("Ukjent tasktype $taskType")
}
private fun finnTriggerTidVedFeil(taskType: String): Long = triggerTidVedFeilMap[taskType] ?: error("Ukjent tasktype $taskType")

private fun finnFeilteller(taskType: String): Counter {
return feiltellereForTaskSteps[taskType] ?: error("Ukjent tasktype $taskType")
}
private fun finnFeilteller(taskType: String): Counter = feiltellereForTaskSteps[taskType] ?: error("Ukjent tasktype $taskType")

private fun finnFullførtteller(taskType: String): Counter {
return fullførttellereForTaskSteps[taskType] ?: error("Ukjent tasktype $taskType")
}
private fun finnFullførtteller(taskType: String): Counter = fullførttellereForTaskSteps[taskType] ?: error("Ukjent tasktype $taskType")

private fun finnMaxAntallFeil(taskType: String): Int {
return maxAntallFeilMap[taskType] ?: error("Ukjent tasktype $taskType")
}
private fun finnMaxAntallFeil(taskType: String): Int = maxAntallFeilMap[taskType] ?: error("Ukjent tasktype $taskType")

private fun finnTaskStep(taskType: String): AsyncTaskStep {
return taskStepMap[taskType] ?: error("Ukjent tasktype $taskType")
}
private fun finnTaskStep(taskType: String): AsyncTaskStep = taskStepMap[taskType] ?: error("Ukjent tasktype $taskType")

private fun finnSettTilManuellOppfølgning(taskType: String): Boolean {
return settTilManuellOppfølgningVedFeil[taskType] ?: error("Ukjent tasktype $taskType")
}
private fun finnSettTilManuellOppfølgning(taskType: String): Boolean =
settTilManuellOppfølgningVedFeil[taskType] ?: error("Ukjent tasktype $taskType")

@Transactional(propagation = Propagation.REQUIRES_NEW)
fun markerPlukket(id: Long): Task? {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package no.nav.familie.prosessering.internal

data class TaskerMedStatusFeiletOgManuellOppfølging(val antallFeilet: Long, val antallManuellOppfølging: Long)
data class TaskerMedStatusFeiletOgManuellOppfølging(
val antallFeilet: Long,
val antallManuellOppfølging: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ interface TaskMetricRepository : org.springframework.data.repository.Repository<
fun finnAntallFeiledeTasksPerTypeOgStatus(): List<AntallTaskAvTypeOgStatus>
}

data class AntallTaskAvTypeOgStatus(val type: String, val status: Status, val count: Long)
data class AntallTaskAvTypeOgStatus(
val type: String,
val status: Status,
val count: Long,
)
Loading

0 comments on commit 971b713

Please sign in to comment.