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 / MvelTaskSelectExecutorTest.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.Properties;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
34 import org.onap.policy.apex.context.parameters.DistributorParameters;
35 import org.onap.policy.apex.context.parameters.LockManagerParameters;
36 import org.onap.policy.apex.context.parameters.PersistorParameters;
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
42 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
43 import org.onap.policy.apex.model.policymodel.concepts.AxState;
44 import org.onap.policy.common.parameters.ParameterService;
45
46 /**
47  * Test the MvelTaskSelectExecutor class.
48  *
49  */
50 public class MvelTaskSelectExecutorTest {
51     /**
52      * Initiate Parameters.
53      */
54     @Before
55     public void initiateParameters() {
56         ParameterService.register(new DistributorParameters());
57         ParameterService.register(new LockManagerParameters());
58         ParameterService.register(new PersistorParameters());
59     }
60
61     /**
62      * Clear Parameters.
63      */
64     @After
65     public void clearParameters() {
66         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
67         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
68         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
69     }
70
71     @Test
72     public void testJavaTaskSelectExecutor() throws StateMachineException, ContextException {
73         MvelTaskSelectExecutor mtse = new MvelTaskSelectExecutor();
74         assertNotNull(mtse);
75
76         assertThatThrownBy(mtse::prepare)
77             .isInstanceOf(java.lang.NullPointerException.class);
78         AxState state = new AxState();
79         ApexInternalContext internalContext = null;
80         internalContext = new ApexInternalContext(new AxPolicyModel());
81         mtse.setContext(null, state, internalContext);
82
83         state.getTaskSelectionLogic().setLogic("x > 1 2 ()");
84         assertThatThrownBy(mtse::prepare)
85             .hasMessage("failed to compile MVEL code for state NULL:0.0.0:NULL:NULL");
86         state.getTaskSelectionLogic().setLogic("java.lang.String");
87
88         mtse.prepare();
89
90         assertThatThrownBy(() -> mtse.execute(-1, new Properties(), null))
91             .isInstanceOf(java.lang.NullPointerException.class);
92         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
93         EnEvent event = new EnEvent(axEvent);
94         assertThatThrownBy(() -> mtse.execute(-1, new Properties(), event))
95             .hasMessage("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL");
96         state.getTaskSelectionLogic().setLogic("executionId != -1");
97         mtse.prepare();
98         assertThatThrownBy(() -> mtse.execute(-1, new Properties(), event))
99             .hasMessageContaining("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"");
100         mtse.prepare();
101         AxArtifactKey taskKey = mtse.execute(0, new Properties(), event);
102         assertEquals("NULL:0.0.0", taskKey.getId());
103         mtse.cleanUp();
104     }
105 }