Search in sources :

Example 66 with PropertyDescriptor

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

the class TestServerAndClient method testClientTermination.

@Test
public void testClientTermination() throws InitializationException, IOException, InterruptedException {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final MockControllerServiceInitializationContext serverInitContext = new MockControllerServiceInitializationContext(server, "server");
    server.initialize(serverInitContext);
    final Map<PropertyDescriptor, String> serverProperties = new HashMap<>();
    final MockConfigurationContext serverContext = new MockConfigurationContext(serverProperties, serverInitContext.getControllerServiceLookup());
    server.startServer(serverContext);
    DistributedMapCacheClientService client = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext = new MockControllerServiceInitializationContext(client, "client");
    client.initialize(clientInitContext);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext = new MockConfigurationContext(clientProperties, clientInitContext.getControllerServiceLookup());
    client.cacheConfig(clientContext);
    final Serializer<String> valueSerializer = new StringSerializer();
    final Serializer<String> keySerializer = new StringSerializer();
    final Deserializer<String> deserializer = new StringDeserializer();
    final String original = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals(null, original);
    final boolean contains = client.containsKey("testKey", keySerializer);
    assertTrue(contains);
    final boolean added = client.putIfAbsent("testKey", "test", keySerializer, valueSerializer);
    assertFalse(added);
    final String originalAfterPut = client.getAndPutIfAbsent("testKey", "test", keySerializer, valueSerializer, deserializer);
    assertEquals("test", originalAfterPut);
    final boolean removed = client.remove("testKey", keySerializer);
    assertTrue(removed);
    final boolean containedAfterRemove = client.containsKey("testKey", keySerializer);
    assertFalse(containedAfterRemove);
    client = null;
    clientInitContext = null;
    clientContext = null;
    Thread.sleep(2000);
    System.gc();
    server.shutdownServer();
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Test(org.junit.Test)

Example 67 with PropertyDescriptor

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

the class TestServerAndClient method testOptimisticLock.

@Test
public void testOptimisticLock() throws Exception {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437", SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);
    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final DistributedMapCacheServer server = new MapServer();
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    runner.addControllerService("server", server);
    runner.enableControllerService(server);
    DistributedMapCacheClientService client1 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext1 = new MockControllerServiceInitializationContext(client1, "client1");
    client1.initialize(clientInitContext1);
    DistributedMapCacheClientService client2 = new DistributedMapCacheClientService();
    MockControllerServiceInitializationContext clientInitContext2 = new MockControllerServiceInitializationContext(client2, "client2");
    client1.initialize(clientInitContext2);
    final Map<PropertyDescriptor, String> clientProperties = new HashMap<>();
    clientProperties.put(DistributedMapCacheClientService.HOSTNAME, "localhost");
    clientProperties.put(DistributedMapCacheClientService.PORT, String.valueOf(server.getPort()));
    clientProperties.put(DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "360 secs");
    MockConfigurationContext clientContext1 = new MockConfigurationContext(clientProperties, clientInitContext1.getControllerServiceLookup());
    client1.cacheConfig(clientContext1);
    MockConfigurationContext clientContext2 = new MockConfigurationContext(clientProperties, clientInitContext2.getControllerServiceLookup());
    client2.cacheConfig(clientContext2);
    final Serializer<String> stringSerializer = new StringSerializer();
    final Deserializer<String> stringDeserializer = new StringDeserializer();
    final String key = "test-optimistic-lock";
    // Ensure there's no existing key
    assertFalse(client1.containsKey(key, stringSerializer));
    assertNull(client1.fetch(key, stringSerializer, stringDeserializer));
    // Client 1 inserts the key.
    client1.put(key, "valueC1-0", stringSerializer, stringSerializer);
    // Client 1 and 2 fetch the key
    AtomicCacheEntry<String, String, Long> c1 = client1.fetch(key, stringSerializer, stringDeserializer);
    AtomicCacheEntry<String, String, Long> c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals(new Long(0), c1.getRevision().orElse(0L));
    assertEquals("valueC1-0", c1.getValue());
    assertEquals(new Long(0), c2.getRevision().orElse(0L));
    assertEquals("valueC1-0", c2.getValue());
    // Client 1 replace
    c1.setValue("valueC1-1");
    boolean c1Result = client1.replace(c1, stringSerializer, stringSerializer);
    assertTrue("C1 should be able to replace the key", c1Result);
    // Client 2 replace with the old revision
    c2.setValue("valueC2-1");
    boolean c2Result = client2.replace(c2, stringSerializer, stringSerializer);
    assertFalse("C2 shouldn't be able to replace the key", c2Result);
    // Client 2 fetch the key again
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC1-1", c2.getValue());
    assertEquals(new Long(1), c2.getRevision().orElse(0L));
    // Now, Client 2 knows the correct revision so it can replace the key
    c2.setValue("valueC2-2");
    c2Result = client2.replace(c2, stringSerializer, stringSerializer);
    assertTrue("C2 should be able to replace the key", c2Result);
    // Assert the cache
    c2 = client2.fetch(key, stringSerializer, stringDeserializer);
    assertEquals("valueC2-2", c2.getValue());
    assertEquals(new Long(2), c2.getRevision().orElse(0L));
    client1.close();
    client2.close();
    server.shutdownServer();
}
Also used : Processor(org.apache.nifi.processor.Processor) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) DistributedMapCacheServer(org.apache.nifi.distributed.cache.server.map.DistributedMapCacheServer) DistributedMapCacheClientService(org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService) MockControllerServiceInitializationContext(org.apache.nifi.util.MockControllerServiceInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) Test(org.junit.Test)

Example 68 with PropertyDescriptor

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

the class SchemaRegistryService method storeSchemaAccessStrategy.

@OnEnabled
public void storeSchemaAccessStrategy(final ConfigurationContext context) {
    this.configurationContext = context;
    final SchemaRegistry schemaRegistry = context.getProperty(SCHEMA_REGISTRY).asControllerService(SchemaRegistry.class);
    final PropertyDescriptor descriptor = getSchemaAcessStrategyDescriptor();
    final String schemaAccess = context.getProperty(descriptor).getValue();
    this.schemaAccessStrategy = getSchemaAccessStrategy(schemaAccess, schemaRegistry, context);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) SchemaRegistry(org.apache.nifi.schemaregistry.services.SchemaRegistry) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 69 with PropertyDescriptor

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

the class UpdateAttribute method getDefaultActions.

// Gets the default actions.
private Map<String, Action> getDefaultActions(final Map<PropertyDescriptor, String> properties) {
    final Map<String, Action> defaultActions = new HashMap<>();
    for (final Map.Entry<PropertyDescriptor, String> entry : properties.entrySet()) {
        if (entry.getKey() != STORE_STATE && entry.getKey() != STATEFUL_VARIABLES_INIT_VALUE) {
            final Action action = new Action();
            action.setAttribute(entry.getKey().getName());
            action.setValue(entry.getValue());
            defaultActions.put(action.getAttribute(), action);
        }
    }
    return defaultActions;
}
Also used : Action(org.apache.nifi.update.attributes.Action) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StateMap(org.apache.nifi.components.state.StateMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 70 with PropertyDescriptor

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

the class EmailNotificationService method getMailProperties.

/**
 * Uses the mapping of javax.mail properties to NiFi PropertyDescriptors to build the required Properties object to be used for sending this email
 *
 * @param context context
 * @return mail properties
 */
private Properties getMailProperties(final NotificationContext context) {
    final Properties properties = new Properties();
    for (Entry<String, PropertyDescriptor> entry : propertyToContext.entrySet()) {
        // Evaluate the property descriptor against the flow file
        String property = entry.getKey();
        String propValue = context.getProperty(entry.getValue()).evaluateAttributeExpressions().getValue();
        // Nullable values are not allowed, so filter out
        if (null != propValue) {
            properties.setProperty(property, propValue);
        }
    }
    return properties;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) Properties(java.util.Properties)

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