635430fa125646de56d3dbb72786e90467c5e446
[policy/apex-pdp.git] /
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 org.junit.After;\r
29 import org.junit.Before;\r
30 import org.junit.Test;\r
31 import org.onap.policy.apex.context.ContextException;\r
32 import org.onap.policy.apex.context.parameters.ContextParameterConstants;\r
33 import org.onap.policy.apex.context.parameters.DistributorParameters;\r
34 import org.onap.policy.apex.context.parameters.LockManagerParameters;\r
35 import org.onap.policy.apex.context.parameters.PersistorParameters;\r
36 import org.onap.policy.apex.core.engine.EngineParameterConstants;\r
37 import org.onap.policy.apex.core.engine.EngineParameters;\r
38 import org.onap.policy.apex.core.engine.context.ApexInternalContext;\r
39 import org.onap.policy.apex.core.engine.event.EnEvent;\r
40 import org.onap.policy.apex.core.engine.executor.StateExecutor;\r
41 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;\r
42 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;\r
43 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;\r
44 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;\r
45 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;\r
46 import org.onap.policy.apex.model.policymodel.concepts.AxState;\r
47 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;\r
48 import org.onap.policy.common.parameters.ParameterService;\r
49 \r
50 /**\r
51  * Test the JrubyStateFinalizerExecutor class.\r
52  *\r
53  */\r
54 public class JrubyStateFinalizerExecutorTest {\r
55     /**\r
56      * Initiate Parameters.\r
57      */\r
58     @Before\r
59     public void initiateParameters() {\r
60         ParameterService.register(new DistributorParameters());\r
61         ParameterService.register(new LockManagerParameters());\r
62         ParameterService.register(new PersistorParameters());\r
63         ParameterService.register(new EngineParameters());\r
64     }\r
65 \r
66     /**\r
67      * Clear down Parameters.\r
68      */\r
69     @After\r
70     public void clearParameters() {\r
71         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);\r
72         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);\r
73         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);\r
74         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);\r
75     }\r
76 \r
77     @Test\r
78     public void testJrubyStateFinalizerExecutor() {\r
79         JrubyStateFinalizerExecutor jsfe = new JrubyStateFinalizerExecutor();\r
80         assertNotNull(jsfe);\r
81 \r
82         try {\r
83             Field fieldContainer = JrubyStateFinalizerExecutor.class.getDeclaredField("container");\r
84             fieldContainer.setAccessible(true);\r
85             fieldContainer.set(jsfe, null);\r
86             jsfe.prepare();\r
87             fail("test should throw an exception here");\r
88         } catch (Exception jtseException) {\r
89             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());\r
90         }\r
91         ApexInternalContext internalContext = null;\r
92         try {\r
93             internalContext = new ApexInternalContext(new AxPolicyModel());\r
94         } catch (ContextException e) {\r
95             fail("test should not throw an exception here");\r
96         }\r
97 \r
98         StateExecutor parentStateExcutor = null;\r
99         try {\r
100             parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());\r
101         } catch (StateMachineException e) {\r
102             fail("test should not throw an exception here");\r
103         }\r
104 \r
105         AxState state = new AxState();\r
106         parentStateExcutor.setContext(null, state, internalContext);\r
107         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();\r
108         jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);\r
109         try {\r
110             jsfe.prepare();\r
111         } catch (Exception jtseException) {\r
112             fail("test should not throw an exception here");\r
113         }\r
114 \r
115         try {\r
116             jsfe.execute(-1, null, null);\r
117             fail("test should throw an exception here");\r
118         } catch (Exception jtseException) {\r
119             assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" on "\r
120                     + "finalizer logic NULL:0.0.0:NULL:NULL", jtseException.getMessage());\r
121         }\r
122 \r
123         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));\r
124         EnEvent event = new EnEvent(axEvent);\r
125 \r
126         final String jrubyLogic = "if executor.executionId == -1" + "\n return false" + "\n else "\r
127                 + "\n executor.setSelectedStateOutputName(\"SelectedOutputIsMe\")" + "\n return true" + "\n end";\r
128         stateFinalizerLogic.setLogic(jrubyLogic);\r
129 \r
130         state.getStateOutputs().put("SelectedOutputIsMe", null);\r
131         try {\r
132             jsfe.prepare();\r
133             String stateOutput = jsfe.execute(0, null, event);\r
134             assertEquals("SelectedOutputIsMe", stateOutput);\r
135             jsfe.cleanUp();\r
136         } catch (Exception jtseException) {\r
137             jtseException.printStackTrace();\r
138             fail("test should not throw an exception here");\r
139         }\r
140 \r
141     }\r
142 }\r