Search in sources :

Example 1 with SchemaMetadataInfo

use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project nifi by apache.

the class TestHortonworksSchemaRegistry method setup.

@Before
public void setup() throws SchemaNotFoundException {
    schemaVersionInfoMap.clear();
    schemaMetadataInfoMap.clear();
    client = mock(SchemaRegistryClient.class);
    doAnswer(new Answer<SchemaVersionInfo>() {

        @Override
        public SchemaVersionInfo answer(final InvocationOnMock invocation) throws Throwable {
            final String schemaName = invocation.getArgumentAt(0, String.class);
            final SchemaVersionInfo info = schemaVersionInfoMap.get(schemaName);
            if (info == null) {
                throw new SchemaNotFoundException();
            }
            return info;
        }
    }).when(client).getLatestSchemaVersionInfo(any(String.class));
    doAnswer(new Answer<SchemaMetadataInfo>() {

        @Override
        public SchemaMetadataInfo answer(InvocationOnMock invocation) throws Throwable {
            final String schemaName = invocation.getArgumentAt(0, String.class);
            final SchemaMetadataInfo info = schemaMetadataInfoMap.get(schemaName);
            if (info == null) {
                throw new SchemaNotFoundException();
            }
            return info;
        }
    }).when(client).getSchemaMetadataInfo(any(String.class));
    registry = new HortonworksSchemaRegistry() {

        @Override
        protected synchronized SchemaRegistryClient getClient() {
            return client;
        }
    };
}
Also used : SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Before(org.junit.Before)

Example 2 with SchemaMetadataInfo

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

the class AvroSchemaRegistryClientTest method testValidationLevels.

@Test
public void testValidationLevels() throws Exception {
    SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BOTH);
    String schemaName = schemaMetadata.getName();
    Long id = SCHEMA_REGISTRY_CLIENT.registerSchemaMetadata(schemaMetadata);
    SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc"), "Initial version of the schema"));
    SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-2.avsc"), "Second version of the schema"));
    SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-3.avsc"), "Third version of the schema, removes name field"));
    try {
        SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-4.avsc"), "Forth version of the schema, adds back name field, but different type"));
        Assert.fail("Should throw IncompatibleSchemaException as check against all schema's would find name field is not compatible with v1 and v2");
    } catch (IncompatibleSchemaException ise) {
    // expected
    }
    SchemaMetadata currentSchemaMetadata = SCHEMA_REGISTRY_CLIENT.getSchemaMetadataInfo(schemaName).getSchemaMetadata();
    SchemaMetadata schemaMetadataToUpdateTo = new SchemaMetadata.Builder(currentSchemaMetadata).validationLevel(SchemaValidationLevel.LATEST).build();
    SchemaMetadataInfo updatedSchemaMetadata = SCHEMA_REGISTRY_CLIENT.updateSchemaMetadata(schemaName, schemaMetadataToUpdateTo);
    Assert.assertEquals(SchemaValidationLevel.LATEST, updatedSchemaMetadata.getSchemaMetadata().getValidationLevel());
    try {
        SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-4.avsc"), "Forth version of the schema, adds back name field, but different type"));
    } catch (IncompatibleSchemaException ise) {
        Assert.fail("Should not throw IncompatibleSchemaException as check against only latest schema as such should ignore v1 and v2");
    }
}
Also used : IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) IntegrationTest(com.hortonworks.registries.common.test.IntegrationTest) Test(org.junit.Test)

Example 3 with SchemaMetadataInfo

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

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

the class MessageContextBasedAvroSerDesTest method testSerDes.

@Test
public void testSerDes() throws Exception {
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("msgCtx-" + System.currentTimeMillis()).schemaGroup("custom").type(AvroSchemaProvider.TYPE).compatibility(SchemaCompatibility.BACKWARD).build();
    SchemaIdVersion schemaIdVersion = new SchemaIdVersion(1L, 1, 1L);
    Device input = new Device(1L, "device", 1, System.currentTimeMillis());
    SchemaVersionInfo schemaVersionInfo = new SchemaVersionInfo(1l, input.getName().toString(), schemaIdVersion.getVersion(), input.getSchema().toString(), System.currentTimeMillis(), "");
    new Expectations() {

        {
            mockSchemaRegistryClient.addSchemaVersion(withInstanceOf(SchemaMetadata.class), withInstanceOf(SchemaVersion.class));
            result = schemaIdVersion;
            mockSchemaRegistryClient.getSchemaMetadataInfo(anyString);
            result = new SchemaMetadataInfo(schemaMetadata);
            mockSchemaRegistryClient.getSchemaVersionInfo(withInstanceOf(SchemaVersionKey.class));
            result = schemaVersionInfo;
        }
    };
    MessageContextBasedAvroSerializer serializer = new MessageContextBasedAvroSerializer();
    serializer.init(Collections.emptyMap());
    MessageContext messageContext = serializer.serialize(input, schemaMetadata);
    MessageContextBasedAvroDeserializer deserializer = new MessageContextBasedAvroDeserializer();
    deserializer.init(Collections.emptyMap());
    Object deserializedObject = deserializer.deserialize(messageContext, null);
    Assert.assertTrue(SpecificData.get().compare(input, deserializedObject, 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) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Example 5 with SchemaMetadataInfo

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

Aggregations

SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)22 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)14 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)13 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)10 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)9 Test (org.junit.Test)8 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)7 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)6 IOException (java.io.IOException)6 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)5 AggregatedSchemaMetadataInfo (com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo)5 Response (javax.ws.rs.core.Response)5 Timed (com.codahale.metrics.annotation.Timed)4 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)4 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)4 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)4 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)4 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)4 ApiOperation (io.swagger.annotations.ApiOperation)4 Path (javax.ws.rs.Path)4