use of com.hortonworks.streamline.streams.runtime.storm.spout.AvroStreamsSnapshotDeserializer in project streamline by hortonworks.
the class AvroStreamsSnapshotDeserializerTest method testAvroPayloadConversions.
@Test
public void testAvroPayloadConversions() throws Exception {
try (InputStream schemaStream = AvroStreamsSnapshotDeserializerTest.class.getResourceAsStream("/avro/complex.avsc")) {
CustomAvroSerializer customAvroSerializer = new CustomAvroSerializer();
customAvroSerializer.init(Collections.singletonMap("serdes.protocol.version", (byte) 1));
final Schema schema = new Schema.Parser().parse(schemaStream);
GenericRecord inputRecord = generateGenericRecord(schema);
LOG.info("Generated record [{}]", inputRecord);
byte[] serializedBytes = customAvroSerializer.customSerialize(inputRecord);
SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("topic-1").type("avro").schemaGroup("kafka").build();
new Expectations() {
{
mockSchemaRegistryClient.getSchemaVersionInfo(withEqual(SCHEMA_ID_VERSION));
// only versions of schema matters for this mock, as getVersion is only used during de-serialization
result = new SchemaVersionInfo(1l, schemaMetadata.getName(), SCHEMA_ID_VERSION.getVersion(), "doesNotMatter", 1l, "doesNotMatter");
mockSchemaRegistryClient.getSchemaMetadataInfo(withEqual(schemaMetadata.getName()));
result = new SchemaMetadataInfo(schemaMetadata);
}
};
AvroStreamsSnapshotDeserializer avroStreamsSnapshotDeserializer = new AvroStreamsSnapshotDeserializer() {
@Override
protected Schema getSchema(SchemaVersionKey schemaVersionKey) {
return schema;
}
};
Map<String, String> config = Collections.singletonMap(SchemaRegistryClient.Configuration.SCHEMA_REGISTRY_URL.name(), "http://localhost:8080/api/v1");
avroStreamsSnapshotDeserializer.init(config);
Object deserializedObject = avroStreamsSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedBytes), 1);
Map<Object, Object> map = (Map<Object, Object>) deserializedObject;
String deserializedJson = new ObjectMapper().writeValueAsString(map);
String inputJson = GenericData.get().toString(inputRecord);
LOG.info("inputJson #{}# " + inputJson);
LOG.info("deserializedJson = #{}#" + deserializedJson);
ObjectMapper objectMapper = new ObjectMapper();
Assert.assertEquals(objectMapper.readTree(inputJson), objectMapper.readTree(deserializedJson));
}
}
Aggregations