|
1 | 1 | package io.ably.lib.objects.unit |
2 | 2 |
|
3 | | -import io.ably.lib.objects.gson |
4 | | -import io.ably.lib.objects.msgpackMapper |
| 3 | +import com.google.gson.Gson |
| 4 | +import com.google.gson.GsonBuilder |
| 5 | +import com.google.gson.JsonElement |
| 6 | +import com.google.gson.JsonNull |
5 | 7 | import io.ably.lib.objects.unit.fixtures.* |
6 | 8 | import io.ably.lib.types.ProtocolMessage |
| 9 | +import io.ably.lib.types.ProtocolMessage.ActionSerializer |
7 | 10 | import io.ably.lib.types.ProtocolSerializer |
8 | 11 | import io.ably.lib.util.Serialisation |
9 | 12 | import kotlinx.coroutines.test.runTest |
10 | 13 | import org.junit.Assert.assertEquals |
11 | 14 | import org.junit.Test |
12 | 15 | import kotlin.test.assertNotNull |
| 16 | +import kotlin.test.assertNull |
| 17 | +import kotlin.test.assertTrue |
13 | 18 |
|
14 | 19 | class ObjectMessageSerializationTest { |
15 | 20 |
|
@@ -61,18 +66,91 @@ class ObjectMessageSerializationTest { |
61 | 66 | } |
62 | 67 |
|
63 | 68 | @Test |
64 | | - fun testOmitNullInSerialization() = runTest { |
65 | | - val nullableObject = object { |
66 | | - val name = "Test Object" |
67 | | - val description: String? = null // This will be omitted if using Gson with excludeNulls |
68 | | - val value = 42 |
| 69 | + fun testOmitNullsInObjectMessageSerialization() = runTest { |
| 70 | + val objectMessage = dummyObjectMessageWithStringData() |
| 71 | + val objectMessageWithNullFields = objectMessage.copy( |
| 72 | + id = null, |
| 73 | + timestamp = null, |
| 74 | + clientId = "test-client", |
| 75 | + connectionId = "test-connection", |
| 76 | + extras = null, |
| 77 | + operation = null, |
| 78 | + objectState = null, |
| 79 | + serial = null, |
| 80 | + siteCode = null |
| 81 | + ) |
| 82 | + val protocolMessage = ProtocolMessage() |
| 83 | + protocolMessage.action = ProtocolMessage.Action.`object` |
| 84 | + protocolMessage.state = arrayOf(objectMessageWithNullFields) |
| 85 | + |
| 86 | + // check if Gson/Msgpack serialization omits null fields |
| 87 | + fun assertSerializedObjectMessage(serializedProtoMsg: String) { |
| 88 | + val deserializedProtoMsg = Gson().fromJson(serializedProtoMsg, JsonElement::class.java).asJsonObject |
| 89 | + val serializedObjectMessage = deserializedProtoMsg.get("state").asJsonArray[0].asJsonObject.toString() |
| 90 | + assertEquals("""{"clientId":"test-client","connectionId":"test-connection"}""", serializedObjectMessage) |
69 | 91 | } |
70 | | - val serializedJsonString = gson.toJson(nullableObject) |
71 | | - // check serializedObject does not contain the null field |
72 | | - assertEquals("""{"name":"Test Object","value":42}""", serializedJsonString) |
73 | 92 |
|
74 | | - val serializedMsgpackBytes = msgpackMapper.writeValueAsBytes(nullableObject) |
75 | | - // check serializedObject does not contain the null field |
76 | | - assertEquals("""{"name":"Test Object","value":42}""", Serialisation.msgpackToGson(serializedMsgpackBytes).toString()) |
| 93 | + // Serialize using Gson |
| 94 | + val serializedProtoMsg = ProtocolSerializer.writeJSON(protocolMessage).toString(Charsets.UTF_8) |
| 95 | + assertSerializedObjectMessage(serializedProtoMsg) |
| 96 | + |
| 97 | + // Serialize using MsgPack |
| 98 | + val serializedMsgpackBytes = ProtocolSerializer.writeMsgpack(protocolMessage) |
| 99 | + val serializedJsonStringFromMsgpackBytes = Serialisation.msgpackToGson(serializedMsgpackBytes).toString() |
| 100 | + assertSerializedObjectMessage(serializedJsonStringFromMsgpackBytes) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun testHandleNullsInObjectMessageDeserialization() = runTest { |
| 105 | + val protocolMessage = ProtocolMessage() |
| 106 | + protocolMessage.id = "id" |
| 107 | + protocolMessage.action = ProtocolMessage.Action.`object` |
| 108 | + protocolMessage.state = null |
| 109 | + |
| 110 | + // Serialize using Gson with serializeNulls enabled |
| 111 | + val gsonBuilderCreatingNulls = GsonBuilder() |
| 112 | + .registerTypeAdapter(ProtocolMessage.Action::class.java, ActionSerializer()) |
| 113 | + .serializeNulls().create() |
| 114 | + |
| 115 | + var protoMsgJsonObject = gsonBuilderCreatingNulls.toJsonTree(protocolMessage).asJsonObject |
| 116 | + assertTrue(protoMsgJsonObject.has("state")) |
| 117 | + assertEquals(JsonNull.INSTANCE, protoMsgJsonObject.get("state")) |
| 118 | + |
| 119 | + var deserializedProtoMsg = ProtocolSerializer.fromJSON(protoMsgJsonObject.toString()) |
| 120 | + assertNull(deserializedProtoMsg.state) |
| 121 | + |
| 122 | + var serializedMsgpackBytes = Serialisation.gsonToMsgpack(protoMsgJsonObject) |
| 123 | + deserializedProtoMsg = ProtocolSerializer.readMsgpack(serializedMsgpackBytes) |
| 124 | + assertNull(deserializedProtoMsg.state) |
| 125 | + |
| 126 | + // Create ObjectMessage and serialize in a way that resulting string/bytes include null fields |
| 127 | + val objectMessage = dummyObjectMessageWithStringData() |
| 128 | + val objectMessageWithNullFields = objectMessage.copy( |
| 129 | + id = null, |
| 130 | + timestamp = null, |
| 131 | + clientId = "test-client", |
| 132 | + connectionId = "test-connection", |
| 133 | + extras = null, |
| 134 | + operation = objectMessage.operation?.copy( |
| 135 | + initialValue = null, // initialValue set to null |
| 136 | + mapOp = objectMessage.operation.mapOp?.copy( |
| 137 | + data = null // objectData set to null |
| 138 | + ) |
| 139 | + ), |
| 140 | + objectState = null, |
| 141 | + serial = null, |
| 142 | + siteCode = null |
| 143 | + ) |
| 144 | + protocolMessage.state = arrayOf(objectMessageWithNullFields) |
| 145 | + protoMsgJsonObject = gsonBuilderCreatingNulls.toJsonTree(protocolMessage).asJsonObject |
| 146 | + |
| 147 | + // Check if gson deserialization works correctly |
| 148 | + deserializedProtoMsg = ProtocolSerializer.fromJSON(protoMsgJsonObject.toString()) |
| 149 | + assertEquals(objectMessageWithNullFields, deserializedProtoMsg.state[0] as? io.ably.lib.objects.ObjectMessage) |
| 150 | + |
| 151 | + // Check if msgpack deserialization works correctly |
| 152 | + serializedMsgpackBytes = Serialisation.gsonToMsgpack(protoMsgJsonObject) |
| 153 | + deserializedProtoMsg = ProtocolSerializer.readMsgpack(serializedMsgpackBytes) |
| 154 | + assertEquals(objectMessageWithNullFields, deserializedProtoMsg.state[0] as? io.ably.lib.objects.ObjectMessage) |
77 | 155 | } |
78 | 156 | } |
0 commit comments