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