Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* enabled default constructor Default constructor allows the SerDe to be used as default serde in KStreams config. * added logic to resolve the reflection schema when it is not provided during construction * fixed whitespace warning * added missing constructors * cloned the original test case to support dynamic type recognition * cache only generated schemas The failure in testVersionMaintained was caused by caching the schema for given name. In case of GenericDatumReader the schema is as it comes from the serialized record. This means if the schema evolves, there could be multiple representations of the same avro record.
- Loading branch information
1 parent
ea2fcfb
commit f8ca0ce
Showing
5 changed files
with
206 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...src/test/java/io/confluent/kafka/streams/serdes/avro/ReflectionAvroSerdeSpecificTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2018 Confluent Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.confluent.kafka.streams.serdes.avro; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import io.confluent.kafka.example.Widget; | ||
import io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient; | ||
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient; | ||
import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
|
||
public class ReflectionAvroSerdeSpecificTest { | ||
|
||
private static final String ANY_TOPIC = "any-topic"; | ||
|
||
private static <T> ReflectionAvroSerde<T> | ||
createConfiguredSerdeForRecordValues(Class<T> type) { | ||
SchemaRegistryClient schemaRegistryClient = new MockSchemaRegistryClient(); | ||
ReflectionAvroSerde<T> serde = new ReflectionAvroSerde<>(schemaRegistryClient, type); | ||
Map<String, Object> serdeConfig = new HashMap<>(); | ||
serdeConfig.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "fake"); | ||
serde.configure(serdeConfig, false); | ||
return serde; | ||
} | ||
|
||
private static <T> ReflectionAvroSerde<T> | ||
createConfiguredSerdeForAnyValues() { | ||
SchemaRegistryClient schemaRegistryClient = new MockSchemaRegistryClient(); | ||
ReflectionAvroSerde<T> serde = new ReflectionAvroSerde<>(schemaRegistryClient); | ||
Map<String, Object> serdeConfig = new HashMap<>(); | ||
serdeConfig.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "fake"); | ||
serde.configure(serdeConfig, false); | ||
return serde; | ||
} | ||
|
||
@Test | ||
public void shouldRoundTripRecords() { | ||
// Given | ||
ReflectionAvroSerde<Widget> serde = createConfiguredSerdeForRecordValues(Widget.class); | ||
Widget record = new Widget("alice"); | ||
|
||
// When | ||
Widget roundtrippedRecord = serde.deserializer().deserialize( | ||
ANY_TOPIC, | ||
serde.serializer().serialize(ANY_TOPIC, record)); | ||
|
||
// Then | ||
assertThat(roundtrippedRecord, equalTo(record)); | ||
|
||
// Cleanup | ||
serde.close(); | ||
} | ||
|
||
@Test | ||
public void shouldRoundTripNullRecordsToNull() { | ||
// Given | ||
ReflectionAvroSerde<Widget> serde = createConfiguredSerdeForRecordValues(Widget.class); | ||
|
||
// When | ||
Widget roundtrippedRecord = serde.deserializer().deserialize( | ||
ANY_TOPIC, | ||
serde.serializer().serialize(ANY_TOPIC, null)); | ||
|
||
// Then | ||
assertThat(roundtrippedRecord, nullValue()); | ||
|
||
// Cleanup | ||
serde.close(); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void shouldFailWhenInstantiatedWithNullSchemaRegistryClient() { | ||
new ReflectionAvroSerde<>(null, Widget.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters