Search in sources :

Example 16 with Validator

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());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 17 with Validator

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());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 18 with Validator

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());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 19 with Validator

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());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 20 with Validator

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();
        }
    };
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) File(java.io.File) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

Aggregations

ValidationResult (org.apache.nifi.components.ValidationResult)20 Validator (org.apache.nifi.components.Validator)20 ValidationContext (org.apache.nifi.components.ValidationContext)17 Test (org.junit.Test)11 File (java.io.File)3 ArrayList (java.util.ArrayList)3 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)2 ResultType (org.apache.nifi.expression.AttributeExpression.ResultType)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 MalformedURLException (java.net.MalformedURLException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1