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.HashMap;
29 import java.util.Properties;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.ContextException;
35 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
36 import org.onap.policy.apex.context.parameters.DistributorParameters;
37 import org.onap.policy.apex.context.parameters.LockManagerParameters;
38 import org.onap.policy.apex.context.parameters.PersistorParameters;
39 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
40 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
41 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
42 import org.onap.policy.common.parameters.ParameterService;
45 * Test the MvelTaskExecutor class.
48 public class MvelTaskExecutorTest {
50 * Initiate Parameters.
53 public void initiateParameters() {
54 ParameterService.register(new DistributorParameters());
55 ParameterService.register(new LockManagerParameters());
56 ParameterService.register(new PersistorParameters());
63 public void clearParameters() {
64 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
65 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
66 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
70 public void testMvelTaskExecutor() {
71 MvelTaskExecutor mte = new MvelTaskExecutor();
76 fail("test should throw an exception here");
77 } catch (Exception mteException) {
78 assertEquals(java.lang.NullPointerException.class, mteException.getClass());
81 AxTask task = new AxTask();
82 ApexInternalContext internalContext = null;
84 internalContext = new ApexInternalContext(new AxPolicyModel());
85 } catch (ContextException e) {
86 fail("test should not throw an exception here");
88 mte.setContext(null, task, internalContext);
90 task.getTaskLogic().setLogic("x > 1 2 ()");
93 fail("test should throw an exception here");
94 } catch (Exception mteException) {
95 assertEquals("failed to compile MVEL code for task NULL:0.0.0", mteException.getMessage());
98 task.getTaskLogic().setLogic("x");
102 } catch (Exception mteException) {
103 fail("test should not throw an exception here");
107 mte.execute(-1, new Properties(), null);
108 fail("test should throw an exception here");
109 } catch (Exception mteException) {
110 assertEquals(java.lang.NullPointerException.class, mteException.getClass());
113 Map<String, Object> incomingParameters = new HashMap<>();
115 mte.execute(-1, new Properties(), incomingParameters);
116 fail("test should throw an exception here");
117 } catch (Exception mteException) {
118 assertEquals("failed to execute MVEL code for task NULL:0.0.0", mteException.getMessage());
121 task.getTaskLogic().setLogic("executionId != -1");
124 mte.execute(-1, new Properties(), incomingParameters);
125 fail("test should throw an exception here");
126 } catch (Exception mteException) {
127 assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0",
128 mteException.getMessage());
133 Map<String, Object> returnMap = mte.execute(0, new Properties(), incomingParameters);
134 assertEquals(0, returnMap.size());
136 } catch (Exception mteException) {
137 fail("test should not throw an exception here");