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;
}
};
}
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");
}
}
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);
}
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);
}
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;
});
}
Aggregations