You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "markap14 (via GitHub)" <gi...@apache.org> on 2024/11/04 17:11:43 UTC

Re: [PR] NIFI-13745 StateManager available via the Python API [nifi]

markap14 commented on code in PR #9419:
URL: https://github.com/apache/nifi/pull/9419#discussion_r1828085446


##########
nifi-extension-bundles/nifi-py4j-extension-bundle/nifi-py4j-integration-tests/src/test/java/org.apache.nifi.py4j/PythonControllerInteractionIT.java:
##########
@@ -610,6 +612,75 @@ public void testCreateNothing() {
         runner.assertTransferCount("success", 0);
     }
 
+    @Test
+    public void testStateManagerSetState() {
+        final TestRunner runner = createStateManagerTesterProcessor("setState");
+
+        final MockStateManager stateManager = runner.getStateManager();
+        stateManager.assertStateNotSet();
+
+        runner.run();
+
+        stateManager.assertStateSet(Scope.CLUSTER);
+        stateManager.assertStateEquals(Map.of("state_key_1", "state_value_1"), Scope.CLUSTER);
+        runner.assertTransferCount("success", 1);
+    }
+
+    @Test
+    public void testStateManagerGetState() throws IOException {
+        final TestRunner runner = createStateManagerTesterProcessor("getState");
+
+        final MockStateManager stateManager = initializeStateManager(runner);
+
+        runner.run();
+
+        // The processor reads the state and adds the key-value pairs
+        // to the FlowFile as attributes.
+        stateManager.assertStateEquals(Map.of("state_key_1", "state_value_1"), Scope.CLUSTER);
+        final MockFlowFile flowFile = runner.getFlowFilesForRelationship("success").get(0);
+        flowFile.assertAttributeEquals("state_key_1", "state_value_1");
+    }
+
+    @Test
+    public void testStateManagerReplace() throws IOException {
+        final TestRunner runner = createStateManagerTesterProcessor("replace");
+
+        final MockStateManager stateManager = initializeStateManager(runner);
+
+        runner.run();
+
+        final Map finalState = Map.of("state_key_2", "state_value_2");
+        stateManager.assertStateEquals(finalState, Scope.CLUSTER);
+        runner.assertTransferCount("success", 1);
+    }
+
+    @Test
+    public void testStateManagerClear() throws IOException {
+        final TestRunner runner = createStateManagerTesterProcessor("clear");
+
+        final MockStateManager stateManager = initializeStateManager(runner);
+
+        runner.run();
+
+        stateManager.assertStateEquals(Map.of(), Scope.CLUSTER);
+        runner.assertTransferCount("success", 1);
+    }
+
+    @Test
+    public void testStateManagerExceptionHandling() {
+        // Use-case tested: StateManager's exception can be caught
+        // in the Python code and execution continued without producing errors.
+        final TestRunner runner = createProcessor("TestStateManagerException");
+        waitForValid(runner);
+
+        final MockStateManager stateManager = runner.getStateManager();
+        stateManager.setFailOnStateSet(Scope.CLUSTER, true);
+
+        runner.run();
+
+        runner.assertTransferCount("success", 1);

Review Comment:
   I'm not sure that this is very reliably testing the the specified use case. There are a lot of things that could go wrong and still satisfy the given assertions. Would recommend updating the TestStateManagerException processor to specify the text of the Exception, or the name of the Exception class, etc. in the output FlowFile's content, and then making an assertion here that the expected Exception is, in fact, what what is being thrown.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org