47e98f391a787b9cb109a09a3f7983e79e4642ec
[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 java.util.HashMap;
28 import java.util.Map;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
34 import org.onap.policy.apex.context.parameters.DistributorParameters;
35 import org.onap.policy.apex.context.parameters.LockManagerParameters;
36 import org.onap.policy.apex.context.parameters.PersistorParameters;
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
40 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
41 import org.onap.policy.common.parameters.ParameterService;
42
43 /**
44  * Test the JavaTaskExecutor class.
45  *
46  */
47 public class JythonTaskExecutorTest {
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 testJythonTaskExecutor() {
70         JythonTaskExecutor jte = new JythonTaskExecutor();
71         assertNotNull(jte);
72
73         try {
74             jte.prepare();
75             fail("test should throw an exception here");
76         } catch (Exception jteException) {
77             assertEquals(java.lang.NullPointerException.class, jteException.getClass());
78         }
79
80         AxTask task = new AxTask();
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         jte.setContext(null, task, internalContext);
88
89         task.getTaskLogic().setLogic("return false");
90         try {
91             jte.prepare();
92             fail("test should throw an exception here");
93         } catch (Exception jteException) {
94             assertEquals("failed to compile Jython code for task NULL:0.0.0", jteException.getMessage());
95         }
96
97         task.getTaskLogic().setLogic("java.lang.String");
98
99         try {
100             jte.prepare();
101         } catch (Exception jteException) {
102             fail("test should not throw an exception here");
103         }
104
105         try {
106             jte.execute(-1, null, null);
107             fail("test should throw an exception here");
108         } catch (Exception jteException) {
109             assertEquals(java.lang.NullPointerException.class, jteException.getClass());
110         }
111
112         Map<String, Object> incomingParameters = new HashMap<>();
113         try {
114             jte.execute(-1, null, incomingParameters);
115             fail("test should throw an exception here");
116         } catch (Exception jteException) {
117             assertEquals("failed to execute Jython code for task NULL:0.0.0", jteException.getMessage());
118         }
119
120         String scriptSource = "for i in range(0,10): print(i)";
121         task.getTaskLogic().setLogic(scriptSource);
122         AxArtifactKey taskKey = new AxArtifactKey("String", "0.0.1");
123         task.setKey(taskKey);
124
125         try {
126             jte.prepare();
127             Map<String, Object> returnMap = jte.execute(-1, null, incomingParameters);
128             assertEquals(0, returnMap.size());
129             jte.cleanUp();
130             fail("test should throw an exception here");
131         } catch (Exception jteException) {
132             assertEquals("failed to execute Jython code for task String:0.0.1", jteException.getMessage());
133         }
134
135         scriptSource = "returnValue=('' if executor == -1 else True)";
136         task.getTaskLogic().setLogic(scriptSource);
137         try {
138             jte.prepare();
139             Map<String, Object> returnMap = jte.execute(0, null, incomingParameters);
140             assertEquals(0, returnMap.size());
141             jte.cleanUp();
142         } catch (Exception jteException) {
143             fail("test should not throw an exception here");
144         }
145     }
146 }