4fc7507ed0a4bf9cddb6ea5e11a67a8b5a45027d
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-javascript / src / test / java / org / onap / policy / apex / plugins / executor / javascript / JavascriptTaskExecutorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.Map;
31 import java.util.Properties;
32
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.apex.context.ContextAlbum;
37 import org.onap.policy.apex.context.ContextException;
38 import org.onap.policy.apex.context.Distributor;
39 import org.onap.policy.apex.context.impl.ContextAlbumImpl;
40 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
41 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
42 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
43 import org.onap.policy.apex.context.parameters.ContextParameters;
44 import org.onap.policy.apex.context.parameters.SchemaParameters;
45 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
47 import org.onap.policy.apex.model.basicmodel.service.ModelService;
48 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
49 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
50 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
51 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
52 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
53 import org.onap.policy.common.parameters.ParameterService;
54 import org.onap.policy.common.utils.resources.TextFileUtils;
55
56 /**
57  * Test the JavaTaskExecutor class.
58  *
59  */
60 public class JavascriptTaskExecutorTest {
61     /**
62      * Set ups everything for the test.
63      */
64     @BeforeClass
65     public static void prepareForTest() {
66         final ContextParameters contextParameters = new ContextParameters();
67         contextParameters.getLockManagerParameters()
68             .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");
69
70         contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
71         contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
72         contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
73         contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
74
75         ParameterService.register(contextParameters);
76         ParameterService.register(contextParameters.getDistributorParameters());
77         ParameterService.register(contextParameters.getLockManagerParameters());
78         ParameterService.register(contextParameters.getPersistorParameters());
79
80         final SchemaParameters schemaParameters = new SchemaParameters();
81         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
82         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
83
84         ParameterService.register(schemaParameters);
85     }
86
87     /**
88      * Clear down the test data.
89      */
90     @AfterClass
91     public static void cleanUpAfterTest() {
92         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
93         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
94         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
95         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
96         ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
97         ParameterService.clear();
98     }
99
100     @Test
101     public void testJavascriptTaskExecutor() throws Exception {
102         assertThatThrownBy(() -> {
103             JavascriptTaskExecutor jteBadPrep = new JavascriptTaskExecutor();
104             jteBadPrep.prepare();
105         }).isInstanceOf(NullPointerException.class);
106
107         AxTask task = new AxTask(new AxArtifactKey("TestTask:0.0.1"));
108         final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
109
110         JavascriptTaskExecutor jteBadLogic = new JavascriptTaskExecutor();
111         assertNotNull(jteBadLogic);
112
113         jteBadLogic.setContext(null, task, internalContext);
114
115         task.getTaskLogic().setLogic("return boolean;");
116
117         assertThatThrownBy(() -> {
118             jteBadLogic.prepare();
119         }).hasMessage("logic failed to compile for TestTask:0.0.1 with message: invalid return (TestTask:0.0.1#1)");
120
121         task.getTaskLogic().setLogic("var x = 5;");
122
123         JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
124         jte.setContext(null, task, internalContext);
125
126         jte.prepare();
127
128         assertThatThrownBy(() -> {
129             jte.prepare();
130         }).hasMessage("initiation failed, executor TestTask:0.0.1 already initialized, run cleanUp to clear executor");
131
132         assertThatThrownBy(() -> {
133             jte.execute(-1, new Properties(), null);
134         }).isInstanceOf(NullPointerException.class);
135
136         assertThatThrownBy(() -> {
137             jte.execute(-1, new Properties(), null);
138         }).isInstanceOf(NullPointerException.class);
139
140         assertThatCode(() -> {
141             jte.cleanUp();
142         }).doesNotThrowAnyException();
143
144         task.getTaskLogic().setLogic("var returnValue = false;\nreturnValue;");
145
146         Map<String, Object> incomingParameters = new HashMap<>();
147
148         assertThatThrownBy(() -> {
149             jte.prepare();
150             jte.execute(-1, new Properties(), incomingParameters);
151         }).hasMessage("execute-post: task logic execution failure on task \"TestTask\" in model NULL:0.0.0");
152
153         jte.cleanUp();
154
155         task.getTaskLogic().setLogic("var returnValue = true;\nreturnValue;");
156
157         jte.prepare();
158         Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);
159         assertEquals(0, returnMap.size());
160         jte.cleanUp();
161
162         assertThatCode(() -> {
163             jte.cleanUp();
164         }).doesNotThrowAnyException();
165     }
166
167     @Test
168     public void testJavascriptTaskExecutorLogic() throws Exception {
169         JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
170         assertNotNull(jte);
171
172         assertThatThrownBy(() -> {
173             jte.prepare();
174         }).isInstanceOf(NullPointerException.class);
175
176         AxTask task = new AxTask(new AxArtifactKey("TestTask:0.0.1"));
177
178         ContextAlbum contextAlbum = createTestContextAlbum();
179
180         final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
181         internalContext.getContextAlbums().put(contextAlbum.getKey(), contextAlbum);
182
183         task.getContextAlbumReferences().add(contextAlbum.getKey());
184         task.getOutputFields().put("par0", null);
185         task.getOutputFields().put("par1", null);
186
187         jte.setContext(null, task, internalContext);
188
189         Map<String, Object> incomingParameters = new HashMap<>();
190         incomingParameters.put("par0", "value0");
191
192         task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic00.js"));
193
194         jte.prepare();
195         jte.execute(-1, new Properties(), incomingParameters);
196         jte.cleanUp();
197
198         task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic01.js"));
199         jte.prepare();
200
201         Map<String, Object> outcomingParameters = jte.execute(-1, new Properties(), incomingParameters);
202         jte.cleanUp();
203
204         assertEquals("returnVal0", outcomingParameters.get("par0"));
205         assertEquals("returnVal1", outcomingParameters.get("par1"));
206     }
207
208     private ContextAlbum createTestContextAlbum() throws ContextException {
209         AxContextSchemas schemas = new AxContextSchemas();
210         AxContextSchema simpleStringSchema =
211             new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String");
212         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
213         ModelService.registerModel(AxContextSchemas.class, schemas);
214
215         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
216             true, AxArtifactKey.getNullKey());
217
218         axContextAlbum.setItemSchema(simpleStringSchema.getKey());
219         Distributor distributor = new JvmLocalDistributor();
220         distributor.init(axContextAlbum.getKey());
221         return new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
222     }
223 }