Java Code Examples for org.apache.nifi.script.ScriptingComponentUtils

The following examples show how to use org.apache.nifi.script.ScriptingComponentUtils. These examples are extracted from open source projects. 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 Project: nifi-scripting-samples   Source File: TestProperties.java    License: 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 2
Source Project: nifi-scripting-samples   Source File: TestProperties.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading values from nifi.properties
 * @throws Exception
 */
@Test
public void testPropertiesPython() 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, "python");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/properties/properties.py");
    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 Project: nifi-scripting-samples   Source File: TestCounter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates writing to counters
 * @throws Exception
 */
@Test
public void testCounterJavascript() 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/counter/counter.js");
    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 4
Source Project: nifi-scripting-samples   Source File: TestCounter.java    License: 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 5
Source Project: nifi-scripting-samples   Source File: TestParseUri.java    License: 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 6
Source Project: nifi-scripting-samples   Source File: TestParseUri.java    License: 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 7
Source Project: nifi-scripting-samples   Source File: TestDateTime.java    License: 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 8
Source Project: nifi-scripting-samples   Source File: TestDateTime.java    License: 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 9
Source Project: nifi-scripting-samples   Source File: TestXmlToJson.java    License: 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 10
Source Project: nifi-scripting-samples   Source File: TestXmlToJson.java    License: Apache License 2.0 6 votes vote down vote up
private String runErrorMessageTest(String inputResouce) 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(inputResouce);
    runner.enqueue(inputXmlPath);
    runner.run();

    runner.assertAllFlowFilesTransferred("failure", 1);
    final List<MockFlowFile> failureFlowFiles = runner.getFlowFilesForRelationship("failure");
    MockFlowFile result = failureFlowFiles.get(0);
    String errorMessage = result.getAttribute("xmlToJson.error");
    return errorMessage;
}
 
Example 11
Source Project: nifi-scripting-samples   Source File: TestXmlToJson.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBatchProcessing() 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("batchSize", "7");
    runner.assertValid();

    for (int i = 0; i < 10; i++) {
        String flowFileContent = "<test id=\"" + String.valueOf(i) + "\"/>";
        if (i % 3 == 0) {
            flowFileContent += "<bad><xml>";
        }
        runner.enqueue(flowFileContent);
    }
    
    runner.run(1);

    runner.assertTransferCount("success", 4);
    runner.assertTransferCount("failure", 3);
    runner.assertQueueNotEmpty();
}
 
Example 12
Source Project: nifi-scripting-samples   Source File: TestState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing processor state values
 * @throws Exception
 */
@Test
public void testStateJavascript() 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/state/state.js");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    StateManager stateManager = runner.getStateManager();
    stateManager.clear(Scope.CLUSTER);
    Map<String, String> initialStateValues = new HashMap<>();
    initialStateValues.put("some-state", "foo");
    stateManager.setState(initialStateValues, Scope.CLUSTER);

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

    runner.assertAllFlowFilesTransferred("success", 1);
    StateMap resultStateValues = stateManager.getState(Scope.CLUSTER);
    Assert.assertEquals("foobar", resultStateValues.get("some-state"));
}
 
Example 13
Source Project: nifi-scripting-samples   Source File: TestState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing processor state values
 * @throws Exception
 */
@Test
public void testStatePython() 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/state/state.py");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    StateManager stateManager = runner.getStateManager();
    stateManager.clear(Scope.CLUSTER);
    Map<String, String> initialStateValues = new HashMap<>();
    initialStateValues.put("some-state", "foo");
    stateManager.setState(initialStateValues, Scope.CLUSTER);

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

    runner.assertAllFlowFilesTransferred("success", 1);
    StateMap resultStateValues = stateManager.getState(Scope.CLUSTER);
    Assert.assertEquals("foobar", resultStateValues.get("some-state"));
}
 
Example 14
Source Project: nifi-scripting-samples   Source File: TestAttributes.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing FlowFile attributes from within scripts
 * @throws Exception
 */
@Test
public void testAttributeAccessJavascript() 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/attributes/attributes.js");
    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 15
Source Project: nifi-scripting-samples   Source File: TestAttributes.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing FlowFile attributes from within scripts
 * @throws Exception
 */
@Test
public void testAttributesAccessPython() 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/attributes/attributes.py");
    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 16
Source Project: nifi-scripting-samples   Source File: TestAttributes.java    License: 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 17
Source Project: nifi   Source File: InvokeScriptedProcessor.java    License: 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 18
Source Project: nifi   Source File: BaseScriptedLookupService.java    License: 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 19
Source Project: nifi   Source File: TestExecuteJRuby.java    License: 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 20
Source Project: nifi   Source File: TestInvokeJavascript.java    License: 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 21
Source Project: nifi   Source File: TestInvokeJavascript.java    License: 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 22
Source Project: nifi   Source File: TestExecuteJavascript.java    License: 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 23
Source Project: nifi   Source File: TestExecuteJython.java    License: 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 24
Source Project: nifi   Source File: TestExecuteLua.java    License: 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 25
Source Project: nifi   Source File: TestExecuteClojure.java    License: 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 26
Source Project: nifi   Source File: TestExecuteClojure.java    License: 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 27
Source Project: nifi   Source File: TestExecuteClojure.java    License: 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 28
Source Project: nifi   Source File: TestExecuteGroovy.java    License: 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 29
Source Project: nifi   Source File: TestExecuteGroovy.java    License: 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 30
Source Project: nifi   Source File: TestExecuteGroovy.java    License: 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");
}