Search in sources :

Example 6 with UpdateAttribute

use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.

the class TestUpdateAttribute method testRegexLiteralDotDelete.

@Test
public void testRegexLiteralDotDelete() {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setProperty(UpdateAttribute.DELETE_ATTRIBUTES, "attribute\\.2");
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("attribute.1", "value.1");
    attributes.put("attribute.2", "value.2");
    attributes.put("attributex2", "valuex2");
    runner.enqueue(new byte[0], attributes);
    runner.run();
    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_SUCCESS, 1);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS);
    result.get(0).assertAttributeEquals("attribute.1", "value.1");
    result.get(0).assertAttributeNotExists("attribute.2");
    result.get(0).assertAttributeExists("attributex2");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) UpdateAttribute(org.apache.nifi.processors.attributes.UpdateAttribute) Test(org.junit.Test)

Example 7 with UpdateAttribute

use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.

the class TestUpdateAttribute method testDefaultBooleanAsString.

@Test
public void testDefaultBooleanAsString() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setProperty("NewAttr", "${a:equals('b'):toString()}");
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("a", "b");
    runner.enqueue(new byte[0], attributes);
    runner.run();
    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_SUCCESS, 1);
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS).get(0).assertAttributeEquals("NewAttr", "true");
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) UpdateAttribute(org.apache.nifi.processors.attributes.UpdateAttribute) Test(org.junit.Test)

Example 8 with UpdateAttribute

use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.

the class TestUpdateAttribute method testDefaultAddAttribute.

@Test
public void testDefaultAddAttribute() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setProperty("NewAttr", "${one:plus(${two})}");
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("one", "1");
    attributes.put("two", "2");
    runner.enqueue(new byte[0], attributes);
    runner.run();
    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_SUCCESS, 1);
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS).get(0).assertAttributeEquals("NewAttr", "3");
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) UpdateAttribute(org.apache.nifi.processors.attributes.UpdateAttribute) Test(org.junit.Test)

Example 9 with UpdateAttribute

use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.

the class TestUpdateAttribute method testStateWithInitValue.

@Test
public void testStateWithInitValue() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setProperty(UpdateAttribute.STORE_STATE, STORE_STATE_LOCALLY);
    runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "10");
    runner.setProperty("count", "${getStateValue('count'):plus(1)}");
    runner.setProperty("sum", "${getStateValue('sum'):plus(${pencils})}");
    runner.assertValid();
    final Map<String, String> attributes2 = new HashMap<>();
    attributes2.put("pencils", "2");
    runner.enqueue(new byte[0], attributes2);
    runner.enqueue(new byte[0], attributes2);
    final Map<String, String> attributes3 = new HashMap<>();
    attributes3.put("pencils", "3");
    runner.enqueue(new byte[0], attributes3);
    runner.enqueue(new byte[0], attributes3);
    final Map<String, String> attributes5 = new HashMap<>();
    attributes5.put("pencils", "5");
    runner.enqueue(new byte[0], attributes5);
    runner.run(5);
    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_SUCCESS, 5);
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS).get(4).assertAttributeEquals("count", "15");
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS).get(4).assertAttributeEquals("sum", "25");
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) UpdateAttribute(org.apache.nifi.processors.attributes.UpdateAttribute) Test(org.junit.Test)

Example 10 with UpdateAttribute

use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.

the class TestUpdateAttribute method testMultipleRuleHitsWithNoFlowFilePolicySpecified.

@Test
public void testMultipleRuleHitsWithNoFlowFilePolicySpecified() throws Exception {
    final Criteria criteria = getCriteria();
    addRule(criteria, "rule 1", Arrays.asList(// conditions
    "${attribute.1:equals('value.1')}"), getMap(// actions
    "attribute.2", "value.2"));
    addRule(criteria, "rule 2", Arrays.asList(// conditions
    "${attribute.1:equals('value.1')}"), getMap(// actions
    "attribute.3", "value.3"));
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setAnnotationData(serialize(criteria));
    runner.setProperty("attribute.2", "default.value.2");
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("attribute.1", "value.1");
    runner.enqueue(TEST_CONTENT.getBytes(StandardCharsets.UTF_8), attributes);
    runner.run();
    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_SUCCESS, 2);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS);
    final MockFlowFile flowfile1 = result.get(0);
    final MockFlowFile flowfile2 = result.get(1);
    // ensure the attributes are as expected
    if ("rule 1".equals(flowfile1.getAttribute(runner.getProcessor().getClass().getSimpleName() + ".matchedRule"))) {
        flowfile1.assertAttributeEquals("attribute.2", "value.2");
        flowfile2.assertAttributeEquals("attribute.3", "value.3");
        flowfile2.assertAttributeEquals("attribute.2", "default.value.2");
    } else {
        flowfile2.assertAttributeEquals("attribute.2", "value.2");
        flowfile1.assertAttributeEquals("attribute.3", "value.3");
        flowfile1.assertAttributeEquals("attribute.2", "default.value.2");
    }
    // ensure the content was copied as well
    flowfile1.assertContentEquals(TEST_CONTENT.getBytes(StandardCharsets.UTF_8));
    flowfile2.assertContentEquals(TEST_CONTENT.getBytes(StandardCharsets.UTF_8));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) UpdateAttribute(org.apache.nifi.processors.attributes.UpdateAttribute) Test(org.junit.Test)

Aggregations

UpdateAttribute (org.apache.nifi.processors.attributes.UpdateAttribute)32 TestRunner (org.apache.nifi.util.TestRunner)32 Test (org.junit.Test)32 HashMap (java.util.HashMap)31 MockFlowFile (org.apache.nifi.util.MockFlowFile)21 ProcessSessionFactory (org.apache.nifi.processor.ProcessSessionFactory)3 MockStateManager (org.apache.nifi.state.MockStateManager)3 ProcessException (org.apache.nifi.processor.exception.ProcessException)1