Search in sources :

Example 21 with MockStateManager

use of org.apache.nifi.state.MockStateManager in project nifi by apache.

the class TestAttributeRollingWindow method testStateFailures.

@Test
public void testStateFailures() throws InterruptedException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(AttributeRollingWindow.class);
    MockStateManager mockStateManager = runner.getStateManager();
    final AttributeRollingWindow processor = (AttributeRollingWindow) runner.getProcessor();
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    runner.setProperty(AttributeRollingWindow.VALUE_TO_TRACK, "${value}");
    runner.setProperty(AttributeRollingWindow.TIME_WINDOW, "3 sec");
    processor.onScheduled(runner.getProcessContext());
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("value", "1");
    mockStateManager.setFailOnStateGet(Scope.LOCAL, true);
    runner.enqueue(new byte[0], attributes);
    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(AttributeRollingWindow.REL_FAILED_SET_STATE, 1);
    MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(REL_FAILED_SET_STATE).get(0);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_VALUE_KEY);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_COUNT_KEY);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_MEAN_KEY);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) MockStateManager(org.apache.nifi.state.MockStateManager) HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) Test(org.junit.Test)

Example 22 with MockStateManager

use of org.apache.nifi.state.MockStateManager 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");
}
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 23 with MockStateManager

use of org.apache.nifi.state.MockStateManager 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)

Aggregations

MockStateManager (org.apache.nifi.state.MockStateManager)23 HashMap (java.util.HashMap)16 Test (org.junit.Test)16 TestRunner (org.apache.nifi.util.TestRunner)8 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)7 MockFlowFile (org.apache.nifi.util.MockFlowFile)6 ComponentLog (org.apache.nifi.logging.ComponentLog)5 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 PropertyValue (org.apache.nifi.components.PropertyValue)4 ProcessSessionFactory (org.apache.nifi.processor.ProcessSessionFactory)4 ArrayList (java.util.ArrayList)3 FirstInFirstOutPrioritizer (org.apache.nifi.prioritizer.FirstInFirstOutPrioritizer)3 UpdateAttribute (org.apache.nifi.processors.attributes.UpdateAttribute)3 MockComponentLog (org.apache.nifi.util.MockComponentLog)3 StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)2 ValidationContext (org.apache.nifi.components.ValidationContext)2 ProcessGroupStatus (org.apache.nifi.controller.status.ProcessGroupStatus)2 ProvenanceEventRepository (org.apache.nifi.provenance.ProvenanceEventRepository)2 MockConfigurationContext (org.apache.nifi.util.MockConfigurationContext)2