use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.
the class TestUpdateAttribute method testRegexGroupDelete.
@Test
public void testRegexGroupDelete() {
final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
runner.setProperty(UpdateAttribute.DELETE_ATTRIBUTES, "(attribute\\.[2-5]|sample.*)");
final Map<String, String> attributes = new HashMap<>();
attributes.put("attribute.1", "value.1");
attributes.put("attribute.2", "value.2");
attributes.put("attribute.6", "value.6");
attributes.put("sampleSize", "value.size");
attributes.put("sample.1", "value.sample.1");
attributes.put("simple.1", "value.simple.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");
result.get(0).assertAttributeExists("attribute.6");
result.get(0).assertAttributeNotExists("sampleSize");
result.get(0).assertAttributeNotExists("sample.1");
result.get(0).assertAttributeExists("simple.1");
}
use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.
the class TestUpdateAttribute method testRuleHitWithDefaults.
@Test
public void testRuleHitWithDefaults() throws Exception {
final Criteria criteria = getCriteria();
addRule(criteria, "rule", Arrays.asList(// conditions
"${attribute.1:equals('value.1')}"), getMap(// actions
"attribute.2", "value.2"));
final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
runner.setAnnotationData(serialize(criteria));
runner.setProperty("attribute.1", "new.value.1");
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", "new.value.1");
result.get(0).assertAttributeEquals("attribute.2", "value.2");
}
use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.
the class TestUpdateAttribute method testRuleHitWithStateWithDefault.
@Test
public void testRuleHitWithStateWithDefault() throws Exception {
final Criteria criteria = getCriteria();
addRule(criteria, "rule", Arrays.asList(// conditions
"${getStateValue('maxValue'):lt(${value})}"), getMap(// actions
"maxValue", "${value}"));
TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
runner.setProperty(UpdateAttribute.STORE_STATE, STORE_STATE_LOCALLY);
runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "0");
runner.setAnnotationData(serialize(criteria));
runner.setProperty("maxValue", "${getStateValue('maxValue')}");
final Map<String, String> attributes = new HashMap<>();
attributes.put("value", "1");
runner.enqueue(new byte[0], attributes);
runner.run();
attributes.put("value", "2");
runner.enqueue(new byte[0], attributes);
runner.run();
attributes.put("value", "4");
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(2).assertAttributeEquals("maxValue", "4");
result.get(3).assertAttributeEquals("maxValue", "4");
}
use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.
the class TestUpdateAttribute method testRuleHitWithAConflictingDefault.
@Test
public void testRuleHitWithAConflictingDefault() throws Exception {
final Criteria criteria = getCriteria();
addRule(criteria, "rule", Arrays.asList(// conditions
"${attribute.1:equals('value.1')}"), getMap(// actions
"attribute.2", "value.2"));
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(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.2", "value.2");
}
use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.
the class TestUpdateAttribute method testMultipleRuleHitsWithUseOriginal.
@Test
public void testMultipleRuleHitsWithUseOriginal() throws Exception {
final Criteria criteria = getCriteria();
criteria.setFlowFilePolicy(FlowFilePolicy.USE_ORIGINAL);
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"));
addRule(criteria, "rule 3", Arrays.asList(// conditions
"${attribute.1:equals('value.1')}"), getMap(// actions
"attribute.2", "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, 1);
final List<MockFlowFile> result = runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS);
final MockFlowFile flowfile = result.get(0);
// ensure the attributes are as expected
flowfile.assertAttributeEquals("attribute.2", "value.3");
flowfile.assertAttributeEquals("attribute.3", "value.3");
// ensure the content was copied as well
flowfile.assertContentEquals(TEST_CONTENT.getBytes(StandardCharsets.UTF_8));
}
Aggregations