NIFI-5436 Add ability to set the processor name in the ProcessContext when using the TestRunner.

This closes #2902.

Signed-off-by: Andy LoPresto <alopresto@apache.org>
This commit is contained in:
Otto Fowler 2018-07-17 11:26:32 -04:00 committed by Andy LoPresto
parent 8b9d446118
commit 3746ae258d
No known key found for this signature in database
GPG Key ID: 6EC293152D90B61D
5 changed files with 101 additions and 23 deletions

View File

@ -2144,9 +2144,10 @@ you can use the Maven dependency:
</dependency>
----
We create a new `TestRunner` by calling the static `newTestRunner` method of the `TestRunners` class
(located in the `org.apache.nifi.util` package). This method takes a single argument. That argument can
either be the class of the Processor to test or can be an instance of a Processor.
We create a new `TestRunner` by calling one of the static `newTestRunner` methods of the `TestRunners` class
(located in the `org.apache.nifi.util` package). These methods take an argument for the Processor under test (can
either be the class of the Processor to test or can be an instance of a Processor), and allow the setting of
the processor name as well.
=== Add ControllerServices

View File

@ -16,19 +16,6 @@
*/
package org.apache.nifi.util;
import static java.util.Objects.requireNonNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.apache.nifi.annotation.behavior.InputRequirement;
import org.apache.nifi.attribute.expression.language.Query;
import org.apache.nifi.attribute.expression.language.Query.Range;
@ -48,9 +35,23 @@ import org.apache.nifi.registry.VariableRegistry;
import org.apache.nifi.state.MockStateManager;
import org.junit.Assert;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import static java.util.Objects.requireNonNull;
public class MockProcessContext extends MockControllerServiceLookup implements SchedulingContext, ControllerServiceLookup, NodeTypeProvider {
private final ConfigurableComponent component;
private final String componentName;
private final Map<PropertyDescriptor, String> properties = new HashMap<>();
private final StateManager stateManager;
private final VariableRegistry variableRegistry;
@ -71,7 +72,11 @@ public class MockProcessContext extends MockControllerServiceLookup implements S
private volatile boolean isPrimaryNode;
public MockProcessContext(final ConfigurableComponent component) {
this(component, new MockStateManager(component), VariableRegistry.EMPTY_REGISTRY);
this(component, null);
}
public MockProcessContext(final ConfigurableComponent component, final String componentName) {
this(component, componentName, new MockStateManager(component), VariableRegistry.EMPTY_REGISTRY);
}
/**
@ -82,14 +87,41 @@ public class MockProcessContext extends MockControllerServiceLookup implements S
* @param variableRegistry variableRegistry
*/
public MockProcessContext(final ConfigurableComponent component, final StateManager stateManager, final VariableRegistry variableRegistry) {
this(component,null,stateManager,variableRegistry);
}
/**
* Creates a new MockProcessContext for the given Processor with given name
*
* @param component being mocked
* @param componentName the name to be given the component;
* @param stateManager state manager
* @param variableRegistry variableRegistry
*/
public MockProcessContext(final ConfigurableComponent component,
final String componentName,
final StateManager stateManager,
final VariableRegistry variableRegistry) {
this.component = Objects.requireNonNull(component);
this.componentName = componentName == null ? "" : componentName;
this.inputRequirement = component.getClass().getAnnotation(InputRequirement.class);
this.stateManager = stateManager;
this.variableRegistry = variableRegistry;
}
public MockProcessContext(final ControllerService component, final MockProcessContext context, final StateManager stateManager, final VariableRegistry variableRegistry) {
this(component, stateManager, variableRegistry);
public MockProcessContext(final ControllerService component,
final MockProcessContext context,
final StateManager stateManager,
final VariableRegistry variableRegistry) {
this(component, null, context, stateManager, variableRegistry);
}
public MockProcessContext(final ControllerService component,
final String componentName,
final MockProcessContext context,
final StateManager stateManager,
final VariableRegistry variableRegistry) {
this(component, componentName, stateManager, variableRegistry);
try {
annotationData = context.getControllerServiceAnnotationData(component);
@ -463,7 +495,7 @@ public class MockProcessContext extends MockControllerServiceLookup implements S
@Override
public String getName() {
return "";
return componentName;
}
protected void setMaxConcurrentTasks(int maxConcurrentTasks) {

View File

@ -95,6 +95,10 @@ public class StandardProcessorTestRunner implements TestRunner {
private boolean enforceReadStreamsClosed = true;
StandardProcessorTestRunner(final Processor processor) {
this(processor, null);
}
StandardProcessorTestRunner(final Processor processor, String processorName) {
this.processor = processor;
this.idGenerator = new AtomicLong(0L);
this.sharedState = new SharedSessionState(processor, idGenerator);
@ -102,7 +106,7 @@ public class StandardProcessorTestRunner implements TestRunner {
this.sessionFactory = new MockSessionFactory(sharedState, processor, enforceReadStreamsClosed);
this.processorStateManager = new MockStateManager(processor);
this.variableRegistry = new MockVariableRegistry();
this.context = new MockProcessContext(processor, processorStateManager, variableRegistry);
this.context = new MockProcessContext(processor, processorName, processorStateManager, variableRegistry);
final MockProcessorInitializationContext mockInitContext = new MockProcessorInitializationContext(processor, context);
processor.initialize(mockInitContext);

View File

@ -20,13 +20,47 @@ import org.apache.nifi.processor.Processor;
public class TestRunners {
/**
* Returns a {@code TestRunner} for the given {@code Processor}.
* The processor name available from {@code TestRunner.getProcessContext().getName()} will have the default name of {@code processor.getClass().getName()}
* @param processor the {@code Processor} under test
* @return a {@code TestRunner}
*/
public static TestRunner newTestRunner(final Processor processor) {
return new StandardProcessorTestRunner(processor);
return newTestRunner(processor,processor.getClass().getName());
}
/**
* Returns a {@code TestRunner} for the given {@code Processor}.
* The processor name available from {@code TestRunner.getProcessContext().getName()} will be the passed name.
* @param processor the {@code Processor} under test
* @param name the name to give the {@code Processor}
* @return a {@code TestRunner}
*/
public static TestRunner newTestRunner(final Processor processor, String name) {
return new StandardProcessorTestRunner(processor, name);
}
/**
* Returns a {@code TestRunner} for the given {@code Processor} class.
* The processor name available from {@code TestRunner.getProcessContext().getName()} will have the default name of {@code processor.getClass().getName()}
* @param processorClass the {@code Processor} class
* @return a {@code TestRunner}
*/
public static TestRunner newTestRunner(final Class<? extends Processor> processorClass) {
return newTestRunner(processorClass, processorClass.getName());
}
/**
* Returns a {@code TestRunner} for the given {@code Processor} class.
* The processor name available from {@code TestRunner.getProcessContext().getName()} will have the default name of {@code processor.getClass().getName()}
* @param processorClass the {@code Processor} class
* @param name the name to give the {@code Processor}
* @return a {@code TestRunner}
*/
public static TestRunner newTestRunner(final Class<? extends Processor> processorClass, String name) {
try {
return newTestRunner(processorClass.newInstance());
return newTestRunner(processorClass.newInstance(), name);
} catch (final Exception e) {
System.err.println("Could not instantiate instance of class " + processorClass.getName() + " due to: " + e);
throw new RuntimeException(e);

View File

@ -207,6 +207,13 @@ public class TestStandardProcessorTestRunner {
assertTrue("onPropertyModified has not been called", ((SimpleTestService) testService).isOpmCalled());
}
@Test
public void testProcessorNameShouldBeSet() {
final AddAttributeProcessor proc = new AddAttributeProcessor();
final TestRunner runner = TestRunners.newTestRunner(proc, "TestName");
assertEquals("TestName",runner.getProcessContext().getName());
}
@Test
public void testProcessorInvalidWhenControllerServiceDisabled() {
final ControllerService testService = new RequiredPropertyTestService();