af34e434d696e990eb82401198e18cd7abee5295
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-jruby / src / test / java / org / onap / policy / apex / plugins / executor / jruby / JrubyStateFinalizerExecutorTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  *  Copyright (C) 2019 Nordix Foundation.\r
4  * ================================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  *\r
17  * SPDX-License-Identifier: Apache-2.0\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.policy.apex.plugins.executor.jruby;\r
22 \r
23 import static org.junit.Assert.assertEquals;\r
24 import static org.junit.Assert.assertNotNull;\r
25 import static org.junit.Assert.fail;\r
26 \r
27 import java.lang.reflect.Field;\r
28 import java.util.Properties;\r
29 \r
30 import org.junit.After;\r
31 import org.junit.Before;\r
32 import org.junit.Test;\r
33 import org.onap.policy.apex.context.ContextException;\r
34 import org.onap.policy.apex.context.parameters.ContextParameterConstants;\r
35 import org.onap.policy.apex.context.parameters.DistributorParameters;\r
36 import org.onap.policy.apex.context.parameters.LockManagerParameters;\r
37 import org.onap.policy.apex.context.parameters.PersistorParameters;\r
38 import org.onap.policy.apex.core.engine.EngineParameterConstants;\r
39 import org.onap.policy.apex.core.engine.EngineParameters;\r
40 import org.onap.policy.apex.core.engine.context.ApexInternalContext;\r
41 import org.onap.policy.apex.core.engine.event.EnEvent;\r
42 import org.onap.policy.apex.core.engine.executor.StateExecutor;\r
43 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;\r
44 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;\r
45 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;\r
46 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;\r
47 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;\r
48 import org.onap.policy.apex.model.policymodel.concepts.AxState;\r
49 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;\r
50 import org.onap.policy.common.parameters.ParameterService;\r
51 \r
52 /**\r
53  * Test the JrubyStateFinalizerExecutor class.\r
54  *\r
55  */\r
56 public class JrubyStateFinalizerExecutorTest {\r
57     /**\r
58      * Initiate Parameters.\r
59      */\r
60     @Before\r
61     public void initiateParameters() {\r
62         ParameterService.register(new DistributorParameters());\r
63         ParameterService.register(new LockManagerParameters());\r
64         ParameterService.register(new PersistorParameters());\r
65         ParameterService.register(new EngineParameters());\r
66     }\r
67 \r
68     /**\r
69      * Clear down Parameters.\r
70      */\r
71     @After\r
72     public void clearParameters() {\r
73         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);\r
74         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);\r
75         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);\r
76         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);\r
77     }\r
78 \r
79     @Test\r
80     public void testJrubyStateFinalizerExecutor() {\r
81         JrubyStateFinalizerExecutor jsfe = new JrubyStateFinalizerExecutor();\r
82         assertNotNull(jsfe);\r
83 \r
84         try {\r
85             Field fieldContainer = JrubyStateFinalizerExecutor.class.getDeclaredField("container");\r
86             fieldContainer.setAccessible(true);\r
87             fieldContainer.set(jsfe, null);\r
88             jsfe.prepare();\r
89             fail("test should throw an exception here");\r
90         } catch (Exception jtseException) {\r
91             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());\r
92         }\r
93         ApexInternalContext internalContext = null;\r
94         try {\r
95             internalContext = new ApexInternalContext(new AxPolicyModel());\r
96         } catch (ContextException e) {\r
97             fail("test should not throw an exception here");\r
98         }\r
99 \r
100         StateExecutor parentStateExcutor = null;\r
101         try {\r
102             parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());\r
103         } catch (StateMachineException e) {\r
104             fail("test should not throw an exception here");\r
105         }\r
106 \r
107         AxState state = new AxState();\r
108         parentStateExcutor.setContext(null, state, internalContext);\r
109         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();\r
110         jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);\r
111         try {\r
112             jsfe.prepare();\r
113         } catch (Exception jtseException) {\r
114             fail("test should not throw an exception here");\r
115         }\r
116 \r
117         try {\r
118             jsfe.execute(-1, new Properties(), null);\r
119             fail("test should throw an exception here");\r
120         } catch (Exception jtseException) {\r
121             assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" on "\r
122                     + "finalizer logic NULL:0.0.0:NULL:NULL", jtseException.getMessage());\r
123         }\r
124 \r
125         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));\r
126         EnEvent event = new EnEvent(axEvent);\r
127 \r
128         final String jrubyLogic = "if executor.executionId == -1" + "\n return false" + "\n else "\r
129                 + "\n executor.setSelectedStateOutputName(\"SelectedOutputIsMe\")" + "\n return true" + "\n end";\r
130         stateFinalizerLogic.setLogic(jrubyLogic);\r
131 \r
132         state.getStateOutputs().put("SelectedOutputIsMe", null);\r
133         try {\r
134             jsfe.prepare();\r
135             String stateOutput = jsfe.execute(0, new Properties(), event);\r
136             assertEquals("SelectedOutputIsMe", stateOutput);\r
137             jsfe.cleanUp();\r
138         } catch (Exception jtseException) {\r
139             jtseException.printStackTrace();\r
140             fail("test should not throw an exception here");\r
141         }\r
142 \r
143     }\r
144 }\r