3d59c4b21e6081342f05f2fe2cd78f19cdb4f81e
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.executor.mvel;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import java.util.HashMap;
30 import java.util.Map;
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;
47
48 /**
49  * Test the MvelTaskExecutor class.
50  *
51  */
52 public class MvelTaskExecutorTest {
53     /**
54      * Initiate Parameters.
55      */
56     @Before
57     public void initiateParameters() {
58         ParameterService.register(new DistributorParameters());
59         ParameterService.register(new LockManagerParameters());
60         ParameterService.register(new PersistorParameters());
61     }
62
63     /**
64      * Clear Parameters.
65      */
66     @After
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);
71     }
72
73     @Test
74     public void testMvelTaskExecutor() throws StateMachineException, ContextException {
75         MvelTaskExecutor mte = new MvelTaskExecutor();
76         assertNotNull(mte);
77
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);
86
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");
91
92         mte.prepare();
93
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");
100         mte.prepare();
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");
103
104         mte.prepare();
105         Map<String, Map<String, Object>> returnMap = mte.execute(0, new Properties(), incomingParameters);
106         assertEquals(0, returnMap.size());
107         mte.cleanUp();
108     }
109 }