Search in sources :

Example 51 with ValidationResult

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

the class StandardSSLContextService method onConfigured.

@OnEnabled
public void onConfigured(final ConfigurationContext context) throws InitializationException {
    configContext = context;
    final Collection<ValidationResult> results = new ArrayList<>();
    results.addAll(validateStore(context.getProperties(), KeystoreValidationGroup.KEYSTORE));
    results.addAll(validateStore(context.getProperties(), KeystoreValidationGroup.TRUSTSTORE));
    if (!results.isEmpty()) {
        final StringBuilder sb = new StringBuilder(this + " is not valid due to:");
        for (final ValidationResult result : results) {
            sb.append("\n").append(result.toString());
        }
        throw new InitializationException(sb.toString());
    }
    if (countNulls(context.getProperty(KEYSTORE).getValue(), context.getProperty(KEYSTORE_PASSWORD).getValue(), context.getProperty(KEYSTORE_TYPE).getValue(), context.getProperty(TRUSTSTORE).getValue(), context.getProperty(TRUSTSTORE_PASSWORD).getValue(), context.getProperty(TRUSTSTORE_TYPE).getValue()) >= 4) {
        throw new InitializationException(this + " does not have the KeyStore or the TrustStore populated");
    }
    // verify that the filename, password, and type match
    createSSLContext(ClientAuth.REQUIRED);
}
Also used : ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) InitializationException(org.apache.nifi.reporting.InitializationException) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 52 with ValidationResult

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

the class StandardSSLContextService method validateStore.

private static Collection<ValidationResult> validateStore(final Map<PropertyDescriptor, String> properties, final KeystoreValidationGroup keyStoreOrTrustStore) {
    final Collection<ValidationResult> results = new ArrayList<>();
    final String filename;
    final String password;
    final String type;
    if (keyStoreOrTrustStore == KeystoreValidationGroup.KEYSTORE) {
        filename = properties.get(KEYSTORE);
        password = properties.get(KEYSTORE_PASSWORD);
        type = properties.get(KEYSTORE_TYPE);
    } else {
        filename = properties.get(TRUSTSTORE);
        password = properties.get(TRUSTSTORE_PASSWORD);
        type = properties.get(TRUSTSTORE_TYPE);
    }
    final String keystoreDesc = (keyStoreOrTrustStore == KeystoreValidationGroup.KEYSTORE) ? "Keystore" : "Truststore";
    final int nulls = countNulls(filename, password, type);
    if (nulls != 3 && nulls != 0) {
        results.add(new ValidationResult.Builder().valid(false).explanation("Must set either 0 or 3 properties for " + keystoreDesc).subject(keystoreDesc + " Properties").build());
    } else if (nulls == 0) {
        // all properties were filled in.
        final File file = new File(filename);
        if (!file.exists() || !file.canRead()) {
            results.add(new ValidationResult.Builder().valid(false).subject(keystoreDesc + " Properties").explanation("Cannot access file " + file.getAbsolutePath()).build());
        } else {
            try {
                final boolean storeValid = CertificateUtils.isStoreValid(file.toURI().toURL(), KeystoreType.valueOf(type), password.toCharArray());
                if (!storeValid) {
                    results.add(new ValidationResult.Builder().subject(keystoreDesc + " Properties").valid(false).explanation("Invalid KeyStore Password or Type specified for file " + filename).build());
                }
            } catch (MalformedURLException e) {
                results.add(new ValidationResult.Builder().subject(keystoreDesc + " Properties").valid(false).explanation("Malformed URL from file: " + e).build());
            }
        }
    }
    return results;
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) File(java.io.File)

Example 53 with ValidationResult

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

the class TestMergeContent method testTextDelimitersValidation.

@Test
public void testTextDelimitersValidation() throws IOException, InterruptedException {
    final TestRunner runner = TestRunners.newTestRunner(new MergeContent());
    runner.setProperty(MergeContent.MAX_BIN_AGE, "1 sec");
    runner.setProperty(MergeContent.MERGE_FORMAT, MergeContent.MERGE_FORMAT_CONCAT);
    runner.setProperty(MergeContent.DELIMITER_STRATEGY, MergeContent.DELIMITER_STRATEGY_TEXT);
    runner.setProperty(MergeContent.HEADER, "");
    runner.setProperty(MergeContent.DEMARCATOR, "");
    runner.setProperty(MergeContent.FOOTER, "");
    Collection<ValidationResult> results = new HashSet<>();
    ProcessContext context = runner.getProcessContext();
    if (context instanceof MockProcessContext) {
        MockProcessContext mockContext = (MockProcessContext) context;
        results = mockContext.validate();
    }
    Assert.assertEquals(3, results.size());
    for (ValidationResult vr : results) {
        Assert.assertTrue(vr.toString().contains("cannot be empty"));
    }
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) ValidationResult(org.apache.nifi.components.ValidationResult) MockProcessContext(org.apache.nifi.util.MockProcessContext) ProcessContext(org.apache.nifi.processor.ProcessContext) MockProcessContext(org.apache.nifi.util.MockProcessContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 54 with ValidationResult

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

the class TestRouteOnAttribute method testInvalidOnNonBooleanProperty.

@Test
public void testInvalidOnNonBooleanProperty() {
    final RouteOnAttribute proc = new RouteOnAttribute();
    final MockProcessContext ctx = new MockProcessContext(proc);
    // Should be boolean
    final ValidationResult validationResult = ctx.setProperty("RouteA", "${a:length()");
    assertFalse(validationResult.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) MockProcessContext(org.apache.nifi.util.MockProcessContext) Test(org.junit.Test)

Example 55 with ValidationResult

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

Aggregations

ValidationResult (org.apache.nifi.components.ValidationResult)179 Test (org.junit.Test)80 ArrayList (java.util.ArrayList)64 TestRunner (org.apache.nifi.util.TestRunner)46 ValidationContext (org.apache.nifi.components.ValidationContext)37 MockProcessContext (org.apache.nifi.util.MockProcessContext)26 TdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.api.TdchConnectionService)23 Validator (org.apache.nifi.components.Validator)21 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)20 DevTdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.core.controllerservice.DevTdchConnectionService)18 DummyTdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.core.controllerservice.DummyTdchConnectionService)18 HashSet (java.util.HashSet)18 ProcessContext (org.apache.nifi.processor.ProcessContext)15 File (java.io.File)12 HashMap (java.util.HashMap)12 Collection (java.util.Collection)11 SSLContextService (org.apache.nifi.ssl.SSLContextService)11 Map (java.util.Map)10 List (java.util.List)9 ComponentLog (org.apache.nifi.logging.ComponentLog)9