2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
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.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
26 import java.util.Properties;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
31 import org.onap.policy.apex.context.parameters.DistributorParameters;
32 import org.onap.policy.apex.context.parameters.LockManagerParameters;
33 import org.onap.policy.apex.context.parameters.PersistorParameters;
34 import org.onap.policy.apex.core.engine.EngineParameterConstants;
35 import org.onap.policy.apex.core.engine.EngineParameters;
36 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
37 import org.onap.policy.apex.core.engine.event.EnEvent;
38 import org.onap.policy.apex.core.engine.executor.StateExecutor;
39 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
42 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
43 import org.onap.policy.apex.model.policymodel.concepts.AxState;
44 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
45 import org.onap.policy.common.parameters.ParameterService;
48 * Test the JavascriptStateFinalizerExecutor class.
51 public class JavascriptStateFinalizerExecutorTest {
53 * Initiate Parameters.
56 public void initiateParameters() {
57 ParameterService.register(new DistributorParameters());
58 ParameterService.register(new LockManagerParameters());
59 ParameterService.register(new PersistorParameters());
60 ParameterService.register(new EngineParameters());
64 * Clear down Parameters.
67 public void clearParameters() {
68 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
69 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
70 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
71 ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
75 public void testJavaStateFinalizerExecutor() throws Exception {
76 JavascriptStateFinalizerExecutor jsfe = new JavascriptStateFinalizerExecutor();
78 assertThatThrownBy(() -> {
80 }).isInstanceOf(java.lang.NullPointerException.class);
82 ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
83 StateExecutor parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
85 AxState state = new AxState();
86 parentStateExcutor.setContext(null, state, internalContext);
87 AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
88 jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
90 stateFinalizerLogic.setLogic("return false");
91 assertThatThrownBy(() -> {
93 }).hasMessage("logic failed to compile for NULL:0.0.0:NULL:NULL "
94 + "with message: invalid return (NULL:0.0.0:NULL:NULL#1)");
96 stateFinalizerLogic.setLogic("java.lang.String");
99 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
100 EnEvent event = new EnEvent(axEvent);
101 stateFinalizerLogic.setLogic("if(executor.executionId==-1)" + "{\r\n"
102 + "var returnValueType = java.lang.Boolean;" + "var returnValue = new returnValueType(false); }\n"
103 + "else{\n" + "executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n"
104 + "var returnValueType = java.lang.Boolean;\n" + "\n"
105 + "var returnValue = new returnValueType(true);} true;");
107 assertThatThrownBy(() -> {
109 jsfe.execute(-1, new Properties(), event);
110 }).hasMessage("execute-post: state finalizer logic \"NULL:0.0.0:NULL:NULL\" did not select an output state");
112 state.getStateOutputs().put("SelectedOutputIsMe", null);
115 String stateOutput = jsfe.execute(0, new Properties(), event);
116 assertEquals("SelectedOutputIsMe", stateOutput);