5ccbd255f072417f6139a7a7dcf8a54eeb82a98f
[policy/apex-pdp.git] /
1 /*-
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
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.javascript;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.fail;
27
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Properties;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
36 import org.onap.policy.apex.context.parameters.DistributorParameters;
37 import org.onap.policy.apex.context.parameters.LockManagerParameters;
38 import org.onap.policy.apex.context.parameters.PersistorParameters;
39 import org.onap.policy.apex.core.engine.EngineParameterConstants;
40 import org.onap.policy.apex.core.engine.EngineParameters;
41 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
42 import org.onap.policy.apex.core.engine.event.EnEvent;
43 import org.onap.policy.apex.core.engine.executor.StateExecutor;
44 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
46 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
47 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
48 import org.onap.policy.apex.model.policymodel.concepts.AxState;
49 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
50 import org.onap.policy.common.parameters.ParameterService;
51
52 /**
53  * Test the JavascriptStateFinalizerExecutor class.
54  *
55  */
56 public class JavascriptStateFinalizerExecutorTest {
57     /**
58      * Initiate Parameters.
59      */
60     @Before
61     public void initiateParameters() {
62         ParameterService.register(new DistributorParameters());
63         ParameterService.register(new LockManagerParameters());
64         ParameterService.register(new PersistorParameters());
65         ParameterService.register(new EngineParameters());
66     }
67
68     /**
69      * Clear down Parameters.
70      */
71     @After
72     public void clearParameters() {
73         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
74         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
75         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
76         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
77     }
78
79     @Test
80     public void testJavaStateFinalizerExecutor() throws Exception {
81         JavascriptStateFinalizerExecutor jsfe = new JavascriptStateFinalizerExecutor();
82         assertNotNull(jsfe);
83
84         assertThatThrownBy(() -> {
85             jsfe.prepare();
86         }).isInstanceOf(java.lang.NullPointerException.class);
87
88         ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
89         StateExecutor parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
90
91         AxState state = new AxState();
92         parentStateExcutor.setContext(null, state, internalContext);
93         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
94         jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
95
96         stateFinalizerLogic.setLogic("return false");
97         jsfe.prepare();
98
99         Map<String, Object> incomingParameters1 = new HashMap<>();
100         assertThatThrownBy(() -> {
101             jsfe.execute(-1, new Properties(), incomingParameters1);
102             fail("test should throw an exception here");
103         }).hasMessage("execute: logic failed to run for \"NULL:0.0.0:NULL:NULL\"");
104
105         stateFinalizerLogic.setLogic("java.lang.String");
106         jsfe.prepare();
107
108         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
109         EnEvent event = new EnEvent(axEvent);
110         stateFinalizerLogic.setLogic(
111                 "if(executor.executionId==-1)" + "{\r\n" + "var returnValueType = Java.type(\"java.lang.Boolean\");"
112                         + "var returnValue = new returnValueType(false); }\n" + "else{\n"
113                         + "executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n"
114                         + "var returnValueType = Java.type(\"java.lang.Boolean\");\n" + "\n"
115                         + "var returnValue = new returnValueType(true);}");
116
117         assertThatThrownBy(() -> {
118             jsfe.prepare();
119             jsfe.execute(-1, new Properties(), event);
120         }).hasMessage("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
121                 + "on finalizer logic NULL:0.0.0:NULL:NULL");
122
123         state.getStateOutputs().put("SelectedOutputIsMe", null);
124
125         jsfe.prepare();
126         String stateOutput = jsfe.execute(0, new Properties(), event);
127         assertEquals("SelectedOutputIsMe", stateOutput);
128
129         jsfe.cleanUp();
130     }
131 }