c8522547accabc4eda4b3df6ccdb1388fba45fb3
[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     @Before
55     public void initiateParameters() {
56         ParameterService.register(new DistributorParameters());
57         ParameterService.register(new LockManagerParameters());
58         ParameterService.register(new PersistorParameters());
59         ParameterService.register(new EngineParameters());
60     }
61
62     @After
63     public void clearParameters() {
64         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
65         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
66         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
67         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
68     }
69
70     @Test
71     public void testJavaStateFinalizerExecutor() {
72         JavaStateFinalizerExecutor jsfe = new JavaStateFinalizerExecutor();
73         assertNotNull(jsfe);
74
75         try {
76             jsfe.prepare();
77             fail("test should throw an exception here");
78         } catch (Exception jtseException) {
79             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
80         }
81
82         ApexInternalContext internalContext = null;
83         try {
84             internalContext = new ApexInternalContext(new AxPolicyModel());
85         } catch (ContextException e) {
86             fail("test should not throw an exception here");
87         }
88
89         StateExecutor parentStateExcutor = null;
90         try {
91             parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
92         } catch (StateMachineException e) {
93         }
94         AxState state = new AxState();
95         parentStateExcutor.setContext(null, state , internalContext);
96         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
97         jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
98
99         try {
100             jsfe.prepare();
101             fail("test should throw an exception here");
102         } catch (Exception jtseException) {
103             assertEquals("instantiation error on Java class \"\"", jtseException.getMessage());
104         }
105
106         stateFinalizerLogic.setLogic("java.lang.String");
107
108         try {
109             jsfe.prepare();
110         } catch (Exception jtseException) {
111             fail("test should not throw an exception here");
112         }
113
114         try {
115             jsfe.execute(-1, null);
116             fail("test should throw an exception here");
117         } catch (Exception jtseException) {
118             assertEquals("state finalizer logic failed to run for state finalizer  \"NULL:0.0.0:NULL:NULL\"",
119                             jtseException.getMessage());
120         }
121
122         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
123         EnEvent event = new EnEvent(axEvent);
124         try {
125             jsfe.execute(-1, event);
126             fail("test should throw an exception here");
127         } catch (Exception jtseException) {
128             assertEquals("state finalizer logic failed to run for state finalizer  \"NULL:0.0.0:NULL:NULL\"",
129                             jtseException.getMessage());
130         }
131
132         stateFinalizerLogic.setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaStateFinalizerLogic");
133         try {
134             jsfe.prepare();
135             jsfe.execute(-1, event);
136             fail("test should throw an exception here");
137         } catch (Exception jtseException) {
138             assertEquals("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
139                             + "on finalizer logic NULL:0.0.0:NULL:NULL", jtseException.getMessage());
140         }
141
142         state.getStateOutputs().put("SelectedOutputIsMe", null);
143         try {
144             jsfe.prepare();
145             String stateOutput = jsfe.execute(0, event);
146             assertEquals("SelectedOutputIsMe", stateOutput);
147             jsfe.cleanUp();
148         } catch (Exception jtseException) {
149             jtseException.printStackTrace();
150             fail("test should not throw an exception here");
151         }
152     }
153 }