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