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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.executor.javascript;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
30 import java.util.Properties;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.ContextAlbum;
36 import org.onap.policy.apex.context.ContextException;
37 import org.onap.policy.apex.context.Distributor;
38 import org.onap.policy.apex.context.impl.ContextAlbumImpl;
39 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
40 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
41 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
42 import org.onap.policy.apex.context.parameters.ContextParameters;
43 import org.onap.policy.apex.context.parameters.SchemaParameters;
44 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
46 import org.onap.policy.apex.model.basicmodel.service.ModelService;
47 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
48 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
49 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
50 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
51 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
52 import org.onap.policy.common.parameters.ParameterService;
53 import org.onap.policy.common.utils.resources.TextFileUtils;
56 * Test the JavaTaskExecutor class.
59 public class JavascriptTaskExecutorTest {
61 * Set ups everything for the test.
64 public static void prepareForTest() {
65 final ContextParameters contextParameters = new ContextParameters();
66 contextParameters.getLockManagerParameters()
67 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");
69 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
70 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
71 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
72 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
74 ParameterService.register(contextParameters);
75 ParameterService.register(contextParameters.getDistributorParameters());
76 ParameterService.register(contextParameters.getLockManagerParameters());
77 ParameterService.register(contextParameters.getPersistorParameters());
79 final SchemaParameters schemaParameters = new SchemaParameters();
80 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
81 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
83 ParameterService.register(schemaParameters);
87 * Clear down the test data.
90 public static void cleanUpAfterTest() {
91 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
92 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
93 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
94 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
95 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
96 ParameterService.clear();
100 public void testJavascriptTaskExecutor() throws Exception {
101 JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
104 assertThatThrownBy(() -> {
106 }).isInstanceOf(NullPointerException.class);
108 AxTask task = new AxTask();
109 final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
111 jte.setContext(null, task, internalContext);
113 task.getTaskLogic().setLogic("return boolean;");
116 Map<String, Object> incomingParameters2 = new HashMap<>();
117 assertThatThrownBy(() -> {
118 jte.execute(-1, new Properties(), incomingParameters2);
119 }).hasMessage("execute: logic failed to run for \"NULL:0.0.0\"");
121 task.getTaskLogic().setLogic("var x = 5;");
124 assertThatThrownBy(() -> {
125 jte.execute(-1, new Properties(), null);
126 }).isInstanceOf(NullPointerException.class);
128 Map<String, Object> incomingParameters = new HashMap<>();
129 assertThatThrownBy(() -> {
130 jte.execute(-1, new Properties(), incomingParameters);
131 }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0\"");
133 task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\n"
134 + "var returnValue = new returnValueType(false); ");
136 assertThatThrownBy(() -> {
138 jte.execute(-1, new Properties(), incomingParameters);
139 }).hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0");
141 task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
142 + "var returnValue = new returnValueType(true); ");
145 Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);
146 assertEquals(0, returnMap.size());
151 public void testJavascriptTaskExecutorLogic() throws Exception {
152 JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
155 assertThatThrownBy(() -> {
157 }).isInstanceOf(NullPointerException.class);
159 AxTask task = new AxTask(new AxArtifactKey("TestTask:0.0.1"));
161 ContextAlbum contextAlbum = createTestContextAlbum();
163 final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
164 internalContext.getContextAlbums().put(contextAlbum.getKey(), contextAlbum);
166 task.getContextAlbumReferences().add(contextAlbum.getKey());
167 task.getOutputFields().put("par0", null);
168 task.getOutputFields().put("par1", null);
170 jte.setContext(null, task, internalContext);
172 Map<String, Object> incomingParameters = new HashMap<>();
173 incomingParameters.put("par0", "value0");
175 task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic00.js"));
178 jte.execute(-1, new Properties(), incomingParameters);
180 task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic01.js"));
183 Map<String, Object> outcomingParameters = jte.execute(-1, new Properties(), incomingParameters);
185 assertEquals("returnVal0", outcomingParameters.get("par0"));
186 assertEquals("returnVal1", outcomingParameters.get("par1"));
189 private ContextAlbum createTestContextAlbum() throws ContextException {
190 AxContextSchemas schemas = new AxContextSchemas();
191 AxContextSchema simpleStringSchema =
192 new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String");
193 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
194 ModelService.registerModel(AxContextSchemas.class, schemas);
196 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
197 true, AxArtifactKey.getNullKey());
199 axContextAlbum.setItemSchema(simpleStringSchema.getKey());
200 Distributor distributor = new JvmLocalDistributor();
201 distributor.init(axContextAlbum.getKey());
202 return new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());