Search in sources :

Example 61 with PropertyDescriptor

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

the class ExtractText method customValidate.

@Override
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
    final List<ValidationResult> problems = new ArrayList<>(super.customValidate(validationContext));
    // If the capture group zero is not going to be included, each dynamic property must have at least one group
    final boolean includeCaptureGroupZero = validationContext.getProperty(INCLUDE_CAPTURE_GROUP_ZERO).getValue().equalsIgnoreCase("true");
    getLogger().debug("Include capture group zero is " + includeCaptureGroupZero);
    if (!includeCaptureGroupZero) {
        final Validator oneGroupMinimumValidator = StandardValidators.createRegexValidator(1, 40, true);
        for (Map.Entry<PropertyDescriptor, String> prop : validationContext.getProperties().entrySet()) {
            PropertyDescriptor pd = prop.getKey();
            if (pd.isDynamic()) {
                String value = validationContext.getProperty(pd).getValue();
                getLogger().debug("Evaluating dynamic property " + pd.getDisplayName() + " (" + pd.getName() + ") with value " + value);
                ValidationResult result = oneGroupMinimumValidator.validate(pd.getDisplayName(), value, validationContext);
                getLogger().debug("Validation result: " + result.toString());
                if (!result.isValid()) {
                    problems.add(result);
                }
            }
        }
    }
    return problems;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) HashMap(java.util.HashMap) Map(java.util.Map) Validator(org.apache.nifi.components.Validator)

Example 62 with PropertyDescriptor

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

the class ExtractText method onScheduled.

@OnScheduled
public final void onScheduled(final ProcessContext context) throws IOException {
    final Map<String, Pattern> compiledPatternsMap = new HashMap<>();
    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        if (!entry.getKey().isDynamic()) {
            continue;
        }
        final int flags = getCompileFlags(context);
        final Pattern pattern = Pattern.compile(entry.getValue(), flags);
        compiledPatternsMap.put(entry.getKey().getName(), pattern);
    }
    compiledPattersMapRef.set(compiledPatternsMap);
    for (int i = 0; i < context.getMaxConcurrentTasks(); i++) {
        final int maxBufferSize = context.getProperty(MAX_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
        final byte[] buffer = new byte[maxBufferSize];
        bufferQueue.add(buffer);
    }
}
Also used : Pattern(java.util.regex.Pattern) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 63 with PropertyDescriptor

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

the class PutSolrContentStream method getRequestParams.

// get all of the dynamic properties and values into a Map for later adding to the Solr request
private Map<String, String[]> getRequestParams(ProcessContext context, FlowFile flowFile) {
    final Map<String, String[]> paramsMap = new HashMap<>();
    final SortedMap<String, String> repeatingParams = new TreeMap<>();
    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            final String paramName = descriptor.getName();
            final String paramValue = context.getProperty(descriptor).evaluateAttributeExpressions(flowFile).getValue();
            if (!paramValue.trim().isEmpty()) {
                if (paramName.matches(REPEATING_PARAM_PATTERN)) {
                    repeatingParams.put(paramName, paramValue);
                } else {
                    MultiMapSolrParams.addParam(paramName, paramValue, paramsMap);
                }
            }
        }
    }
    for (final Map.Entry<String, String> entry : repeatingParams.entrySet()) {
        final String paramName = entry.getKey();
        final String paramValue = entry.getValue();
        final int idx = paramName.lastIndexOf(".");
        MultiMapSolrParams.addParam(paramName.substring(0, idx), paramValue, paramsMap);
    }
    return paramsMap;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 64 with PropertyDescriptor

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

the class ITRedisStateProvider method setup.

@Before
public void setup() throws Exception {
    final int redisPort = getAvailablePort();
    this.redisServer = new RedisServer(redisPort);
    redisServer.start();
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(RedisUtils.CONNECTION_STRING, "localhost:" + redisPort);
    this.provider = createProvider(properties);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RedisServer(redis.embedded.RedisServer) Before(org.junit.Before)

Example 65 with PropertyDescriptor

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

Aggregations

PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)206 HashMap (java.util.HashMap)97 Test (org.junit.Test)67 Map (java.util.Map)57 ArrayList (java.util.ArrayList)49 HashSet (java.util.HashSet)24 IOException (java.io.IOException)23 Relationship (org.apache.nifi.processor.Relationship)22 ComponentLog (org.apache.nifi.logging.ComponentLog)21 LinkedHashMap (java.util.LinkedHashMap)20 ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)19 TestRunner (org.apache.nifi.util.TestRunner)19 ValidationResult (org.apache.nifi.components.ValidationResult)17 ProcessException (org.apache.nifi.processor.exception.ProcessException)17 FlowFile (org.apache.nifi.flowfile.FlowFile)16 LinkedHashSet (java.util.LinkedHashSet)15 BundleCoordinate (org.apache.nifi.bundle.BundleCoordinate)14 PropertyValue (org.apache.nifi.components.PropertyValue)14 URL (java.net.URL)13 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)13