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();
}
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");
}
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");
}
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");
}
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");
}
Aggregations