2ae41d6e6b9f27427a4f5558e0467e131ef25e49
[policy/apex-pdp.git] /
1 /*-
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
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 import java.util.Map;
27 import java.util.TreeMap;
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.EngineParameterConstants;
37 import org.onap.policy.apex.core.engine.EngineParameters;
38 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
39 import org.onap.policy.apex.core.engine.event.EnEvent;
40 import org.onap.policy.apex.core.engine.executor.StateExecutor;
41 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
42 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
45 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
46 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
47 import org.onap.policy.apex.model.policymodel.concepts.AxState;
48 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
49 import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput;
50 import org.onap.policy.common.parameters.ParameterService;
51
52 /**
53  * Test the JythonStateFinalizerExecutor class.
54  *
55  */
56 public class JythonStateFinalizerExecutorTest {
57     /**
58      * Initiate Parameters.
59      */
60     @Before
61     public void initiateParameters() {
62         ParameterService.register(new DistributorParameters());
63         ParameterService.register(new LockManagerParameters());
64         ParameterService.register(new PersistorParameters());
65         ParameterService.register(new EngineParameters());
66     }
67
68     /**
69      * Clear down Parameters.
70      */
71     @After
72     public void clearParameters() {
73         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
74         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
75         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
76         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
77     }
78
79     @Test
80     public void testJythonStateFinalizerExecutor() {
81         JythonStateFinalizerExecutor jsfe = new JythonStateFinalizerExecutor();
82         assertNotNull(jsfe);
83
84         try {
85             jsfe.prepare();
86             fail("test should throw an exception here");
87         } catch (Exception jtseException) {
88             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
89         }
90
91         ApexInternalContext internalContext = null;
92         try {
93             internalContext = new ApexInternalContext(new AxPolicyModel());
94         } catch (ContextException e) {
95             fail("test should not throw an exception here");
96         }
97
98         StateExecutor parentStateExcutor = null;
99         try {
100             parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
101         } catch (StateMachineException e) {
102             fail("test should not throw an exception here");
103         }
104
105         AxState state = new AxState();
106         Map<String, AxStateOutput> stateOutputs = new TreeMap<>();
107         AxArtifactKey triggerKey = new AxArtifactKey("TriggerName", "0.0.1");
108         AxStateOutput isMe = new AxStateOutput(new AxReferenceKey(), triggerKey,
109                 new AxReferenceKey());
110         stateOutputs.put("SelectedOutputIsMe", isMe);
111         state.setStateOutputs(stateOutputs);
112
113         parentStateExcutor.setContext(null, state, internalContext);
114         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
115         jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
116
117         stateFinalizerLogic.setLogic("return false");
118
119         try {
120             jsfe.prepare();
121             fail("test should throw an exception here");
122         } catch (Exception jtseException) {
123             assertEquals(
124                     "failed to compile Jython code for state finalizer AxReferenceKey:"
125                             + "(parentKeyName=NULL,parentKeyVersion=0.0.0,parentLocalName=NULL,localName=NULL)",
126                     jtseException.getMessage());
127         }
128
129         String scriptSource = "for i in range(0,10): print(i)";
130         stateFinalizerLogic.setLogic(scriptSource);
131         try {
132             jsfe.prepare();
133             jsfe.execute(-1, null);
134             fail("test should throw an exception here");
135         } catch (Exception jtseException) {
136             assertEquals(
137                     "failed to execute Jython code for state finalizer AxReferenceKey:(parentKeyName=NULL,"
138                             + "parentKeyVersion=0.0.0,parentLocalName=NULL,localName=NULL)",
139                     jtseException.getMessage());
140         }
141
142         scriptSource = "setattr(executor, 'selectedStateOutputName', 'SelectedOutputIsMe')\n"
143                 + "returnValue=('' if executor == -1 else True)";
144           stateFinalizerLogic.setLogic(scriptSource);
145          try {
146              jsfe.prepare();
147          } catch (Exception jteException) {
148              fail("test should not throw an exception here");
149          }
150
151         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
152         EnEvent event = new EnEvent(axEvent);
153         try {
154             jsfe.execute(0, event);
155         } catch (Exception jtseException) {
156             jtseException.printStackTrace();
157             fail("test should not throw an exception here");
158         }
159
160         try {
161             jsfe.prepare();
162             String stateOutput = jsfe.execute(0, event);
163             assertEquals("SelectedOutputIsMe", stateOutput);
164             jsfe.cleanUp();
165         } catch (Exception jtseException) {
166             jtseException.printStackTrace();
167             fail("test should not throw an exception here");
168         }
169     }
170 }