5e66be30f20b142ab30105a14153a1548058e907
[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.mvel;
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.EngineParameterConstants;
36 import org.onap.policy.apex.core.engine.EngineParameters;
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.core.engine.executor.StateExecutor;
40 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
41 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
44 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
45 import org.onap.policy.apex.model.policymodel.concepts.AxState;
46 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
47 import org.onap.policy.common.parameters.ParameterService;
48 import org.slf4j.ext.XLogger;
49 import org.slf4j.ext.XLoggerFactory;
50
51 /**
52  * Test the MvelStateFinalizerExecutor class.
53  *
54  */
55 public class MvelStateFinalizerExecutorTest {
56
57     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MvelStateFinalizerExecutorTest.class);
58
59     /**
60      * Initiate Parameters.
61      */
62     @Before
63     public void initiateParameters() {
64         ParameterService.register(new DistributorParameters());
65         ParameterService.register(new LockManagerParameters());
66         ParameterService.register(new PersistorParameters());
67         ParameterService.register(new EngineParameters());
68     }
69
70     /**
71      * Clear down Parameters.
72      */
73     @After
74     public void clearParameters() {
75         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
76         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
77         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
78         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
79     }
80
81     @Test
82     public void testJavaStateFinalizerExecutor() {
83         MvelStateFinalizerExecutor msfe = new MvelStateFinalizerExecutor();
84         assertNotNull(msfe);
85
86         try {
87             msfe.prepare();
88             fail("test should throw an exception here");
89         } catch (Exception msfeException) {
90             assertEquals(java.lang.NullPointerException.class, msfeException.getClass());
91         }
92
93         ApexInternalContext internalContext = null;
94         try {
95             internalContext = new ApexInternalContext(new AxPolicyModel());
96         } catch (ContextException e) {
97             fail("test should not throw an exception here");
98         }
99
100         StateExecutor parentStateExcutor = null;
101         try {
102             parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
103         } catch (StateMachineException e) {
104             fail("test should not throw an exception here");
105         }
106
107         AxState state = new AxState();
108         parentStateExcutor.setContext(null, state, internalContext);
109         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
110         msfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
111
112         stateFinalizerLogic.setLogic("x > 1 2 ()");
113         try {
114             msfe.prepare();
115             fail("test should throw an exception here");
116         } catch (Exception msfeException) {
117             assertEquals("failed to compile MVEL code for state NULL:0.0.0:NULL:NULL", msfeException.getMessage());
118         }
119
120         stateFinalizerLogic.setLogic("java.lang.String");
121
122         try {
123             msfe.prepare();
124         } catch (Exception msfeException) {
125             fail("test should not throw an exception here");
126         }
127
128         try {
129             msfe.execute(-1, null, null);
130             fail("test should throw an exception here");
131         } catch (Exception msfeException) {
132             assertEquals("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL",
133                     msfeException.getMessage());
134         }
135
136         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
137         EnEvent event = new EnEvent(axEvent);
138         try {
139             msfe.execute(-1, null, event);
140             fail("test should throw an exception here");
141         } catch (Exception msfeException) {
142             assertEquals("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL",
143                     msfeException.getMessage());
144         }
145
146         stateFinalizerLogic.setLogic("executionId !=-1");
147         try {
148             msfe.prepare();
149             msfe.execute(-1, null, event);
150             fail("test should throw an exception here");
151         } catch (Exception msfeException) {
152             assertEquals(
153                     "execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
154                             + "on finalizer logic NULL:0.0.0:NULL:NULL",
155                     msfeException.getMessage());
156         }
157
158         stateFinalizerLogic.setLogic(
159                 "if (executionId == -1) {return false;}setSelectedStateOutputName(\"SelectedOutputIsMe\");"
160                         + "return true;");
161         state.getStateOutputs().put("SelectedOutputIsMe", null);
162         try {
163             msfe.prepare();
164             String stateOutput = msfe.execute(0, null, event);
165             assertEquals("SelectedOutputIsMe", stateOutput);
166         } catch (Exception msfeException) {
167             LOGGER.warn("Unexpected exception happened here.", msfeException);
168             fail("test should not throw an exception here");
169         } finally {
170             try {
171                 msfe.cleanUp();
172             } catch (StateMachineException msfeException) {
173                 LOGGER.warn("Unexpected exception happened here.", msfeException);
174                 fail("test should not throw an exception here");
175             }
176         }
177     }
178 }