NIFI-3269 Added call to onPropertyModified when updating controller services in StandardProcessorTestRunner.

Added unit test to demonstrate change.

This closes #1374.

Signed-off-by: Andy LoPresto <alopresto@apache.org>
This commit is contained in:
Pierre Villard 2016-12-30 12:36:11 +01:00 committed by Andy LoPresto
parent 2c0f1c348e
commit 41189b055f
No known key found for this signature in database
GPG Key ID: 3C6EF65B2F7DEF69
2 changed files with 81 additions and 10 deletions

View File

@ -754,9 +754,14 @@ public class StandardProcessorTestRunner implements TestRunner {
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;
}

View File

@ -17,16 +17,24 @@
package org.apache.nifi.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.nifi.annotation.lifecycle.OnStopped;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.controller.ControllerService;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.AbstractProcessor;
import org.apache.nifi.processor.ProcessContext;
@ -34,6 +42,7 @@ import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.ProcessorInitializationContext;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.reporting.InitializationException;
import org.junit.Test;
public class TestStandardProcessorTestRunner {
@ -167,6 +176,37 @@ public class TestStandardProcessorTestRunner {
assertNull(runner.getVariableValue("hello"));
}
@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());
}
private static class ProcessorWithOnStop extends AbstractProcessor {
private int callsWithContext = 0;
@ -264,4 +304,30 @@ public class TestStandardProcessorTestRunner {
}
}
}
private static class SimpleTestService extends AbstractControllerService {
private final String PD_NAME = "name";
private PropertyDescriptor namePropertyDescriptor = new PropertyDescriptor.Builder()
.name(PD_NAME)
.displayName("Controller Service Name")
.required(false)
.sensitive(false)
.allowableValues("exampleName", "anotherExampleName")
.build();
private boolean opmCalled = false;
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return Arrays.asList(namePropertyDescriptor);
}
public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) {
getLogger().info("onPropertyModified called for PD {} with old value {} and new value {}", new Object[]{descriptor.getName(), oldValue, newValue});
opmCalled = true;
}
public boolean isOpmCalled() {
return opmCalled;
}
}
}