2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2020 Nordix Foundation
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.executor.mvel;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
29 import java.util.HashMap;
31 import java.util.Properties;
32 import java.util.TreeMap;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.apex.context.ContextException;
37 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
38 import org.onap.policy.apex.context.parameters.DistributorParameters;
39 import org.onap.policy.apex.context.parameters.LockManagerParameters;
40 import org.onap.policy.apex.context.parameters.PersistorParameters;
41 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
42 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
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.AxTask;
46 import org.onap.policy.common.parameters.ParameterService;
49 * Test the MvelTaskExecutor class.
52 public class MvelTaskExecutorTest {
54 * Initiate Parameters.
57 public void initiateParameters() {
58 ParameterService.register(new DistributorParameters());
59 ParameterService.register(new LockManagerParameters());
60 ParameterService.register(new PersistorParameters());
67 public void clearParameters() {
68 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
69 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
70 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
74 public void testMvelTaskExecutor() throws StateMachineException, ContextException {
75 MvelTaskExecutor mte = new MvelTaskExecutor();
78 assertThatThrownBy(mte::prepare)
79 .isInstanceOf(java.lang.NullPointerException.class);
80 AxTask task = new AxTask();
81 ApexInternalContext internalContext = null;
82 internalContext = new ApexInternalContext(new AxPolicyModel());
83 task.setInputEvent(new AxEvent());
84 task.setOutputEvents(new TreeMap<>());
85 mte.setContext(null, task, internalContext);
87 task.getTaskLogic().setLogic("x > 1 2 ()");
88 assertThatThrownBy(mte::prepare)
89 .hasMessage("failed to compile MVEL code for task NULL:0.0.0");
90 task.getTaskLogic().setLogic("x");
94 assertThatThrownBy(() -> mte.execute(-1, new Properties(), null))
95 .isInstanceOf(java.lang.NullPointerException.class);
96 Map<String, Object> incomingParameters = new HashMap<>();
97 assertThatThrownBy(() -> mte.execute(-1, new Properties(), incomingParameters))
98 .hasMessage("failed to execute MVEL code for task NULL:0.0.0");
99 task.getTaskLogic().setLogic("executionId != -1");
101 assertThatThrownBy(() -> mte.execute(-1, new Properties(), incomingParameters))
102 .hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0");
105 Map<String, Map<String, Object>> returnMap = mte.execute(0, new Properties(), incomingParameters);
106 assertEquals(0, returnMap.size());