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