Skip to content

Commit fd9c3e9

Browse files
authored
2D point serialization fix added for version 5.0 (#626)
1 parent afeae43 commit fd9c3e9

File tree

2 files changed

+106
-9
lines changed

2 files changed

+106
-9
lines changed

common/src/main/kotlin/streams/utils/JSONUtils.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ import java.time.temporal.TemporalAccessor
3838
import kotlin.reflect.full.isSubclassOf
3939

4040
abstract class StreamsPoint { abstract val crs: String }
41-
data class StreamsPointCartesian(override val crs: String, val x: Double, val y: Double, val z: Double? = null): StreamsPoint()
42-
data class StreamsPointWgs(override val crs: String, val latitude: Double, val longitude: Double, val height: Double? = null): StreamsPoint()
41+
data class StreamsPointCartesian2D(override val crs: String, val x: Double, val y: Double): StreamsPoint()
42+
data class StreamsPointCartesian3D(override val crs: String, val x: Double, val y: Double, val z: Double? = null): StreamsPoint()
43+
data class StreamsPointWgs2D(override val crs: String, val latitude: Double, val longitude: Double): StreamsPoint()
44+
data class StreamsPointWgs3D(override val crs: String, val latitude: Double, val longitude: Double, val height: Double? = null): StreamsPoint()
4345

4446
fun PointValue.toStreamsPoint(): StreamsPoint {
4547
val point = this.asPoint()
@@ -49,10 +51,10 @@ fun PointValue.toStreamsPoint(): StreamsPoint {
4951
fun Point.toStreamsPoint(): StreamsPoint {
5052
val point = this
5153
return when (val crsType = point.srid()) {
52-
7203 -> StreamsPointCartesian("cartesian", point.x(), point.y())
53-
9157 -> StreamsPointCartesian("cartesian-3d", point.x(), point.y(), point.z())
54-
4326 -> StreamsPointWgs("wgs-84", point.x(), point.y())
55-
4979 -> StreamsPointWgs("wgs-84-3d", point.x(), point.y(), point.z())
54+
7203 -> StreamsPointCartesian2D("cartesian", point.x(), point.y())
55+
9157 -> StreamsPointCartesian3D("cartesian-3d", point.x(), point.y(), point.z())
56+
4326 -> StreamsPointWgs2D("wgs-84", point.x(), point.y())
57+
4979 -> StreamsPointWgs3D("wgs-84-3d", point.x(), point.y(), point.z())
5658
else -> throw IllegalArgumentException("Point type $crsType not supported")
5759
}
5860
}

common/src/test/kotlin/streams/utils/JSONUtilsTest.kt

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import streams.events.NodePayload
1313
import streams.events.OperationType
1414
import streams.events.Schema
1515
import streams.events.StreamsTransactionEvent
16-
import java.time.LocalTime
1716
import java.time.OffsetTime
1817
import java.time.ZoneOffset.UTC
1918
import java.time.ZonedDateTime
@@ -25,9 +24,9 @@ class JSONUtilsTest {
2524
@Test
2625
fun `should serialize driver Point Data Types`() {
2726
// Given
28-
val expected = "{\"point2dCartesian\":{\"crs\":\"cartesian\",\"x\":1.0,\"y\":2.0,\"z\":null}," +
27+
val expected = "{\"point2dCartesian\":{\"crs\":\"cartesian\",\"x\":1.0,\"y\":2.0}," +
2928
"\"point3dCartesian\":{\"crs\":\"cartesian-3d\",\"x\":1.0,\"y\":2.0,\"z\":3.0}," +
30-
"\"point2dWgs84\":{\"crs\":\"wgs-84\",\"latitude\":1.0,\"longitude\":2.0,\"height\":null}," +
29+
"\"point2dWgs84\":{\"crs\":\"wgs-84\",\"latitude\":1.0,\"longitude\":2.0}," +
3130
"\"point3dWgs84\":{\"crs\":\"wgs-84-3d\",\"latitude\":1.0,\"longitude\":2.0,\"height\":3.0}," +
3231
"\"time\":\"14:01:01.000000001Z\",\"dateTime\":\"2017-12-17T17:14:35.123456789Z\"}"
3332

@@ -89,6 +88,102 @@ class JSONUtilsTest {
8988
assertEquals(cdcData, fromString)
9089
}
9190

91+
@Test
92+
fun `should convert cdcMap wgs2D with height null to PointValue`() {
93+
// given
94+
val timestamp = System.currentTimeMillis()
95+
val expectedPointValue = Values.point(4326, 12.78, 56.7)
96+
97+
//when
98+
val cdcMap = mapOf<String, Any>(
99+
"meta" to mapOf("timestamp" to timestamp,
100+
"username" to "user",
101+
"txId" to 1,
102+
"txEventId" to 0,
103+
"txEventsCount" to 1,
104+
"operation" to OperationType.created),
105+
"payload" to mapOf("id" to "0",
106+
"before" to null,
107+
"after" to NodeChange(properties = mapOf("location" to mapOf("crs" to "wgs-84", "longitude" to 12.78, "latitude" to 56.7, "height" to null)),
108+
labels = listOf("LabelCDC")),
109+
"type" to EntityType.node),
110+
"schema" to mapOf("properties" to mapOf("location" to "PointValue"))
111+
)
112+
113+
//then
114+
val actualEvent = JSONUtils.asStreamsTransactionEvent(cdcMap)
115+
val actualPointValue = actualEvent.payload.after?.properties?.get("location")
116+
assertEquals(expectedPointValue, actualPointValue)
117+
}
118+
119+
@Test
120+
fun `should convert cdcString wgs2D with height null to PointValue`() {
121+
// given
122+
val timestamp = System.currentTimeMillis()
123+
val expectedPointValue = Values.point(4326, 12.78, 56.7)
124+
125+
//when
126+
val cdcString = """{
127+
|"meta":{"timestamp":$timestamp,"username":"user","txId":1,"txEventId":0,"txEventsCount":1,"operation":"created"},
128+
|"payload":{"id":"0","before":null,"after":{"properties":{"location":{"crs":"wgs-84","longitude":12.78,"latitude":56.7,"height":null}},
129+
|"labels":["LabelCDC"]},"type":"node"},
130+
|"schema":{"properties":{"location":"PointValue"}}
131+
|}""".trimMargin()
132+
133+
//then
134+
val actualEvent = JSONUtils.asStreamsTransactionEvent(cdcString)
135+
val actualPointValue = actualEvent.payload.after?.properties?.get("location")
136+
assertEquals(expectedPointValue, actualPointValue)
137+
}
138+
139+
@Test
140+
fun `should convert cdcString cartesian2D with z null to PointValue`() {
141+
// given
142+
val timestamp = System.currentTimeMillis()
143+
val expectedPointValue = Values.point(7203, 12.78, 56.7)
144+
145+
//when
146+
val cdcString = """{
147+
|"meta":{"timestamp":$timestamp,"username":"user","txId":1,"txEventId":0,"txEventsCount":1,"operation":"created"},
148+
|"payload":{"id":"0","before":null,"after":{"properties":{"location":{"crs":"cartesian","x":12.78,"y":56.7,"z":null}},
149+
|"labels":["LabelCDC"]},"type":"node"},
150+
|"schema":{"properties":{"location":"PointValue"}}
151+
|}""".trimMargin()
152+
153+
//then
154+
val actualEvent = JSONUtils.asStreamsTransactionEvent(cdcString)
155+
val actualPointValue = actualEvent.payload.after?.properties?.get("location")
156+
assertEquals(expectedPointValue, actualPointValue)
157+
}
158+
159+
@Test
160+
fun `should convert cdcMap cartesian2D with z null to PointValue`() {
161+
// given
162+
val timestamp = System.currentTimeMillis()
163+
val expectedPointValue = Values.point(7203, 12.78, 56.7)
164+
165+
//when
166+
val cdcMap = mapOf<String, Any>(
167+
"meta" to mapOf("timestamp" to timestamp,
168+
"username" to "user",
169+
"txId" to 1,
170+
"txEventId" to 0,
171+
"txEventsCount" to 1,
172+
"operation" to OperationType.created),
173+
"payload" to mapOf("id" to "0",
174+
"before" to null,
175+
"after" to NodeChange(properties = mapOf("location" to mapOf("crs" to "cartesian", "x" to 12.78, "y" to 56.7, "z" to null)),
176+
labels = listOf("LabelCDC")),
177+
"type" to EntityType.node),
178+
"schema" to mapOf("properties" to mapOf("location" to "PointValue"))
179+
)
180+
181+
//then
182+
val actualEvent = JSONUtils.asStreamsTransactionEvent(cdcMap)
183+
val actualPointValue = actualEvent.payload.after?.properties?.get("location")
184+
assertEquals(expectedPointValue, actualPointValue)
185+
}
186+
92187
@Test
93188
fun `should deserialize plain values`() {
94189
assertEquals("a", JSONUtils.readValue("a", stringWhenFailure = true))

0 commit comments

Comments
 (0)