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