use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project nifi by apache.
the class TestHortonworksSchemaRegistry method testCacheUsed.
@Test
public void testCacheUsed() throws Exception {
final String text = new String(Files.readAllBytes(Paths.get("src/test/resources/empty-schema.avsc")));
final SchemaVersionInfo info = new SchemaVersionInfo(1L, "unit-test", 2, text, System.currentTimeMillis(), "description");
schemaVersionInfoMap.put("unit-test", info);
final SchemaMetadata metadata = new SchemaMetadata.Builder("unit-test").compatibility(SchemaCompatibility.NONE).evolve(true).schemaGroup("group").type("AVRO").build();
final Constructor<SchemaMetadataInfo> ctr = SchemaMetadataInfo.class.getDeclaredConstructor(SchemaMetadata.class, Long.class, Long.class);
ctr.setAccessible(true);
final SchemaMetadataInfo schemaMetadataInfo = ctr.newInstance(metadata, 1L, System.currentTimeMillis());
schemaMetadataInfoMap.put("unit-test", schemaMetadataInfo);
final Map<PropertyDescriptor, String> properties = new HashMap<>();
properties.put(HortonworksSchemaRegistry.URL, "http://localhost:44444");
properties.put(HortonworksSchemaRegistry.CACHE_EXPIRATION, "5 mins");
properties.put(HortonworksSchemaRegistry.CACHE_SIZE, "1000");
final ConfigurationContext configurationContext = new MockConfigurationContext(properties, null);
registry.enable(configurationContext);
for (int i = 0; i < 10000; i++) {
final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
assertNotNull(schema);
}
Mockito.verify(client, Mockito.times(1)).getLatestSchemaVersionInfo(any(String.class));
}
use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project nifi by apache.
the class TestHortonworksSchemaRegistry method testCacheExpires.
@Test
@Ignore("This can be useful for manual testing/debugging, but will keep ignored for now because we don't want automated builds to run this, since it depends on timing")
public void testCacheExpires() throws Exception {
final String text = new String(Files.readAllBytes(Paths.get("src/test/resources/empty-schema.avsc")));
final SchemaVersionInfo info = new SchemaVersionInfo(1L, "unit-test", 2, text, System.currentTimeMillis(), "description");
schemaVersionInfoMap.put("unit-test", info);
final SchemaMetadata metadata = new SchemaMetadata.Builder("unit-test").compatibility(SchemaCompatibility.NONE).evolve(true).schemaGroup("group").type("AVRO").build();
final Constructor<SchemaMetadataInfo> ctr = SchemaMetadataInfo.class.getDeclaredConstructor(SchemaMetadata.class, Long.class, Long.class);
ctr.setAccessible(true);
final SchemaMetadataInfo schemaMetadataInfo = ctr.newInstance(metadata, 1L, System.currentTimeMillis());
schemaMetadataInfoMap.put("unit-test", schemaMetadataInfo);
final Map<PropertyDescriptor, String> properties = new HashMap<>();
properties.put(HortonworksSchemaRegistry.URL, "http://localhost:44444");
properties.put(HortonworksSchemaRegistry.CACHE_EXPIRATION, "1 sec");
properties.put(HortonworksSchemaRegistry.CACHE_SIZE, "1000");
final ConfigurationContext configurationContext = new MockConfigurationContext(properties, null);
registry.enable(configurationContext);
for (int i = 0; i < 2; i++) {
final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
assertNotNull(schema);
}
Mockito.verify(client, Mockito.times(1)).getLatestSchemaVersionInfo(any(String.class));
Thread.sleep(2000L);
for (int i = 0; i < 2; i++) {
final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
assertNotNull(schema);
}
Mockito.verify(client, Mockito.times(2)).getLatestSchemaVersionInfo(any(String.class));
}
use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project streamline by hortonworks.
the class AvroStreamsSnapshotDeserializerTest method testAvroPayloadConversions.
@Test
public void testAvroPayloadConversions() throws Exception {
try (InputStream schemaStream = AvroStreamsSnapshotDeserializerTest.class.getResourceAsStream("/avro/complex.avsc")) {
CustomAvroSerializer customAvroSerializer = new CustomAvroSerializer();
customAvroSerializer.init(Collections.singletonMap("serdes.protocol.version", (byte) 1));
final Schema schema = new Schema.Parser().parse(schemaStream);
GenericRecord inputRecord = generateGenericRecord(schema);
LOG.info("Generated record [{}]", inputRecord);
byte[] serializedBytes = customAvroSerializer.customSerialize(inputRecord);
SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("topic-1").type("avro").schemaGroup("kafka").build();
new Expectations() {
{
mockSchemaRegistryClient.getSchemaVersionInfo(withEqual(SCHEMA_ID_VERSION));
// only versions of schema matters for this mock, as getVersion is only used during de-serialization
result = new SchemaVersionInfo(1l, schemaMetadata.getName(), SCHEMA_ID_VERSION.getVersion(), "doesNotMatter", 1l, "doesNotMatter");
mockSchemaRegistryClient.getSchemaMetadataInfo(withEqual(schemaMetadata.getName()));
result = new SchemaMetadataInfo(schemaMetadata);
}
};
AvroStreamsSnapshotDeserializer avroStreamsSnapshotDeserializer = new AvroStreamsSnapshotDeserializer() {
@Override
protected Schema getSchema(SchemaVersionKey schemaVersionKey) {
return schema;
}
};
Map<String, String> config = Collections.singletonMap(SchemaRegistryClient.Configuration.SCHEMA_REGISTRY_URL.name(), "http://localhost:8080/api/v1");
avroStreamsSnapshotDeserializer.init(config);
Object deserializedObject = avroStreamsSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedBytes), 1);
Map<Object, Object> map = (Map<Object, Object>) deserializedObject;
String deserializedJson = new ObjectMapper().writeValueAsString(map);
String inputJson = GenericData.get().toString(inputRecord);
LOG.info("inputJson #{}# " + inputJson);
LOG.info("deserializedJson = #{}#" + deserializedJson);
ObjectMapper objectMapper = new ObjectMapper();
Assert.assertEquals(objectMapper.readTree(inputJson), objectMapper.readTree(deserializedJson));
}
}
use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project registry by hortonworks.
the class SchemaRegistryClient method doAddSchemaVersion.
private SchemaIdVersion doAddSchemaVersion(String schemaBranchName, String schemaName, SchemaVersion schemaVersion) throws IncompatibleSchemaException, InvalidSchemaException, SchemaNotFoundException {
SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
if (schemaMetadataInfo == null) {
throw new SchemaNotFoundException("Schema with name " + schemaName + " not found");
}
WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaName).path("/versions").queryParam("branch", schemaBranchName);
Response response = Subject.doAs(subject, new PrivilegedAction<Response>() {
@Override
public Response run() {
return target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(schemaVersion), Response.class);
}
});
return handleSchemaIdVersionResponse(schemaMetadataInfo, response);
}
use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project registry by hortonworks.
the class SchemaRegistryClient method uploadSchemaVersion.
public SchemaIdVersion uploadSchemaVersion(final String schemaBranchName, final String schemaName, final String description, final InputStream schemaVersionInputStream) throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName);
if (schemaMetadataInfo == null) {
throw new SchemaNotFoundException("Schema with name " + schemaName + " not found");
}
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", schemaVersionInputStream);
WebTarget target = currentSchemaRegistryTargets().schemasTarget.path(schemaName).path("/versions/upload").queryParam("branch", schemaBranchName);
MultiPart multipartEntity = new FormDataMultiPart().field("description", description, MediaType.APPLICATION_JSON_TYPE).bodyPart(streamDataBodyPart);
Entity<MultiPart> multiPartEntity = Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA);
Response response = Subject.doAs(subject, new PrivilegedAction<Response>() {
@Override
public Response run() {
return target.request().post(multiPartEntity, Response.class);
}
});
return handleSchemaIdVersionResponse(schemaMetadataInfo, response);
}
Aggregations