Fix sonars in apex-pdp
[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  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.executor.javascript;
23
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 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;
54
55 /**
56  * Test the JavaTaskExecutor class.
57  *
58  */
59 public class JavascriptTaskExecutorTest {
60     /**
61      * Set ups everything for the test.
62      */
63     @BeforeClass
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");
68
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);
73
74         ParameterService.register(contextParameters);
75         ParameterService.register(contextParameters.getDistributorParameters());
76         ParameterService.register(contextParameters.getLockManagerParameters());
77         ParameterService.register(contextParameters.getPersistorParameters());
78
79         final SchemaParameters schemaParameters = new SchemaParameters();
80         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
81         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
82
83         ParameterService.register(schemaParameters);
84     }
85
86     /**
87      * Clear down the test data.
88      */
89     @AfterClass
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();
97     }
98
99     @Test
100     public void testJavascriptTaskExecutor() throws Exception {
101         JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
102
103         assertThatThrownBy(() -> {
104             jte.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         jte.setContext(null, task, internalContext);
111
112         task.getTaskLogic().setLogic("return boolean;");
113
114         assertThatThrownBy(() -> {
115             jte.prepare();
116         }).hasMessage("logic failed to compile for TestTask:0.0.1 with message: invalid return (TestTask:0.0.1#1)");
117
118         task.getTaskLogic().setLogic("var x = 5;");
119
120         jte.prepare();
121         Properties props = new Properties();
122         assertThatThrownBy(() -> {
123             jte.execute(-1, props, null);
124         }).isInstanceOf(NullPointerException.class);
125         jte.cleanUp();
126
127         task.getTaskLogic().setLogic("var returnValue = false;\nreturnValue;");
128
129         Map<String, Object> incomingParameters = new HashMap<>();
130
131         assertThatThrownBy(() -> {
132             jte.prepare();
133             jte.execute(-1, props, incomingParameters);
134         }).hasMessage("execute-post: task logic execution failure on task \"TestTask\" in model NULL:0.0.0");
135
136         jte.cleanUp();
137
138         task.getTaskLogic().setLogic("var returnValue = true;\nreturnValue;");
139
140         jte.prepare();
141         Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);
142         assertEquals(0, returnMap.size());
143         jte.cleanUp();
144     }
145
146     @Test
147     public void testJavascriptTaskExecutorLogic() throws Exception {
148         JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
149         assertNotNull(jte);
150
151         assertThatThrownBy(() -> {
152             jte.prepare();
153         }).isInstanceOf(NullPointerException.class);
154
155         AxTask task = new AxTask(new AxArtifactKey("TestTask:0.0.1"));
156
157         ContextAlbum contextAlbum = createTestContextAlbum();
158
159         final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
160         internalContext.getContextAlbums().put(contextAlbum.getKey(), contextAlbum);
161
162         task.getContextAlbumReferences().add(contextAlbum.getKey());
163         task.getOutputFields().put("par0", null);
164         task.getOutputFields().put("par1", null);
165
166         jte.setContext(null, task, internalContext);
167
168         Map<String, Object> incomingParameters = new HashMap<>();
169         incomingParameters.put("par0", "value0");
170
171         task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic00.js"));
172
173         jte.prepare();
174         jte.execute(-1, new Properties(), incomingParameters);
175         jte.cleanUp();
176
177         task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic01.js"));
178         jte.prepare();
179
180         Map<String, Object> outcomingParameters = jte.execute(-1, new Properties(), incomingParameters);
181         jte.cleanUp();
182
183         assertEquals("returnVal0", outcomingParameters.get("par0"));
184         assertEquals("returnVal1", outcomingParameters.get("par1"));
185     }
186
187     private ContextAlbum createTestContextAlbum() throws ContextException {
188         AxContextSchemas schemas = new AxContextSchemas();
189         AxContextSchema simpleStringSchema =
190                 new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String");
191         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
192         ModelService.registerModel(AxContextSchemas.class, schemas);
193
194         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
195                 true, AxArtifactKey.getNullKey());
196
197         axContextAlbum.setItemSchema(simpleStringSchema.getKey());
198         Distributor distributor = new JvmLocalDistributor();
199         distributor.init(axContextAlbum.getKey());
200         return new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
201     }
202 }