use of org.apache.nifi.components.Validator in project nifi by apache.
the class TestStandardValidators method testListValidator.
@Test
public void testListValidator() {
Validator val = StandardValidators.createListValidator(true, false, StandardValidators.NON_EMPTY_VALIDATOR);
ValidationResult vr;
final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
vr = val.validate("List", null, validationContext);
assertFalse(vr.isValid());
vr = val.validate("List", "", validationContext);
assertFalse(vr.isValid());
// Whitespace will be trimmed
vr = val.validate("List", " ", validationContext);
assertFalse(vr.isValid());
vr = val.validate("List", "1", validationContext);
assertTrue(vr.isValid());
vr = val.validate("List", "1,2,3", validationContext);
assertTrue(vr.isValid());
// The parser will not bother with whitespace after the last comma
vr = val.validate("List", "a,", validationContext);
assertTrue(vr.isValid());
// However it will bother if there is an empty element in the list (two commas in a row, e.g.)
vr = val.validate("List", "a,,c", validationContext);
assertFalse(vr.isValid());
vr = val.validate("List", "a, ,c, ", validationContext);
assertFalse(vr.isValid());
// Try without trim and use a non-blank validator instead of a non-empty one
val = StandardValidators.createListValidator(false, true, StandardValidators.NON_BLANK_VALIDATOR);
vr = val.validate("List", null, validationContext);
assertFalse(vr.isValid());
// Validator will ignore empty entries
vr = val.validate("List", "", validationContext);
assertTrue(vr.isValid());
// Whitespace will not be trimmed, but it is still invalid because a non-blank validator is used
vr = val.validate("List", " ", validationContext);
assertFalse(vr.isValid());
vr = val.validate("List", "a,,c", validationContext);
assertTrue(vr.isValid());
vr = val.validate("List", "a, ,c, ", validationContext);
assertFalse(vr.isValid());
// Try without trim and use a non-empty validator
val = StandardValidators.createListValidator(false, false, StandardValidators.NON_EMPTY_VALIDATOR);
vr = val.validate("List", null, validationContext);
assertFalse(vr.isValid());
vr = val.validate("List", "", validationContext);
assertFalse(vr.isValid());
// Whitespace will not be trimmed
vr = val.validate("List", " ", validationContext);
assertTrue(vr.isValid());
vr = val.validate("List", "a, ,c, ", validationContext);
assertTrue(vr.isValid());
// Try with trim and use a boolean validator
val = StandardValidators.createListValidator(true, true, StandardValidators.BOOLEAN_VALIDATOR);
vr = val.validate("List", "notbool", validationContext);
assertFalse(vr.isValid());
vr = val.validate("List", " notbool \n ", validationContext);
assertFalse(vr.isValid());
vr = val.validate("List", "true", validationContext);
assertTrue(vr.isValid());
vr = val.validate("List", " true \n ", validationContext);
assertTrue(vr.isValid());
vr = val.validate("List", " , false, true,\n", validationContext);
assertTrue(vr.isValid());
}
use of org.apache.nifi.components.Validator in project nifi by apache.
the class TestStandardValidators method testiso8061InstantValidator.
@Test
public void testiso8061InstantValidator() {
Validator val = StandardValidators.ISO8061_INSTANT_VALIDATOR;
ValidationContext vc = mock(ValidationContext.class);
ValidationResult vr = val.validate("foo", "", vc);
assertFalse(vr.isValid());
vr = val.validate("foo", "2016-01-01T01:01:01.000-0100", vc);
assertFalse(vr.isValid());
vr = val.validate("foo", "2016-01-01T01:01:01.000Z", vc);
assertTrue(vr.isValid());
}
use of org.apache.nifi.components.Validator in project nifi by apache.
the class TestStandardValidators method testDataSizeBoundsValidator.
@Test
public void testDataSizeBoundsValidator() {
Validator val = StandardValidators.createDataSizeBoundsValidator(100, 1000);
ValidationResult vr;
final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
vr = val.validate("DataSizeBounds", "5 GB", validationContext);
assertFalse(vr.isValid());
vr = val.validate("DataSizeBounds", "0 B", validationContext);
assertFalse(vr.isValid());
vr = val.validate("DataSizeBounds", "99 B", validationContext);
assertFalse(vr.isValid());
vr = val.validate("DataSizeBounds", "100 B", validationContext);
assertTrue(vr.isValid());
vr = val.validate("DataSizeBounds", "999 B", validationContext);
assertTrue(vr.isValid());
vr = val.validate("DataSizeBounds", "1000 B", validationContext);
assertTrue(vr.isValid());
vr = val.validate("DataSizeBounds", "1001 B", validationContext);
assertFalse(vr.isValid());
vr = val.validate("DataSizeBounds", "water", validationContext);
assertFalse(vr.isValid());
}
use of org.apache.nifi.components.Validator in project nifi by apache.
the class TestCSVValidators method testUnEscapedSingleCharNullValue.
/**
* Unescaped SingleCharValidator *
*/
@Test
public void testUnEscapedSingleCharNullValue() {
Validator validator = CSVValidators.UNESCAPED_SINGLE_CHAR_VALIDATOR;
ValidationContext mockContext = Mockito.mock(ValidationContext.class);
ValidationResult result = validator.validate("Delimiter", null, mockContext);
assertEquals("Input is null for this property", result.getExplanation());
assertFalse(result.isValid());
}
use of org.apache.nifi.components.Validator in project nifi by apache.
the class StandardSSLContextService method createFileExistsAndReadableValidator.
private static Validator createFileExistsAndReadableValidator() {
return new Validator() {
// Not using the FILE_EXISTS_VALIDATOR because the default is to
// allow expression language
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
final File file = new File(input);
final boolean valid = file.exists() && file.canRead();
final String explanation = valid ? null : "File " + file + " does not exist or cannot be read";
return new ValidationResult.Builder().subject(subject).input(input).valid(valid).explanation(explanation).build();
}
};
}
Aggregations