|
18 | 18 |
|
19 | 19 | import com.google.common.collect.ImmutableList;
|
20 | 20 | import com.google.common.collect.ImmutableMap;
|
| 21 | +import com.google.protobuf.Descriptors; |
21 | 22 | import com.google.protobuf.Descriptors.Descriptor;
|
22 | 23 | import com.google.protobuf.ListValue;
|
23 | 24 | import com.google.protobuf.Timestamp;
|
|
28 | 29 | import io.confluent.kafka.serializers.protobuf.test.KeyTimestampValueOuterClass.KeyTimestampValue;
|
29 | 30 | import io.confluent.kafka.serializers.protobuf.test.TestMessageProtos.TestMessage2;
|
30 | 31 | import io.confluent.kafka.serializers.protobuf.test.TimestampValueOuterClass.TimestampValue;
|
31 |
| -import java.util.List; |
32 |
| -import org.apache.kafka.connect.data.Schema; |
33 |
| -import org.apache.kafka.connect.data.SchemaAndValue; |
34 |
| -import org.apache.kafka.connect.data.SchemaBuilder; |
35 |
| -import org.apache.kafka.connect.data.Struct; |
| 32 | + |
| 33 | +import java.util.*; |
| 34 | + |
| 35 | +import org.apache.kafka.connect.data.*; |
36 | 36 | import org.junit.Before;
|
37 | 37 | import org.junit.Ignore;
|
38 | 38 | import org.junit.Test;
|
39 | 39 |
|
40 | 40 | import java.io.IOException;
|
41 | 41 | import java.nio.ByteBuffer;
|
42 |
| -import java.util.Arrays; |
43 |
| -import java.util.Collections; |
44 |
| -import java.util.HashMap; |
45 |
| -import java.util.Map; |
46 | 42 |
|
47 | 43 | import io.confluent.connect.protobuf.test.Key;
|
48 | 44 | import io.confluent.connect.protobuf.test.KeyValue;
|
@@ -281,6 +277,23 @@ public void testFromConnectDataForValue() {
|
281 | 277 | assertArrayEquals(expected, Arrays.copyOfRange(result, PROTOBUF_BYTES_START, result.length));
|
282 | 278 | }
|
283 | 279 |
|
| 280 | + @Test |
| 281 | + public void testFromConnectDataForValueUseJsonFieldNames() { |
| 282 | + final byte[] expected = HELLO_WORLD_MESSAGE.toByteArray(); |
| 283 | + |
| 284 | + Map<String, Object> configs = new HashMap<>(SR_CONFIG); |
| 285 | + configs.put(ProtobufDataConfig.JSON_FIELD_NAMES_CONFIG, true); |
| 286 | + converter.configure(configs, false); |
| 287 | + |
| 288 | + SchemaAndValue schemaAndValue = getExpectedTestMessageWithJsonFieldNames(); |
| 289 | + |
| 290 | + byte[] result = converter.fromConnectData("my-topic", |
| 291 | + schemaAndValue.schema(), schemaAndValue.value() |
| 292 | + ); |
| 293 | + |
| 294 | + assertArrayEquals(expected, Arrays.copyOfRange(result, PROTOBUF_BYTES_START, result.length)); |
| 295 | + } |
| 296 | + |
284 | 297 | @Test
|
285 | 298 | public void testFromConnectDataForValueWithNamespace() {
|
286 | 299 | final byte[] expected = HELLO_WORLD_MESSAGE.toByteArray();
|
@@ -531,6 +544,44 @@ public void testToConnectDataForValue() throws Exception {
|
531 | 544 | assertEquals(expected, result);
|
532 | 545 | }
|
533 | 546 |
|
| 547 | + @Test |
| 548 | + public void testToConnectDataForValueUseJsonFieldNames() throws Exception { |
| 549 | + Map<String, Object> configs = new HashMap<>(SR_CONFIG); |
| 550 | + configs.put(ProtobufDataConfig.JSON_FIELD_NAMES_CONFIG, true); |
| 551 | + converter.configure(configs, false); |
| 552 | + // extra byte for message index |
| 553 | + final byte[] input = concat(new byte[]{0, 0, 0, 0, 1, 0}, HELLO_WORLD_MESSAGE.toByteArray()); |
| 554 | + schemaRegistry.register("my-topic-value", getSchema(TestMessage.getDescriptor())); |
| 555 | + SchemaAndValue result = converter.toConnectData("my-topic", input); |
| 556 | + |
| 557 | + SchemaAndValue expected = getExpectedTestMessageWithJsonFieldNames(); |
| 558 | + |
| 559 | + assertEquals(expected.schema(), result.schema()); |
| 560 | + assertEquals(expected, result); |
| 561 | + } |
| 562 | + |
| 563 | + private SchemaAndValue getExpectedTestMessageWithJsonFieldNames() { |
| 564 | + Struct testMessageStruct = getTestMessageStruct(TEST_MSG_STRING, 123); |
| 565 | + Schema testMessageSchema = getTestMessageSchema(); |
| 566 | + |
| 567 | + final SchemaBuilder builder = SchemaBuilder.struct(); |
| 568 | + builder.name("TestMessage").version(1); |
| 569 | + List values = new ArrayList<>(); |
| 570 | + for (Field field : testMessageSchema.fields()) { |
| 571 | + String jsonFieldName = TestMessage.getDescriptor() |
| 572 | + .findFieldByName(field.name()).getJsonName(); |
| 573 | + builder.field(jsonFieldName, field.schema()); |
| 574 | + values.add(testMessageStruct.get(field)); |
| 575 | + } |
| 576 | + final Schema jsonSchema = builder.build(); |
| 577 | + final Struct jsonStruct = new Struct(jsonSchema); |
| 578 | + final Iterator<Object> valuesIt = values.iterator(); |
| 579 | + for (Field field : jsonSchema.fields()) { |
| 580 | + jsonStruct.put(field, valuesIt.next()); |
| 581 | + } |
| 582 | + return new SchemaAndValue(jsonSchema, jsonStruct); |
| 583 | + } |
| 584 | + |
534 | 585 | @Test
|
535 | 586 | public void testToConnectDataForValueWithSecondMessage() throws Exception {
|
536 | 587 | converter.configure(SR_CONFIG, false);
|
|
0 commit comments