2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Ericsson. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.executor.jython;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26 import java.util.HashMap;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.ContextException;
32 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
33 import org.onap.policy.apex.context.parameters.DistributorParameters;
34 import org.onap.policy.apex.context.parameters.LockManagerParameters;
35 import org.onap.policy.apex.context.parameters.PersistorParameters;
36 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
39 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
40 import org.onap.policy.common.parameters.ParameterService;
43 * Test the JavaTaskExecutor class.
46 public class JythonTaskExecutorTest {
48 * Initiate Parameters.
51 public void initiateParameters() {
52 ParameterService.register(new DistributorParameters());
53 ParameterService.register(new LockManagerParameters());
54 ParameterService.register(new PersistorParameters());
61 public void clearParameters() {
62 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
63 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
64 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
68 public void testJythonTaskExecutor() {
69 JythonTaskExecutor jte = new JythonTaskExecutor();
74 fail("test should throw an exception here");
75 } catch (Exception jteException) {
76 assertEquals(java.lang.NullPointerException.class, jteException.getClass());
79 AxTask task = new AxTask();
80 ApexInternalContext internalContext = null;
82 internalContext = new ApexInternalContext(new AxPolicyModel());
83 } catch (ContextException e) {
84 fail("test should not throw an exception here");
86 jte.setContext(null, task, internalContext);
88 task.getTaskLogic().setLogic("return false");
91 fail("test should throw an exception here");
92 } catch (Exception jteException) {
93 assertEquals("failed to compile Jython code for task NULL:0.0.0", jteException.getMessage());
96 task.getTaskLogic().setLogic("java.lang.String");
100 } catch (Exception jteException) {
101 fail("test should not throw an exception here");
105 jte.execute(-1, null);
106 fail("test should throw an exception here");
107 } catch (Exception jteException) {
108 assertEquals(java.lang.NullPointerException.class, jteException.getClass());
111 Map<String, Object> incomingParameters = new HashMap<>();
113 jte.execute(-1, incomingParameters);
114 fail("test should throw an exception here");
115 } catch (Exception jteException) {
116 assertEquals("failed to execute Jython code for task NULL:0.0.0", jteException.getMessage());
119 String scriptSource = "for i in range(0,10): print(i)";
120 task.getTaskLogic().setLogic(scriptSource);
121 AxArtifactKey taskKey = new AxArtifactKey("String", "0.0.1");
122 task.setKey(taskKey);
126 Map<String, Object> returnMap = jte.execute(-1, incomingParameters);
127 assertEquals(0, returnMap.size());
129 fail("test should throw an exception here");
130 } catch (Exception jteException) {
131 assertEquals("failed to execute Jython code for task String:0.0.1", jteException.getMessage());
134 scriptSource = "returnValue=('' if executor == -1 else True)";
135 task.getTaskLogic().setLogic(scriptSource);
138 Map<String, Object> returnMap = jte.execute(0, incomingParameters);
139 assertEquals(0, returnMap.size());
141 } catch (Exception jteException) {
142 fail("test should not throw an exception here");