Search in sources :

Example 21 with PropertyDescriptor

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

the class StandardProcessorTestRunner method addControllerService.

@Override
public void addControllerService(final String identifier, final ControllerService service, final Map<String, String> properties) throws InitializationException {
    final MockComponentLog logger = new MockComponentLog(identifier, service);
    controllerServiceLoggers.put(identifier, logger);
    final MockStateManager serviceStateManager = new MockStateManager(service);
    final MockControllerServiceInitializationContext initContext = new MockControllerServiceInitializationContext(requireNonNull(service), requireNonNull(identifier), logger, serviceStateManager);
    controllerServiceStateManagers.put(identifier, serviceStateManager);
    initContext.addControllerServices(context);
    service.initialize(initContext);
    final Map<PropertyDescriptor, String> resolvedProps = new HashMap<>();
    for (final Map.Entry<String, String> entry : properties.entrySet()) {
        resolvedProps.put(service.getPropertyDescriptor(entry.getKey()), entry.getValue());
    }
    try {
        ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, service);
    } catch (final InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
        throw new InitializationException(e);
    }
    context.addControllerService(identifier, service, resolvedProps, null);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) InitializationException(org.apache.nifi.reporting.InitializationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MockStateManager(org.apache.nifi.state.MockStateManager) Map(java.util.Map) HashMap(java.util.HashMap)

Example 22 with PropertyDescriptor

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

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

the class TestStandardProcessorTestRunner method testControllerServiceUpdateShouldCallOnSetProperty.

@Test
public void testControllerServiceUpdateShouldCallOnSetProperty() {
    // Arrange
    final ControllerService testService = new SimpleTestService();
    final AddAttributeProcessor proc = new AddAttributeProcessor();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    final String serviceIdentifier = "test";
    final String pdName = "name";
    final String pdValue = "exampleName";
    try {
        runner.addControllerService(serviceIdentifier, testService);
    } catch (InitializationException e) {
        fail(e.getMessage());
    }
    assertFalse("onPropertyModified has been called", ((SimpleTestService) testService).isOpmCalled());
    // Act
    ValidationResult vr = runner.setProperty(testService, pdName, pdValue);
    // Assert
    assertTrue(vr.isValid());
    ControllerServiceConfiguration csConf = ((MockProcessContext) runner.getProcessContext()).getConfiguration(serviceIdentifier);
    PropertyDescriptor propertyDescriptor = testService.getPropertyDescriptor(pdName);
    String retrievedPDValue = csConf.getProperties().get(propertyDescriptor);
    assertEquals(pdValue, retrievedPDValue);
    assertTrue("onPropertyModified has not been called", ((SimpleTestService) testService).isOpmCalled());
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) InitializationException(org.apache.nifi.reporting.InitializationException) ValidationResult(org.apache.nifi.components.ValidationResult) AbstractControllerService(org.apache.nifi.controller.AbstractControllerService) ControllerService(org.apache.nifi.controller.ControllerService) Test(org.junit.Test)

Example 24 with PropertyDescriptor

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

the class TestCredentialsProviderFactory method testAssumeRoleCredentials.

@Test
public void testAssumeRoleCredentials() throws Throwable {
    final TestRunner runner = TestRunners.newTestRunner(MockAWSProcessor.class);
    runner.setProperty(CredentialPropertyDescriptors.CREDENTIALS_FILE, "src/test/resources/mock-aws-credentials.properties");
    runner.setProperty(CredentialPropertyDescriptors.ASSUME_ROLE_ARN, "BogusArn");
    runner.setProperty(CredentialPropertyDescriptors.ASSUME_ROLE_NAME, "BogusSession");
    runner.assertValid();
    Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties();
    final CredentialsProviderFactory factory = new CredentialsProviderFactory();
    final AWSCredentialsProvider credentialsProvider = factory.getCredentialsProvider(properties);
    Assert.assertNotNull(credentialsProvider);
    assertEquals("credentials provider should be equal", STSAssumeRoleSessionCredentialsProvider.class, credentialsProvider.getClass());
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) TestRunner(org.apache.nifi.util.TestRunner) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) Test(org.junit.Test)

Example 25 with PropertyDescriptor

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

the class TestCredentialsProviderFactory method testAnonymousCredentials.

@Test
public void testAnonymousCredentials() throws Throwable {
    final TestRunner runner = TestRunners.newTestRunner(MockAWSProcessor.class);
    runner.setProperty(CredentialPropertyDescriptors.USE_ANONYMOUS_CREDENTIALS, "true");
    runner.assertValid();
    Map<PropertyDescriptor, String> properties = runner.getProcessContext().getProperties();
    final CredentialsProviderFactory factory = new CredentialsProviderFactory();
    final AWSCredentialsProvider credentialsProvider = factory.getCredentialsProvider(properties);
    Assert.assertNotNull(credentialsProvider);
    final AWSCredentials creds = credentialsProvider.getCredentials();
    assertEquals("credentials should be equal", AnonymousAWSCredentials.class, creds.getClass());
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) TestRunner(org.apache.nifi.util.TestRunner) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) 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