e2785d816e419f3ece4757a2f35e238d38f3d503
[policy/apex-pdp.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  *  Copyright (C) 2019 Nordix Foundation.\r
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.\r
5  * ================================================================================\r
6  * Licensed under the Apache License, Version 2.0 (the "License");\r
7  * you may not use this file except in compliance with the License.\r
8  * You may obtain a copy of the License at\r
9  *\r
10  *      http://www.apache.org/licenses/LICENSE-2.0\r
11  *\r
12  * Unless required by applicable law or agreed to in writing, software\r
13  * distributed under the License is distributed on an "AS IS" BASIS,\r
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15  * See the License for the specific language governing permissions and\r
16  * limitations under the License.\r
17  *\r
18  * SPDX-License-Identifier: Apache-2.0\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 \r
22 package org.onap.policy.apex.plugins.executor.jruby;\r
23 \r
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;\r
25 import static org.junit.Assert.assertEquals;\r
26 import static org.junit.Assert.assertNotNull;\r
27 \r
28 import java.lang.reflect.Field;\r
29 import java.util.HashMap;\r
30 import java.util.Map;\r
31 import java.util.Properties;\r
32 import org.junit.After;\r
33 import org.junit.Before;\r
34 import org.junit.Test;\r
35 import org.onap.policy.apex.context.ContextException;\r
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;\r
37 import org.onap.policy.apex.context.parameters.DistributorParameters;\r
38 import org.onap.policy.apex.context.parameters.LockManagerParameters;\r
39 import org.onap.policy.apex.context.parameters.PersistorParameters;\r
40 import org.onap.policy.apex.core.engine.context.ApexInternalContext;\r
41 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;\r
42 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;\r
43 import org.onap.policy.apex.model.policymodel.concepts.AxTask;\r
44 import org.onap.policy.common.parameters.ParameterService;\r
45 \r
46 /**\r
47  * Test the JrubyTaskExecutor class.\r
48  *\r
49  */\r
50 public class JrubyTaskExecutorTest {\r
51     /**\r
52      * Initiate Parameters.\r
53      */\r
54     @Before\r
55     public void initiateParameters() {\r
56         ParameterService.register(new DistributorParameters());\r
57         ParameterService.register(new LockManagerParameters());\r
58         ParameterService.register(new PersistorParameters());\r
59     }\r
60 \r
61     /**\r
62      * Clear Parameters.\r
63      */\r
64     @After\r
65     public void clearParameters() {\r
66         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);\r
67         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);\r
68         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);\r
69     }\r
70 \r
71     @Test\r
72     public void testJrubyTaskExecutor() throws StateMachineException, ContextException,\r
73         IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {\r
74         // Run test twice to check for incorrect shutdown activity\r
75         jrubyExecutorTest();\r
76         jrubyExecutorTest();\r
77     }\r
78 \r
79     /**\r
80      * Test the JRuby executor.\r
81      */\r
82     private void jrubyExecutorTest() throws StateMachineException, ContextException,\r
83         IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {\r
84         JrubyTaskExecutor jte = new JrubyTaskExecutor();\r
85         assertNotNull(jte);\r
86 \r
87         Field fieldContainer = JrubyTaskExecutor.class.getDeclaredField("container");\r
88         fieldContainer.setAccessible(true);\r
89         fieldContainer.set(jte, null);\r
90         assertThatThrownBy(jte::prepare).isInstanceOf(java.lang.NullPointerException.class);\r
91         AxTask task = new AxTask();\r
92         ApexInternalContext internalContext = null;\r
93         internalContext = new ApexInternalContext(new AxPolicyModel());\r
94 \r
95         jte.setContext(null, task, internalContext);\r
96 \r
97         jte.prepare();\r
98 \r
99         Map<String, Object> incomingParameters = new HashMap<>();\r
100         assertThatThrownBy(() -> jte.execute(-1, new Properties(), incomingParameters))\r
101             .hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0");\r
102         final String jrubyLogic = "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true"\r
103                         + "\n end";\r
104         task.getTaskLogic().setLogic(jrubyLogic);\r
105 \r
106         jte.prepare();\r
107         Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);\r
108         assertEquals(0, returnMap.size());\r
109         jte.cleanUp();\r
110 \r
111         jte.prepare();\r
112         Map<String, Object> returnMap1 = jte.execute(0, new Properties(), incomingParameters);\r
113         assertEquals(0, returnMap1.size());\r
114         jte.cleanUp();\r
115     }\r
116 }\r