Search in sources :

Example 1 with AvroStreamsSnapshotDeserializer

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));
    }
}
Also used : Expectations(mockit.Expectations) AvroStreamsSnapshotDeserializer(com.hortonworks.streamline.streams.runtime.storm.spout.AvroStreamsSnapshotDeserializer) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(org.apache.avro.Schema) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericRecord(org.apache.avro.generic.GenericRecord) Map(java.util.Map) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)1 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)1 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)1 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)1 AvroStreamsSnapshotDeserializer (com.hortonworks.streamline.streams.runtime.storm.spout.AvroStreamsSnapshotDeserializer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 Expectations (mockit.Expectations)1 Schema (org.apache.avro.Schema)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 Test (org.junit.Test)1