Replace try/catch with assertj
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-mvel / src / test / java / org / onap / policy / apex / plugins / executor / mvel / MvelTaskExecutorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.executor.mvel;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import java.util.HashMap;
29 import java.util.Map;
30 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.core.engine.executor.exception.StateMachineException;
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
42 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
43 import org.onap.policy.common.parameters.ParameterService;
44
45 /**
46  * Test the MvelTaskExecutor class.
47  *
48  */
49 public class MvelTaskExecutorTest {
50     /**
51      * Initiate Parameters.
52      */
53     @Before
54     public void initiateParameters() {
55         ParameterService.register(new DistributorParameters());
56         ParameterService.register(new LockManagerParameters());
57         ParameterService.register(new PersistorParameters());
58     }
59
60     /**
61      * Clear Parameters.
62      */
63     @After
64     public void clearParameters() {
65         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
66         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
67         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
68     }
69
70     @Test
71     public void testMvelTaskExecutor() throws StateMachineException, ContextException {
72         MvelTaskExecutor mte = new MvelTaskExecutor();
73         assertNotNull(mte);
74
75         assertThatThrownBy(mte::prepare)
76             .isInstanceOf(java.lang.NullPointerException.class);
77         AxTask task = new AxTask();
78         ApexInternalContext internalContext = null;
79         internalContext = new ApexInternalContext(new AxPolicyModel());
80         mte.setContext(null, task, internalContext);
81
82         task.getTaskLogic().setLogic("x > 1 2 ()");
83         assertThatThrownBy(mte::prepare)
84             .hasMessage("failed to compile MVEL code for task NULL:0.0.0");
85         task.getTaskLogic().setLogic("x");
86
87         mte.prepare();
88
89         assertThatThrownBy(() -> mte.execute(-1, new Properties(), null))
90             .isInstanceOf(java.lang.NullPointerException.class);
91         Map<String, Object> incomingParameters = new HashMap<>();
92         assertThatThrownBy(() -> mte.execute(-1, new Properties(), incomingParameters))
93             .hasMessage("failed to execute MVEL code for task NULL:0.0.0");
94         task.getTaskLogic().setLogic("executionId != -1");
95         mte.prepare();
96         assertThatThrownBy(() -> mte.execute(-1, new Properties(), incomingParameters))
97             .hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0");
98
99         mte.prepare();
100         Map<String, Object> returnMap = mte.execute(0, new Properties(), incomingParameters);
101         assertEquals(0, returnMap.size());
102         mte.cleanUp();
103     }
104 }