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();
}
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();
}
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);
}
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;
}
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;
}
Aggregations