Search in sources :

Example 6 with SchemaVersion

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

the class LocalRegistryServerHATest method testHASanity.

@Test
public void testHASanity() throws Exception {
    SchemaRegistryTestServerClientWrapper followerServer = followerSchemaRegistryServer();
    SchemaRegistryClient schemaRegistryClient = followerServer.getClient();
    // registering schema metadata on follower, this should have been redirected to leader.
    String schemaName = "foo";
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(schemaName).type("avro").build();
    Long schemaId = schemaRegistryClient.registerSchemaMetadata(schemaMetadata);
    Assert.assertNotNull(schemaId);
    // registering a new schema on follower, this should have been redirected to leader.
    String schema1 = IOUtils.toString(LocalRegistryServerTest.class.getResourceAsStream("/schema-1.avsc"), "UTF-8");
    SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaName, new SchemaVersion(schema1, "Initial version of the schema"));
    // retrieve schema on leader as the schema data is stored in memory in leader. this data does not exist on
    // followers as the storage is inmemory.
    SchemaRegistryTestServerClientWrapper leaderServer = leaderSchemaRegistryServer();
    int leaderPort = leaderServer.getLocalPort();
    SchemaRegistryClient leaderClient = leaderServer.getClient();
    SchemaMetadataInfo schemaMetadataInfo = leaderClient.getSchemaMetadataInfo(schemaName);
    Assert.assertEquals(schemaMetadata, schemaMetadataInfo.getSchemaMetadata());
    // stop the leader server
    leaderServer.stopTestServer();
    // get the new leader server and run operations.
    SchemaRegistryTestServerClientWrapper newLeaderServer = leaderSchemaRegistryServer();
    Assert.assertNotEquals(leaderPort, newLeaderServer.getLocalPort());
    leaderClient = newLeaderServer.getClient();
    String receivedSchema = leaderClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v1.getVersion())).getSchemaText();
    Assert.assertEquals(schema1, receivedSchema);
}
Also used : SchemaRegistryTestServerClientWrapper(com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Example 7 with SchemaVersion

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

the class AvroSchemaRegistryTest method testSchemaTextAsNull.

@Test(expected = InvalidSchemaException.class)
public void testSchemaTextAsNull() throws Exception {
    SchemaMetadata schemaMetadataInfo = createSchemaInfo(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BACKWARD);
    // registering a new schema
    Integer v1 = schemaRegistry.addSchemaVersion(schemaMetadataInfo, new SchemaVersion(null, "Initial version of the schema")).getVersion();
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) Test(org.junit.Test)

Example 8 with SchemaVersion

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

the class AvroSchemaRegistryTest method testIncompatibleSchemas.

@Test(expected = IncompatibleSchemaException.class)
public void testIncompatibleSchemas() throws Exception {
    String schema = getSchema("/device.avsc");
    String incompatSchema = getSchema("/device-incompat.avsc");
    SchemaMetadata schemaMetadata = createSchemaInfo(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BACKWARD);
    // registering a new schema
    Integer v1 = schemaRegistry.addSchemaVersion(schemaMetadata, new SchemaVersion(schema, "Initial version of the schema")).getVersion();
    // adding a new version of the schema
    Integer v2 = schemaRegistry.addSchemaVersion(schemaMetadata, new SchemaVersion(incompatSchema, "second version")).getVersion();
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) Test(org.junit.Test)

Example 9 with SchemaVersion

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

the class ConfluentSchemaRegistryCompatibleResource method registerSchemaVersion.

@POST
@Path("/subjects/{subject}/versions")
@ApiOperation(value = "Register a new version of the schema", notes = "Registers the given schema version to schema with subject if the given schemaText is not registered as a version for this schema, " + "and returns respective unique id." + "In case of incompatible schema errors, it throws error message like 'Unable to read schema: <> using schema <>' ", response = Id.class, tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response registerSchemaVersion(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "Details about the schema", required = true) String schema, @Context UriInfo uriInfo) {
    LOG.info("registerSchema for [{}] is [{}]", subject);
    return handleLeaderAction(uriInfo, () -> {
        Response response;
        try {
            LOG.info("registerSchema for [{}] is [{}]", subject);
            SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject);
            if (schemaMetadataInfo == null) {
                SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(subject).type(AvroSchemaProvider.TYPE).schemaGroup("Kafka").build();
                schemaRegistry.addSchemaMetadata(schemaMetadata);
                schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject);
            }
            SchemaIdVersion schemaVersionInfo = schemaRegistry.addSchemaVersion(schemaMetadataInfo.getSchemaMetadata(), new SchemaVersion(schemaStringFromJson(schema).getSchema(), null));
            Id id = new Id();
            id.setId(schemaVersionInfo.getSchemaVersionId());
            response = WSUtils.respondEntity(id, Response.Status.OK);
        } catch (InvalidSchemaException ex) {
            LOG.error("Invalid schema error encountered while adding subject [{}]", subject, ex);
            response = invalidSchemaError();
        } catch (IncompatibleSchemaException ex) {
            LOG.error("Incompatible schema error encountered while adding subject [{}]", subject, ex);
            response = incompatibleSchemaError();
        } catch (UnsupportedSchemaTypeException ex) {
            LOG.error("Unsupported schema type encountered while adding subject [{}]", subject, ex);
            response = incompatibleSchemaError();
        } catch (Exception ex) {
            LOG.error("Encountered error while adding subject [{}]", subject, ex);
            response = serverError();
        }
        return response;
    });
}
Also used : CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) Response(javax.ws.rs.core.Response) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) 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) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation)

Example 10 with SchemaVersion

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

the class BasicSchemaRegistryClientOpsTest method doTestSchemaOps.

private void doTestSchemaOps(SchemaValidationLevel validationLevel) throws IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
    String testName = TEST_NAME_RULE.getMethodName();
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(testName + "-schema").type(AvroSchemaProvider.TYPE).schemaGroup(testName + "-group").description("Schema for " + testName).validationLevel(validationLevel).compatibility(SchemaCompatibility.BOTH).build();
    Long id = schemaRegistryClient.registerSchemaMetadata(schemaMetadata);
    Assert.assertNotNull(id);
    // registering a new schema
    String schemaName = schemaMetadata.getName();
    String schema1 = AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc");
    SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaName, new SchemaVersion(schema1, "Initial version of the schema"));
    Assert.assertNotNull(v1.getSchemaMetadataId());
    Assert.assertEquals(1, v1.getVersion().intValue());
    SchemaMetadataInfo schemaMetadataInfoForId = schemaRegistryClient.getSchemaMetadataInfo(v1.getSchemaMetadataId());
    SchemaMetadataInfo schemaMetadataInfoForName = schemaRegistryClient.getSchemaMetadataInfo(schemaName);
    Assert.assertEquals(schemaMetadataInfoForId, schemaMetadataInfoForName);
    // adding a new version of the schema using uploadSchemaVersion API
    SchemaIdVersion v2 = schemaRegistryClient.uploadSchemaVersion(schemaMetadata.getName(), "second version", AvroSchemaRegistryClientTest.class.getResourceAsStream("/schema-2.avsc"));
    Assert.assertEquals(v1.getVersion() + 1, v2.getVersion().intValue());
    SchemaVersionInfo schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v2.getVersion()));
    SchemaVersionInfo latest = schemaRegistryClient.getLatestSchemaVersionInfo(schemaName);
    Assert.assertEquals(latest, schemaVersionInfo);
    Collection<SchemaVersionInfo> allVersions = schemaRegistryClient.getAllVersions(schemaName);
    Assert.assertEquals(2, allVersions.size());
    // receive the same version as earlier without adding a new schema entry as it exists in the same schema group.
    SchemaIdVersion version = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "already added schema"));
    Assert.assertEquals(version, v1);
    Collection<SchemaVersionKey> md5SchemaVersionKeys = schemaRegistryClient.findSchemasByFields(new SchemaFieldQuery.Builder().name("md5").build());
    Assert.assertEquals(2, md5SchemaVersionKeys.size());
    Collection<SchemaVersionKey> txidSchemaVersionKeys = schemaRegistryClient.findSchemasByFields(new SchemaFieldQuery.Builder().name("txid").build());
    Assert.assertEquals(1, txidSchemaVersionKeys.size());
    // checks we can update schema meta data.
    SchemaMetadata currentSchemaMetadata = schemaRegistryClient.getSchemaMetadataInfo(schemaName).getSchemaMetadata();
    SchemaMetadata schemaMetadataToUpdateTo = new SchemaMetadata.Builder(currentSchemaMetadata).validationLevel(SchemaValidationLevel.LATEST).build();
    SchemaMetadataInfo updatedSchemaMetadata = schemaRegistryClient.updateSchemaMetadata(schemaName, schemaMetadataToUpdateTo);
    Assert.assertEquals(SchemaValidationLevel.LATEST, updatedSchemaMetadata.getSchemaMetadata().getValidationLevel());
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaFieldQuery(com.hortonworks.registries.schemaregistry.SchemaFieldQuery) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Aggregations

SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)27 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)25 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)18 Test (org.junit.Test)16 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)7 IntegrationTest (com.hortonworks.registries.common.test.IntegrationTest)6 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)6 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)6 SchemaBranch (com.hortonworks.registries.schemaregistry.SchemaBranch)4 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)4 IOException (java.io.IOException)4 Timed (com.codahale.metrics.annotation.Timed)3 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)3 SchemaRegistryTestServerClientWrapper (com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper)3 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)3 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)3 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Response (javax.ws.rs.core.Response)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2