Search in sources :

Example 6 with ValidationContext

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

the class TestAvroSchemaRegistry method validateStrictAndNonStrictSchemaRegistrationFromDynamicProperties.

@Test
public void validateStrictAndNonStrictSchemaRegistrationFromDynamicProperties() throws Exception {
    String schemaName = "fooSchema";
    ConfigurationContext configContext = mock(ConfigurationContext.class);
    Map<PropertyDescriptor, String> properties = new HashMap<>();
    PropertyDescriptor fooSchema = new PropertyDescriptor.Builder().name(schemaName).dynamic(true).build();
    // NOTE: name of record and name of first field are not Avro-compliant, verified below
    String fooSchemaText = "{\"namespace\": \"example.avro\", " + "\"type\": \"record\", " + "\"name\": \"$User\", " + "\"fields\": [ " + "{\"name\": \"@name\", \"type\": [\"string\", \"null\"]}, " + "{\"name\": \"favorite_number\",  \"type\": [\"int\", \"null\"]}, " + "{\"name\": \"foo\",  \"type\": [\"int\", \"null\"]}, " + "{\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]} " + "]" + "}";
    PropertyDescriptor barSchema = new PropertyDescriptor.Builder().name("barSchema").dynamic(false).build();
    properties.put(fooSchema, fooSchemaText);
    properties.put(barSchema, "");
    AvroSchemaRegistry delegate = new AvroSchemaRegistry();
    delegate.getSupportedPropertyDescriptors().forEach(prop -> properties.put(prop, prop.getDisplayName()));
    when(configContext.getProperties()).thenReturn(properties);
    ValidationContext validationContext = mock(ValidationContext.class);
    when(validationContext.getProperties()).thenReturn(properties);
    PropertyValue propertyValue = mock(PropertyValue.class);
    when(validationContext.getProperty(AvroSchemaRegistry.VALIDATE_FIELD_NAMES)).thenReturn(propertyValue);
    // Strict parsing
    when(propertyValue.asBoolean()).thenReturn(true);
    Collection<ValidationResult> results = delegate.customValidate(validationContext);
    assertTrue(results.stream().anyMatch(result -> !result.isValid()));
    // Non-strict parsing
    when(propertyValue.asBoolean()).thenReturn(false);
    results = delegate.customValidate(validationContext);
    results.forEach(result -> assertTrue(result.isValid()));
}
Also used : ValidationContext(org.apache.nifi.components.ValidationContext) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) PropertyValue(org.apache.nifi.components.PropertyValue) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Map(java.util.Map) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) ValidationResult(org.apache.nifi.components.ValidationResult) Mockito.mock(org.mockito.Mockito.mock) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) PropertyValue(org.apache.nifi.components.PropertyValue) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 7 with ValidationContext

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

the class TestCSVValidators method testSingleCharTab.

@Test
public void testSingleCharTab() {
    CSVValidators.SingleCharacterValidator validator = new CSVValidators.SingleCharacterValidator();
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    ValidationResult result = validator.validate("EscapeChar", "\\t", mockContext);
    assertTrue(result.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 8 with ValidationContext

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

the class TestCSVValidators method testUnescapedSingleCharGoodChar.

@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());
}
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 ValidationContext

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

the class TestCSVValidators method testUnescapedSingleCharUnicodeChar.

@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());
}
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 ValidationContext

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

the class TestCSVValidators method testSingleCharIllegalChar.

@Test
public void testSingleCharIllegalChar() {
    CSVValidators.SingleCharacterValidator validator = new CSVValidators.SingleCharacterValidator();
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    ValidationResult result = validator.validate("EscapeChar", "\\r", mockContext);
    assertEquals("\\r is not a valid character 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)

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