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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.executor.java;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
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;
51 * Test the JavaStateFinalizerExecutor class.
54 public class JavaStateFinalizerExecutorTest {
56 * Initiate Parameters.
59 public void initiateParameters() {
60 ParameterService.register(new DistributorParameters());
61 ParameterService.register(new LockManagerParameters());
62 ParameterService.register(new PersistorParameters());
63 ParameterService.register(new EngineParameters());
67 * Clear down Parameters.
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);
78 public void testJavaStateFinalizerExecutor() {
79 JavaStateFinalizerExecutor jsfe = new JavaStateFinalizerExecutor();
84 fail("test should throw an exception here");
85 } catch (Exception jtseException) {
86 assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
89 ApexInternalContext internalContext = null;
91 internalContext = new ApexInternalContext(new AxPolicyModel());
92 } catch (ContextException e) {
93 fail("test should not throw an exception here");
96 StateExecutor parentStateExcutor = null;
98 parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
99 } catch (StateMachineException e) {
100 fail("test should not throw an exception here");
103 AxState state = new AxState();
104 parentStateExcutor.setContext(null, state, internalContext);
105 AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
106 jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
110 fail("test should throw an exception here");
111 } catch (Exception jtseException) {
112 assertEquals("instantiation error on Java class \"\"", jtseException.getMessage());
115 stateFinalizerLogic.setLogic("java.lang.String");
119 } catch (Exception jtseException) {
120 fail("test should not throw an exception here");
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());
131 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
132 EnEvent event = new EnEvent(axEvent);
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());
141 stateFinalizerLogic.setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaStateFinalizerLogic");
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());
151 state.getStateOutputs().put("SelectedOutputIsMe", null);
154 String stateOutput = jsfe.execute(0, new Properties(), event);
155 assertEquals("SelectedOutputIsMe", stateOutput);
157 } catch (Exception jtseException) {
158 jtseException.printStackTrace();
159 fail("test should not throw an exception here");