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.mvel;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
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;
53 * Test the MvelStateFinalizerExecutor class.
56 public class MvelStateFinalizerExecutorTest {
58 private static final XLogger LOGGER = XLoggerFactory.getXLogger(MvelStateFinalizerExecutorTest.class);
61 * Initiate Parameters.
64 public void initiateParameters() {
65 ParameterService.register(new DistributorParameters());
66 ParameterService.register(new LockManagerParameters());
67 ParameterService.register(new PersistorParameters());
68 ParameterService.register(new EngineParameters());
72 * Clear down Parameters.
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);
83 public void testJavaStateFinalizerExecutor() {
84 MvelStateFinalizerExecutor msfe = new MvelStateFinalizerExecutor();
89 fail("test should throw an exception here");
90 } catch (Exception msfeException) {
91 assertEquals(java.lang.NullPointerException.class, msfeException.getClass());
94 ApexInternalContext internalContext = null;
96 internalContext = new ApexInternalContext(new AxPolicyModel());
97 } catch (ContextException e) {
98 fail("test should not throw an exception here");
101 StateExecutor parentStateExcutor = null;
103 parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
104 } catch (StateMachineException e) {
105 fail("test should not throw an exception here");
108 AxState state = new AxState();
109 parentStateExcutor.setContext(null, state, internalContext);
110 AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
111 msfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
113 stateFinalizerLogic.setLogic("x > 1 2 ()");
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());
121 stateFinalizerLogic.setLogic("java.lang.String");
125 } catch (Exception msfeException) {
126 fail("test should not throw an exception here");
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());
137 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
138 EnEvent event = new EnEvent(axEvent);
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());
147 stateFinalizerLogic.setLogic("executionId !=-1");
150 msfe.execute(-1, new Properties(), event);
151 fail("test should throw an exception here");
152 } catch (Exception msfeException) {
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());
159 stateFinalizerLogic.setLogic(
160 "if (executionId == -1) {return false;}setSelectedStateOutputName(\"SelectedOutputIsMe\");"
162 state.getStateOutputs().put("SelectedOutputIsMe", null);
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");
173 } catch (StateMachineException msfeException) {
174 LOGGER.warn("Unexpected exception happened here.", msfeException);
175 fail("test should not throw an exception here");