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