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