Search in sources :

Example 31 with ValidationContext

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

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

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

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

the class TestCSVValidators method testSingleCharNullValue.

/**
 * SingleCharValidator *
 */
@Test
public void testSingleCharNullValue() {
    CSVValidators.SingleCharacterValidator validator = new CSVValidators.SingleCharacterValidator();
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    ValidationResult result = validator.validate("EscapeChar", null, mockContext);
    assertEquals("Input is null for this property", result.getExplanation());
    assertFalse(result.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 35 with ValidationContext

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

the class TestCSVValidators method testSingleCharGoodChar.

@Test
public void testSingleCharGoodChar() {
    CSVValidators.SingleCharacterValidator validator = new CSVValidators.SingleCharacterValidator();
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    ValidationResult result = validator.validate("EscapeChar", "'", mockContext);
    assertTrue(result.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) 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