2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.javascript;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26 import java.util.HashMap;
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 JavascriptStateFinalizerExecutor class.
54 public class JavascriptStateFinalizerExecutorTest {
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 JavascriptStateFinalizerExecutor jsfe = new JavascriptStateFinalizerExecutor();
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);
108 stateFinalizerLogic.setLogic("return false");
111 fail("test should throw an exception here");
112 } catch (Exception jtseException) {
113 assertEquals("state finalizer logic failed to compile for state finalizer \"NULL:0.0.0:NULL:NULL\"",
114 jtseException.getMessage());
117 Map<String, Object> incomingParameters1 = new HashMap<>();
119 jsfe.execute(-1, incomingParameters1);
120 fail("test should throw an exception here");
121 } catch (Exception jteException) {
122 assertEquals("state finalizer logic failed to run for state finalizer \"NULL:0.0.0:NULL:NULL\"",
123 jteException.getMessage());
126 stateFinalizerLogic.setLogic("java.lang.String");
129 } catch (Exception jtseException) {
130 fail("test should not throw an exception here");
133 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
134 EnEvent event = new EnEvent(axEvent);
135 stateFinalizerLogic.setLogic(
136 "if(executor.executionId==-1)" + "{\r\n" + "var returnValueType = Java.type(\"java.lang.Boolean\");"
137 + "var returnValue = new returnValueType(false); }\n" + "else{\n"
138 + "executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n"
139 + "var returnValueType = Java.type(\"java.lang.Boolean\");\n" + "\n"
140 + "var returnValue = new returnValueType(true);}");
143 jsfe.execute(-1, event);
144 fail("test should throw an exception here");
145 } catch (Exception jtseException) {
147 "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",
149 jtseException.getMessage());
152 state.getStateOutputs().put("SelectedOutputIsMe", null);
155 String stateOutput = jsfe.execute(0, event);
156 assertEquals("SelectedOutputIsMe", stateOutput);
158 } catch (Exception jtseException) {
159 jtseException.printStackTrace();
160 fail("test should not throw an exception here");