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 / MvelStateFinalizerExecutorTest.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.EngineParameterConstants;
38 import org.onap.policy.apex.core.engine.EngineParameters;
39 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
40 import org.onap.policy.apex.core.engine.event.EnEvent;
41 import org.onap.policy.apex.core.engine.executor.StateExecutor;
42 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
43 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
46 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
47 import org.onap.policy.apex.model.policymodel.concepts.AxState;
48 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
49 import org.onap.policy.common.parameters.ParameterService;
50 import org.slf4j.ext.XLogger;
51 import org.slf4j.ext.XLoggerFactory;
52
53 /**
54  * Test the MvelStateFinalizerExecutor class.
55  *
56  */
57 public class MvelStateFinalizerExecutorTest {
58
59     private static final XLogger LOGGER = XLoggerFactory.getXLogger(MvelStateFinalizerExecutorTest.class);
60
61     /**
62      * Initiate Parameters.
63      */
64     @Before
65     public void initiateParameters() {
66         ParameterService.register(new DistributorParameters());
67         ParameterService.register(new LockManagerParameters());
68         ParameterService.register(new PersistorParameters());
69         ParameterService.register(new EngineParameters());
70     }
71
72     /**
73      * Clear down Parameters.
74      */
75     @After
76     public void clearParameters() {
77         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
78         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
79         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
80         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
81     }
82
83     @Test
84     public void testJavaStateFinalizerExecutor() throws StateMachineException, ContextException {
85         MvelStateFinalizerExecutor msfe = new MvelStateFinalizerExecutor();
86         assertNotNull(msfe);
87
88         assertThatThrownBy(msfe::prepare)
89             .isInstanceOf(java.lang.NullPointerException.class);
90         ApexInternalContext internalContext = null;
91         internalContext = new ApexInternalContext(new AxPolicyModel());
92
93         StateExecutor parentStateExcutor = null;
94
95         parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
96
97         AxState state = new AxState();
98         parentStateExcutor.setContext(null, state, internalContext);
99         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
100         msfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
101
102         stateFinalizerLogic.setLogic("x > 1 2 ()");
103         assertThatThrownBy(msfe::prepare)
104             .hasMessage("failed to compile MVEL code for state NULL:0.0.0:NULL:NULL");
105         stateFinalizerLogic.setLogic("java.lang.String");
106
107         msfe.prepare();
108
109         assertThatThrownBy(() -> msfe.execute(-1, new Properties(), null))
110             .hasMessage("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL");
111         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
112         EnEvent event = new EnEvent(axEvent);
113         assertThatThrownBy(() -> msfe.execute(-1, new Properties(), event))
114             .hasMessage("failed to execute MVEL code for state NULL:0.0.0:NULL:NULL");
115         stateFinalizerLogic.setLogic("executionId !=-1");
116         msfe.prepare();
117         assertThatThrownBy(() -> msfe.execute(-1, new Properties(), event))
118             .hasMessage("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:"
119                 + "NULL:NULL\" on finalizer logic NULL:0.0.0:NULL:NULL");
120         stateFinalizerLogic.setLogic(
121                 "if (executionId == -1) {return false;}setSelectedStateOutputName(\"SelectedOutputIsMe\");"
122                         + "return true;");
123         state.getStateOutputs().put("SelectedOutputIsMe", null);
124
125         msfe.prepare();
126         String stateOutput = msfe.execute(0, new Properties(), event);
127         assertEquals("SelectedOutputIsMe", stateOutput);
128         msfe.cleanUp();
129     }
130 }