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