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