Skip to content

Commit

Permalink
test(AvroConverter): add test case for single field serialization
Browse files Browse the repository at this point in the history
Adds test case for single field serialization that shows that the order of fields in the input schema affects the output.
  • Loading branch information
baumac committed Oct 23, 2024
1 parent c781363 commit 313f559
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,55 @@ public void testPrimitive() {
assertEquals(expected, schemaAndValue);
}

@Test
public void testSingleFieldSerialization() throws RestClientException, IOException {
SchemaRegistryClient schemaRegistry = new MockSchemaRegistryClient();
AvroConverter avroConverter = new AvroConverter(schemaRegistry);
Map<String, ?> converterConfig = ImmutableMap.of(
AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "localhost",
AbstractKafkaSchemaSerDeConfig.AUTO_REGISTER_SCHEMAS, false,
AbstractKafkaSchemaSerDeConfig.LATEST_COMPATIBILITY_STRICT, false,
AbstractKafkaSchemaSerDeConfig.NORMALIZE_SCHEMAS, true,
AbstractKafkaSchemaSerDeConfig.USE_LATEST_VERSION, true,
AbstractKafkaSchemaSerDeConfig.VALUE_SUBJECT_NAME_STRATEGY, TopicNameStrategy.class.getName());
avroConverter.configure(converterConfig, false);

org.apache.avro.Schema registredSchema = org.apache.avro.SchemaBuilder
.record("MySchema")
.fields()
.requiredString("id")
.endRecord();

schemaRegistry.register("topic-value", new AvroSchema(registredSchema));

Schema inputSchema1 = SchemaBuilder.struct()
.field("foo", org.apache.kafka.connect.data.Schema.STRING_SCHEMA)
.field("id", org.apache.kafka.connect.data.Schema.STRING_SCHEMA)
.build();

Struct inputValue1 = new Struct(inputSchema1)
.put("foo", "123")
.put("id", "456");

Schema inputSchema2 = SchemaBuilder.struct()
.field("id", org.apache.kafka.connect.data.Schema.STRING_SCHEMA)
.field("foo", org.apache.kafka.connect.data.Schema.STRING_SCHEMA)
.build();

Struct inputValue2 = new Struct(inputSchema2)
.put("id", "456")
.put("foo", "123");

final byte[] bytes1 = avroConverter.fromConnectData("topic", inputSchema1, inputValue1);
final SchemaAndValue schemaAndValue1 = avroConverter.toConnectData("topic", bytes1);

final byte[] bytes2 = avroConverter.fromConnectData("topic", inputSchema2, inputValue2);
final SchemaAndValue schemaAndValue2 = avroConverter.toConnectData("topic", bytes2);


assertEquals(schemaAndValue1.value(), schemaAndValue2.value());
}

@Test
public void testComplex() {
SchemaBuilder builder = SchemaBuilder.struct()
Expand Down

0 comments on commit 313f559

Please sign in to comment.