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