1
1
package ch.srgssr.pillarbox.monitoring.event.model
2
2
3
+ import com.fasterxml.jackson.databind.JsonMappingException
3
4
import com.fasterxml.jackson.databind.ObjectMapper
4
5
import com.fasterxml.jackson.module.kotlin.readValue
6
+ import io.kotest.assertions.throwables.shouldThrow
5
7
import io.kotest.core.spec.style.ShouldSpec
6
8
import io.kotest.matchers.shouldBe
7
9
import org.springframework.boot.test.context.SpringBootTest
@@ -10,6 +12,68 @@ import org.springframework.boot.test.context.SpringBootTest
10
12
class EventRequestTest (
11
13
private val objectMapper : ObjectMapper ,
12
14
) : ShouldSpec({
15
+ should("deserialize successfully if all required fields are present") {
16
+ // Given: an event as json
17
+ val jsonInput =
18
+ """
19
+ {
20
+ "session_id": "12345",
21
+ "event_name": "START",
22
+ "timestamp": 1630000000000,
23
+ "user_ip": "127.0.0.1",
24
+ "version": 1,
25
+ "data": { }
26
+ }
27
+ }
28
+ """ .trimIndent()
29
+
30
+ // When: the event is deserialized
31
+ val eventRequest = objectMapper.readValue<EventRequest >(jsonInput)
32
+
33
+ // Then: The data of the event should be correctly parsed.
34
+ eventRequest.sessionId shouldBe " 12345"
35
+ eventRequest.eventName shouldBe " START"
36
+ eventRequest.timestamp shouldBe 1630000000000
37
+ eventRequest.ip shouldBe " 127.0.0.1"
38
+ eventRequest.version shouldBe 1
39
+ }
40
+
41
+ context("fail to deserialize if missing any required field") {
42
+ val baseJson =
43
+ mapOf(
44
+ "session_id" to "\"12345\"",
45
+ "event_name" to "\"START \"",
46
+ "timestamp" to "1630000000000",
47
+ "version" to "1",
48
+ "data" to "{}",
49
+ )
50
+
51
+ baseJson.keys.forEach { missingField ->
52
+ should("fail if $missingField is missing") {
53
+ val jsonInput =
54
+ baseJson
55
+ .filterKeys { it != missingField } // Exclude the current field
56
+ .map { (key, value) -> " \" $key \" : $value " }
57
+ .joinToString(prefix = "{", postfix = "}")
58
+
59
+ shouldThrow<JsonMappingException > {
60
+ objectMapper.readValue<EventRequest >(jsonInput)
61
+ }
62
+ }
63
+ should("fail if $missingField is null") {
64
+ val jsonInput =
65
+ baseJson
66
+ .map { (key, value) ->
67
+ " \" $key \" : ${if (key == missingField) " null" else value} "
68
+ }.joinToString(prefix = "{", postfix = "}")
69
+
70
+ shouldThrow<JsonMappingException > {
71
+ objectMapper.readValue<EventRequest >(jsonInput)
72
+ }
73
+ }
74
+ }
75
+ }
76
+
13
77
should("deserialize an event and resolve user agent") {
14
78
// Given: an input with a user agent
15
79
val jsonInput =
@@ -18,6 +82,7 @@ class EventRequestTest(
18
82
"session_id": "12345",
19
83
"event_name": "START",
20
84
"timestamp": 1630000000000,
85
+ "user_ip": "127.0.0.1",
21
86
"version": 1,
22
87
"data": {
23
88
"browser": {
@@ -55,6 +120,7 @@ class EventRequestTest(
55
120
"session_id": "12345",
56
121
"event_name": "START",
57
122
"timestamp": 1630000000000,
123
+ "user_ip": "127.0.0.1",
58
124
"version": 1,
59
125
"data": {
60
126
"browser": {
@@ -92,6 +158,7 @@ class EventRequestTest(
92
158
"session_id": "12345",
93
159
"event_name": "START",
94
160
"timestamp": 1630000000000,
161
+ "user_ip": "127.0.0.1",
95
162
"version": 1,
96
163
"data": {
97
164
"browser": {
0 commit comments