org.apache.nifi.script.ScriptingComponentUtils Java Examples

The following examples show how to use org.apache.nifi.script.ScriptingComponentUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestExecuteJavascript.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that has provides the body of an onTrigger() function.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttribute() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/javascript/test_onTrigger.js");
    // Use basic manipulation to validate that EL is working
    runner.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/${literal('JAVASCRIPT'):toLower()}");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship("success");
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #2
Source File: TestProperties.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading values from nifi.properties
 * @throws Exception
 */
@Test
public void testPropertiesJavascript() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    System.setProperty(NiFiProperties.PROPERTIES_FILE_PATH, "src/test/resources/executescript/properties/nifi.properties");
    NiFiPropertiesLoader nifiPropertiesLoader = new NiFiPropertiesLoader();
    NiFiProperties nifiProperties = nifiPropertiesLoader.get();

    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/properties/properties.js");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("property-name", "nifi.version");
    runner.enqueue("sample text".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship("success");
    MockFlowFile result = successFlowFiles.get(0);
    result.assertAttributeEquals("property-value", nifiProperties.getProperty("nifi.version"));
}
 
Example #3
Source File: TestParseUri.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Parses domain from url
 * @throws Exception
 */
@Test
public void testParseUriSimple() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/misc/parse_uri.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("input.url", "http://batchiq.com/foobar");
    runner.enqueue("nothing".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship("success");
    MockFlowFile result = successFlowFiles.get(0);
    result.assertAttributeEquals("url.protocol", "http");
    result.assertAttributeEquals("url.host", "batchiq.com");
    result.assertAttributeEquals("url.path", "/foobar");
    result.assertAttributeEquals("input.url", "http://batchiq.com/foobar");
}
 
Example #4
Source File: TestParseUri.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseUriFail() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/misc/parse_uri.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("input.url", "total_bogus");
    runner.enqueue("nothing".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run();

    runner.assertAllFlowFilesTransferred("failure", 1);
    final List<MockFlowFile> failureFlowFiles = runner.getFlowFilesForRelationship("failure");
    MockFlowFile result = failureFlowFiles.get(0);
    result.assertAttributeNotExists("uri.scheme");
    result.assertAttributeNotExists("uri.host");
    result.assertAttributeNotExists("uri.path");
    result.assertAttributeEquals("parse_url.error", "no protocol: total_bogus");
    result.assertAttributeEquals("input.url", "total_bogus");
}
 
Example #5
Source File: TestDateTime.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates applying date/time parsing and formatting.
 * @throws Exception
 */
@Test
public void testDateTimeTransformGroovy() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/content/datetime.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();
    String inputDateTime = "2018/12/23 10:01:23";

    runner.enqueue(inputDateTime);
    runner.run();

    List<MockFlowFile> failedFlowFiles = runner.getFlowFilesForRelationship(ExecuteScript.REL_FAILURE);
    Assert.assertEquals(0, failedFlowFiles.size());

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship("success");
    MockFlowFile result = successFlowFiles.get(0);
    byte[] flowFileBytes = result.toByteArray();
    String outputDateTime = new String(flowFileBytes);
    Assert.assertEquals("2018/12/23 05:01:23", outputDateTime);
}
 
Example #6
Source File: TestDateTime.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates applying date/time parsing and formatting.
 * @throws Exception
 */
@Test
public void testDateTimeTransformJavascript() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/content/datetime.js");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();
    String inputDateTime = "2018/12/23 10:01:23";

    runner.enqueue(inputDateTime);
    runner.run();

    List<MockFlowFile> failedFlowFiles = runner.getFlowFilesForRelationship(ExecuteScript.REL_FAILURE);
    Assert.assertEquals(0, failedFlowFiles.size());

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship("success");
    MockFlowFile result = successFlowFiles.get(0);
    byte[] flowFileBytes = result.toByteArray();
    String outputDateTime = new String(flowFileBytes);
    Assert.assertEquals("2018/12/23 05:01:23", outputDateTime);
}
 
Example #7
Source File: TestXmlToJson.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
private void runOutputComparisonTest(String inputResource, String expectedResource) throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/content/xml-to-json/xmlToJson.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.setProperty("prettyPrintJson", "true");
    runner.assertValid();

    Path inputXmlPath = Paths.get(inputResource);
    runner.enqueue(inputXmlPath);
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship("success");
    MockFlowFile result = successFlowFiles.get(0);
    byte[] flowFileBytes = result.toByteArray();

    String actual = new String(flowFileBytes);
    String expected = getFileContentsAsString(expectedResource);
    Assert.assertEquals(expected, actual);
}
 
Example #8
Source File: TestCounter.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates writing to counters
 * @throws Exception
 */
@Test
public void testCounter() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "python");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/counter/counter.py");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    runner.enqueue("sample text".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    double counterValue = runner.getCounterValue("SampleScriptCounter");
    Assert.assertEquals(1d, counterValue, 0.01d);
}
 
Example #9
Source File: TestAttributes.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing FlowFile attributes from within scripts
 * @throws Exception
 */
@Test
public void testAttributeAccessGroovy() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/attributes/attributes.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("greeting", "Hello");
    runner.enqueue("nothing".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship("success");
    MockFlowFile result = successFlowFiles.get(0);
    result.assertAttributeEquals("message", "Hello, Script!");
    result.assertAttributeEquals("attribute.one", "true");
    result.assertAttributeEquals("attribute.two", "2");
}
 
Example #10
Source File: TestInvokeJython.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Test a script that has a Jython processor that reads a value from a processor property and another from a flowfile attribute then stores both in the attributes of the flowfile being routed.
 * <p>
 * This may seem contrived but it verifies that the Jython processors properties are being considered and are able to be set and validated. It verifies the processor is able to access the property
 * values and flowfile attribute values during onTrigger. Lastly, it verifies the processor is able to route the flowfile to a relationship it specified.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testUpdateAttributeFromProcessorPropertyAndFlowFileAttribute() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new InvokeScriptedProcessor());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "python");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/jython/test_update_attribute.py");
    runner.setProperty("for-attributes", "value-1");

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("for-attributes", "value-2");

    runner.assertValid();
    runner.enqueue(new byte[0], attributes);
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship("success");

    // verify reading a property value
    result.get(0).assertAttributeEquals("from-property", "value-1");

    // verify reading an attribute value
    result.get(0).assertAttributeEquals("from-attribute", "value-2");
}
 
Example #11
Source File: TestInvokeGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that derive from AbstractProcessor as base class
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testAbstractProcessorImplementationWithBodyScriptFile() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_BODY, getFileContentsAsString(TEST_RESOURCE_LOCATION + "groovy/test_implementingabstractProcessor.groovy"));
    runner.setProperty(ScriptingComponentUtils.MODULES, TEST_RESOURCE_LOCATION + "groovy");
    runner.setProperty("custom_prop", "bla bla");

    runner.assertValid();
    runner.enqueue("test".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship("success");
    assertTrue(result.size() == 1);
    final String expectedOutput = new String(Hex.encodeHex(MessageDigest.getInstance("MD5").digest("testbla bla".getBytes())));
    final MockFlowFile outputFlowFile = result.get(0);
    outputFlowFile.assertContentEquals(expectedOutput);
    outputFlowFile.assertAttributeEquals("outAttr", expectedOutput);
}
 
Example #12
Source File: BaseScriptedLookupService.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Handles changes to this processor's properties. If changes are made to
 * script- or engine-related properties, the script will be reloaded.
 *
 * @param descriptor of the modified property
 * @param oldValue   non-null property value (previous)
 * @param newValue   the new property value or if null indicates the property
 */
@Override
public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) {
    final ComponentLog logger = getLogger();
    final ConfigurableComponent instance = lookupService.get();

    if (ScriptingComponentUtils.SCRIPT_FILE.equals(descriptor)
            || ScriptingComponentUtils.SCRIPT_BODY.equals(descriptor)
            || ScriptingComponentUtils.MODULES.equals(descriptor)
            || scriptingComponentHelper.SCRIPT_ENGINE.equals(descriptor)) {
        scriptNeedsReload.set(true);
        // Need to reset scriptEngine if the value has changed
        if (scriptingComponentHelper.SCRIPT_ENGINE.equals(descriptor)) {
            scriptEngine = null;
        }
    } else if (instance != null) {
        // If the script provides a ConfigurableComponent, call its onPropertyModified() method
        try {
            instance.onPropertyModified(descriptor, oldValue, newValue);
        } catch (final Exception e) {
            final String message = "Unable to invoke onPropertyModified from scripted LookupService: " + e;
            logger.error(message, e);
        }
    }
}
 
Example #13
Source File: TestExecuteJRuby.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that has provides the body of an onTrigger() function.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttribute() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "ruby");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/jruby/test_onTrigger.rb");
    runner.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/jruby");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship("success");
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #14
Source File: TestInvokeJavascript.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a scripted processor written in Javascript that reads the first line of text from the flowfiles content
 * and stores the value in an attribute of the outgoing flowfile.
 * Confirms that the scripted processor transfers the incoming flowfile with an attribute added.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttribute() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/javascript/test_reader.js");
    runner.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/javascript");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("test", 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship("test");
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #15
Source File: TestInvokeJavascript.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that routes the FlowFile to failure.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testScriptRoutesToFailure() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_BODY, getFileContentsAsString(
            TEST_RESOURCE_LOCATION + "javascript/testScriptRoutesToFailure.js")
    );
    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("FAILURE", 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship("FAILURE");
    assertFalse(result.isEmpty());
}
 
Example #16
Source File: InvokeScriptedProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Handles changes to this processor's properties. If changes are made to
 * script- or engine-related properties, the script will be reloaded.
 *
 * @param descriptor of the modified property
 * @param oldValue non-null property value (previous)
 * @param newValue the new property value or if null indicates the property
 */
@Override
public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) {

    validationResults.set(new HashSet<>());

    final ComponentLog logger = getLogger();
    final Processor instance = processor.get();

    if (ScriptingComponentUtils.SCRIPT_FILE.equals(descriptor)
            || ScriptingComponentUtils.SCRIPT_BODY.equals(descriptor)
            || ScriptingComponentUtils.MODULES.equals(descriptor)
            || scriptingComponentHelper.SCRIPT_ENGINE.equals(descriptor)) {
        scriptNeedsReload.set(true);
        scriptEngine = null; //reset engine. This happens only when a processor is stopped, so there won't be any performance impact in run-time.
    } else if (instance != null) {
        // If the script provides a Processor, call its onPropertyModified() method
        try {
            instance.onPropertyModified(descriptor, oldValue, newValue);
        } catch (final Exception e) {
            final String message = "Unable to invoke onPropertyModified from script Processor: " + e;
            logger.error(message, e);
        }
    }
}
 
Example #17
Source File: TestExecuteJython.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a Jython script that has provides the body of an onTrigger() function.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttributeWithScriptBody() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "python");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_BODY,
            "from org.apache.nifi.processors.script import ExecuteScript\n"
                    + "flowFile = session.get()\n"
                    + "flowFile = session.putAttribute(flowFile, \"from-content\", \"test content\")\n"
                    + "session.transfer(flowFile, ExecuteScript.REL_SUCCESS)");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #18
Source File: TestExecuteLua.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that has provides the body of an onTrigger() function.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttribute() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "lua");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/lua/test_onTrigger.lua");
    runner.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/lua");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #19
Source File: TestExecuteClojure.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script file that has provides the body of an onTrigger() function.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttributeWithScriptFile() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Clojure");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "clojure/test_onTrigger.clj");
    runner.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/clojure");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #20
Source File: TestExecuteClojure.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script file that creates and transfers a new flow file.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testCreateNewFlowFileWithScriptFile() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Clojure");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "clojure/test_onTrigger_newFlowFile.clj");
    runner.setProperty(ScriptingComponentUtils.MODULES, TEST_RESOURCE_LOCATION + "clojure");

    runner.assertValid();
    runner.enqueue(TEST_CSV_DATA.getBytes(StandardCharsets.UTF_8));
    runner.run();

    // The script removes the original file and transfers only the new one
    assertEquals(1, runner.getRemovedCount());
    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("selected.columns", "title,first");
    result.get(0).assertAttributeEquals("filename", "split_cols.txt");
}
 
Example #21
Source File: TestExecuteClojure.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script file that uses dynamic properties defined on the processor.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testDynamicProperties() throws Exception {
    runner.setValidateExpressionUsage(true);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Clojure");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "clojure/test_dynamicProperties.clj");
    runner.setProperty("myProp", "${myAttr}");

    runner.assertValid();
    runner.enqueue(TEST_CSV_DATA.getBytes(StandardCharsets.UTF_8),
            new HashMap<String, String>(1) {{
                put("myAttr", "testValue");
            }});
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "testValue");
}
 
Example #22
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script file that has provides the body of an onTrigger() function.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttributeWithScriptFile() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "groovy/test_onTrigger.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/groovy");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #23
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that throws an Exception within. The expected result is that the flow file is rolled back
 * and penalized. Besides we check that we yielded the processor.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testScriptException() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_BODY, getFileContentsAsString(TEST_RESOURCE_LOCATION + "groovy/testScriptException.groovy"));

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    try {
        runner.run();
        fail();
    } catch (AssertionError e) {
        runner.assertPenalizeCount(1); // penalized
        runner.assertQueueNotEmpty(); // flow file back in the input queue
        assertTrue(((MockProcessContext) runner.getProcessContext()).isYieldCalled()); // processor yielded
    }
}
 
Example #24
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that uses a dynamic property to set a FlowFile attribute.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileCustomAttribute() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_BODY, getFileContentsAsString(
            TEST_RESOURCE_LOCATION + "groovy/testReadFlowFileContentAndStoreInFlowFileCustomAttribute.groovy")
    );
    runner.setProperty("testprop", "test content");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #25
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script file that creates and transfers a new flow file.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testCreateNewFlowFileWithScriptFile() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "groovy/test_onTrigger_newFlowFile.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, TEST_RESOURCE_LOCATION + "groovy");

    runner.assertValid();
    runner.enqueue(TEST_CSV_DATA.getBytes(StandardCharsets.UTF_8));
    runner.run();

    // The script removes the original file and transfers only the new one
    assertEquals(1, runner.getRemovedCount());
    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("filename", "split_cols.txt");
}
 
Example #26
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script file that creates and transfers a new flow file.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testCreateNewFlowFileWithNoInputFile() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_BODY,
            getFileContentsAsString(TEST_RESOURCE_LOCATION + "groovy/testCreateNewFlowFileWithNoInputFile.groovy")
    );

    runner.assertValid();
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("filename", "newfile");
}
 
Example #27
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script file that creates and transfers a new flow file.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testDynamicProperties() throws Exception {
    runner.setValidateExpressionUsage(true);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, TEST_RESOURCE_LOCATION + "groovy/test_dynamicProperties.groovy");
    runner.setProperty("myProp", "${myAttr}");

    runner.assertValid();
    runner.enqueue(TEST_CSV_DATA.getBytes(StandardCharsets.UTF_8),
            new HashMap<String, String>(1) {{
                put("myAttr", "testValue");
            }});
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "testValue");
}
 
Example #28
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script file that changes the content of the incoming flowfile.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testChangeFlowFileWithScriptFile() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "target/test/resources/groovy/test_onTrigger_changeContent.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, "target/test/resources/groovy");

    runner.assertValid();
    runner.enqueue(TEST_CSV_DATA.getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    MockFlowFile resultFile = result.get(0);
    resultFile.assertAttributeEquals("selected.columns", "first,last");
    resultFile.assertContentEquals("Marlene Shaw\nTodd Graham\n");
}
 
Example #29
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that has provides the body of an onTrigger() function.
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttributeWithScriptBody() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_BODY, getFileContentsAsString(
            TEST_RESOURCE_LOCATION + "groovy/testReadFlowFileContentAndStoreInFlowFileAttributeWithScriptBody.groovy")
    );
    runner.setProperty(ScriptingComponentUtils.MODULES, TEST_RESOURCE_LOCATION + "groovy");

    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "test content");
}
 
Example #30
Source File: TestExecuteGroovy.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a script that has provides the body of an onTrigger() function, where the ExecuteScript processor does
 * not specify a modules path
 *
 * @throws Exception Any error encountered while testing
 */
@Test
public void testReadFlowFileContentAndStoreInFlowFileAttributeWithScriptBodyNoModules() throws Exception {
    runner.setValidateExpressionUsage(false);
    runner.setProperty(scriptingComponent.getScriptingComponentHelper().SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_BODY, getFileContentsAsString(
            TEST_RESOURCE_LOCATION + "groovy/testReadFlowFileContentAndStoreInFlowFileAttributeWithScriptBodyNoModules.groovy")
    );
    runner.assertValid();
    runner.enqueue("test content".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteScript.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(ExecuteScript.REL_SUCCESS);
    result.get(0).assertAttributeEquals("from-content", "test content");
}