b98e2c03cc19e7ece2fed398f1d64ac6ac3c1829
[policy/apex-pdp.git] /
1 /*-
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
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 import java.util.HashMap;
27 import java.util.Map;
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.context.ApexInternalContext;
37 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
38 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
39 import org.onap.policy.common.parameters.ParameterService;
40
41 /**
42  * Test the JavaTaskExecutor class.
43  *
44  */
45 public class JavascriptTaskExecutorTest {
46     /**
47      * Initiate Parameters.
48      */
49     @Before
50     public void initiateParameters() {
51         ParameterService.register(new DistributorParameters());
52         ParameterService.register(new LockManagerParameters());
53         ParameterService.register(new PersistorParameters());
54     }
55
56     /**
57      * Clear Parameters.
58      */
59     @After
60     public void clearParameters() {
61         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
62         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
63         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
64     }
65
66     @Test
67     public void testJavascriptTaskExecutor() {
68         JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
69         assertNotNull(jte);
70
71         try {
72             jte.prepare();
73             fail("test should throw an exception here");
74         } catch (Exception jteException) {
75             assertEquals(java.lang.NullPointerException.class, jteException.getClass());
76         }
77
78         AxTask task = new AxTask();
79         ApexInternalContext internalContext = null;
80         try {
81             internalContext = new ApexInternalContext(new AxPolicyModel());
82         } catch (ContextException e) {
83             fail("test should not throw an exception here");
84         }
85         jte.setContext(null, task, internalContext);
86
87         task.getTaskLogic().setLogic("return boolean");
88         try {
89             jte.prepare();
90             fail("test should throw an exception here");
91         } catch (Exception jteException) {
92             assertEquals("task logic failed to compile for task  \"NULL:0.0.0\"", jteException.getMessage());
93         }
94
95         Map<String, Object> incomingParameters2 = new HashMap<>();
96         try {
97             jte.execute(-1, incomingParameters2);
98             fail("test should throw an exception here");
99         } catch (Exception jteException) {
100             assertEquals("task logic failed to run for task  \"NULL:0.0.0\"", jteException.getMessage());
101         }
102
103         task.getTaskLogic().setLogic("java.lang.String");
104
105         try {
106             jte.prepare();
107         } catch (Exception jteException) {
108             fail("test should not throw an exception here");
109         }
110
111         try {
112             jte.execute(-1, null);
113             fail("test should throw an exception here");
114         } catch (Exception jteException) {
115             assertEquals(java.lang.NullPointerException.class, jteException.getClass());
116         }
117
118         Map<String, Object> incomingParameters = new HashMap<>();
119         try {
120             jte.execute(-1, incomingParameters);
121             fail("test should throw an exception here");
122         } catch (Exception jteException) {
123             assertEquals("execute: task logic failed to set a return value for task  \"NULL:0.0.0\"",
124                     jteException.getMessage());
125         }
126
127         task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
128                 + "var returnValue = new returnValueType(false); ");
129         try {
130             jte.prepare();
131             jte.execute(-1, incomingParameters);
132             fail("test should throw an exception here");
133         } catch (Exception jteException) {
134             assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0",
135                     jteException.getMessage());
136         }
137
138         task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
139                 + "var returnValue = new returnValueType(true); ");
140         try {
141             jte.prepare();
142             Map<String, Object> returnMap = jte.execute(0, incomingParameters);
143             assertEquals(0, returnMap.size());
144             jte.cleanUp();
145         } catch (Exception jteException) {
146             fail("test should not throw an exception here");
147         }
148     }
149 }