Search in sources :

Example 1 with ValidationContext

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

the class StandardProcessorTestRunner method assertNotValid.

@Override
public void assertNotValid(final ControllerService service) {
    final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier());
    if (serviceStateManager == null) {
        throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method");
    }
    final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service);
    final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext);
    for (final ValidationResult result : results) {
        if (!result.isValid()) {
            return;
        }
    }
    Assert.fail("Expected Controller Service " + service + " to be invalid but it is valid");
}
Also used : MockStateManager(org.apache.nifi.state.MockStateManager) StateManager(org.apache.nifi.components.state.StateManager) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 2 with ValidationContext

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

the class StandardProcessorTestRunner method setProperty.

@Override
public ValidationResult setProperty(final ControllerService service, final PropertyDescriptor property, final String value) {
    final MockStateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier());
    if (serviceStateManager == null) {
        throw new IllegalStateException("Controller service " + service + " has not been added to this TestRunner via the #addControllerService method");
    }
    final ControllerServiceConfiguration configuration = getConfigToUpdate(service);
    final Map<PropertyDescriptor, String> curProps = configuration.getProperties();
    final Map<PropertyDescriptor, String> updatedProps = new HashMap<>(curProps);
    final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service);
    final ValidationResult validationResult = property.validate(value, validationContext);
    final String oldValue = updatedProps.get(property);
    updatedProps.put(property, value);
    configuration.setProperties(updatedProps);
    if ((value == null && oldValue != null) || (value != null && !value.equals(oldValue))) {
        service.onPropertyModified(property, oldValue, value);
    }
    return validationResult;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) MockStateManager(org.apache.nifi.state.MockStateManager) HashMap(java.util.HashMap) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 3 with ValidationContext

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

the class ReportLineageToAtlas method customValidate.

@Override
protected Collection<ValidationResult> customValidate(ValidationContext context) {
    final Collection<ValidationResult> results = new ArrayList<>();
    final boolean isSSLContextServiceSet = context.getProperty(SSL_CONTEXT_SERVICE).isSet();
    final ValidationResult.Builder invalidSSLService = new ValidationResult.Builder().subject(SSL_CONTEXT_SERVICE.getDisplayName()).valid(false);
    parseAtlasUrls(context.getProperty(ATLAS_URLS), input -> {
        final ValidationResult.Builder builder = new ValidationResult.Builder().subject(ATLAS_URLS.getDisplayName()).input(input);
        try {
            final URL url = new URL(input);
            if ("https".equalsIgnoreCase(url.getProtocol()) && !isSSLContextServiceSet) {
                results.add(invalidSSLService.explanation("required by HTTPS Atlas access").build());
            } else {
                results.add(builder.explanation("Valid URI").valid(true).build());
            }
        } catch (Exception e) {
            results.add(builder.explanation("Contains invalid URI: " + e).valid(false).build());
        }
    });
    final String atlasAuthNMethod = context.getProperty(ATLAS_AUTHN_METHOD).getValue();
    final AtlasAuthN atlasAuthN = getAtlasAuthN(atlasAuthNMethod);
    results.addAll(atlasAuthN.validate(context));
    clusterResolverLoader.forEach(resolver -> results.addAll(resolver.validate(context)));
    if (context.getProperty(ATLAS_CONF_CREATE).asBoolean()) {
        Stream.of(ATLAS_CONF_DIR, ATLAS_DEFAULT_CLUSTER_NAME, KAFKA_BOOTSTRAP_SERVERS).filter(p -> !context.getProperty(p).isSet()).forEach(p -> results.add(new ValidationResult.Builder().subject(p.getDisplayName()).explanation("required to create Atlas configuration file.").valid(false).build()));
        validateKafkaProperties(context, results, isSSLContextServiceSet, invalidSSLService);
    }
    return results;
}
Also used : AtlasAuthN(org.apache.nifi.atlas.security.AtlasAuthN) StandardValidators(org.apache.nifi.processor.util.StandardValidators) Arrays(java.util.Arrays) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) URL(java.net.URL) Kerberos(org.apache.nifi.atlas.security.Kerberos) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) PropertyValue(org.apache.nifi.components.PropertyValue) Scope(org.apache.nifi.components.state.Scope) AbstractReportingTask(org.apache.nifi.reporting.AbstractReportingTask) Map(java.util.Map) PROVENANCE_START_POSITION(org.apache.nifi.reporting.util.provenance.ProvenanceEventConsumer.PROVENANCE_START_POSITION) ProvenanceRepository(org.apache.nifi.provenance.ProvenanceRepository) ZoneOffset(java.time.ZoneOffset) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) PROVENANCE_BATCH_SIZE(org.apache.nifi.reporting.util.provenance.ProvenanceEventConsumer.PROVENANCE_BATCH_SIZE) RequiresInstanceClassLoading(org.apache.nifi.annotation.behavior.RequiresInstanceClassLoading) OnUnscheduled(org.apache.nifi.annotation.lifecycle.OnUnscheduled) AtlasServiceException(org.apache.atlas.AtlasServiceException) Collection(java.util.Collection) ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) SSLContextService(org.apache.nifi.ssl.SSLContextService) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) AllowableValue(org.apache.nifi.components.AllowableValue) Instant(java.time.Instant) AnalysisContext(org.apache.nifi.atlas.provenance.AnalysisContext) CompleteFlowPathLineage(org.apache.nifi.atlas.provenance.lineage.CompleteFlowPathLineage) Stateful(org.apache.nifi.annotation.behavior.Stateful) List(java.util.List) DynamicProperty(org.apache.nifi.annotation.behavior.DynamicProperty) Stream(java.util.stream.Stream) NiFiFlowAnalyzer(org.apache.nifi.atlas.NiFiFlowAnalyzer) Tags(org.apache.nifi.annotation.documentation.Tags) ClusterResolver(org.apache.nifi.atlas.resolver.ClusterResolver) EventAccess(org.apache.nifi.reporting.EventAccess) ProvenanceEventConsumer(org.apache.nifi.reporting.util.provenance.ProvenanceEventConsumer) StandardAnalysisContext(org.apache.nifi.atlas.provenance.StandardAnalysisContext) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) Basic(org.apache.nifi.atlas.security.Basic) ValidationContext(org.apache.nifi.components.ValidationContext) ClientResponse(com.sun.jersey.api.client.ClientResponse) NIFI_KERBEROS_PRINCIPAL(org.apache.nifi.atlas.reporting.ReportLineageToAtlas.NIFI_KERBEROS_PRINCIPAL) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) RegexClusterResolver(org.apache.nifi.atlas.resolver.RegexClusterResolver) NiFiAtlasHook(org.apache.nifi.atlas.NiFiAtlasHook) ProvenanceEventRecord(org.apache.nifi.provenance.ProvenanceEventRecord) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) SslConfigs(org.apache.kafka.common.config.SslConfigs) ValidationResult(org.apache.nifi.components.ValidationResult) LinkedHashSet(java.util.LinkedHashSet) PropertyContext(org.apache.nifi.context.PropertyContext) NiFiFlow(org.apache.nifi.atlas.NiFiFlow) Properties(java.util.Properties) NiFiAtlasClient(org.apache.nifi.atlas.NiFiAtlasClient) MalformedURLException(java.net.MalformedURLException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ReportingContext(org.apache.nifi.reporting.ReportingContext) File(java.io.File) SimpleFlowPathLineage(org.apache.nifi.atlas.provenance.lineage.SimpleFlowPathLineage) Consumer(java.util.function.Consumer) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) KerberosCredentialsService(org.apache.nifi.kerberos.KerberosCredentialsService) AtlasAuthN(org.apache.nifi.atlas.security.AtlasAuthN) NIFI_KERBEROS_KEYTAB(org.apache.nifi.atlas.reporting.ReportLineageToAtlas.NIFI_KERBEROS_KEYTAB) LineageStrategy(org.apache.nifi.atlas.provenance.lineage.LineageStrategy) ClusterResolvers(org.apache.nifi.atlas.resolver.ClusterResolvers) DateTimeFormatter(java.time.format.DateTimeFormatter) ApplicationProperties(org.apache.atlas.ApplicationProperties) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) Collections(java.util.Collections) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) URL(java.net.URL) AtlasServiceException(org.apache.atlas.AtlasServiceException) ProcessException(org.apache.nifi.processor.exception.ProcessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 4 with ValidationContext

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

the class LookupRecord method customValidate.

@Override
@SuppressWarnings("unchecked")
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
    final Set<String> dynamicPropNames = validationContext.getProperties().keySet().stream().filter(prop -> prop.isDynamic()).map(prop -> prop.getName()).collect(Collectors.toSet());
    if (dynamicPropNames.isEmpty()) {
        return Collections.singleton(new ValidationResult.Builder().subject("User-Defined Properties").valid(false).explanation("At least one user-defined property must be specified.").build());
    }
    final Set<String> requiredKeys = validationContext.getProperty(LOOKUP_SERVICE).asControllerService(LookupService.class).getRequiredKeys();
    final Set<String> missingKeys = requiredKeys.stream().filter(key -> !dynamicPropNames.contains(key)).collect(Collectors.toSet());
    if (!missingKeys.isEmpty()) {
        final List<ValidationResult> validationResults = new ArrayList<>();
        for (final String missingKey : missingKeys) {
            final ValidationResult result = new ValidationResult.Builder().subject(missingKey).valid(false).explanation("The configured Lookup Services requires that a key be provided with the name '" + missingKey + "'. Please add a new property to this Processor with a name '" + missingKey + "' and provide a RecordPath that can be used to retrieve the appropriate value.").build();
            validationResults.add(result);
        }
        return validationResults;
    }
    return Collections.emptyList();
}
Also used : Arrays(java.util.Arrays) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) ValidationContext(org.apache.nifi.components.ValidationContext) HashMap(java.util.HashMap) EventDriven(org.apache.nifi.annotation.behavior.EventDriven) SideEffectFree(org.apache.nifi.annotation.behavior.SideEffectFree) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) RecordPath(org.apache.nifi.record.path.RecordPath) ArrayList(java.util.ArrayList) RecordPathValidator(org.apache.nifi.record.path.validation.RecordPathValidator) HashSet(java.util.HashSet) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) WritesAttributes(org.apache.nifi.annotation.behavior.WritesAttributes) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) Requirement(org.apache.nifi.annotation.behavior.InputRequirement.Requirement) ValidationResult(org.apache.nifi.components.ValidationResult) Record(org.apache.nifi.serialization.record.Record) RecordPathResult(org.apache.nifi.record.path.RecordPathResult) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) DataTypeUtils(org.apache.nifi.serialization.record.util.DataTypeUtils) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) WritesAttribute(org.apache.nifi.annotation.behavior.WritesAttribute) SeeAlso(org.apache.nifi.annotation.documentation.SeeAlso) AllowableValue(org.apache.nifi.components.AllowableValue) Collectors(java.util.stream.Collectors) List(java.util.List) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) DynamicProperty(org.apache.nifi.annotation.behavior.DynamicProperty) SupportsBatching(org.apache.nifi.annotation.behavior.SupportsBatching) LookupService(org.apache.nifi.lookup.LookupService) Tuple(org.apache.nifi.util.Tuple) Optional(java.util.Optional) RecordPathCache(org.apache.nifi.record.path.util.RecordPathCache) Tags(org.apache.nifi.annotation.documentation.Tags) FieldValue(org.apache.nifi.record.path.FieldValue) Collections(java.util.Collections) LookupService(org.apache.nifi.lookup.LookupService) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult)

Example 5 with ValidationContext

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

the class TestSiteToSiteBulletinReportingTask method testUrls.

@Test
public void testUrls() throws IOException {
    final ValidationContext context = Mockito.mock(ValidationContext.class);
    Mockito.when(context.newPropertyValue(Mockito.anyString())).then(new Answer<PropertyValue>() {

        @Override
        public PropertyValue answer(InvocationOnMock invocation) throws Throwable {
            String value = (String) invocation.getArguments()[0];
            return new StandardPropertyValue(value, null);
        }
    });
    assertTrue(new NiFiUrlValidator().validate("url", "http://localhost:8080/nifi", context).isValid());
    assertTrue(new NiFiUrlValidator().validate("url", "http://localhost:8080", context).isValid());
    assertFalse(new NiFiUrlValidator().validate("url", "", context).isValid());
    assertTrue(new NiFiUrlValidator().validate("url", "https://localhost:8080/nifi", context).isValid());
    assertTrue(new NiFiUrlValidator().validate("url", "https://localhost:8080/nifi,https://localhost:8080/nifi", context).isValid());
    assertTrue(new NiFiUrlValidator().validate("url", "https://localhost:8080/nifi, https://localhost:8080/nifi", context).isValid());
    assertFalse(new NiFiUrlValidator().validate("url", "http://localhost:8080/nifi, https://localhost:8080/nifi", context).isValid());
    assertTrue(new NiFiUrlValidator().validate("url", "http://localhost:8080/nifi,http://localhost:8080/nifi", context).isValid());
    assertTrue(new NiFiUrlValidator().validate("url", "http://localhost:8080/nifi,http://localhost:8080", context).isValid());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) ValidationContext(org.apache.nifi.components.ValidationContext) NiFiUrlValidator(org.apache.nifi.reporting.AbstractSiteToSiteReportingTask.NiFiUrlValidator) 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