use of org.apache.nifi.controller.ConfigurationContext 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 org.apache.nifi.controller.ConfigurationContext 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 org.apache.nifi.controller.ConfigurationContext in project nifi by apache.
the class TestCSVHeaderSchemaStrategy method testSimple.
@Test
public void testSimple() throws SchemaNotFoundException, IOException {
final String headerLine = "a, b, c, d, e\\,z, f";
final byte[] headerBytes = headerLine.getBytes();
final Map<PropertyDescriptor, String> properties = new HashMap<>();
properties.put(CSVUtils.CSV_FORMAT, CSVUtils.CUSTOM.getValue());
properties.put(CSVUtils.COMMENT_MARKER, "#");
properties.put(CSVUtils.VALUE_SEPARATOR, ",");
properties.put(CSVUtils.TRIM_FIELDS, "true");
properties.put(CSVUtils.QUOTE_CHAR, "\"");
properties.put(CSVUtils.ESCAPE_CHAR, "\\");
final ConfigurationContext context = new MockConfigurationContext(properties, null);
final CSVHeaderSchemaStrategy strategy = new CSVHeaderSchemaStrategy(context);
final RecordSchema schema;
try (final InputStream bais = new ByteArrayInputStream(headerBytes)) {
schema = strategy.getSchema(null, bais, null);
}
final List<String> expectedFieldNames = Arrays.asList("a", "b", "c", "d", "e,z", "f");
assertEquals(expectedFieldNames, schema.getFieldNames());
assertTrue(schema.getFields().stream().allMatch(field -> field.getDataType().equals(RecordFieldType.STRING.getDataType())));
}
Aggregations