Search in sources :

Example 41 with SchemaIdVersion

use of com.hortonworks.registries.schemaregistry.SchemaIdVersion 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 42 with SchemaIdVersion

use of com.hortonworks.registries.schemaregistry.SchemaIdVersion 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)

Example 43 with SchemaIdVersion

use of com.hortonworks.registries.schemaregistry.SchemaIdVersion in project registry by hortonworks.

the class AvroSchemaRegistryClientTest method testSchemaVersionStatesThroughIds.

@Test
public void testSchemaVersionStatesThroughIds() throws Exception {
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(TEST_NAME_RULE.getMethodName() + "-schema").type(AvroSchemaProvider.TYPE).schemaGroup("group").compatibility(SchemaCompatibility.BOTH).build();
    String schemaName = schemaMetadata.getName();
    // build nextTransitions from state machine
    SchemaVersionLifecycleStateMachineInfo stateMachine = SCHEMA_REGISTRY_CLIENT.getSchemaVersionLifecycleStateMachineInfo();
    Map<Byte, List<SchemaVersionLifecycleStateTransition>> nextTransitionsForStateIds = new HashMap<>();
    for (SchemaVersionLifecycleStateTransition transition : stateMachine.getTransitions()) {
        List<SchemaVersionLifecycleStateTransition> nextTransitions = nextTransitionsForStateIds.computeIfAbsent(transition.getSourceStateId(), aByte -> new ArrayList<>());
        nextTransitions.add(transition);
    }
    Long id = SCHEMA_REGISTRY_CLIENT.registerSchemaMetadata(schemaMetadata);
    SchemaIdVersion schemaIdVersion_1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc"), "Initial version of the schema"));
    SchemaIdVersion schemaIdVersion_2 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-2.avsc"), "Second version of the schema"));
    // disable version 2
    SchemaVersionInfo schemaVersionInfo = SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2);
    Byte stateId = schemaVersionInfo.getStateId();
    List<SchemaVersionLifecycleStateTransition> nextTransitions = nextTransitionsForStateIds.get(stateId);
    Byte targetStateId = nextTransitions.get(0).getTargetStateId();
    SCHEMA_REGISTRY_CLIENT.transitionState(schemaVersionInfo.getId(), targetStateId, null);
    Assert.assertEquals(targetStateId, SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2).getStateId());
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaVersionLifecycleStateMachineInfo(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateMachineInfo) HashMap(java.util.HashMap) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionLifecycleStateTransition(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransition) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) List(java.util.List) ArrayList(java.util.ArrayList) IntegrationTest(com.hortonworks.registries.common.test.IntegrationTest) Test(org.junit.Test)

Example 44 with SchemaIdVersion

use of com.hortonworks.registries.schemaregistry.SchemaIdVersion in project registry by hortonworks.

the class AvroCompositeSchemasTest method testComposites.

@Before
public void testComposites() throws Exception {
    versions = new HashMap<>();
    versions.put("utils", cretaeSchemaVersionInfo("/avro/composites/util.avsc"));
    versions.put("account", cretaeSchemaVersionInfo("/avro/composites/account.avsc"));
    versions.put("account-cyclic", cretaeSchemaVersionInfo("/avro/composites/account-cyclic.avsc"));
    versions.put("account-dep", cretaeSchemaVersionInfo("/avro/composites/account-dep.avsc"));
    avroSchemaProvider = new AvroSchemaProvider();
    SchemaVersionRetriever schemaVersionRetriever = new SchemaVersionRetriever() {

        @Override
        public SchemaVersionInfo retrieveSchemaVersion(SchemaVersionKey key) throws SchemaNotFoundException {
            return versions.get(key.getSchemaName());
        }

        @Override
        public SchemaVersionInfo retrieveSchemaVersion(SchemaIdVersion key) throws SchemaNotFoundException {
            return null;
        }
    };
    Map<String, Object> config = Collections.singletonMap(SchemaProvider.SCHEMA_VERSION_RETRIEVER_CONFIG, schemaVersionRetriever);
    avroSchemaProvider.init(config);
}
Also used : SchemaVersionRetriever(com.hortonworks.registries.schemaregistry.SchemaVersionRetriever) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) Before(org.junit.Before)

Example 45 with SchemaIdVersion

use of com.hortonworks.registries.schemaregistry.SchemaIdVersion in project registry by hortonworks.

the class SchemaVersionInfoCache method invalidateSchema.

public void invalidateSchema(SchemaVersionInfoCache.Key key) {
    LOG.debug("Invalidating cache entry for key [{}]", key);
    loadingCache.invalidate(key);
    SchemaVersionKey schemaVersionKey = key.schemaIdVersion != null ? idWithNameVersion.get(key.schemaIdVersion) : key.schemaVersionKey;
    // it can be null if it is not accessed earlier.
    if (schemaVersionKey != null) {
        loadingCache.invalidate(Key.of(schemaVersionKey));
        List<SchemaIdVersion> schemaIdVersions = nameVersionWithIds.get(schemaVersionKey);
        if (schemaIdVersions != null) {
            for (SchemaIdVersion schemaIdVersion : schemaIdVersions) {
                loadingCache.invalidate(Key.of(schemaIdVersion));
            }
        }
    }
}
Also used : SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey)

Aggregations

SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)45 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)32 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)22 Test (org.junit.Test)21 SchemaBranch (com.hortonworks.registries.schemaregistry.SchemaBranch)15 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)15 IOException (java.io.IOException)11 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)10 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)10 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)9 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)9 Timed (com.codahale.metrics.annotation.Timed)5 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)5 SchemaRegistryTestServerClientWrapper (com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper)5 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)5 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)5 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)5 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)5 SchemaLifecycleException (com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException)5 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)4