Java Code Examples for org.apache.nifi.components.Validator#validate()

The following examples show how to use org.apache.nifi.components.Validator#validate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestStandardValidators.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testURIListValidator() {
    Validator val = StandardValidators.URI_LIST_VALIDATOR;
    ValidationContext vc = mock(ValidationContext.class);
    ValidationResult vr = val.validate("foo", null, vc);
    assertFalse(vr.isValid());

    vr = val.validate("foo", "", vc);
    assertFalse(vr.isValid());

    vr = val.validate("foo", "/no_scheme", vc);
    assertTrue(vr.isValid());

    vr = val.validate("foo", "http://localhost 8080, https://host2:8080 ", vc);
    assertFalse(vr.isValid());

    vr = val.validate("foo", "http://localhost , https://host2:8080 ", vc);
    assertTrue(vr.isValid());
}
 
Example 2
Source File: TestStandardValidators.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonEmptyELValidator() {
    Validator val = StandardValidators.NON_EMPTY_EL_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", "    h", 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);
    assertTrue(vr.isValid());
}
 
Example 3
Source File: TestStandardValidators.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimePeriodValidator() {
    Validator val = StandardValidators.createTimePeriodValidator(1L, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    ValidationResult vr;

    final ValidationContext validationContext = Mockito.mock(ValidationContext.class);

    vr = val.validate("TimePeriodTest", "0 sense made", validationContext);
    assertFalse(vr.isValid());

    vr = val.validate("TimePeriodTest", null, validationContext);
    assertFalse(vr.isValid());

    vr = val.validate("TimePeriodTest", "0 secs", validationContext);
    assertFalse(vr.isValid());

    vr = val.validate("TimePeriodTest", "999 millis", validationContext);
    assertFalse(vr.isValid());

    vr = val.validate("TimePeriodTest", "999999999 nanos", validationContext);
    assertFalse(vr.isValid());

    vr = val.validate("TimePeriodTest", "1 sec", validationContext);
    assertTrue(vr.isValid());
}
 
Example 4
Source File: TestStandardValidators.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonEmptyELValidator() {
    Validator val = StandardValidators.NON_EMPTY_EL_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", "    h", 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);
    assertTrue(vr.isValid());
}
 
Example 5
Source File: TestStandardValidators.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 6
Source File: TestStandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 7
Source File: TestStandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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());

}
 
Example 8
Source File: TestStandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 9
Source File: TestCSVValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
/*** 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());

    }
 
Example 10
Source File: TestStandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 11
Source File: TestStandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 12
Source File: StandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static Validator createListValidator(boolean trimEntries, boolean excludeEmptyEntries,
    Validator validator,
    boolean ensureElementValidation) {
    return (subject, input, context) -> {
        if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
            return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
        }
        try {
            if (input == null) {
                return new ValidationResult.Builder().subject(subject).input(null).explanation("List must have at least one non-empty element").valid(false).build();
            }

            final String[] list =  ensureElementValidation ? input.split(",",-1) : input.split(",");
            if (list.length == 0) {
                return new ValidationResult.Builder().subject(subject).input(null).explanation("List must have at least one non-empty element").valid(false).build();
            }

            for (String item : list) {
                String itemToValidate = trimEntries ? item.trim() : item;
                if (!isEmpty(itemToValidate) || !excludeEmptyEntries) {
                    ValidationResult result = validator.validate(subject, itemToValidate, context);
                    if (!result.isValid()) {
                        return result;
                    }
                }
            }
            return new ValidationResult.Builder().subject(subject).input(input).explanation("Valid List").valid(true).build();
        } catch (final Exception e) {
            return new ValidationResult.Builder().subject(subject).input(input).explanation("Not a valid list").valid(false).build();
        }
    };
}
 
Example 13
Source File: ControlRate.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(context));

    final Validator rateValidator;
    switch (context.getProperty(RATE_CONTROL_CRITERIA).getValue().toLowerCase()) {
        case DATA_RATE:
            rateValidator = StandardValidators.DATA_SIZE_VALIDATOR;
            break;
        case ATTRIBUTE_RATE:
            rateValidator = StandardValidators.POSITIVE_LONG_VALIDATOR;
            final String rateAttr = context.getProperty(RATE_CONTROL_ATTRIBUTE_NAME).getValue();
            if (rateAttr == null) {
                validationResults.add(new ValidationResult.Builder()
                        .subject(RATE_CONTROL_ATTRIBUTE_NAME.getName())
                        .explanation("<Rate Controlled Attribute> property must be set if using <Rate Control Criteria> of 'attribute value'")
                        .build());
            }
            break;
        case FLOWFILE_RATE:
        default:
            rateValidator = StandardValidators.POSITIVE_LONG_VALIDATOR;
            break;
    }

    final ValidationResult rateResult = rateValidator.validate("Maximum Rate", context.getProperty(MAX_RATE).getValue(), context);
    if (!rateResult.isValid()) {
        validationResults.add(rateResult);
    }

    return validationResults;
}
 
Example 14
Source File: TestCSVValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnescapedSingleCharExpressionLanguage() {
    Validator validator = CSVValidators.UNESCAPED_SINGLE_CHAR_VALIDATOR;
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    Mockito.when(mockContext.isExpressionLanguageSupported(Mockito.any())).thenReturn(true);
    Mockito.when(mockContext.isExpressionLanguagePresent(Mockito.any())).thenReturn(true);
    ValidationResult result = validator.validate("Delimiter", "${csv.delimiter}", mockContext);
    assertTrue(result.isValid());
}
 
Example 15
Source File: TestStandardValidators.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@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());

}
 
Example 16
Source File: TestStandardValidators.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 17
Source File: TestCSVValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnescapedSingleCharGoodChar() {
    Validator validator = CSVValidators.UNESCAPED_SINGLE_CHAR_VALIDATOR;
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    ValidationResult result = validator.validate("Delimiter", ",", mockContext);
    assertTrue(result.isValid());
}
 
Example 18
Source File: TestCSVValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnescapedSingleCharUnicodeChar() {
    Validator validator = CSVValidators.UNESCAPED_SINGLE_CHAR_VALIDATOR;
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    ValidationResult result = validator.validate("Delimiter", "\\u0001", mockContext);
    assertTrue(result.isValid());
}
 
Example 19
Source File: RouteText.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    Collection<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext));
    boolean dynamicProperty = false;

    final String matchStrategy = validationContext.getProperty(MATCH_STRATEGY).getValue();
    final boolean compileRegex = matchStrategy.equals(matchesRegularExpressionValue) || matchStrategy.equals(containsRegularExpressionValue);
    final boolean requiresExpression = matchStrategy.equalsIgnoreCase(satisfiesExpression);

    Validator validator = null;
    if (compileRegex) {
        validator = StandardValidators.createRegexValidator(0, Integer.MAX_VALUE, true);
    }

    Map<PropertyDescriptor, String> allProperties = validationContext.getProperties();
    for (final PropertyDescriptor descriptor : allProperties.keySet()) {
        if (descriptor.isDynamic()) {
            dynamicProperty = true;

            final String propValue = validationContext.getProperty(descriptor).getValue();

            if (compileRegex) {
                ValidationResult validationResult = validator.validate(descriptor.getName(), propValue, validationContext);
                if (validationResult != null) {
                    results.add(validationResult);
                }
            } else if (requiresExpression) {
                try {
                    final ResultType resultType = validationContext.newExpressionLanguageCompiler().compile(propValue).getResultType();
                    if (resultType != ResultType.BOOLEAN) {
                        results.add(new ValidationResult.Builder().valid(false).input(propValue).subject(descriptor.getName())
                            .explanation("expression returns type of " + resultType.name() + " but is required to return a Boolean value").build());
                    }
                } catch (final IllegalArgumentException iae) {
                    results.add(new ValidationResult.Builder().valid(false).input(propValue).subject(descriptor.getName())
                        .explanation("input is not a valid Expression Language expression").build());
                }
            }
        }
    }

    if (!dynamicProperty) {
        results.add(new ValidationResult.Builder().subject("Dynamic Properties")
            .explanation("In order to route text there must be dynamic properties to match against").valid(false).build());
    }

    return results;
}
 
Example 20
Source File: RouteText.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    Collection<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext));
    boolean dynamicProperty = false;

    final String matchStrategy = validationContext.getProperty(MATCH_STRATEGY).getValue();
    final boolean compileRegex = matchStrategy.equals(matchesRegularExpressionValue) || matchStrategy.equals(containsRegularExpressionValue);
    final boolean requiresExpression = matchStrategy.equalsIgnoreCase(satisfiesExpression);

    Validator validator = null;
    if (compileRegex) {
        validator = StandardValidators.createRegexValidator(0, Integer.MAX_VALUE, true);
    }

    Map<PropertyDescriptor, String> allProperties = validationContext.getProperties();
    for (final PropertyDescriptor descriptor : allProperties.keySet()) {
        if (descriptor.isDynamic()) {
            dynamicProperty = true;

            final String propValue = validationContext.getProperty(descriptor).getValue();

            if (compileRegex) {
                ValidationResult validationResult = validator.validate(descriptor.getName(), propValue, validationContext);
                if (validationResult != null) {
                    results.add(validationResult);
                }
            } else if (requiresExpression) {
                try {
                    final ResultType resultType = validationContext.newExpressionLanguageCompiler().compile(propValue).getResultType();
                    if (resultType != ResultType.BOOLEAN) {
                        results.add(new ValidationResult.Builder().valid(false).input(propValue).subject(descriptor.getName())
                            .explanation("expression returns type of " + resultType.name() + " but is required to return a Boolean value").build());
                    }
                } catch (final IllegalArgumentException iae) {
                    results.add(new ValidationResult.Builder().valid(false).input(propValue).subject(descriptor.getName())
                        .explanation("input is not a valid Expression Language expression").build());
                }
            }
        }
    }

    if (!dynamicProperty) {
        results.add(new ValidationResult.Builder().subject("Dynamic Properties")
            .explanation("In order to route text there must be dynamic properties to match against").valid(false).build());
    }

    return results;
}