Search in sources :

Example 31 with UpdateAttribute

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

the class TestUpdateAttribute method testStateFailures.

@Test
public void testStateFailures() throws Exception {
    final 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("count", "${getStateValue('count'):plus(1)}");
    runner.setProperty("sum", "${getStateValue('sum'):plus(${pencils})}");
    runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "0");
    processor.onScheduled(runner.getProcessContext());
    final Map<String, String> attributes2 = new HashMap<>();
    attributes2.put("pencils", "2");
    mockStateManager.setFailOnStateGet(Scope.LOCAL, true);
    runner.enqueue(new byte[0], attributes2);
    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("count", "1");
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_FAILED_SET_STATE).get(0).assertAttributeEquals("sum", "2");
}
Also used : MockStateManager(org.apache.nifi.state.MockStateManager) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) UpdateAttribute(org.apache.nifi.processors.attributes.UpdateAttribute) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) Test(org.junit.Test)

Example 32 with UpdateAttribute

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

the class TestUpdateAttribute method testRuleHitWithStateWithInitValue.

@Test
public void testRuleHitWithStateWithInitValue() throws Exception {
    final Criteria criteria = getCriteria();
    addRule(criteria, "rule", Arrays.asList(// conditions
    "${getStateValue('minValue'):ge(${value})}"), getMap(// actions
    "minValue", "${value}"));
    TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setProperty(UpdateAttribute.STORE_STATE, STORE_STATE_LOCALLY);
    runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "5");
    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(3).assertAttributeEquals("minValue", "1");
}
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