a1c2964368623815fa0806de2bd2c52cb5dea629
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
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.java;
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
51 /**
52  * Test the JavaStateFinalizerExecutor class.
53  *
54  */
55 public class JavaStateFinalizerExecutorTest {
56     /**
57      * Initiate Parameters.
58      */
59     @Before
60     public void initiateParameters() {
61         ParameterService.register(new DistributorParameters());
62         ParameterService.register(new LockManagerParameters());
63         ParameterService.register(new PersistorParameters());
64         ParameterService.register(new EngineParameters());
65     }
66
67     /**
68      * Clear down Parameters.
69      */
70     @After
71     public void clearParameters() {
72         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
73         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
74         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
75         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
76     }
77
78     @Test
79     public void testJavaStateFinalizerExecutor() throws StateMachineException, ContextException {
80         JavaStateFinalizerExecutor jsfe = new JavaStateFinalizerExecutor();
81         assertNotNull(jsfe);
82
83         assertThatThrownBy(jsfe::prepare).isInstanceOf(java.lang.NullPointerException.class);
84         ApexInternalContext internalContext = null;
85
86         internalContext = new ApexInternalContext(new AxPolicyModel());
87
88         StateExecutor parentStateExcutor = null;
89
90         parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
91
92         AxState state = new AxState();
93         parentStateExcutor.setContext(null, state, internalContext);
94         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
95         jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
96
97         assertThatThrownBy(jsfe::prepare)
98             .hasMessage("instantiation error on Java class \"\"");
99         stateFinalizerLogic.setLogic("java.lang.String");
100
101         jsfe.prepare();
102
103         assertThatThrownBy(() -> jsfe.execute(-1, new Properties(), null))
104             .hasMessage("state finalizer logic failed to run for state finalizer  \"NULL:0.0.0:NULL:NULL\"");
105         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
106         EnEvent event = new EnEvent(axEvent);
107         assertThatThrownBy(() -> jsfe.execute(-1, new Properties(), event))
108             .hasMessage("state finalizer logic failed to run for state finalizer  \"NULL:0.0.0:NULL:NULL\"");
109         stateFinalizerLogic.setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaStateFinalizerLogic");
110         jsfe.prepare();
111         assertThatThrownBy(() -> {
112             jsfe.execute(-1, new Properties(), event);
113         }).hasMessage("execute-post: state finalizer logic execution failure on state "
114                 + "\"NULL:0.0.0:NULL:NULL\" on finalizer logic NULL:0.0.0:NULL:NULL");
115         state.getStateOutputs().put("SelectedOutputIsMe", null);
116
117         jsfe.prepare();
118         String stateOutput = jsfe.execute(0, new Properties(), event);
119         assertEquals("SelectedOutputIsMe", stateOutput);
120         jsfe.cleanUp();
121
122     }
123 }