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