|
50 | 50 | import javax.jms.TextMessage; |
51 | 51 |
|
52 | 52 | import org.apache.kafka.connect.data.Schema; |
| 53 | +import org.apache.kafka.connect.data.Struct; |
53 | 54 | import org.apache.kafka.connect.errors.ConnectException; |
54 | 55 | import org.apache.kafka.connect.header.Headers; |
55 | 56 | import org.apache.kafka.connect.runtime.ConnectorConfig; |
@@ -167,6 +168,189 @@ public void verifyJmsJsonMessages() throws Exception { |
167 | 168 | } |
168 | 169 | } |
169 | 170 |
|
| 171 | + // verify that user can use the standard approach for the JsonConverter |
| 172 | + // of embedding schemas in message payloads (enabling this using a |
| 173 | + // record builder config option) |
| 174 | + @Test |
| 175 | + public void verifyJmsSchemaMessages() throws Exception { |
| 176 | + connectTask = getSourceTaskWithEmptyKafkaOffset(); |
| 177 | + |
| 178 | + final Map<String, String> connectorConfigProps = createDefaultConnectorProperties(); |
| 179 | + connectorConfigProps.put("mq.message.body.jms", "true"); |
| 180 | + connectorConfigProps.put("mq.record.builder", "com.ibm.eventstreams.connect.mqsource.builders.JsonRecordBuilder"); |
| 181 | + connectorConfigProps.put("mq.record.builder.json.schemas.enable", "true"); |
| 182 | + |
| 183 | + connectTask.start(connectorConfigProps); |
| 184 | + |
| 185 | + final List<Message> messages = new ArrayList<>(); |
| 186 | + for (int i = 0; i < 5; i++) { |
| 187 | + messages.add(getJmsContext().createTextMessage( |
| 188 | + "{\n" + |
| 189 | + "\"schema\": {\n" + |
| 190 | + " \"type\": \"struct\", \n" + |
| 191 | + " \"fields\": [\n" + |
| 192 | + " {\n" + |
| 193 | + " \"field\": \"idx\", \n" + |
| 194 | + " \"type\": \"int64\"\n" + |
| 195 | + " },\n" + |
| 196 | + " {\n" + |
| 197 | + " \"field\": \"test\", \n" + |
| 198 | + " \"type\": \"string\"\n" + |
| 199 | + " }" + |
| 200 | + " ]\n" + |
| 201 | + "}, " + |
| 202 | + "\"payload\": { " + |
| 203 | + " \"idx\": " + i + ", " + |
| 204 | + " \"test\" : \"abcdef\" " + |
| 205 | + "}" + |
| 206 | + "}")); |
| 207 | + } |
| 208 | + putAllMessagesToQueue(DEFAULT_SOURCE_QUEUE, messages); |
| 209 | + |
| 210 | + final List<SourceRecord> kafkaMessages = connectTask.poll(); |
| 211 | + assertEquals(5, kafkaMessages.size()); |
| 212 | + |
| 213 | + for (int i = 0; i < 5; i++) { |
| 214 | + final SourceRecord kafkaMessage = kafkaMessages.get(i); |
| 215 | + assertNull(kafkaMessage.key()); |
| 216 | + |
| 217 | + assertNotNull(kafkaMessage.valueSchema()); |
| 218 | + assertEquals(Schema.INT64_SCHEMA, kafkaMessage.valueSchema().field("idx").schema()); |
| 219 | + assertEquals(Schema.STRING_SCHEMA, kafkaMessage.valueSchema().field("test").schema()); |
| 220 | + |
| 221 | + final Struct value = (Struct) kafkaMessage.value(); |
| 222 | + assertEquals(Long.valueOf(i), value.getInt64("idx")); |
| 223 | + assertEquals("abcdef", value.getString("test")); |
| 224 | + |
| 225 | + connectTask.commitRecord(kafkaMessage, null); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + // verify that a reusable schema can be provided to the JSON record builder |
| 230 | + // as part of the connector config, so that this can be reused across |
| 231 | + // multiple MQ messages |
| 232 | + @Test |
| 233 | + public void verifyJmsReusableSchemaMessages() throws Exception { |
| 234 | + connectTask = getSourceTaskWithEmptyKafkaOffset(); |
| 235 | + |
| 236 | + final String SCHEMA = "{\n" + |
| 237 | + " \"type\": \"struct\", \n" + |
| 238 | + " \"fields\": [\n" + |
| 239 | + " {\n" + |
| 240 | + " \"field\": \"idx\", \n" + |
| 241 | + " \"type\": \"int32\"\n" + |
| 242 | + " },\n" + |
| 243 | + " {\n" + |
| 244 | + " \"field\": \"a\", \n" + |
| 245 | + " \"type\": \"string\"\n" + |
| 246 | + " },\n" + |
| 247 | + " {\n" + |
| 248 | + " \"field\": \"b\", \n" + |
| 249 | + " \"type\": \"int64\"\n" + |
| 250 | + " },\n" + |
| 251 | + " {\n" + |
| 252 | + " \"field\": \"c\", \n" + |
| 253 | + " \"type\": \"double\"\n" + |
| 254 | + " },\n" + |
| 255 | + " {\n" + |
| 256 | + " \"field\": \"d\", \n" + |
| 257 | + " \"type\": \"boolean\"\n" + |
| 258 | + " },\n" + |
| 259 | + " {\n" + |
| 260 | + " \"field\": \"e\", \n" + |
| 261 | + " \"type\": \"float\"\n" + |
| 262 | + " },\n" + |
| 263 | + " {\n" + |
| 264 | + " \"field\": \"f\", \n" + |
| 265 | + " \"type\": \"array\",\n" + |
| 266 | + " \"items\": {\n" + |
| 267 | + " \"type\": \"string\"\n" + |
| 268 | + " }\n" + |
| 269 | + " },\n" + |
| 270 | + " {\n" + |
| 271 | + " \"field\": \"g\", \n" + |
| 272 | + " \"type\": \"array\", \n" + |
| 273 | + " \"items\": {\n" + |
| 274 | + " \"type\": \"int32\"\n" + |
| 275 | + " }\n" + |
| 276 | + " },\n" + |
| 277 | + " {\n" + |
| 278 | + " \"field\": \"h\", \n" + |
| 279 | + " \"type\": \"struct\", \n" + |
| 280 | + " \"fields\": [\n" + |
| 281 | + " {\n" + |
| 282 | + " \"field\": \"innerstr\", \n" + |
| 283 | + " \"type\": \"string\"\n" + |
| 284 | + " },\n" + |
| 285 | + " {\n" + |
| 286 | + " \"field\": \"innernum\", \n" + |
| 287 | + " \"type\": \"int64\"\n" + |
| 288 | + " }\n" + |
| 289 | + " ]\n" + |
| 290 | + " }\n" + |
| 291 | + " ]\n" + |
| 292 | + "}"; |
| 293 | + |
| 294 | + final Map<String, String> connectorConfigProps = createDefaultConnectorProperties(); |
| 295 | + connectorConfigProps.put("mq.message.body.jms", "true"); |
| 296 | + connectorConfigProps.put("mq.record.builder", "com.ibm.eventstreams.connect.mqsource.builders.JsonRecordBuilder"); |
| 297 | + connectorConfigProps.put("mq.record.builder.json.schemas.enable", "true"); |
| 298 | + connectorConfigProps.put("mq.record.builder.json.schema.content", SCHEMA); |
| 299 | + |
| 300 | + connectTask.start(connectorConfigProps); |
| 301 | + |
| 302 | + final List<Message> messages = new ArrayList<>(); |
| 303 | + for (int i = 0; i < 5; i++) { |
| 304 | + messages.add(getJmsContext().createTextMessage( |
| 305 | + "{ " + |
| 306 | + "\"idx\": " + i + ", \n" + |
| 307 | + "\"a\" : \"test\", \n" + |
| 308 | + "\"b\" : 1234, \n" + |
| 309 | + "\"c\" : 5.67, \n" + |
| 310 | + "\"d\" : false, \n" + |
| 311 | + "\"e\" : 12.34, \n" + |
| 312 | + "\"f\" : [ \"a\", \"b\", \"c\" ], \n" + |
| 313 | + "\"g\" : [ 1, 2, 3 ], \n" + |
| 314 | + "\"h\" : { \"innerstr\" : \"testing\", \"innernum\" : 89 }" + |
| 315 | + "}")); |
| 316 | + } |
| 317 | + putAllMessagesToQueue(DEFAULT_SOURCE_QUEUE, messages); |
| 318 | + |
| 319 | + final List<SourceRecord> kafkaMessages = connectTask.poll(); |
| 320 | + assertEquals(5, kafkaMessages.size()); |
| 321 | + |
| 322 | + for (int i = 0; i < 5; i++) { |
| 323 | + final SourceRecord kafkaMessage = kafkaMessages.get(i); |
| 324 | + assertNull(kafkaMessage.key()); |
| 325 | + |
| 326 | + assertNotNull(kafkaMessage.valueSchema()); |
| 327 | + assertEquals(Schema.INT32_SCHEMA, kafkaMessage.valueSchema().field("idx").schema()); |
| 328 | + assertEquals(Schema.STRING_SCHEMA, kafkaMessage.valueSchema().field("a").schema()); |
| 329 | + assertEquals(Schema.INT64_SCHEMA, kafkaMessage.valueSchema().field("b").schema()); |
| 330 | + assertEquals(Schema.FLOAT64_SCHEMA, kafkaMessage.valueSchema().field("c").schema()); |
| 331 | + assertEquals(Schema.BOOLEAN_SCHEMA, kafkaMessage.valueSchema().field("d").schema()); |
| 332 | + assertEquals(Schema.FLOAT32_SCHEMA, kafkaMessage.valueSchema().field("e").schema()); |
| 333 | + assertEquals(Schema.STRING_SCHEMA, kafkaMessage.valueSchema().field("f").schema().valueSchema()); |
| 334 | + assertEquals(Schema.INT32_SCHEMA, kafkaMessage.valueSchema().field("g").schema().valueSchema()); |
| 335 | + assertEquals(Schema.STRING_SCHEMA, kafkaMessage.valueSchema().field("h").schema().field("innerstr").schema()); |
| 336 | + assertEquals(Schema.INT64_SCHEMA, kafkaMessage.valueSchema().field("h").schema().field("innernum").schema()); |
| 337 | + |
| 338 | + final Struct value = (Struct) kafkaMessage.value(); |
| 339 | + assertEquals(Integer.valueOf(i), value.getInt32("idx")); |
| 340 | + assertEquals("test", value.getString("a")); |
| 341 | + assertEquals(Long.valueOf(1234), value.getInt64("b")); |
| 342 | + assertEquals(Double.valueOf(5.67), value.getFloat64("c")); |
| 343 | + assertEquals(false, value.getBoolean("d")); |
| 344 | + assertEquals(Float.valueOf(12.34f), value.getFloat32("e")); |
| 345 | + assertArrayEquals(new String[]{ "a", "b", "c"}, value.getArray("f").toArray(new String[]{})); |
| 346 | + assertArrayEquals(new Integer[] { 1, 2, 3 }, value.getArray("g").toArray(new Integer[]{})); |
| 347 | + assertEquals("testing", value.getStruct("h").getString("innerstr")); |
| 348 | + assertEquals(Long.valueOf(89), value.getStruct("h").getInt64("innernum")); |
| 349 | + |
| 350 | + connectTask.commitRecord(kafkaMessage, null); |
| 351 | + } |
| 352 | + } |
| 353 | + |
170 | 354 | @Test |
171 | 355 | public void verifyMQMessage() throws Exception { |
172 | 356 | connectTask = getSourceTaskWithEmptyKafkaOffset(); |
|
0 commit comments