11package io.homeassistant.companion.android.common.push
22
3- import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
4- import com.fasterxml.jackson.module.kotlin.readValue
3+ import kotlinx.serialization.json.Json
4+ import kotlinx.serialization.json.JsonArray
5+ import kotlinx.serialization.json.JsonObject
6+ import kotlinx.serialization.json.JsonPrimitive
7+ import kotlinx.serialization.json.boolean
8+ import kotlinx.serialization.json.contentOrNull
9+ import kotlinx.serialization.json.jsonArray
10+ import kotlinx.serialization.json.jsonObject
11+ import kotlinx.serialization.json.jsonPrimitive
512import org.junit.Assert.assertEquals
613import org.junit.Assert.assertNotNull
714import org.junit.Assert.assertTrue
@@ -16,25 +23,26 @@ import org.junit.Test
1623 */
1724class UnifiedPushMessageParsingTest {
1825
19- private val objectMapper = jacksonObjectMapper()
26+ private fun parseJson ( json : String ): JsonObject = Json .parseToJsonElement(json).jsonObject
2027
2128 @Test
2229 fun `parse simple notification message` () {
23- val json = """
30+ val data = parseJson(
31+ """
2432 {
2533 "message": "Test notification",
2634 "title": "Test Title"
2735 }
28- """ .trimIndent()
29-
30- val data: Map <String , Any > = objectMapper.readValue(json)
31- assertEquals(" Test notification" , data[" message" ])
32- assertEquals(" Test Title" , data[" title" ])
36+ """ .trimIndent()
37+ )
38+ assertEquals(" Test notification" , data[" message" ]?.jsonPrimitive?.contentOrNull)
39+ assertEquals(" Test Title" , data[" title" ]?.jsonPrimitive?.contentOrNull)
3340 }
3441
3542 @Test
3643 fun `parse notification with nested data` () {
37- val json = """
44+ val data = parseJson(
45+ """
3846 {
3947 "message": "Hello",
4048 "title": "Greetings",
@@ -44,20 +52,20 @@ class UnifiedPushMessageParsingTest {
4452 "ttl": 0
4553 }
4654 }
47- """ .trimIndent()
48-
49- val data: Map <String , Any > = objectMapper.readValue(json)
50- assertEquals(" Hello" , data[" message" ])
51-
52- @Suppress(" UNCHECKED_CAST" )
53- val nestedData = data[" data" ] as Map <String , Any >
54- assertEquals(" alerts" , nestedData[" channel" ])
55- assertEquals(" high" , nestedData[" importance" ])
55+ """ .trimIndent()
56+ )
57+ assertEquals(" Hello" , data[" message" ]?.jsonPrimitive?.contentOrNull)
58+
59+ val nestedData = data[" data" ]?.jsonObject
60+ assertNotNull(nestedData)
61+ assertEquals(" alerts" , nestedData!! [" channel" ]?.jsonPrimitive?.contentOrNull)
62+ assertEquals(" high" , nestedData[" importance" ]?.jsonPrimitive?.contentOrNull)
5663 }
5764
5865 @Test
5966 fun `parse notification with actions` () {
60- val json = """
67+ val data = parseJson(
68+ """
6169 {
6270 "message": "Motion detected",
6371 "title": "Security",
@@ -74,41 +82,41 @@ class UnifiedPushMessageParsingTest {
7482 ]
7583 }
7684 }
77- """ .trimIndent()
78-
79- val data: Map <String , Any > = objectMapper.readValue(json)
80-
81- @Suppress(" UNCHECKED_CAST" )
82- val nestedData = data[" data" ] as Map <String , Any >
83- @Suppress(" UNCHECKED_CAST" )
84- val actions = nestedData[" actions" ] as List <Map <String , Any >>
85- assertEquals(2 , actions.size)
86- assertEquals(" OPEN" , actions[0 ][" action" ])
87- assertEquals(" Open Camera" , actions[0 ][" title" ])
88- assertEquals(" DISMISS" , actions[1 ][" action" ])
85+ """ .trimIndent()
86+ )
87+
88+ val nestedData = data[" data" ]?.jsonObject
89+ assertNotNull(nestedData)
90+ val actions = nestedData!! [" actions" ]?.jsonArray
91+ assertNotNull(actions)
92+ assertEquals(2 , actions!! .size)
93+ assertEquals(" OPEN" , actions[0 ].jsonObject[" action" ]?.jsonPrimitive?.contentOrNull)
94+ assertEquals(" Open Camera" , actions[0 ].jsonObject[" title" ]?.jsonPrimitive?.contentOrNull)
95+ assertEquals(" DISMISS" , actions[1 ].jsonObject[" action" ]?.jsonPrimitive?.contentOrNull)
8996 }
9097
9198 @Test
9299 fun `parse notification with registration_info` () {
93- val json = """
100+ val data = parseJson(
101+ """
94102 {
95103 "message": "Test",
96104 "registration_info": {
97105 "webhook_id": "abc123def456"
98106 }
99107 }
100- """ .trimIndent()
101-
102- val data: Map <String , Any > = objectMapper.readValue(json)
108+ """ .trimIndent()
109+ )
103110
104- @Suppress( " UNCHECKED_CAST " )
105- val regInfo = data[ " registration_info " ] as Map < String , Any >
106- assertEquals(" abc123def456" , regInfo[" webhook_id" ])
111+ val regInfo = data[ " registration_info " ]?.jsonObject
112+ assertNotNull( regInfo)
113+ assertEquals(" abc123def456" , regInfo!! [" webhook_id" ]?.jsonPrimitive?.contentOrNull )
107114 }
108115
109116 @Test
110117 fun `parse notification with image and url` () {
111- val json = """
118+ val data = parseJson(
119+ """
112120 {
113121 "message": "Doorbell pressed",
114122 "title": "Front Door",
@@ -117,26 +125,25 @@ class UnifiedPushMessageParsingTest {
117125 "clickAction": "https://example.com/dashboard"
118126 }
119127 }
120- """ .trimIndent()
128+ """ .trimIndent()
129+ )
130+ assertEquals(" Doorbell pressed" , data[" message" ]?.jsonPrimitive?.contentOrNull)
121131
122- val data: Map <String , Any > = objectMapper.readValue(json)
123- assertEquals(" Doorbell pressed" , data[" message" ])
124-
125- @Suppress(" UNCHECKED_CAST" )
126- val nestedData = data[" data" ] as Map <String , Any >
127- assertEquals(" https://example.com/camera/snapshot.jpg" , nestedData[" image" ])
132+ val nestedData = data[" data" ]?.jsonObject
133+ assertNotNull(nestedData)
134+ assertEquals(" https://example.com/camera/snapshot.jpg" , nestedData!! [" image" ]?.jsonPrimitive?.contentOrNull)
128135 }
129136
130137 @Test
131138 fun `parse empty data map` () {
132- val json = """ {}"""
133- val data: Map <String , Any > = objectMapper.readValue(json)
139+ val data = parseJson(""" {}""" )
134140 assertTrue(data.isEmpty())
135141 }
136142
137143 @Test
138144 fun `parse notification with numeric values` () {
139- val json = """
145+ val data = parseJson(
146+ """
140147 {
141148 "message": "Temperature alert",
142149 "data": {
@@ -145,38 +152,37 @@ class UnifiedPushMessageParsingTest {
145152 "vibrationPattern": "100, 200, 100"
146153 }
147154 }
148- """ .trimIndent()
149-
150- val data: Map <String , Any > = objectMapper.readValue(json)
155+ """ .trimIndent()
156+ )
151157 assertNotNull(data[" data" ])
152158 }
153159
154160 @Test
155161 fun `parse notification with confirm id for websocket ack` () {
156- val json = """
162+ val data = parseJson(
163+ """
157164 {
158165 "message": "Test",
159166 "hass_confirm_id": "confirm_12345"
160167 }
161- """ .trimIndent()
162-
163- val data: Map <String , Any > = objectMapper.readValue(json)
164- assertEquals(" confirm_12345" , data[" hass_confirm_id" ])
168+ """ .trimIndent()
169+ )
170+ assertEquals(" confirm_12345" , data[" hass_confirm_id" ]?.jsonPrimitive?.contentOrNull)
165171 }
166172
167173 @Test
168174 fun `parse notification preserves unknown fields` () {
169- val json = """
175+ val data = parseJson(
176+ """
170177 {
171178 "message": "Test",
172179 "custom_field": "custom_value",
173180 "another_field": true
174181 }
175- """ .trimIndent()
176-
177- val data: Map <String , Any > = objectMapper.readValue(json)
182+ """ .trimIndent()
183+ )
178184 assertEquals(3 , data.size)
179- assertEquals(" custom_value" , data[" custom_field" ])
180- assertEquals(true , data[" another_field" ])
185+ assertEquals(" custom_value" , data[" custom_field" ]?.jsonPrimitive?.contentOrNull )
186+ assertEquals(true , data[" another_field" ]?.jsonPrimitive?.boolean )
181187 }
182188}
0 commit comments