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