From 2004ed2fdeaf1190cc8ac8ccb137909b17f07ba2 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Wed, 3 Aug 2016 20:44:04 -0400 Subject: [PATCH] NIFI-2474: Remove VariableRegistry from outward facing API so that the it is more flexible to evolve --- .../nifi/registry/VariableRegistry.java | 2 +- .../nifi/util/MockVariableRegistry.java | 43 +++++++++++++++++++ .../util/StandardProcessorTestRunner.java | 33 ++++++++++++-- .../java/org/apache/nifi/util/TestRunner.java | 31 +++++++++++++ .../org/apache/nifi/util/TestRunners.java | 8 +--- ...urrentTestStandardProcessorTestRunner.java | 3 +- .../util/TestStandardProcessorTestRunner.java | 14 ++++++ 7 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 nifi-mock/src/main/java/org/apache/nifi/util/MockVariableRegistry.java diff --git a/nifi-api/src/main/java/org/apache/nifi/registry/VariableRegistry.java b/nifi-api/src/main/java/org/apache/nifi/registry/VariableRegistry.java index d9ae4b15a5d8..6997faf45ba1 100644 --- a/nifi-api/src/main/java/org/apache/nifi/registry/VariableRegistry.java +++ b/nifi-api/src/main/java/org/apache/nifi/registry/VariableRegistry.java @@ -32,7 +32,7 @@ public interface VariableRegistry { * Returns an empty registry which can be used as a more intentional null * value. */ - public static final VariableRegistry EMPTY_REGISTRY = () -> Collections.EMPTY_MAP; + public static final VariableRegistry EMPTY_REGISTRY = () -> Collections.emptyMap(); /** * Provides a registry containing all environment variables and system diff --git a/nifi-mock/src/main/java/org/apache/nifi/util/MockVariableRegistry.java b/nifi-mock/src/main/java/org/apache/nifi/util/MockVariableRegistry.java new file mode 100644 index 000000000000..c782b4f40a3c --- /dev/null +++ b/nifi-mock/src/main/java/org/apache/nifi/util/MockVariableRegistry.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.util; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.nifi.registry.VariableDescriptor; +import org.apache.nifi.registry.VariableRegistry; + +public class MockVariableRegistry implements VariableRegistry { + + private final Map variables = new HashMap<>(); + + @Override + public Map getVariableMap() { + return Collections.unmodifiableMap(variables); + } + + public void setVariable(final VariableDescriptor descriptor, final String value) { + variables.put(descriptor, value); + } + + public String removeVariable(final VariableDescriptor descriptor) { + return variables.remove(descriptor); + } +} diff --git a/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java b/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java index 6607e85498e5..69118dbccb2a 100644 --- a/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java +++ b/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java @@ -33,6 +33,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; @@ -41,6 +42,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; + import org.apache.nifi.annotation.behavior.TriggerSerially; import org.apache.nifi.annotation.lifecycle.OnAdded; import org.apache.nifi.annotation.lifecycle.OnConfigurationRestored; @@ -65,7 +67,7 @@ import org.apache.nifi.processor.Processor; import org.apache.nifi.processor.Relationship; import org.apache.nifi.provenance.ProvenanceEventRecord; -import org.apache.nifi.registry.VariableRegistry; +import org.apache.nifi.registry.VariableDescriptor; import org.apache.nifi.reporting.InitializationException; import org.apache.nifi.state.MockStateManager; import org.junit.Assert; @@ -81,7 +83,7 @@ public class StandardProcessorTestRunner implements TestRunner { private final boolean triggerSerially; private final MockStateManager processorStateManager; private final Map controllerServiceStateManagers = new HashMap<>(); - private final VariableRegistry variableRegistry; + private final MockVariableRegistry variableRegistry; private int numThreads = 1; private final AtomicInteger invocations = new AtomicInteger(0); @@ -89,14 +91,14 @@ public class StandardProcessorTestRunner implements TestRunner { private final Map controllerServiceLoggers = new HashMap<>(); private final MockComponentLog logger; - StandardProcessorTestRunner(final Processor processor,final VariableRegistry variableRegistry) { + StandardProcessorTestRunner(final Processor processor) { this.processor = processor; this.idGenerator = new AtomicLong(0L); this.sharedState = new SharedSessionState(processor, idGenerator); this.flowFileQueue = sharedState.getFlowFileQueue(); this.sessionFactory = new MockSessionFactory(sharedState, processor); this.processorStateManager = new MockStateManager(processor); - this.variableRegistry = variableRegistry; + this.variableRegistry = new MockVariableRegistry(); this.context = new MockProcessContext(processor, processorStateManager, variableRegistry); final MockProcessorInitializationContext mockInitContext = new MockProcessorInitializationContext(processor, context); @@ -824,4 +826,27 @@ public void setClustered(boolean clustered) { public void setPrimaryNode(boolean primaryNode) { context.setPrimaryNode(primaryNode); } + + @Override + public String getVariableValue(final String name) { + Objects.requireNonNull(name); + + return variableRegistry.getVariableValue(name); + } + + @Override + public void setVariable(final String name, final String value) { + Objects.requireNonNull(name); + Objects.requireNonNull(value); + + final VariableDescriptor descriptor = new VariableDescriptor.Builder(name).build(); + variableRegistry.setVariable(descriptor, value); + } + + @Override + public String removeVariable(final String name) { + Objects.requireNonNull(name); + + return variableRegistry.removeVariable(new VariableDescriptor.Builder(name).build()); + } } diff --git a/nifi-mock/src/main/java/org/apache/nifi/util/TestRunner.java b/nifi-mock/src/main/java/org/apache/nifi/util/TestRunner.java index 023ef643594a..78d4d008f8e9 100644 --- a/nifi-mock/src/main/java/org/apache/nifi/util/TestRunner.java +++ b/nifi-mock/src/main/java/org/apache/nifi/util/TestRunner.java @@ -901,4 +901,35 @@ public interface TestRunner { * @param primaryNode Specify if this test emulates running as a primary node */ void setPrimaryNode(boolean primaryNode); + + /** + * Sets the value of the variable with the given name to be the given value. This exposes the variable + * for use by the Expression Language. + * + * @param name the name of the variable to set + * @param value the value of the variable + * + * @throws NullPointerException if either the name or the value is null + */ + void setVariable(String name, String value); + + /** + * Returns the current value of the variable with the given name + * + * @param name the name of the variable whose value should be returned. + * @return the current value of the variable with the given name or null if no value is currently set + * + * @throws NullPointerException if the name is null + */ + String getVariableValue(String name); + + /** + * Removes the variable with the given name from this Test Runner, if it is set. + * + * @param name the name of the variable to remove + * @return the value that was set for the variable, or null if the variable was not set + * + * @throws NullPointerException if the name is null + */ + String removeVariable(String name); } diff --git a/nifi-mock/src/main/java/org/apache/nifi/util/TestRunners.java b/nifi-mock/src/main/java/org/apache/nifi/util/TestRunners.java index 4b5fbd3c0d29..f473b05b84a0 100644 --- a/nifi-mock/src/main/java/org/apache/nifi/util/TestRunners.java +++ b/nifi-mock/src/main/java/org/apache/nifi/util/TestRunners.java @@ -17,22 +17,16 @@ package org.apache.nifi.util; import org.apache.nifi.processor.Processor; -import org.apache.nifi.registry.VariableRegistry; public class TestRunners { public static TestRunner newTestRunner(final Processor processor) { - return newTestRunner(processor,VariableRegistry.ENVIRONMENT_SYSTEM_REGISTRY); - } - - public static TestRunner newTestRunner(final Processor processor, VariableRegistry variableRegistry){ - return new StandardProcessorTestRunner(processor, variableRegistry); + return new StandardProcessorTestRunner(processor); } public static TestRunner newTestRunner(final Class processorClass) { try { return newTestRunner(processorClass.newInstance()); - } catch (final Exception e) { System.err.println("Could not instantiate instance of class " + processorClass.getName() + " due to: " + e); throw new RuntimeException(e); diff --git a/nifi-mock/src/test/java/org/apache/nifi/util/CurrentTestStandardProcessorTestRunner.java b/nifi-mock/src/test/java/org/apache/nifi/util/CurrentTestStandardProcessorTestRunner.java index 4c4302b8bfe5..6b403af25c21 100644 --- a/nifi-mock/src/test/java/org/apache/nifi/util/CurrentTestStandardProcessorTestRunner.java +++ b/nifi-mock/src/test/java/org/apache/nifi/util/CurrentTestStandardProcessorTestRunner.java @@ -20,7 +20,6 @@ import org.apache.nifi.processor.ProcessContext; import org.apache.nifi.processor.ProcessSession; import org.apache.nifi.processor.exception.ProcessException; -import org.apache.nifi.registry.VariableRegistry; import org.junit.Assert; import org.junit.Test; @@ -32,7 +31,7 @@ public class CurrentTestStandardProcessorTestRunner { @Test public void testOnScheduledCalledAfterRunFinished() { SlowRunProcessor processor = new SlowRunProcessor(); - StandardProcessorTestRunner runner = new StandardProcessorTestRunner(processor, VariableRegistry.ENVIRONMENT_SYSTEM_REGISTRY); + StandardProcessorTestRunner runner = new StandardProcessorTestRunner(processor); final int iterations = 5; runner.run(iterations); // if the counter is not equal to iterations, the the processor must have been unscheduled diff --git a/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java b/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java index f5b28aaf87c9..c65a7ba1231b 100644 --- a/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java +++ b/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java @@ -17,6 +17,7 @@ package org.apache.nifi.util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import java.util.Collections; import java.util.HashSet; @@ -110,6 +111,19 @@ public void testAllFlowFilesContainAttribute() { runner.assertAllFlowFilesContainAttribute(AddAttributeProcessor.KEY); } + @Test + public void testVariables() { + final AddAttributeProcessor proc = new AddAttributeProcessor(); + final TestRunner runner = TestRunners.newTestRunner(proc); + assertNull(runner.getVariableValue("hello")); + + runner.setVariable("hello", "world"); + assertEquals("world", runner.getVariableValue("hello")); + + assertEquals("world", runner.removeVariable("hello")); + assertNull(runner.getVariableValue("hello")); + } + private static class ProcessorWithOnStop extends AbstractProcessor { private int callsWithContext = 0;