|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.messaging.converter |
| 18 | + |
| 19 | +import kotlinx.serialization.Serializable |
| 20 | +import org.assertj.core.api.Assertions |
| 21 | +import org.junit.jupiter.api.Test |
| 22 | +import org.springframework.core.MethodParameter |
| 23 | +import org.springframework.messaging.support.MessageBuilder |
| 24 | +import java.nio.charset.StandardCharsets |
| 25 | +import kotlin.reflect.typeOf |
| 26 | + |
| 27 | +@Suppress("UsePropertyAccessSyntax") |
| 28 | +class KotlinSerializationJsonMessageConverterTests { |
| 29 | + |
| 30 | + private val converter = KotlinSerializationJsonMessageConverter() |
| 31 | + |
| 32 | + @Test |
| 33 | + fun readObject() { |
| 34 | + val payload = """ |
| 35 | + { |
| 36 | + "bytes": [ |
| 37 | + 1, |
| 38 | + 2 |
| 39 | + ], |
| 40 | + "array": [ |
| 41 | + "Foo", |
| 42 | + "Bar" |
| 43 | + ], |
| 44 | + "number": 42, |
| 45 | + "string": "Foo", |
| 46 | + "bool": true, |
| 47 | + "fraction": 42 |
| 48 | + } |
| 49 | + """.trimIndent() |
| 50 | + val message = MessageBuilder.withPayload(payload.toByteArray(StandardCharsets.UTF_8)).build() |
| 51 | + val result = converter.fromMessage(message, SerializableBean::class.java) as SerializableBean |
| 52 | + |
| 53 | + Assertions.assertThat(result.bytes).containsExactly(0x1, 0x2) |
| 54 | + Assertions.assertThat(result.array).containsExactly("Foo", "Bar") |
| 55 | + Assertions.assertThat(result.number).isEqualTo(42) |
| 56 | + Assertions.assertThat(result.string).isEqualTo("Foo") |
| 57 | + Assertions.assertThat(result.bool).isTrue() |
| 58 | + Assertions.assertThat(result.fraction).isEqualTo(42.0f) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @Suppress("UNCHECKED_CAST") |
| 63 | + fun readArrayOfObjects() { |
| 64 | + val payload = """ |
| 65 | + [ |
| 66 | + { |
| 67 | + "bytes": [ |
| 68 | + 1, |
| 69 | + 2 |
| 70 | + ], |
| 71 | + "array": [ |
| 72 | + "Foo", |
| 73 | + "Bar" |
| 74 | + ], |
| 75 | + "number": 42, |
| 76 | + "string": "Foo", |
| 77 | + "bool": true, |
| 78 | + "fraction": 42 |
| 79 | + } |
| 80 | + ] |
| 81 | + """.trimIndent() |
| 82 | + val message = MessageBuilder.withPayload(payload.toByteArray(StandardCharsets.UTF_8)).build() |
| 83 | + val result = converter.fromMessage(message, Array<SerializableBean>::class.java) as Array<SerializableBean> |
| 84 | + |
| 85 | + Assertions.assertThat(result).hasSize(1) |
| 86 | + Assertions.assertThat(result[0].bytes).containsExactly(0x1, 0x2) |
| 87 | + Assertions.assertThat(result[0].array).containsExactly("Foo", "Bar") |
| 88 | + Assertions.assertThat(result[0].number).isEqualTo(42) |
| 89 | + Assertions.assertThat(result[0].string).isEqualTo("Foo") |
| 90 | + Assertions.assertThat(result[0].bool).isTrue() |
| 91 | + Assertions.assertThat(result[0].fraction).isEqualTo(42.0f) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @Suppress("UNCHECKED_CAST") |
| 96 | + @ExperimentalStdlibApi |
| 97 | + fun readGenericCollection() { |
| 98 | + val payload = """ |
| 99 | + [ |
| 100 | + { |
| 101 | + "bytes": [ |
| 102 | + 1, |
| 103 | + 2 |
| 104 | + ], |
| 105 | + "array": [ |
| 106 | + "Foo", |
| 107 | + "Bar" |
| 108 | + ], |
| 109 | + "number": 42, |
| 110 | + "string": "Foo", |
| 111 | + "bool": true, |
| 112 | + "fraction": 42 |
| 113 | + } |
| 114 | + ] |
| 115 | + """.trimIndent() |
| 116 | + val method = javaClass.getDeclaredMethod("handleList", List::class.java) |
| 117 | + val param = MethodParameter(method, 0) |
| 118 | + val message = MessageBuilder.withPayload(payload.toByteArray(StandardCharsets.UTF_8)).build() |
| 119 | + val result = converter.fromMessage(message, typeOf<List<SerializableBean>>()::class.java, param) as List<SerializableBean> |
| 120 | + |
| 121 | + Assertions.assertThat(result).hasSize(1) |
| 122 | + Assertions.assertThat(result[0].bytes).containsExactly(0x1, 0x2) |
| 123 | + Assertions.assertThat(result[0].array).containsExactly("Foo", "Bar") |
| 124 | + Assertions.assertThat(result[0].number).isEqualTo(42) |
| 125 | + Assertions.assertThat(result[0].string).isEqualTo("Foo") |
| 126 | + Assertions.assertThat(result[0].bool).isTrue() |
| 127 | + Assertions.assertThat(result[0].fraction).isEqualTo(42.0f) |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + fun readFailsOnInvalidJson() { |
| 132 | + val payload = """ |
| 133 | + this is an invalid JSON document |
| 134 | + """.trimIndent() |
| 135 | + |
| 136 | + val message = MessageBuilder.withPayload(payload.toByteArray(StandardCharsets.UTF_8)).build() |
| 137 | + Assertions.assertThatExceptionOfType(MessageConversionException::class.java).isThrownBy { |
| 138 | + converter.fromMessage(message, SerializableBean::class.java) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun writeObject() { |
| 144 | + val serializableBean = SerializableBean(byteArrayOf(0x1, 0x2), arrayOf("Foo", "Bar"), 42, "Foo", true, 42.0f) |
| 145 | + val message = converter.toMessage(serializableBean, null) |
| 146 | + val result = String((message!!.payload as ByteArray), StandardCharsets.UTF_8) |
| 147 | + |
| 148 | + Assertions.assertThat(result) |
| 149 | + .contains("\"bytes\":[1,2]") |
| 150 | + .contains("\"array\":[\"Foo\",\"Bar\"]") |
| 151 | + .contains("\"number\":42") |
| 152 | + .contains("\"string\":\"Foo\"") |
| 153 | + .contains("\"bool\":true") |
| 154 | + .contains("\"fraction\":42.0") |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + fun writeObjectWithNullableProperty() { |
| 159 | + val serializableBean = SerializableBean(byteArrayOf(0x1, 0x2), arrayOf("Foo", "Bar"), 42, null, true, 42.0f) |
| 160 | + val message = converter.toMessage(serializableBean, null) |
| 161 | + val result = String((message!!.payload as ByteArray), StandardCharsets.UTF_8) |
| 162 | + |
| 163 | + Assertions.assertThat(result) |
| 164 | + .contains("\"bytes\":[1,2]") |
| 165 | + .contains("\"array\":[\"Foo\",\"Bar\"]") |
| 166 | + .contains("\"number\":42") |
| 167 | + .contains("\"string\":null") |
| 168 | + .contains("\"bool\":true") |
| 169 | + .contains("\"fraction\":42.0") |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + fun writeArrayOfObjects() { |
| 174 | + val serializableBean = SerializableBean(byteArrayOf(0x1, 0x2), arrayOf("Foo", "Bar"), 42, "Foo", true, 42.0f) |
| 175 | + val expectedJson = """ |
| 176 | + [{"bytes":[1,2],"array":["Foo","Bar"],"number":42,"string":"Foo","bool":true,"fraction":42.0}] |
| 177 | + """.trimIndent() |
| 178 | + |
| 179 | + val message = converter.toMessage(arrayOf(serializableBean), null) |
| 180 | + val result = String((message!!.payload as ByteArray), StandardCharsets.UTF_8) |
| 181 | + |
| 182 | + Assertions.assertThat(result).isEqualTo(expectedJson) |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + @ExperimentalStdlibApi |
| 187 | + fun writeGenericCollection() { |
| 188 | + val serializableBean = SerializableBean(byteArrayOf(0x1, 0x2), arrayOf("Foo", "Bar"), 42, "Foo", true, 42.0f) |
| 189 | + val expectedJson = """ |
| 190 | + [{"bytes":[1,2],"array":["Foo","Bar"],"number":42,"string":"Foo","bool":true,"fraction":42.0}] |
| 191 | + """.trimIndent() |
| 192 | + |
| 193 | + val method = javaClass.getDeclaredMethod("handleList", List::class.java) |
| 194 | + val param = MethodParameter(method, 0) |
| 195 | + val message = converter.toMessage(arrayListOf(serializableBean), null, param) |
| 196 | + val result = String((message!!.payload as ByteArray), StandardCharsets.UTF_8) |
| 197 | + |
| 198 | + Assertions.assertThat(result).isEqualTo(expectedJson) |
| 199 | + } |
| 200 | + |
| 201 | + @Suppress("UNUSED_PARAMETER") |
| 202 | + fun handleList(payload: List<SerializableBean>) {} |
| 203 | + |
| 204 | + @Serializable |
| 205 | + @Suppress("ArrayInDataClass") |
| 206 | + data class SerializableBean( |
| 207 | + val bytes: ByteArray, |
| 208 | + val array: Array<String>, |
| 209 | + val number: Int, |
| 210 | + val string: String?, |
| 211 | + val bool: Boolean, |
| 212 | + val fraction: Float |
| 213 | + ) |
| 214 | +} |
0 commit comments