Skip to content

Commit

Permalink
Merge pull request #33 from navikt/task/correlation_id
Browse files Browse the repository at this point in the history
Add correlation id to packet
  • Loading branch information
geiralund authored Dec 20, 2019
2 parents 9ad662a + ef93265 commit fc5d430
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ val sourcesJar by tasks.registering(Jar::class) {
from(sourceSets["main"].allSource)
}

tasks.named("compileKotlin") {
dependsOn("spotlessCheck")
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging {
Expand Down
2 changes: 2 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jdk:
- openjdk12
24 changes: 12 additions & 12 deletions src/main/kotlin/no/nav/dagpenger/events/Packet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.nav.dagpenger.events
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.Types
import de.huxhorn.sulky.ulid.ULID
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime
Expand All @@ -12,11 +13,13 @@ class Packet constructor(jsonString: String = "{}") {

companion object {
internal const val READ_COUNT = "system_read_count"
internal const val CORRELATION_ID = "system_correlation_id"
internal const val STARTED = "system_started"
internal const val PROBLEM = "system_problem"
internal const val BREADCRUMBS = "system_breadcrumbs"

private val adapter = moshiInstance.adapter<Map<String, Any?>>(Map::class.java).lenient()
private val ulidGen = ULID()
}

private var problem: Problem? = null
Expand All @@ -31,6 +34,9 @@ class Packet constructor(jsonString: String = "{}") {
if (!json.containsKey(STARTED)) {
json[STARTED] = LocalDateTime.now()
}
if (!json.containsKey(CORRELATION_ID)) {
json[CORRELATION_ID] = ulidGen.nextULID()
}
json[READ_COUNT] = (json[READ_COUNT] as Double).toInt() + 1
if (json.containsKey(PROBLEM)) {
problem = Problem.fromJson(this.getMapValue(PROBLEM))
Expand Down Expand Up @@ -134,23 +140,17 @@ class Packet constructor(jsonString: String = "{}") {
}
}

fun hasProblem(): Boolean {
return problem != null
}
fun getCorrelationId(): String = getStringValue(CORRELATION_ID)

fun hasProblem(): Boolean = problem != null

fun addProblem(problem: Problem) {
this.problem = problem
}

fun getProblem(): Problem? {
return problem
}
fun getProblem(): Problem? = problem

fun getBreadcrumbs(): List<Breadcrumb> {
return breadcrumbs
}
fun getBreadcrumbs(): List<Breadcrumb> = breadcrumbs

fun getReadCount(): Int {
return getIntValue(READ_COUNT)
}
fun getReadCount(): Int = getIntValue(READ_COUNT)
}
13 changes: 13 additions & 0 deletions src/test/kotlin/no/nav/dagpenger/events/PacketTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.dagpenger.events

import com.squareup.moshi.JsonEncodingException
import io.kotlintest.shouldBe
import org.json.JSONObject

import org.junit.jupiter.api.Assertions.assertEquals
Expand Down Expand Up @@ -378,6 +379,18 @@ class PacketTest {
assertFalse(packetString.contains("problemValue"))
}

@Test
fun `Packet should have system-correlation-id`() {
val packet = Packet()
packet.hasField("system_correlation_id") shouldBe true
}

@Test
fun `system-correlation-id should be preserved on serialization and deserialization`() {
val packet = Packet()
Packet(packet.toJson()!!).getCorrelationId() shouldBe packet.getCorrelationId()
}

data class ClassA(
val id: String,
val list: List<ClassB>
Expand Down

0 comments on commit fc5d430

Please sign in to comment.