Search in sources :

Example 26 with SchemaVersionInfo

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

the class AvroSchemaRegistryTest method testRegistrySchemaOps.

@Test
public void testRegistrySchemaOps() throws Exception {
    SchemaCompatibility compatibility = SchemaCompatibility.BOTH;
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(schemaName).type(AvroSchemaProvider.TYPE).description("devices schema").compatibility(compatibility).schemaGroup(SCHEMA_GROUP).build();
    Long schemaMetadataId = schemaRegistry.registerSchemaMetadata(schemaMetadata);
    SchemaMetadata schemaMetadataReturned = schemaRegistry.getSchemaMetadataInfo(schemaMetadataId).getSchemaMetadata();
    Assert.assertEquals(schemaMetadata, schemaMetadataReturned);
    Integer v1 = schemaRegistry.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "initial version of the schema")).getVersion();
    Integer v2 = schemaRegistry.addSchemaVersion(schemaName, new SchemaVersion(schema2, "second version of the the schema")).getVersion();
    Assert.assertTrue(v2 == v1 + 1);
    Collection<SchemaVersionInfo> allSchemaVersions = schemaRegistry.getAllVersions(schemaName);
    Assert.assertTrue(allSchemaVersions.size() == 2);
    SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(schemaName);
    Assert.assertEquals(schemaMetadata, schemaMetadataInfo.getSchemaMetadata());
    Integer schemaVersion = schemaRegistry.getSchemaVersionInfo(schemaName, schema1).getVersion();
    Assert.assertEquals(v1, schemaVersion);
    SchemaVersionInfo schemaVersionInfo1 = schemaRegistry.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v1));
    Assert.assertEquals(schemaVersionInfo1.getSchemaText(), schema1);
    SchemaVersionInfo schemaVersionInfo2 = schemaRegistry.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v2));
    Assert.assertEquals(schemaVersionInfo2.getSchemaText(), schema2);
    // receive the same version as earlier without adding a new schema entry as it exists in the same schema group.
    Integer version = schemaRegistry.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "already added schema")).getVersion();
    Assert.assertEquals(version, v1);
    // aggregate apis
    AggregatedSchemaMetadataInfo aggregatedSchemaMetadata = schemaRegistry.getAggregatedSchemaMetadataInfo(schemaName);
    Assert.assertEquals(allSchemaVersions.size(), aggregatedSchemaMetadata.getSchemaBranches().iterator().next().getSchemaVersionInfos().size());
    Assert.assertTrue(aggregatedSchemaMetadata.getSerDesInfos().isEmpty());
    Collection<AggregatedSchemaMetadataInfo> aggregatedSchemaMetadataCollection = schemaRegistry.findAggregatedSchemaMetadata(Collections.emptyMap());
    Assert.assertEquals(1, aggregatedSchemaMetadataCollection.size());
    // Serializing and deserializing AggregatedSchemaMetadataInfo should not throw any errors
    String aggregateSchemaMetadataStr = new ObjectMapper().writeValueAsString(aggregatedSchemaMetadataCollection);
    Collection<AggregatedSchemaMetadataInfo> returnedResult = new ObjectMapper().readValue(aggregateSchemaMetadataStr, new TypeReference<Collection<AggregatedSchemaMetadataInfo>>() {
    });
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaCompatibility(com.hortonworks.registries.schemaregistry.SchemaCompatibility) Collection(java.util.Collection) AggregatedSchemaMetadataInfo(com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AggregatedSchemaMetadataInfo(com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Example 27 with SchemaVersionInfo

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

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

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

the class ConfluentSchemaRegistryCompatibleResource method getSchemaVersion.

@GET
@Path("/subjects/{subject}/versions/{versionId}")
@ApiOperation(value = "Get the schema information for given subject and versionId", response = Integer.class, responseContainer = "Collection", tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response getSchemaVersion(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "versionId", required = true) @PathParam("versionId") String versionId) {
    Response response;
    try {
        SchemaVersionInfo schemaVersionInfo = null;
        if ("latest".equals(versionId)) {
            schemaVersionInfo = schemaRegistry.getLatestSchemaVersionInfo(subject);
        } else {
            SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject);
            if (schemaMetadataInfo == null) {
                throw new SchemaNotFoundException();
            }
            SchemaVersionInfo fetchedSchemaVersionInfo = null;
            try {
                Integer version = Integer.valueOf(versionId);
                if (version > 0 && version <= Integer.MAX_VALUE) {
                    fetchedSchemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaVersionKey(subject, version));
                } else {
                    LOG.error("versionId is not in valid range [{}, {}] ", 1, Integer.MAX_VALUE);
                }
            } catch (NumberFormatException e) {
                LOG.error("Invalid version id string ", versionId, e);
            } catch (SchemaNotFoundException e) {
                LOG.error("Schema version not found with version id [{}]", versionId, e);
            }
            if (fetchedSchemaVersionInfo != null) {
                if (subject.equals(fetchedSchemaVersionInfo.getName())) {
                    schemaVersionInfo = fetchedSchemaVersionInfo;
                } else {
                    LOG.error("Received schema version for id [{}] belongs to subject [{}] which is different from requested subject [{}]", versionId, fetchedSchemaVersionInfo.getName(), subject);
                }
            }
        }
        if (schemaVersionInfo == null) {
            response = versionNotFoundError();
        } else {
            Schema schema = new Schema(schemaVersionInfo.getName(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId(), schemaVersionInfo.getSchemaText());
            response = WSUtils.respondEntity(schema, Response.Status.OK);
        }
    } catch (SchemaNotFoundException ex) {
        LOG.error("No schema found with subject [{}]", subject, ex);
        response = subjectNotFoundError();
    } catch (Exception ex) {
        LOG.error("Encountered error while retrieving all subjects", ex);
        response = serverError();
    }
    return response;
}
Also used : CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) Response(javax.ws.rs.core.Response) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 30 with SchemaVersionInfo

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

the class SchemaVersionLifecycleStatesTest method setup.

@Before
public void setup() {
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("schema-1").type("avro").schemaGroup("kafka").build();
    SchemaMetadataInfo schemaMetadataInfo = new SchemaMetadataInfo(schemaMetadata, new Random().nextLong(), System.currentTimeMillis());
    SchemaVersionInfo schemaVersionInfo = new SchemaVersionInfo(new Random().nextLong(), schemaMetadata.getName(), 1, schemaMetadataInfo.getId(), "{\"type\":\"string\"}", System.currentTimeMillis(), "", SchemaVersionLifecycleStates.ENABLED.getId());
    SchemaVersionService schemaVersionServiceMock = new SchemaVersionService() {

        @Override
        public void updateSchemaVersionState(SchemaVersionLifecycleContext schemaLifeCycleContext) {
            LOG.info("Updating schema version: [{}]", schemaLifeCycleContext);
        }

        @Override
        public void deleteSchemaVersion(Long schemaVersionId) {
            LOG.info("Deleting schema version [{}]", schemaVersionId);
        }

        @Override
        public SchemaMetadataInfo getSchemaMetadata(long schemaVersionId) throws SchemaNotFoundException {
            return schemaMetadataInfo;
        }

        @Override
        public SchemaVersionInfo getSchemaVersionInfo(long schemaVersionId) throws SchemaNotFoundException {
            return schemaVersionInfo;
        }

        @Override
        public CompatibilityResult checkForCompatibility(SchemaMetadata schemaMetadata, String toSchemaText, String existingSchemaText) {
            return CompatibilityResult.SUCCESS;
        }

        @Override
        public Collection<SchemaVersionInfo> getAllSchemaVersions(String schemaBranchName, String schemaName) throws SchemaNotFoundException {
            return Collections.singletonList(schemaVersionInfo);
        }
    };
    SchemaVersionLifecycleStateMachine lifecycleStateMachine = SchemaVersionLifecycleStateMachine.newBuilder().build();
    context = new SchemaVersionLifecycleContext(schemaVersionInfo.getId(), 1, schemaVersionServiceMock, lifecycleStateMachine, new DefaultCustomSchemaStateExecutor());
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) Random(java.util.Random) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Before(org.junit.Before)

Aggregations

SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)31 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)19 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)17 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)14 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)13 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)13 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)11 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)11 IOException (java.io.IOException)11 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)10 Test (org.junit.Test)10 Timed (com.codahale.metrics.annotation.Timed)7 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)7 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)7 GET (javax.ws.rs.GET)7 Path (javax.ws.rs.Path)7 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)6 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)6 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)6 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)6