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