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