88227106b83431a232063597da3377b65a0cf28d
[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 java.util.HashMap;\r
28 import java.util.Map;\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.context.ApexInternalContext;\r
38 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;\r
39 import org.onap.policy.apex.model.policymodel.concepts.AxTask;\r
40 import org.onap.policy.common.parameters.ParameterService;\r
41 \r
42 /**\r
43  * Test the JrubyTaskExecutor class.\r
44  *\r
45  */\r
46 public class JrubyTaskExecutorTest {\r
47     /**\r
48      * Initiate Parameters.\r
49      */\r
50     @Before\r
51     public void initiateParameters() {\r
52         ParameterService.register(new DistributorParameters());\r
53         ParameterService.register(new LockManagerParameters());\r
54         ParameterService.register(new PersistorParameters());\r
55     }\r
56 \r
57     /**\r
58      * Clear Parameters.\r
59      */\r
60     @After\r
61     public void clearParameters() {\r
62         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);\r
63         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);\r
64         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);\r
65     }\r
66 \r
67     @Test\r
68     public void testJrubyTaskExecutor() {\r
69         JrubyTaskExecutor jte = new JrubyTaskExecutor();\r
70         assertNotNull(jte);\r
71         try {\r
72             Field fieldContainer = JrubyTaskExecutor.class.getDeclaredField("container");\r
73             fieldContainer.setAccessible(true);\r
74             fieldContainer.set(jte, null);\r
75             jte.prepare();\r
76             fail("test should throw an exception here");\r
77         } catch (Exception jtseException) {\r
78             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());\r
79         }\r
80 \r
81         AxTask task = new AxTask();\r
82         ApexInternalContext internalContext = null;\r
83         try {\r
84             internalContext = new ApexInternalContext(new AxPolicyModel());\r
85         } catch (ContextException e) {\r
86             fail("test should not throw an exception here");\r
87         }\r
88         jte.setContext(null, task, internalContext);\r
89         try {\r
90             jte.prepare();\r
91         } catch (Exception jtseException) {\r
92             fail("test should not throw an exception here");\r
93         }\r
94 \r
95         Map<String, Object> incomingParameters = new HashMap<>();\r
96         try {\r
97             jte.execute(-1, incomingParameters);\r
98             fail("test should throw an exception here");\r
99         } catch (Exception jteException) {\r
100             assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0",\r
101                     jteException.getMessage());\r
102         }\r
103 \r
104         final String jrubyLogic =\r
105                 "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true" + "\n end";\r
106         task.getTaskLogic().setLogic(jrubyLogic);\r
107 \r
108         try {\r
109             jte.prepare();\r
110             Map<String, Object> returnMap = jte.execute(0, incomingParameters);\r
111             assertEquals(0, returnMap.size());\r
112             jte.cleanUp();\r
113         } catch (Exception jteException) {\r
114             fail("test should not throw an exception here");\r
115         }\r
116     }\r
117 }\r