def5e1a7f6542d029b995693309f0a797e12b2c0
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-jruby / src / test / java / org / onap / policy / apex / plugins / executor / jruby / JrubyTaskSelectExecutorTest.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 \r
30 import org.junit.After;\r
31 import org.junit.Before;\r
32 import org.junit.Test;\r
33 import org.onap.policy.apex.context.ContextException;\r
34 import org.onap.policy.apex.context.parameters.ContextParameterConstants;\r
35 import org.onap.policy.apex.context.parameters.DistributorParameters;\r
36 import org.onap.policy.apex.context.parameters.LockManagerParameters;\r
37 import org.onap.policy.apex.context.parameters.PersistorParameters;\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.model.basicmodel.concepts.AxArtifactKey;\r
41 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;\r
42 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;\r
43 import org.onap.policy.apex.model.policymodel.concepts.AxState;\r
44 import org.onap.policy.common.parameters.ParameterService;\r
45 \r
46 /**\r
47  * Test the JrubyTaskSelectExecutor class.\r
48  *\r
49  */\r
50 public class JrubyTaskSelectExecutorTest {\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 testJrubyTaskSelectExecutor() {\r
73         JrubyTaskSelectExecutor jtse = new JrubyTaskSelectExecutor();\r
74         assertNotNull(jtse);\r
75         assertNotNull(jtse.getOutputEventSet());\r
76         try {\r
77             Field fieldContainer = JrubyTaskSelectExecutor.class.getDeclaredField("container");\r
78             fieldContainer.setAccessible(true);\r
79             fieldContainer.set(jtse, null);\r
80             jtse.prepare();\r
81             fail("test should throw an exception here");\r
82         } catch (Exception jtseException) {\r
83             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());\r
84         }\r
85 \r
86         AxState state = new AxState();\r
87         ApexInternalContext internalContext = null;\r
88         try {\r
89             internalContext = new ApexInternalContext(new AxPolicyModel());\r
90         } catch (ContextException e) {\r
91             fail("test should not throw an exception here");\r
92         }\r
93         jtse.setContext(null, state, internalContext);\r
94 \r
95 \r
96         try {\r
97             jtse.prepare();\r
98         } catch (Exception jtseException) {\r
99             fail("test should not throw an exception here");\r
100         }\r
101         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));\r
102         EnEvent event = new EnEvent(axEvent);\r
103         try {\r
104             jtse.execute(-1, new Properties(), event);\r
105             fail("test should throw an exception here");\r
106         } catch (Exception jtseException) {\r
107             assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",\r
108                     jtseException.getMessage());\r
109         }\r
110 \r
111         final String jrubyLogic =\r
112                 "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true" + "\n end";\r
113         state.getTaskSelectionLogic().setLogic(jrubyLogic);\r
114 \r
115         try {\r
116             jtse.prepare();\r
117             AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);\r
118             assertEquals("NULL:0.0.0", taskKey.getId());\r
119             jtse.cleanUp();\r
120         } catch (Exception jtseException) {\r
121             fail("test should not throw an exception here");\r
122         }\r
123     }\r
124 }\r