Search in sources :

Example 6 with AvroSnapshotDeserializer

use of com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer in project registry by hortonworks.

the class AvroSerDesInitClosureTest method testDeserClosedInit.

@Test(expected = IllegalStateException.class)
public void testDeserClosedInit() throws Exception {
    AvroSnapshotDeserializer deserializer = new AvroSnapshotDeserializer();
    deserializer.init(Collections.emptyMap());
    deserializer.close();
    deserializer.init(Collections.emptyMap());
}
Also used : AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) Test(org.junit.Test)

Example 7 with AvroSnapshotDeserializer

use of com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer in project registry by hortonworks.

the class SchemaVersionProtocolHandlerTest method _testSerDes.

private void _testSerDes(Long id, Number serdesProtocolVersion) throws Exception {
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("random-" + System.currentTimeMillis()).schemaGroup("custom").type(AvroSchemaProvider.TYPE).compatibility(SchemaCompatibility.BACKWARD).build();
    SchemaIdVersion schemaIdVersion = new SchemaIdVersion(1L, 1, id);
    Device input = new Device(1L, "device", 1, System.currentTimeMillis());
    SchemaVersionInfo schemaVersionInfo = new SchemaVersionInfo(id, input.getName().toString(), schemaIdVersion.getVersion(), input.getSchema().toString(), System.currentTimeMillis(), "some device");
    new Expectations() {

        {
            mockSchemaRegistryClient.getSchemaMetadataInfo(anyString);
            result = new SchemaMetadataInfo(schemaMetadata);
            minTimes = 0;
            maxTimes = 1;
            mockSchemaRegistryClient.addSchemaVersion(withInstanceOf(SchemaMetadata.class), withInstanceOf(SchemaVersion.class));
            result = schemaIdVersion;
            minTimes = 0;
            maxTimes = 1;
            mockSchemaRegistryClient.getSchemaVersionInfo(withInstanceOf(SchemaVersionKey.class));
            result = schemaVersionInfo;
            minTimes = 0;
            maxTimes = 1;
        }
    };
    AvroSnapshotSerializer serializer = new AvroSnapshotSerializer();
    serializer.init(Collections.singletonMap(SERDES_PROTOCOL_VERSION, serdesProtocolVersion));
    AvroSnapshotDeserializer deserializer = new AvroSnapshotDeserializer();
    deserializer.init(Collections.emptyMap());
    byte[] serializedData = serializer.serialize(input, schemaMetadata);
    Object deserializedObj = deserializer.deserialize(new ByteArrayInputStream(serializedData), null);
    Assert.assertTrue(SpecificData.get().compare(input, deserializedObj, input.getSchema()) == 0);
}
Also used : Expectations(mockit.Expectations) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) Device(com.hortonworks.registries.serdes.Device) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) AvroSnapshotSerializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Example 8 with AvroSnapshotDeserializer

use of com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer in project registry by hortonworks.

the class AvroSchemaRegistryClientTest method _testAvroSerDesGenericObj.

private void _testAvroSerDesGenericObj(Byte protocolId) throws IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    Map<String, Object> config = Maps.newHashMap();
    config.putAll(SCHEMA_REGISTRY_CLIENT_CONF);
    config.put(SERDES_PROTOCOL_VERSION, protocolId);
    AvroSnapshotSerializer avroSnapshotSerializer = new AvroSnapshotSerializer();
    avroSnapshotSerializer.init(config);
    AvroSnapshotDeserializer avroSnapshotDeserializer = new AvroSnapshotDeserializer();
    avroSnapshotDeserializer.init(config);
    String deviceSchema = AvroSchemaRegistryClientUtil.getSchema("/device.avsc");
    SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BOTH);
    SchemaIdVersion v1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaMetadata, new SchemaVersion(deviceSchema, "Initial version of the schema"));
    Assert.assertNotNull(v1);
    Object deviceObject = AvroSchemaRegistryClientUtil.createGenericRecordForDevice();
    byte[] serializedData = avroSnapshotSerializer.serialize(deviceObject, schemaMetadata);
    Object deserializedObj = avroSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedData), null);
    Assert.assertEquals(deviceObject, deserializedObj);
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) AvroSnapshotSerializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer)

Aggregations

AvroSnapshotDeserializer (com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)5 AvroSnapshotSerializer (com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer)5 Test (org.junit.Test)3 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)2 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)2 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)1 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)1 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)1 Device (com.hortonworks.registries.serdes.Device)1 Expectations (mockit.Expectations)1 Utf8 (org.apache.avro.util.Utf8)1