Search in sources :

Example 21 with UpdateAttribute

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

the class TestUpdateAttribute method testInvalidRegex.

@Test
public void testInvalidRegex() {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setProperty(UpdateAttribute.DELETE_ATTRIBUTES, "(");
    runner.assertNotValid();
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) UpdateAttribute(org.apache.nifi.processors.attributes.UpdateAttribute) Test(org.junit.Test)

Example 22 with UpdateAttribute

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

the class TestUpdateAttribute method testMultipleRulesWithStateAndDelete.

@Test
public void testMultipleRulesWithStateAndDelete() throws Exception {
    final Criteria criteria = getCriteria();
    criteria.setFlowFilePolicy(FlowFilePolicy.USE_ORIGINAL);
    addRule(criteria, "rule", Collections.singletonList(// conditions
    "${getStateValue('maxValue'):lt(${value})}"), getMap(// actions
    "maxValue", "${value}"));
    addRule(criteria, "rule2", Collections.singletonList(// conditions
    "${value:mod(2):equals(0)}"), getMap(// actions
    "whatIsIt", "even"));
    TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setProperty(UpdateAttribute.STORE_STATE, STORE_STATE_LOCALLY);
    runner.setProperty(UpdateAttribute.DELETE_ATTRIBUTES, "badValue");
    runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "0");
    runner.setAnnotationData(serialize(criteria));
    runner.setProperty("maxValue", "${getStateValue('maxValue')}");
    runner.setProperty("whatIsIt", "odd");
    runner.setProperty("whatWasIt", "${getStateValue('whatIsIt')}");
    runner.setProperty("theCount", "${getStateValue('theCount'):plus(1)}");
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("value", "1");
    attributes.put("badValue", "10");
    runner.enqueue(new byte[0], attributes);
    runner.run();
    attributes.put("value", "2");
    runner.enqueue(new byte[0], attributes);
    runner.run();
    attributes.put("value", "5");
    runner.enqueue(new byte[0], attributes);
    runner.run();
    attributes.put("value", "1");
    runner.enqueue(new byte[0], attributes);
    runner.run();
    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_SUCCESS, 4);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS);
    result.get(3).assertAttributeEquals("maxValue", "5");
    result.get(3).assertAttributeEquals("theCount", "4");
    result.get(0).assertAttributeEquals("badValue", null);
    result.get(0).assertAttributeEquals("whatIsIt", "odd");
    result.get(1).assertAttributeEquals("whatIsIt", "even");
    result.get(2).assertAttributeEquals("whatWasIt", "even");
    result.get(3).assertAttributeEquals("whatWasIt", "odd");
}
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 23 with UpdateAttribute

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

the class TestUpdateAttribute method testBasicState.

@Test
public void testBasicState() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setProperty(UpdateAttribute.STORE_STATE, STORE_STATE_LOCALLY);
    runner.setProperty("count", "${getStateValue('count'):plus(1)}");
    runner.setProperty("sum", "${getStateValue('sum'):plus(${pencils})}");
    runner.assertNotValid();
    runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "0");
    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", "5");
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS).get(4).assertAttributeEquals("sum", "15");
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) UpdateAttribute(org.apache.nifi.processors.attributes.UpdateAttribute) Test(org.junit.Test)

Example 24 with UpdateAttribute

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

the class TestUpdateAttribute method testRuleMissBecauseAttributeMissing.

@Test
public void testRuleMissBecauseAttributeMissing() throws Exception {
    final Criteria criteria = getCriteria();
    addRule(criteria, "rule", Arrays.asList(// conditions
    "${attribute5:equals('value.5')}"), getMap(// actions
    "attribute.2", "value.2"));
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setAnnotationData(serialize(criteria));
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("attribute.1", "value.1");
    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");
}
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 25 with UpdateAttribute

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

the class TestUpdateAttribute method testRegexDotDelete.

@Test
public void testRegexDotDelete() {
    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).assertAttributeNotExists("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)

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