This is an automated email from the ASF dual-hosted git repository. pvillard pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nifi.git
commit 91e9e65a5c302eac324bcd3a9a84c347238e3649 Author: Matthew Burgess <[email protected]> AuthorDate: Fri Jan 10 10:25:16 2020 -0500 NIFI-6543: Added unit tests for lifecycle methods Signed-off-by: Pierre Villard <[email protected]> This closes #3974. --- .../additionalDetails.html | 6 +++- .../groovyx/ExecuteGroovyScriptTest.java | 36 ++++++++++++++++++++ .../resources/groovy/test_onStart_onStop.groovy | 39 ++++++++++++++++++++++ .../resources/groovy/test_onUnscheduled.groovy | 22 ++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) diff --git a/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/main/resources/docs/org.apache.nifi.processors.groovyx.ExecuteGroovyScript/additionalDetails.html b/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/main/resources/docs/org.apache.nifi.processors.groovyx.ExecuteGroovyScript/additionalDetails.html index 5239aac..5282aec 100644 --- a/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/main/resources/docs/org.apache.nifi.processors.groovyx.ExecuteGroovyScript/additionalDetails.html +++ b/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/main/resources/docs/org.apache.nifi.processors.groovyx.ExecuteGroovyScript/additionalDetails.html @@ -182,7 +182,7 @@ then you can access it from code <code>SQL.mydb.rows('select * from mytable')</c <h2>Handling processor start & stop</h2> -<p>In the extended groovy processor you can catch `start` and `stop` events by providing corresponding static methods:</p> +<p>In the extended groovy processor you can catch `start` and `stop` and `unscheduled` events by providing corresponding static methods:</p> <pre> import org.apache.nifi.processor.ProcessContext import java.util.concurrent.atomic.AtomicLong @@ -203,6 +203,10 @@ then you can access it from code <code>SQL.mydb.rows('select * from mytable')</c println "onStop $context executed ${ Const.triggerCount } times during ${ alive } seconds" } + static onUnscheduled(ProcessContext context){ + def alive = (System.currentTimeMillis() - Const.startTime.getTime()) / 1000 + println "onUnscheduled $context executed ${ Const.triggerCount } times during ${ alive } seconds" + } flowFile.'trigger.count' = Const.triggerCount.incrementAndGet() REL_SUCCESS << flowFile diff --git a/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScriptTest.java b/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScriptTest.java index ddba34a..81d1857 100644 --- a/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScriptTest.java +++ b/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/java/org/apache/nifi/processors/groovyx/ExecuteGroovyScriptTest.java @@ -40,9 +40,11 @@ import org.junit.Test; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -53,6 +55,7 @@ import java.sql.DriverManager; import java.sql.Connection; import java.sql.Statement; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import org.apache.nifi.controller.AbstractControllerService; @@ -471,6 +474,39 @@ public class ExecuteGroovyScriptTest { flowFile.assertContentEquals("5678".getBytes(StandardCharsets.UTF_16LE)); } + @Test + public void test_onStart_onStop() throws Exception { + runner.setProperty(ExecuteGroovyScript.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "test_onStart_onStop.groovy"); + runner.assertValid(); + runner.enqueue(""); + final PrintStream originalOut = System.out; + final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExecuteGroovyScript.REL_SUCCESS.getName(), 1); + final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteGroovyScript.REL_SUCCESS.getName()); + MockFlowFile resultFile = result.get(0); + resultFile.assertAttributeExists("a"); + resultFile.assertAttributeEquals("a", "A"); + System.setOut(originalOut); + assertEquals("onStop invoked successfully\n", outContent.toString()); + + // Inspect the output visually for onStop, no way to pass back values + } + + @Test + public void test_onUnscheduled() throws Exception { + runner.setProperty(ExecuteGroovyScript.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "test_onUnscheduled.groovy"); + runner.assertValid(); + final PrintStream originalOut = System.out; + final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + runner.run(); + System.setOut(originalOut); + assertEquals("onUnscheduled invoked successfully\n", outContent.toString()); + } + private HashMap<String, String> map(String key, String value) { HashMap<String, String> attrs = new HashMap<>(); diff --git a/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/resources/groovy/test_onStart_onStop.groovy b/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/resources/groovy/test_onStart_onStop.groovy new file mode 100644 index 0000000..1a22b1f --- /dev/null +++ b/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/resources/groovy/test_onStart_onStop.groovy @@ -0,0 +1,39 @@ +/* + * 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. + */ + +import org.apache.nifi.processor.ProcessContext + + +class OnStart { + static String a = null +} + +static onStart(ProcessContext context){ + OnStart.a = 'A' +} + +static onStop(ProcessContext context){ + println 'onStop invoked successfully' +} + +def flowFile = session.get() +if (!flowFile) { + return +} + +flowFile.'a' = OnStart.a +REL_SUCCESS << flowFile \ No newline at end of file diff --git a/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/resources/groovy/test_onUnscheduled.groovy b/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/resources/groovy/test_onUnscheduled.groovy new file mode 100644 index 0000000..557aefa --- /dev/null +++ b/nifi-nar-bundles/nifi-groovyx-bundle/nifi-groovyx-processors/src/test/resources/groovy/test_onUnscheduled.groovy @@ -0,0 +1,22 @@ +/* + * 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. + */ + +import org.apache.nifi.processor.ProcessContext + +static onUnscheduled(ProcessContext context){ + println 'onUnscheduled invoked successfully' +} \ No newline at end of file
