119f4d0352d368705991058dd02cd47e62fa1c15
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.executor.jython;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
32 import org.onap.policy.apex.context.parameters.DistributorParameters;
33 import org.onap.policy.apex.context.parameters.LockManagerParameters;
34 import org.onap.policy.apex.context.parameters.PersistorParameters;
35 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
36 import org.onap.policy.apex.core.engine.event.EnEvent;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
39 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
40 import org.onap.policy.apex.model.policymodel.concepts.AxState;
41 import org.onap.policy.common.parameters.ParameterService;
42
43 /**
44  * Test the JythonTaskSelectExecutor class.
45  *
46  */
47 public class JythonTaskSelectExecutorTest {
48     /**
49      * Initiate Parameters.
50      */
51     @Before
52     public void initiateParameters() {
53         ParameterService.register(new DistributorParameters());
54         ParameterService.register(new LockManagerParameters());
55         ParameterService.register(new PersistorParameters());
56     }
57
58     /**
59      * Clear Parameters.
60      */
61     @After
62     public void clearParameters() {
63         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
64         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
65         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
66     }
67
68     @Test
69     public void testJythonTaskSelectExecutor() {
70         JythonTaskSelectExecutor jtse = new JythonTaskSelectExecutor();
71         assertNotNull(jtse);
72
73         try {
74             jtse.prepare();
75             fail("test should throw an exception here");
76         } catch (Exception jtseException) {
77             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
78         }
79
80         AxState state = new AxState();
81         ApexInternalContext internalContext = null;
82         try {
83             internalContext = new ApexInternalContext(new AxPolicyModel());
84         } catch (ContextException e) {
85             fail("test should not throw an exception here");
86         }
87         jtse.setContext(null, state, internalContext);
88
89         state.getTaskSelectionLogic().setLogic("return false");
90         try {
91             jtse.prepare();
92             fail("test should throw an exception here");
93         } catch (Exception jteException) {
94             assertEquals("failed to compile Jython code for task selection logic in NULL:0.0.0:NULL:NULL",
95                     jteException.getMessage());
96         }
97
98         String scriptSource = "for i in range(0,10): print(i)";
99         state.getTaskSelectionLogic().setLogic(scriptSource);
100         try {
101             jtse.prepare();
102             jtse.execute(-1, null, null);
103             fail("test should throw an exception here");
104         } catch (Exception jtseException) {
105             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
106         }
107
108         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
109         EnEvent event = new EnEvent(axEvent);
110         try {
111             jtse.prepare();
112             jtse.execute(-1, null, event);
113             fail("test should throw an exception here");
114         } catch (Exception jtseException) {
115             assertEquals("failed to execute Jython code for task selection logic in NULL:0.0.0:NULL:NULL",
116                     jtseException.getMessage());
117         }
118
119         scriptSource = "returnValue=('' if executor == -1 else True)";
120         state.getTaskSelectionLogic().setLogic(scriptSource);
121         try {
122             jtse.prepare();
123             jtse.execute(-1, null, event);
124             jtse.cleanUp();
125         } catch (Exception jtseException) {
126             fail("test should not throw an exception here");
127         }
128     }
129 }