Search in sources :

Example 11 with ValidationContext

use of org.apache.nifi.components.ValidationContext 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 12 with ValidationContext

use of org.apache.nifi.components.ValidationContext 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 13 with ValidationContext

use of org.apache.nifi.components.ValidationContext 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 14 with ValidationContext

use of org.apache.nifi.components.ValidationContext 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 15 with ValidationContext

use of org.apache.nifi.components.ValidationContext 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

ValidationContext (org.apache.nifi.components.ValidationContext)40 ValidationResult (org.apache.nifi.components.ValidationResult)37 Validator (org.apache.nifi.components.Validator)18 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)9 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)7 Collection (java.util.Collection)6 File (java.io.File)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Map (java.util.Map)5 Collections (java.util.Collections)4 Set (java.util.Set)4 PropertyValue (org.apache.nifi.components.PropertyValue)4 MockStateManager (org.apache.nifi.state.MockStateManager)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)3 Relationship (org.apache.nifi.processor.Relationship)3 FileInputStream (java.io.FileInputStream)2