Search in sources :

Example 6 with Validator

use of org.apache.nifi.components.Validator in project nifi by apache.

the class StandardValidators method createAttributeExpressionLanguageValidator.

public static Validator createAttributeExpressionLanguageValidator(final ResultType expectedResultType, final boolean allowExtraCharacters) {
    return new Validator() {

        @Override
        public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
            final String syntaxError = context.newExpressionLanguageCompiler().validateExpression(input, allowExtraCharacters);
            if (syntaxError != null) {
                return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(syntaxError).build();
            }
            final ResultType resultType = allowExtraCharacters ? ResultType.STRING : context.newExpressionLanguageCompiler().getResultType(input);
            if (!resultType.equals(expectedResultType)) {
                return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Expected Attribute Query to return type " + expectedResultType + " but query returns type " + resultType).build();
            }
            return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
        }
    };
}
Also used : ResultType(org.apache.nifi.expression.AttributeExpression.ResultType) ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 7 with Validator

use of org.apache.nifi.components.Validator in project nifi by apache.

the class StandardValidators method createRegexValidator.

/**
 * Creates a @{link Validator} that ensure that a value is a valid Java
 * Regular Expression with at least <code>minCapturingGroups</code>
 * capturing groups and at most <code>maxCapturingGroups</code> capturing
 * groups. If <code>supportAttributeExpressionLanguage</code> is set to
 * <code>true</code>, the value may also include the Expression Language,
 * but the result of evaluating the Expression Language will be applied
 * before the Regular Expression is performed. In this case, the Expression
 * Language will not support FlowFile Attributes but only System/JVM
 * Properties
 *
 * @param minCapturingGroups minimum capturing groups allowed
 * @param maxCapturingGroups maximum capturing groups allowed
 * @param supportAttributeExpressionLanguage whether or not to support
 * expression language
 * @return validator
 */
public static Validator createRegexValidator(final int minCapturingGroups, final int maxCapturingGroups, final boolean supportAttributeExpressionLanguage) {
    return new Validator() {

        @Override
        public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
            try {
                final String substituted;
                if (supportAttributeExpressionLanguage) {
                    try {
                        substituted = context.newPropertyValue(value).evaluateAttributeExpressions().getValue();
                    } catch (final Exception e) {
                        return new ValidationResult.Builder().subject(subject).input(value).valid(false).explanation("Failed to evaluate the Attribute Expression Language due to " + e.toString()).build();
                    }
                } else {
                    substituted = value;
                }
                final Pattern pattern = Pattern.compile(substituted);
                final int numGroups = pattern.matcher("").groupCount();
                if (numGroups < minCapturingGroups || numGroups > maxCapturingGroups) {
                    return new ValidationResult.Builder().subject(subject).input(value).valid(false).explanation("RegEx is required to have between " + minCapturingGroups + " and " + maxCapturingGroups + " Capturing Groups but has " + numGroups).build();
                }
                return new ValidationResult.Builder().subject(subject).input(value).valid(true).build();
            } catch (final Exception e) {
                return new ValidationResult.Builder().subject(subject).input(value).valid(false).explanation("Not a valid Java Regular Expression").build();
            }
        }
    };
}
Also used : Pattern(java.util.regex.Pattern) ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) MalformedURLException(java.net.MalformedURLException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ParseException(java.text.ParseException) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 8 with Validator

use of org.apache.nifi.components.Validator in project nifi by apache.

the class TestStandardValidators method testHostnamePortListValidator.

@Test
public void testHostnamePortListValidator() {
    Validator val = StandardValidators.HOSTNAME_PORT_LIST_VALIDATOR;
    ValidationContext vc = mock(ValidationContext.class);
    Mockito.when(vc.isExpressionLanguageSupported("foo")).thenReturn(true);
    ValidationResult vr = val.validate("foo", "", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "localhost", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "test:0", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "test:65536", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "test:6666,localhost", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "test:65535", vc);
    assertTrue(vr.isValid());
    vr = val.validate("foo", "test:65535,localhost:666,127.0.0.1:8989", vc);
    assertTrue(vr.isValid());
    Mockito.when(vc.isExpressionLanguagePresent("${test}")).thenReturn(true);
    vr = val.validate("foo", "${test}", vc);
    assertTrue(vr.isValid());
    vr = val.validate("foo", "${test", vc);
    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 9 with Validator

use of org.apache.nifi.components.Validator in project nifi by apache.

the class TestStandardValidators method testCreateURLorFileValidator.

@Test
public void testCreateURLorFileValidator() {
    Validator val = StandardValidators.createURLorFileValidator();
    ValidationResult vr;
    final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
    vr = val.validate("URLorFile", null, validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("URLorFile", "", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("URLorFile", "http://nifi.apache.org", validationContext);
    assertTrue(vr.isValid());
    vr = val.validate("URLorFile", "http//nifi.apache.org", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("URLorFile", "nifi.apache.org", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("URLorFile", "src/test/resources/this_file_exists.txt", validationContext);
    assertTrue(vr.isValid());
    vr = val.validate("URLorFile", "src/test/resources/this_file_does_not_exist.txt", 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 10 with Validator

use of org.apache.nifi.components.Validator in project nifi by apache.

the class TestStandardValidators method testNonBlankValidator.

@Test
public void testNonBlankValidator() {
    Validator val = StandardValidators.NON_BLANK_VALIDATOR;
    ValidationContext vc = mock(ValidationContext.class);
    ValidationResult vr = val.validate("foo", "", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "    ", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "    h", 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)

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