f779a3005d363d04fb8da40733a1a7c7bd2d63f0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.executor.java;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
32 import org.onap.policy.apex.context.parameters.DistributorParameters;
33 import org.onap.policy.apex.context.parameters.LockManagerParameters;
34 import org.onap.policy.apex.context.parameters.PersistorParameters;
35 import org.onap.policy.apex.core.engine.EngineParameterConstants;
36 import org.onap.policy.apex.core.engine.EngineParameters;
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.StateExecutor;
40 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
41 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
44 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
45 import org.onap.policy.apex.model.policymodel.concepts.AxState;
46 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
47 import org.onap.policy.common.parameters.ParameterService;
48
49 /**
50  * Test the JavaStateFinalizerExecutor class.
51  *
52  */
53 public class JavaStateFinalizerExecutorTest {
54     /**
55      * Initiate Parameters.
56      */
57     @Before
58     public void initiateParameters() {
59         ParameterService.register(new DistributorParameters());
60         ParameterService.register(new LockManagerParameters());
61         ParameterService.register(new PersistorParameters());
62         ParameterService.register(new EngineParameters());
63     }
64
65     /**
66      * Clear down Parameters.
67      */
68     @After
69     public void clearParameters() {
70         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
71         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
72         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
73         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
74     }
75
76     @Test
77     public void testJavaStateFinalizerExecutor() {
78         JavaStateFinalizerExecutor jsfe = new JavaStateFinalizerExecutor();
79         assertNotNull(jsfe);
80
81         try {
82             jsfe.prepare();
83             fail("test should throw an exception here");
84         } catch (Exception jtseException) {
85             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
86         }
87
88         ApexInternalContext internalContext = null;
89         try {
90             internalContext = new ApexInternalContext(new AxPolicyModel());
91         } catch (ContextException e) {
92             fail("test should not throw an exception here");
93         }
94
95         StateExecutor parentStateExcutor = null;
96         try {
97             parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
98         } catch (StateMachineException e) {
99             fail("test should not throw an exception here");
100         }
101
102         AxState state = new AxState();
103         parentStateExcutor.setContext(null, state, internalContext);
104         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
105         jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
106
107         try {
108             jsfe.prepare();
109             fail("test should throw an exception here");
110         } catch (Exception jtseException) {
111             assertEquals("instantiation error on Java class \"\"", jtseException.getMessage());
112         }
113
114         stateFinalizerLogic.setLogic("java.lang.String");
115
116         try {
117             jsfe.prepare();
118         } catch (Exception jtseException) {
119             fail("test should not throw an exception here");
120         }
121
122         try {
123             jsfe.execute(-1, null, null);
124             fail("test should throw an exception here");
125         } catch (Exception jtseException) {
126             assertEquals("state finalizer logic failed to run for state finalizer  \"NULL:0.0.0:NULL:NULL\"",
127                 jtseException.getMessage());
128         }
129
130         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
131         EnEvent event = new EnEvent(axEvent);
132         try {
133             jsfe.execute(-1, null, event);
134             fail("test should throw an exception here");
135         } catch (Exception jtseException) {
136             assertEquals("state finalizer logic failed to run for state finalizer  \"NULL:0.0.0:NULL:NULL\"",
137                 jtseException.getMessage());
138         }
139
140         stateFinalizerLogic.setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaStateFinalizerLogic");
141         try {
142             jsfe.prepare();
143             jsfe.execute(-1, null, event);
144             fail("test should throw an exception here");
145         } catch (Exception jtseException) {
146             assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
147                 + "on finalizer logic NULL:0.0.0:NULL:NULL", jtseException.getMessage());
148         }
149
150         state.getStateOutputs().put("SelectedOutputIsMe", null);
151         try {
152             jsfe.prepare();
153             String stateOutput = jsfe.execute(0, null, event);
154             assertEquals("SelectedOutputIsMe", stateOutput);
155             jsfe.cleanUp();
156         } catch (Exception jtseException) {
157             jtseException.printStackTrace();
158             fail("test should not throw an exception here");
159         }
160     }
161 }