Search in sources :

Example 36 with ValidationContext

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

the class TestCSVValidators method testUnEscapedSingleCharNullValue.

/**
 * 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());
}
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 37 with ValidationContext

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

the class StandardSSLContextService method createFileExistsAndReadableValidator.

private static Validator createFileExistsAndReadableValidator() {
    return new Validator() {

        // Not using the FILE_EXISTS_VALIDATOR because the default is to
        // allow expression language
        @Override
        public ValidationResult validate(String subject, String input, ValidationContext context) {
            final File file = new File(input);
            final boolean valid = file.exists() && file.canRead();
            final String explanation = valid ? null : "File " + file + " does not exist or cannot be read";
            return new ValidationResult.Builder().subject(subject).input(input).valid(valid).explanation(explanation).build();
        }
    };
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) File(java.io.File) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 38 with ValidationContext

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

the class SSLContextServiceTest method testValidationResultsCacheShouldExpire.

@Test
public void testValidationResultsCacheShouldExpire() throws InitializationException, IOException {
    // Arrange
    // Copy the keystore and truststore to a tmp directory so the originals are not modified
    File originalKeystore = new File("src/test/resources/localhost-ks.jks");
    File originalTruststore = new File("src/test/resources/localhost-ts.jks");
    File tmpKeystore = tmp.newFile("keystore-tmp.jks");
    File tmpTruststore = tmp.newFile("truststore-tmp.jks");
    Files.copy(originalKeystore.toPath(), tmpKeystore.toPath(), StandardCopyOption.REPLACE_EXISTING);
    Files.copy(originalTruststore.toPath(), tmpTruststore.toPath(), StandardCopyOption.REPLACE_EXISTING);
    final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    SSLContextService service = new StandardSSLContextService();
    final String serviceIdentifier = "test-should-expire";
    runner.addControllerService(serviceIdentifier, service);
    runner.setProperty(service, StandardSSLContextService.KEYSTORE.getName(), tmpKeystore.getAbsolutePath());
    runner.setProperty(service, StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
    runner.setProperty(service, StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
    runner.setProperty(service, StandardSSLContextService.TRUSTSTORE.getName(), tmpTruststore.getAbsolutePath());
    runner.setProperty(service, StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
    runner.setProperty(service, StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
    runner.enableControllerService(service);
    runner.setProperty("SSL Context Svc ID", serviceIdentifier);
    runner.assertValid(service);
    final StandardSSLContextService sslContextService = (StandardSSLContextService) service;
    // Act
    boolean isDeleted = tmpKeystore.delete();
    assert isDeleted;
    assert !tmpKeystore.exists();
    logger.info("Deleted keystore file");
    // Manually validate the service (expecting cached result to be returned)
    final MockProcessContext processContext = (MockProcessContext) runner.getProcessContext();
    // This service does not use the state manager or variable registry
    final ValidationContext validationContext = new MockValidationContext(processContext, null, null);
    // Even though the keystore file is no longer present, because no property changed, the cached result is still valid
    Collection<ValidationResult> validationResults = sslContextService.customValidate(validationContext);
    assertTrue("validation results is not empty", validationResults.isEmpty());
    logger.info("(1) StandardSSLContextService#customValidate() returned true even though the keystore file is no longer available");
    // Have to exhaust the cached result by checking n-1 more times
    for (int i = 2; i < sslContextService.getValidationCacheExpiration(); i++) {
        validationResults = sslContextService.customValidate(validationContext);
        assertTrue("validation results is not empty", validationResults.isEmpty());
        logger.info("(" + i + ") StandardSSLContextService#customValidate() returned true even though the keystore file is no longer available");
    }
    validationResults = sslContextService.customValidate(validationContext);
    assertFalse("validation results is empty", validationResults.isEmpty());
    logger.info("(" + sslContextService.getValidationCacheExpiration() + ") StandardSSLContextService#customValidate() returned false because the cache expired");
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) MockValidationContext(org.apache.nifi.util.MockValidationContext) ValidationResult(org.apache.nifi.components.ValidationResult) File(java.io.File) MockProcessContext(org.apache.nifi.util.MockProcessContext) ValidationContext(org.apache.nifi.components.ValidationContext) MockValidationContext(org.apache.nifi.util.MockValidationContext) Test(org.junit.Test)

Example 39 with ValidationContext

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

the class AbstractHadoopProcessor method createMultipleFilesExistValidator.

/**
 * Validates that one or more files exist, as specified in a single property.
 *
 * @return a validator instance that validates the files given
 */
public static Validator createMultipleFilesExistValidator() {
    return new Validator() {

        @Override
        public ValidationResult validate(String subject, String input, ValidationContext context) {
            final String[] files = input.split(",");
            for (String filename : files) {
                try {
                    final File file = new File(filename.trim());
                    final boolean valid = file.exists() && file.isFile();
                    if (!valid) {
                        final String message = "File " + file + " does not exist or is not a file";
                        return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(message).build();
                    }
                } catch (SecurityException e) {
                    final String message = "Unable to access " + filename + " due to " + e.getMessage();
                    return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(message).build();
                }
            }
            return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
        }
    };
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) File(java.io.File) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 40 with ValidationContext

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

the class ExecuteSparkApps method createArgumentValidators.

private List<Validator> createArgumentValidators(final AppArgument arg) {
    List<Validator> validators = new ArrayList<>();
    validators.add(new Validator() {

        @Override
        public ValidationResult validate(String subject, String input, ValidationContext context) {
            if (appNames.get().contains(arg.appName)) {
                return new ValidationResult.Builder().subject(subject).input(input).explanation("valid argument").valid(true).build();
            } else {
                return new ValidationResult.Builder().subject(subject).input(input).explanation("it contains an unrecognized app name: \"" + arg.appName + "\"").valid(false).build();
            }
        }
    });
    if (arg.argType.equals("class") || arg.argType.equals("jars")) {
        validators.add(StandardValidators.NON_EMPTY_VALIDATOR);
    }
    return validators;
}
Also used : SparkCommandBuilder(com.thinkbiganalytics.spark.multiexec.SparkApplicationCommandsBuilder.SparkCommandBuilder) Builder(org.apache.nifi.components.PropertyDescriptor.Builder) SparkApplicationCommandsBuilder(com.thinkbiganalytics.spark.multiexec.SparkApplicationCommandsBuilder) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

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