use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.
the class TestUpdateAttribute method testRuleHitWithState.
@Test
public void testRuleHitWithState() 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));
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", null);
}
use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.
the class TestUpdateAttribute method testExpressionLiteralDelete.
@Test
public void testExpressionLiteralDelete() {
final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
runner.setProperty(UpdateAttribute.DELETE_ATTRIBUTES, "${literal('attribute\\.'):append(${literal(6)})}");
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).assertAttributeExists("attribute.2");
result.get(0).assertAttributeNotExists("attribute.6");
result.get(0).assertAttributeExists("sampleSize");
result.get(0).assertAttributeExists("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 testAttributeKey.
@Test
public void testAttributeKey() {
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 testStateFailuresWithRulesUsingOriginal.
@Test
public void testStateFailuresWithRulesUsingOriginal() 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
"${getStateValue('maxValue2'):lt(${value})}"), getMap(// actions
"maxValue2", "${value}"));
TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
final UpdateAttribute processor = (UpdateAttribute) runner.getProcessor();
final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
MockStateManager mockStateManager = runner.getStateManager();
runner.setProperty(UpdateAttribute.STORE_STATE, STORE_STATE_LOCALLY);
runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "0");
runner.setAnnotationData(serialize(criteria));
processor.onScheduled(runner.getProcessContext());
final Map<String, String> attributes = new HashMap<>();
attributes.put("value", "1");
runner.enqueue(new byte[0], attributes);
mockStateManager.setFailOnStateGet(Scope.LOCAL, true);
processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());
runner.assertQueueNotEmpty();
mockStateManager.setFailOnStateGet(Scope.LOCAL, false);
mockStateManager.setFailOnStateSet(Scope.LOCAL, true);
processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());
runner.assertQueueEmpty();
runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_FAILED_SET_STATE, 1);
runner.getFlowFilesForRelationship(UpdateAttribute.REL_FAILED_SET_STATE).get(0).assertAttributeEquals("maxValue", "1");
runner.getFlowFilesForRelationship(UpdateAttribute.REL_FAILED_SET_STATE).get(0).assertAttributeEquals("maxValue2", "1");
}
use of org.apache.nifi.processors.attributes.UpdateAttribute in project nifi by apache.
the class TestUpdateAttribute method testDataIsTooShort.
@Test
public void testDataIsTooShort() {
final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
runner.setProperty("attribute.1", "${test:substring(1, 20)}");
runner.assertValid();
final Map<String, String> attributes = new HashMap<>();
attributes.put("test", "chocolate");
runner.enqueue(new byte[0], attributes);
try {
runner.run();
} catch (AssertionError e) {
Assert.assertTrue(e.getMessage().contains("org.apache.nifi.processor.exception.ProcessException"));
}
}
Aggregations