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
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;
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;
44 * Test the JythonTaskSelectExecutor class.
47 public class JythonTaskSelectExecutorTest {
49 * Initiate Parameters.
52 public void initiateParameters() {
53 ParameterService.register(new DistributorParameters());
54 ParameterService.register(new LockManagerParameters());
55 ParameterService.register(new PersistorParameters());
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);
69 public void testJythonTaskSelectExecutor() {
70 JythonTaskSelectExecutor jtse = new JythonTaskSelectExecutor();
75 fail("test should throw an exception here");
76 } catch (Exception jtseException) {
77 assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
80 AxState state = new AxState();
81 ApexInternalContext internalContext = null;
83 internalContext = new ApexInternalContext(new AxPolicyModel());
84 } catch (ContextException e) {
85 fail("test should not throw an exception here");
87 jtse.setContext(null, state, internalContext);
89 state.getTaskSelectionLogic().setLogic("return false");
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());
98 String scriptSource = "for i in range(0,10): print(i)";
99 state.getTaskSelectionLogic().setLogic(scriptSource);
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());
108 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
109 EnEvent event = new EnEvent(axEvent);
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());
119 scriptSource = "returnValue=('' if executor == -1 else True)";
120 state.getTaskSelectionLogic().setLogic(scriptSource);
123 jtse.execute(-1, null, event);
125 } catch (Exception jtseException) {
126 fail("test should not throw an exception here");