Increase test coverage for plugins-executor-jruby 37/79237/2
authorshaoqiu <tim.huang@est.tech>
Thu, 28 Feb 2019 02:56:02 +0000 (02:56 +0000)
committershaoqiu <tim.huang@est.tech>
Thu, 28 Feb 2019 02:56:02 +0000 (02:56 +0000)
Add junit test cases into apex-pdp/plugins/plugins-executor-jruby

Issue-ID: POLICY-1523
Change-Id: I2ed06a69cf991d744aa5c4c69f605fdf1dca6524
Signed-off-by: shaoqiu <tim.huang@est.tech>
plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutor.java
plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutor.java
plugins/plugins-executor/plugins-executor-jruby/src/main/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutor.java
plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyExecutorParametersTest.java [new file with mode: 0644]
plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutorTest.java [new file with mode: 0644]
plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutorTest.java [new file with mode: 0644]
plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutorTest.java [new file with mode: 0644]

index aaccbeb..5c62cf8 100644 (file)
@@ -99,11 +99,7 @@ public class JrubyStateFinalizerExecutor extends StateFinalizerExecutor {
         executePost(returnValue);
 
         // Send back the return event
-        if (returnValue) {
-            return getOutgoing();
-        } else {
-            return null;
-        }
+        return getOutgoing();
     }
 
     /**
index 8019259..8d2ccff 100644 (file)
@@ -99,11 +99,7 @@ public class JrubyTaskExecutor extends TaskExecutor {
         executePost(returnValue);
 
         // Send back the return event
-        if (returnValue) {
-            return getOutgoing();
-        } else {
-            return null;
-        }
+        return getOutgoing();
     }
 
     /**
index 0e239f7..02ae0d8 100644 (file)
@@ -100,11 +100,7 @@ public class JrubyTaskSelectExecutor extends TaskSelectExecutor {
         executePost(returnValue);
 
         // Send back the return event
-        if (returnValue) {
-            return getOutgoing();
-        } else {
-            return null;
-        }
+        return getOutgoing();
     }
 
     /**
diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyExecutorParametersTest.java b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyExecutorParametersTest.java
new file mode 100644 (file)
index 0000000..6e35aaf
--- /dev/null
@@ -0,0 +1,34 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2019 Ericsson. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.policy.apex.plugins.executor.jruby;\r
+\r
+import static org.junit.Assert.assertNotNull;\r
+import org.junit.Test;\r
+\r
+/**\r
+ * Test the JrubyExecutorParameters class.\r
+ */\r
+public class JrubyExecutorParametersTest {\r
+    @Test\r
+    public void testJrubyExecutorParameters() {\r
+        assertNotNull(new JrubyExecutorParameters());\r
+    }\r
+}\r
diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutorTest.java b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyStateFinalizerExecutorTest.java
new file mode 100644 (file)
index 0000000..1c2ea1c
--- /dev/null
@@ -0,0 +1,141 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2019 Ericsson. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.policy.apex.plugins.executor.jruby;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.fail;\r
+import java.lang.reflect.Field;\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.onap.policy.apex.context.ContextException;\r
+import org.onap.policy.apex.context.parameters.ContextParameterConstants;\r
+import org.onap.policy.apex.context.parameters.DistributorParameters;\r
+import org.onap.policy.apex.context.parameters.LockManagerParameters;\r
+import org.onap.policy.apex.context.parameters.PersistorParameters;\r
+import org.onap.policy.apex.core.engine.EngineParameterConstants;\r
+import org.onap.policy.apex.core.engine.EngineParameters;\r
+import org.onap.policy.apex.core.engine.context.ApexInternalContext;\r
+import org.onap.policy.apex.core.engine.event.EnEvent;\r
+import org.onap.policy.apex.core.engine.executor.StateExecutor;\r
+import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;\r
+import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;\r
+import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;\r
+import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;\r
+import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;\r
+import org.onap.policy.apex.model.policymodel.concepts.AxState;\r
+import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;\r
+import org.onap.policy.common.parameters.ParameterService;\r
+\r
+/**\r
+ * Test the JrubyStateFinalizerExecutor class.\r
+ *\r
+ */\r
+public class JrubyStateFinalizerExecutorTest {\r
+    /**\r
+     * Initiate Parameters.\r
+     */\r
+    @Before\r
+    public void initiateParameters() {\r
+        ParameterService.register(new DistributorParameters());\r
+        ParameterService.register(new LockManagerParameters());\r
+        ParameterService.register(new PersistorParameters());\r
+        ParameterService.register(new EngineParameters());\r
+    }\r
+\r
+    /**\r
+     * Clear down Parameters.\r
+     */\r
+    @After\r
+    public void clearParameters() {\r
+        ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);\r
+        ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);\r
+        ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);\r
+        ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);\r
+    }\r
+\r
+    @Test\r
+    public void testJrubyStateFinalizerExecutor() {\r
+        JrubyStateFinalizerExecutor jsfe = new JrubyStateFinalizerExecutor();\r
+        assertNotNull(jsfe);\r
+\r
+        try {\r
+            Field fieldContainer = JrubyStateFinalizerExecutor.class.getDeclaredField("container");\r
+            fieldContainer.setAccessible(true);\r
+            fieldContainer.set(jsfe, null);\r
+            jsfe.prepare();\r
+            fail("test should throw an exception here");\r
+        } catch (Exception jtseException) {\r
+            assertEquals(java.lang.NullPointerException.class, jtseException.getClass());\r
+        }\r
+        ApexInternalContext internalContext = null;\r
+        try {\r
+            internalContext = new ApexInternalContext(new AxPolicyModel());\r
+        } catch (ContextException e) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+\r
+        StateExecutor parentStateExcutor = null;\r
+        try {\r
+            parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());\r
+        } catch (StateMachineException e) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+\r
+        AxState state = new AxState();\r
+        parentStateExcutor.setContext(null, state, internalContext);\r
+        AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();\r
+        jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);\r
+        try {\r
+            jsfe.prepare();\r
+        } catch (Exception jtseException) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+\r
+        try {\r
+            jsfe.execute(-1, null);\r
+            fail("test should throw an exception here");\r
+        } catch (Exception jtseException) {\r
+            assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" on "\r
+                    + "finalizer logic NULL:0.0.0:NULL:NULL", jtseException.getMessage());\r
+        }\r
+\r
+        AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));\r
+        EnEvent event = new EnEvent(axEvent);\r
+\r
+        final String jrubyLogic = "if executor.executionId == -1" + "\n return false" + "\n else "\r
+                + "\n executor.setSelectedStateOutputName(\"SelectedOutputIsMe\")" + "\n return true" + "\n end";\r
+        stateFinalizerLogic.setLogic(jrubyLogic);\r
+\r
+        state.getStateOutputs().put("SelectedOutputIsMe", null);\r
+        try {\r
+            jsfe.prepare();\r
+            String stateOutput = jsfe.execute(0, event);\r
+            assertEquals("SelectedOutputIsMe", stateOutput);\r
+            jsfe.cleanUp();\r
+        } catch (Exception jtseException) {\r
+            jtseException.printStackTrace();\r
+            fail("test should not throw an exception here");\r
+        }\r
+\r
+    }\r
+}\r
diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutorTest.java b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskExecutorTest.java
new file mode 100644 (file)
index 0000000..8822710
--- /dev/null
@@ -0,0 +1,117 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2019 Ericsson. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.policy.apex.plugins.executor.jruby;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.fail;\r
+import java.lang.reflect.Field;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.onap.policy.apex.context.ContextException;\r
+import org.onap.policy.apex.context.parameters.ContextParameterConstants;\r
+import org.onap.policy.apex.context.parameters.DistributorParameters;\r
+import org.onap.policy.apex.context.parameters.LockManagerParameters;\r
+import org.onap.policy.apex.context.parameters.PersistorParameters;\r
+import org.onap.policy.apex.core.engine.context.ApexInternalContext;\r
+import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;\r
+import org.onap.policy.apex.model.policymodel.concepts.AxTask;\r
+import org.onap.policy.common.parameters.ParameterService;\r
+\r
+/**\r
+ * Test the JrubyTaskExecutor class.\r
+ *\r
+ */\r
+public class JrubyTaskExecutorTest {\r
+    /**\r
+     * Initiate Parameters.\r
+     */\r
+    @Before\r
+    public void initiateParameters() {\r
+        ParameterService.register(new DistributorParameters());\r
+        ParameterService.register(new LockManagerParameters());\r
+        ParameterService.register(new PersistorParameters());\r
+    }\r
+\r
+    /**\r
+     * Clear Parameters.\r
+     */\r
+    @After\r
+    public void clearParameters() {\r
+        ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);\r
+        ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);\r
+        ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);\r
+    }\r
+\r
+    @Test\r
+    public void testJrubyTaskExecutor() {\r
+        JrubyTaskExecutor jte = new JrubyTaskExecutor();\r
+        assertNotNull(jte);\r
+        try {\r
+            Field fieldContainer = JrubyTaskExecutor.class.getDeclaredField("container");\r
+            fieldContainer.setAccessible(true);\r
+            fieldContainer.set(jte, null);\r
+            jte.prepare();\r
+            fail("test should throw an exception here");\r
+        } catch (Exception jtseException) {\r
+            assertEquals(java.lang.NullPointerException.class, jtseException.getClass());\r
+        }\r
+\r
+        AxTask task = new AxTask();\r
+        ApexInternalContext internalContext = null;\r
+        try {\r
+            internalContext = new ApexInternalContext(new AxPolicyModel());\r
+        } catch (ContextException e) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+        jte.setContext(null, task, internalContext);\r
+        try {\r
+            jte.prepare();\r
+        } catch (Exception jtseException) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+\r
+        Map<String, Object> incomingParameters = new HashMap<>();\r
+        try {\r
+            jte.execute(-1, incomingParameters);\r
+            fail("test should throw an exception here");\r
+        } catch (Exception jteException) {\r
+            assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0",\r
+                    jteException.getMessage());\r
+        }\r
+\r
+        final String jrubyLogic =\r
+                "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true" + "\n end";\r
+        task.getTaskLogic().setLogic(jrubyLogic);\r
+\r
+        try {\r
+            jte.prepare();\r
+            Map<String, Object> returnMap = jte.execute(0, incomingParameters);\r
+            assertEquals(0, returnMap.size());\r
+            jte.cleanUp();\r
+        } catch (Exception jteException) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+    }\r
+}\r
diff --git a/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutorTest.java b/plugins/plugins-executor/plugins-executor-jruby/src/test/java/org/onap/policy/apex/plugins/executor/jruby/JrubyTaskSelectExecutorTest.java
new file mode 100644 (file)
index 0000000..3914fb5
--- /dev/null
@@ -0,0 +1,121 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ *  Copyright (C) 2019 Ericsson. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.policy.apex.plugins.executor.jruby;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.fail;\r
+import java.lang.reflect.Field;\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.onap.policy.apex.context.ContextException;\r
+import org.onap.policy.apex.context.parameters.ContextParameterConstants;\r
+import org.onap.policy.apex.context.parameters.DistributorParameters;\r
+import org.onap.policy.apex.context.parameters.LockManagerParameters;\r
+import org.onap.policy.apex.context.parameters.PersistorParameters;\r
+import org.onap.policy.apex.core.engine.context.ApexInternalContext;\r
+import org.onap.policy.apex.core.engine.event.EnEvent;\r
+import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;\r
+import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;\r
+import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;\r
+import org.onap.policy.apex.model.policymodel.concepts.AxState;\r
+import org.onap.policy.common.parameters.ParameterService;\r
+\r
+/**\r
+ * Test the JrubyTaskSelectExecutor class.\r
+ *\r
+ */\r
+public class JrubyTaskSelectExecutorTest {\r
+    /**\r
+     * Initiate Parameters.\r
+     */\r
+    @Before\r
+    public void initiateParameters() {\r
+        ParameterService.register(new DistributorParameters());\r
+        ParameterService.register(new LockManagerParameters());\r
+        ParameterService.register(new PersistorParameters());\r
+    }\r
+\r
+    /**\r
+     * Clear Parameters.\r
+     */\r
+    @After\r
+    public void clearParameters() {\r
+        ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);\r
+        ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);\r
+        ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);\r
+    }\r
+\r
+    @Test\r
+    public void testJrubyTaskSelectExecutor() {\r
+        JrubyTaskSelectExecutor jtse = new JrubyTaskSelectExecutor();\r
+        assertNotNull(jtse);\r
+        assertNotNull(jtse.getOutputEventSet());\r
+        try {\r
+            Field fieldContainer = JrubyTaskSelectExecutor.class.getDeclaredField("container");\r
+            fieldContainer.setAccessible(true);\r
+            fieldContainer.set(jtse, null);\r
+            jtse.prepare();\r
+            fail("test should throw an exception here");\r
+        } catch (Exception jtseException) {\r
+            assertEquals(java.lang.NullPointerException.class, jtseException.getClass());\r
+        }\r
+\r
+        AxState state = new AxState();\r
+        ApexInternalContext internalContext = null;\r
+        try {\r
+            internalContext = new ApexInternalContext(new AxPolicyModel());\r
+        } catch (ContextException e) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+        jtse.setContext(null, state, internalContext);\r
+\r
+\r
+        try {\r
+            jtse.prepare();\r
+        } catch (Exception jtseException) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+        AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));\r
+        EnEvent event = new EnEvent(axEvent);\r
+        try {\r
+            jtse.execute(-1, event);\r
+            fail("test should throw an exception here");\r
+        } catch (Exception jtseException) {\r
+            assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",\r
+                    jtseException.getMessage());\r
+        }\r
+\r
+        final String jrubyLogic =\r
+                "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true" + "\n end";\r
+        state.getTaskSelectionLogic().setLogic(jrubyLogic);\r
+\r
+        try {\r
+            jtse.prepare();\r
+            AxArtifactKey taskKey = jtse.execute(0, event);\r
+            assertEquals("NULL:0.0.0", taskKey.getId());\r
+            jtse.cleanUp();\r
+        } catch (Exception jtseException) {\r
+            fail("test should not throw an exception here");\r
+        }\r
+    }\r
+}\r