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