Repository: nifi
Updated Branches:
  refs/heads/master 3943d72e9 -> 1db5e7310


NIFI-2474: Remove VariableRegistry from outward facing API so that the it is 
more flexible to evolve

Signed-off-by: Yolanda M. Davis <[email protected]>

This closes #782


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/1db5e731
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/1db5e731
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/1db5e731

Branch: refs/heads/master
Commit: 1db5e73102adf0704a8050a0837eda3ad9d5ca2d
Parents: 3943d72
Author: Mark Payne <[email protected]>
Authored: Wed Aug 3 20:44:04 2016 -0400
Committer: Yolanda M. Davis <[email protected]>
Committed: Thu Aug 4 10:11:33 2016 -0400

----------------------------------------------------------------------
 .../apache/nifi/registry/VariableRegistry.java  |  2 +-
 .../apache/nifi/util/MockVariableRegistry.java  | 43 ++++++++++++++++++++
 .../nifi/util/StandardProcessorTestRunner.java  | 33 +++++++++++++--
 .../java/org/apache/nifi/util/TestRunner.java   | 31 ++++++++++++++
 .../java/org/apache/nifi/util/TestRunners.java  |  8 +---
 .../CurrentTestStandardProcessorTestRunner.java |  3 +-
 .../util/TestStandardProcessorTestRunner.java   | 14 +++++++
 7 files changed, 120 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/1db5e731/nifi-api/src/main/java/org/apache/nifi/registry/VariableRegistry.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 d9ae4b1..6997faf 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

http://git-wip-us.apache.org/repos/asf/nifi/blob/1db5e731/nifi-mock/src/main/java/org/apache/nifi/util/MockVariableRegistry.java
----------------------------------------------------------------------
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 0000000..c782b4f
--- /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<VariableDescriptor, String> variables = new HashMap<>();
+
+    @Override
+    public Map<VariableDescriptor, String> 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);
+    }
+}

http://git-wip-us.apache.org/repos/asf/nifi/blob/1db5e731/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java
----------------------------------------------------------------------
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 6607e85..69118db 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.HashMap;
 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.Future;
 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.ProcessSessionFactory;
 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<String, MockStateManager> 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<String, MockComponentLog> 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 class StandardProcessorTestRunner implements 
TestRunner {
     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());
+    }
 }

http://git-wip-us.apache.org/repos/asf/nifi/blob/1db5e731/nifi-mock/src/main/java/org/apache/nifi/util/TestRunner.java
----------------------------------------------------------------------
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 023ef64..78d4d00 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 
<code>null</code> 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 <code>null</code> 
if the variable was not set
+     *
+     * @throws NullPointerException if the name is null
+     */
+    String removeVariable(String name);
 }

http://git-wip-us.apache.org/repos/asf/nifi/blob/1db5e731/nifi-mock/src/main/java/org/apache/nifi/util/TestRunners.java
----------------------------------------------------------------------
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 4b5fbd3..f473b05 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<? extends Processor> 
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);

http://git-wip-us.apache.org/repos/asf/nifi/blob/1db5e731/nifi-mock/src/test/java/org/apache/nifi/util/CurrentTestStandardProcessorTestRunner.java
----------------------------------------------------------------------
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 4c4302b..6b403af 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.AbstractProcessor;
 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

http://git-wip-us.apache.org/repos/asf/nifi/blob/1db5e731/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java
----------------------------------------------------------------------
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 f5b28aa..c65a7ba 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 class TestStandardProcessorTestRunner {
         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;

Reply via email to