diff options
Diffstat (limited to 'plugins')
15 files changed, 143 insertions, 261 deletions
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaUnionTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaUnionTest.java deleted file mode 100644 index 47b1593fa..000000000 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroSchemaUnionTest.java +++ /dev/null @@ -1,154 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.plugins.context.schema.avro; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import org.apache.avro.generic.GenericRecord; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.onap.policy.apex.context.SchemaHelper; -import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.service.ModelService; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.common.utils.resources.TextFileUtils; - -/** - * The Class TestAvroSchemaUnion. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @version - */ -public class AvroSchemaUnionTest { - private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); - private AxContextSchemas schemas; - private String uinionSchema; - - /** - * Inits the test. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Before - public void initTest() throws IOException { - schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); - ModelService.registerModel(AxContextSchemas.class, schemas); - uinionSchema = TextFileUtils.getTextFileAsString("src/test/resources/avsc/UnionExample.avsc"); - } - - /** - * Inits the context. - */ - @Before - public void initContext() { - SchemaParameters schemaParameters = new SchemaParameters(); - schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); - schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters()); - ParameterService.register(schemaParameters); - - } - - /** - * Clear context. - */ - @After - public void clearContext() { - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - /** - * Test union all fields. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Ignore - @Test - public void testUnionAllFields() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleAllFields.json"); - final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); - - assertEquals("Ben", user.get("name").toString()); - assertEquals(7, user.get("favourite_number")); - assertEquals("red", user.get("favourite_colour").toString()); - } - - /** - * Test union optional field. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Ignore - @Test - public void testUnionOptionalField() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final String inString = - TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleOptionalField.json"); - final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); - - assertEquals("Ben", user.get("name").toString()); - assertEquals(7, user.get("favourite_number")); - assertEquals("red", user.get("favourite_colour").toString()); - } - - /** - * Test union null field. - * - * @throws IOException Signals that an I/O exception has occurred. - */ - @Ignore - @Test - public void testUnionNullField() throws IOException { - final AxContextSchema avroSchema = - new AxContextSchema(new AxArtifactKey("AvroRecord", "0.0.1"), "AVRO", uinionSchema); - - schemas.getSchemasMap().put(avroSchema.getKey(), avroSchema); - final SchemaHelper schemaHelper = new SchemaHelperFactory().createSchemaHelper(testKey, avroSchema.getKey()); - - final String inString = TextFileUtils.getTextFileAsString("src/test/resources/data/UnionExampleNullField.json"); - final GenericRecord user = (GenericRecord) schemaHelper.createNewInstance(inString); - - assertEquals("Ben", user.get("name").toString()); - assertEquals(7, user.get("favourite_number")); - assertEquals("red", user.get("favourite_colour").toString()); - } -} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java index 8380d0a61..fb59bad2a 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,8 +49,8 @@ public class SupportRestRequestorEndpoint { private static int deleteMessagesReceived = 0; private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n" - + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" - + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}"; + + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}"; /** * Reset counters. @@ -74,10 +75,10 @@ public class SupportRestRequestorEndpoint { statMessagesReceived++; } return Response.status(200) - .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived - + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived - + ",\"DELETE\": " + deleteMessagesReceived + "}") - .build(); + .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": " + + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": " + + deleteMessagesReceived + "}") + .build(); } /** @@ -134,9 +135,9 @@ public class SupportRestRequestorEndpoint { final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class); assertTrue(jsonMap.containsKey("name")); assertEquals("0.0.1", jsonMap.get("version")); - assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace")); - assertEquals("Act", jsonMap.get("source")); - assertEquals("Outside", jsonMap.get("target")); + assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace")); + assertEquals("apex", jsonMap.get("source")); + assertEquals("server", jsonMap.get("target")); return Response.status(200).entity(EVENT_STRING).build(); } @@ -170,9 +171,9 @@ public class SupportRestRequestorEndpoint { final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class); assertTrue(jsonMap.containsKey("name")); assertEquals("0.0.1", jsonMap.get("version")); - assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace")); - assertEquals("Act", jsonMap.get("source")); - assertEquals("Outside", jsonMap.get("target")); + assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace")); + assertEquals("apex", jsonMap.get("source")); + assertEquals("server", jsonMap.get("target")); return Response.status(200).entity(EVENT_STRING).build(); } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/policymodels/RequestorModel.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/policymodels/RequestorModel.json index 98510d2a5..2ab58e4a6 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/policymodels/RequestorModel.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/policymodels/RequestorModel.json @@ -460,7 +460,7 @@ "taskLogic" : { "key" : "TaskLogic", "logicFlavour" : "JAVASCRIPT", - "logic" : "executor.logger.debug(executor.subject.getId());\nvar gc = executor.getContextAlbum(\"BasicContextAlbum\");\nexecutor.logger.debug(gc.getName());\nvar returnValue = executor.isTrue;" + "logic" : "executor.logger.debug(executor.subject.getId());\nvar gc = executor.getContextAlbum(\"BasicContextAlbum\");\nexecutor.logger.debug(gc.getName());\ntrue;" } } } ] diff --git a/plugins/plugins-executor/plugins-executor-javascript/pom.xml b/plugins/plugins-executor/plugins-executor-javascript/pom.xml index d3d3faac0..1fc79dddd 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/pom.xml +++ b/plugins/plugins-executor/plugins-executor-javascript/pom.xml @@ -39,19 +39,9 @@ <dependencies> <dependency> - <groupId>org.graalvm.sdk</groupId> - <artifactId>graal-sdk</artifactId> - <version>${graalvm.version}</version> - </dependency> - <dependency> - <groupId>org.graalvm.truffle</groupId> - <artifactId>truffle-api</artifactId> - <version>${graalvm.version}</version> - </dependency> - <dependency> - <groupId>org.graalvm.js</groupId> - <artifactId>js</artifactId> - <version>${graalvm.version}</version> + <groupId>org.mozilla</groupId> + <artifactId>rhino</artifactId> + <version>1.7.12</version> </dependency> <dependency> <groupId>org.assertj</groupId> diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutor.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutor.java index 93f6216fc..c80f58fbe 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutor.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutor.java @@ -20,9 +20,10 @@ package org.onap.policy.apex.plugins.executor.javascript; -import org.graalvm.polyglot.Context; -import org.graalvm.polyglot.HostAccess; -import org.graalvm.polyglot.Value; +import org.apache.commons.lang3.StringUtils; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.Script; +import org.mozilla.javascript.Scriptable; import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; import org.onap.policy.apex.model.basicmodel.concepts.AxKey; @@ -32,35 +33,50 @@ import org.onap.policy.apex.model.basicmodel.concepts.AxKey; * @author Liam Fallon (liam.fallon@ericsson.com) */ public class JavascriptExecutor { + public static final int DEFAULT_OPTIMIZATION_LEVEL = 9; + + // Recurring string constants + private static final String WITH_MESSAGE = " with message: "; + // The key of the subject that wants to execute Javascript code final AxKey subjectKey; - // The Javascript context - private final Context jsContext; + private Context javascriptContext; + private Script script; /** - * Prepares the executor for processing. + * Initializes the Javascripe executor. * * @param subjectKey the key of the subject that is requesting Javascript execution - * @throws StateMachineException thrown when instantiation of the executor fails */ - public JavascriptExecutor(final AxKey subjectKey) throws StateMachineException { + public JavascriptExecutor(final AxKey subjectKey) { this.subjectKey = subjectKey; + } - // @formatter:off - jsContext = - Context.newBuilder("js") - .allowHostClassLookup(s -> true) - .allowHostAccess(HostAccess.ALL) - .build(); - // @formatter:on + /** + * Prepares the executor for processing and compiles the Javascript code. + * + * @param javascriptCode the Javascript code to execute + * @throws StateMachineException thrown when instantiation of the executor fails + */ + public void init(final String javascriptCode) throws StateMachineException { + if (StringUtils.isEmpty(javascriptCode)) { + throw new StateMachineException("no logic specified for " + subjectKey.getId()); + } try { - jsContext.getBindings("js"); + // Create a Javascript context for this thread + javascriptContext = Context.enter(); + + // Set up the default values of the context + javascriptContext.setOptimizationLevel(DEFAULT_OPTIMIZATION_LEVEL); + javascriptContext.setLanguageVersion(Context.VERSION_1_8); + + script = javascriptContext.compileString(javascriptCode, subjectKey.getId(), 1, null); } catch (Exception e) { - jsContext.close(); + Context.exit(); throw new StateMachineException( - "prepare: javascript engine failed to initialize properly for \"" + subjectKey.getId() + "\"", e); + "logic failed to compile for " + subjectKey.getId() + WITH_MESSAGE + e.getMessage(), e); } } @@ -68,28 +84,30 @@ public class JavascriptExecutor { * Executes the the Javascript code. * * @param executionContext the execution context of the subject to be passed to the Javascript context - * @param javascriptCode the Javascript code to execute * @return true if the Javascript executed properly * @throws StateMachineException thrown when Javascript execution fails */ - public boolean execute(final Object executionContext, final String javascriptCode) throws StateMachineException { + public boolean execute(final Object executionContext) throws StateMachineException { + Object returnObject = null; + try { - // Set up the Javascript engine context - jsContext.getBindings("js").putMember("executor", executionContext); - jsContext.eval("js", javascriptCode); + // Pass the subject context to the Javascript engine + Scriptable javascriptScope = javascriptContext.initStandardObjects(); + javascriptScope.put("executor", javascriptScope, executionContext); + // Run the script + returnObject = script.exec(javascriptContext, javascriptScope); } catch (final Exception e) { - throw new StateMachineException("execute: logic failed to run for \"" + subjectKey.getId() + "\"", e); + throw new StateMachineException( + "logic failed to run for " + subjectKey.getId() + WITH_MESSAGE + e.getMessage(), e); } - Value returnValue = jsContext.getBindings("js").getMember("returnValue"); - - if (returnValue == null || returnValue.isNull()) { + if (!(returnObject instanceof Boolean)) { throw new StateMachineException( - "execute: logic failed to set a return value for \"" + subjectKey.getId() + "\""); + "execute: logic for " + subjectKey.getId() + " returned a non-boolean value " + returnObject); } - return returnValue.asBoolean(); + return (boolean) returnObject; } /** @@ -99,10 +117,10 @@ public class JavascriptExecutor { */ public void cleanUp() throws StateMachineException { try { - jsContext.close(); + Context.exit(); } catch (final Exception e) { - throw new StateMachineException( - "cleanUp: executor cleanup failed to close for \"" + subjectKey.getId() + "\"", e); + throw new StateMachineException("cleanUp: executor cleanup failed to close for " + subjectKey.getId() + + WITH_MESSAGE + e.getMessage(), e); } } } diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutor.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutor.java index 18a6ef58a..27e649fd3 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutor.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutor.java @@ -52,7 +52,12 @@ public class JavascriptStateFinalizerExecutor extends StateFinalizerExecutor { // Call generic prepare logic super.prepare(); + // Create the executor javascriptExecutor = new JavascriptExecutor(getSubject().getKey()); + + // Initialize and cleanup the executor to check the Javascript code + javascriptExecutor.init(getSubject().getLogic()); + javascriptExecutor.cleanUp(); } /** @@ -71,8 +76,13 @@ public class JavascriptStateFinalizerExecutor extends StateFinalizerExecutor { // Do execution pre work executePre(executionId, executionProperties, incomingFields); - // Execute the Javascript and do post processing - executePost(javascriptExecutor.execute(getExecutionContext(), getSubject().getLogic())); + // Execute the Javascript executor + javascriptExecutor.init(getSubject().getLogic()); + boolean result = javascriptExecutor.execute(getExecutionContext()); + javascriptExecutor.cleanUp(); + + // Execute the Javascript + executePost(result); return getOutgoing(); } @@ -86,7 +96,5 @@ public class JavascriptStateFinalizerExecutor extends StateFinalizerExecutor { public void cleanUp() throws StateMachineException { LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getLogicFlavour() + "," + getSubject().getLogic()); - - javascriptExecutor.cleanUp(); } } diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutor.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutor.java index 29fae193e..bec5670c5 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutor.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutor.java @@ -52,7 +52,12 @@ public class JavascriptTaskExecutor extends TaskExecutor { // Call generic prepare logic super.prepare(); + // Create the executor javascriptExecutor = new JavascriptExecutor(getSubject().getKey()); + + // Initialize and cleanup the executor to check the Javascript code + javascriptExecutor.init(getSubject().getTaskLogic().getLogic()); + javascriptExecutor.cleanUp(); } /** @@ -68,11 +73,17 @@ public class JavascriptTaskExecutor extends TaskExecutor { @Override public Map<String, Object> execute(final long executionId, final Properties executionProperties, final Map<String, Object> incomingFields) throws StateMachineException, ContextException { + // Do execution pre work executePre(executionId, executionProperties, incomingFields); - // Execute the Javascript and do post processing - executePost(javascriptExecutor.execute(getExecutionContext(), getSubject().getTaskLogic().getLogic())); + // Execute the Javascript executor + javascriptExecutor.init(getSubject().getTaskLogic().getLogic()); + boolean result = javascriptExecutor.execute(getExecutionContext()); + javascriptExecutor.cleanUp(); + + // Execute the Javascript + executePost(result); return getOutgoing(); } @@ -86,7 +97,5 @@ public class JavascriptTaskExecutor extends TaskExecutor { public void cleanUp() throws StateMachineException { LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour() + "," + getSubject().getTaskLogic().getLogic()); - - javascriptExecutor.cleanUp(); } } diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutor.java b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutor.java index 41585fbd0..c32b70991 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutor.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/main/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutor.java @@ -53,7 +53,11 @@ public class JavascriptTaskSelectExecutor extends TaskSelectExecutor { // Call generic prepare logic super.prepare(); + // Create the executor javascriptExecutor = new JavascriptExecutor(getSubject().getKey()); + + // Initialize and cleanup the executor to check the Javascript code + javascriptExecutor.init(getSubject().getTaskSelectionLogic().getLogic()); } /** @@ -72,8 +76,13 @@ public class JavascriptTaskSelectExecutor extends TaskSelectExecutor { // Do execution pre work executePre(executionId, executionProperties, incomingEvent); - // Execute the Javascript and do post processing - executePost(javascriptExecutor.execute(getExecutionContext(), getSubject().getTaskSelectionLogic().getLogic())); + // Execute the Javascript executor + javascriptExecutor.init(getSubject().getTaskSelectionLogic().getLogic()); + boolean result = javascriptExecutor.execute(getExecutionContext()); + javascriptExecutor.cleanUp(); + + // Execute the Javascript + executePost(result); return getOutgoing(); } @@ -88,7 +97,5 @@ public class JavascriptTaskSelectExecutor extends TaskSelectExecutor { LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskSelectionLogic().getLogicFlavour() + "," + getSubject().getTaskSelectionLogic().getLogic()); - - javascriptExecutor.cleanUp(); } } diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java index 5ccbd255f..8be79558f 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java @@ -23,7 +23,6 @@ package org.onap.policy.apex.plugins.executor.javascript; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; import java.util.HashMap; import java.util.Map; @@ -94,31 +93,32 @@ public class JavascriptStateFinalizerExecutorTest { jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext); stateFinalizerLogic.setLogic("return false"); - jsfe.prepare(); + assertThatThrownBy(() -> { + jsfe.prepare(); + }).hasMessage("logic failed to compile for NULL:0.0.0:NULL:NULL " + + "with message: invalid return (NULL:0.0.0:NULL:NULL#1)"); Map<String, Object> incomingParameters1 = new HashMap<>(); assertThatThrownBy(() -> { jsfe.execute(-1, new Properties(), incomingParameters1); - fail("test should throw an exception here"); - }).hasMessage("execute: logic failed to run for \"NULL:0.0.0:NULL:NULL\""); + }).hasMessage("logic failed to compile for NULL:0.0.0:NULL:NULL " + + "with message: invalid return (NULL:0.0.0:NULL:NULL#1)"); stateFinalizerLogic.setLogic("java.lang.String"); jsfe.prepare(); AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); EnEvent event = new EnEvent(axEvent); - stateFinalizerLogic.setLogic( - "if(executor.executionId==-1)" + "{\r\n" + "var returnValueType = Java.type(\"java.lang.Boolean\");" - + "var returnValue = new returnValueType(false); }\n" + "else{\n" - + "executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n" - + "var returnValueType = Java.type(\"java.lang.Boolean\");\n" + "\n" - + "var returnValue = new returnValueType(true);}"); + stateFinalizerLogic.setLogic("if(executor.executionId==-1)" + "{\r\n" + + "var returnValueType = java.lang.Boolean;" + "var returnValue = new returnValueType(false); }\n" + + "else{\n" + "executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n" + + "var returnValueType = java.lang.Boolean;\n" + "\n" + + "var returnValue = new returnValueType(true);} true;"); assertThatThrownBy(() -> { jsfe.prepare(); jsfe.execute(-1, new Properties(), event); - }).hasMessage("execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" " - + "on finalizer logic NULL:0.0.0:NULL:NULL"); + }).hasMessage("execute-post: state finalizer logic \"NULL:0.0.0:NULL:NULL\" did not select an output state"); state.getStateOutputs().put("SelectedOutputIsMe", null); diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java index 786cebcc3..c327ebb4d 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java @@ -105,41 +105,38 @@ public class JavascriptTaskExecutorTest { jte.prepare(); }).isInstanceOf(NullPointerException.class); - AxTask task = new AxTask(); + AxTask task = new AxTask(new AxArtifactKey("TestTask:0.0.1")); final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel()); jte.setContext(null, task, internalContext); task.getTaskLogic().setLogic("return boolean;"); - jte.prepare(); - Map<String, Object> incomingParameters2 = new HashMap<>(); assertThatThrownBy(() -> { - jte.execute(-1, new Properties(), incomingParameters2); - }).hasMessage("execute: logic failed to run for \"NULL:0.0.0\""); + jte.prepare(); + jte.execute(-1, new Properties(), null); + }).hasMessage("logic failed to compile for TestTask:0.0.1 with message: invalid return (TestTask:0.0.1#1)"); task.getTaskLogic().setLogic("var x = 5;"); - jte.prepare(); + jte.prepare(); assertThatThrownBy(() -> { jte.execute(-1, new Properties(), null); }).isInstanceOf(NullPointerException.class); + jte.cleanUp(); - Map<String, Object> incomingParameters = new HashMap<>(); - assertThatThrownBy(() -> { - jte.execute(-1, new Properties(), incomingParameters); - }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0\""); + task.getTaskLogic().setLogic("var returnValue = false;\nreturnValue;"); - task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\n" - + "var returnValue = new returnValueType(false); "); + Map<String, Object> incomingParameters = new HashMap<>(); assertThatThrownBy(() -> { jte.prepare(); jte.execute(-1, new Properties(), incomingParameters); - }).hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0"); + }).hasMessage("execute-post: task logic execution failure on task \"TestTask\" in model NULL:0.0.0"); - task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n" - + "var returnValue = new returnValueType(true); "); + jte.cleanUp(); + + task.getTaskLogic().setLogic("var returnValue = true;\nreturnValue;"); jte.prepare(); Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters); @@ -176,11 +173,13 @@ public class JavascriptTaskExecutorTest { jte.prepare(); jte.execute(-1, new Properties(), incomingParameters); + jte.cleanUp(); task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic01.js")); jte.prepare(); Map<String, Object> outcomingParameters = jte.execute(-1, new Properties(), incomingParameters); + jte.cleanUp(); assertEquals("returnVal0", outcomingParameters.get("par0")); assertEquals("returnVal1", outcomingParameters.get("par1")); diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java index abbcd19b7..3acf132dd 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java @@ -80,14 +80,17 @@ public class JavascriptTaskSelectExecutorTest { AxState state = new AxState(); ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel()); jtse.setContext(null, state, internalContext); - jtse.prepare(); + + assertThatThrownBy(() -> { + jtse.prepare(); + }).hasMessage("no logic specified for NULL:0.0.0:NULL:NULL"); AxEvent axEvent1 = new AxEvent(new AxArtifactKey("Event", "0.0.1")); EnEvent event1 = new EnEvent(axEvent1); assertThatThrownBy(() -> { jtse.execute(-1, new Properties(), event1); - }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0:NULL:NULL\""); + }).hasMessage("no logic specified for NULL:0.0.0:NULL:NULL"); state.getTaskSelectionLogic().setLogic("java.lang.String"); jtse.prepare(); @@ -101,18 +104,17 @@ public class JavascriptTaskSelectExecutorTest { assertThatThrownBy(() -> { jtse.execute(-1, new Properties(), event); - }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0:NULL:NULL\""); + }).hasMessage( + "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]"); - state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n" - + "var returnValue = new returnValueType(false); "); + state.getTaskSelectionLogic().setLogic("var x=1;\n" + "false; "); assertThatThrownBy(() -> { jtse.prepare(); jtse.execute(-1, new Properties(), event); }).hasMessage("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\""); - state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n" - + "var returnValue = new returnValueType(true); "); + state.getTaskSelectionLogic().setLogic("var x = 1\n" + "true; "); jtse.prepare(); AxArtifactKey taskKey = jtse.execute(0, new Properties(), event); diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/PolicyTaskLogic.js b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/PolicyTaskLogic.js index a7bb81666..d5427582a 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/PolicyTaskLogic.js +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/PolicyTaskLogic.js @@ -32,4 +32,4 @@ executor.outFields.put("intPar", intParBy2); executor.logger.info("outgoing value: " + executor.outFields.get("intPar").toString()); -var returnValue = executor.isTrue;
\ No newline at end of file +executor.isTrue;
\ No newline at end of file diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/TestLogic00.js b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/TestLogic00.js index 8b3bff425..0c15249d2 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/TestLogic00.js +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/TestLogic00.js @@ -19,3 +19,5 @@ */ var x = 1; var returnValue = true; + +returnValue;
\ No newline at end of file diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/TestLogic01.js b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/TestLogic01.js index 7c8a44962..83c3060bd 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/TestLogic01.js +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/javascript/TestLogic01.js @@ -30,4 +30,4 @@ executor.outFields.put("par1", "returnVal1"); executor.logger.info(executor.outFields.get("par0")); executor.logger.info(executor.outFields.get("par1")); -var returnValue = executor.isTrue; +true; diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/policymodels/ExecutorModel.json b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/policymodels/ExecutorModel.json index 79f08e279..d987a0c78 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/policymodels/ExecutorModel.json +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/policymodels/ExecutorModel.json @@ -460,7 +460,7 @@ "taskLogic" : { "key" : "TaskLogic", "logicFlavour" : "JAVASCRIPT", - "logic" : "executor.logger.debug(executor.subject.getId());\nvar gc = executor.getContextAlbum(\"BasicContextAlbum\");\nexecutor.logger.debug(gc.getName());\nexecutor.logger.debug(\"incoming value: \" + executor.inFields.get(\"intPar\").toString());\n\nvar intPar = executor.inFields.get(\"intPar\");\n\nexecutor.logger.debug(\"read value: \" + intPar.toString());\n\nvar intParBy2 = intPar * 2;\n\nexecutor.outFields.put(\"intPar\", intParBy2);\n\nexecutor.logger.debug(\"outgoing value: \" + executor.outFields.get(\"intPar\").toString());\n\nvar returnValue = executor.isTrue;" + "logic" : "executor.logger.debug(executor.subject.getId());\nvar gc = executor.getContextAlbum(\"BasicContextAlbum\");\nexecutor.logger.debug(gc.getName());\nexecutor.logger.debug(\"incoming value: \" + executor.inFields.get(\"intPar\").toString());\n\nvar intPar = executor.inFields.get(\"intPar\");\n\nexecutor.logger.debug(\"read value: \" + intPar.toString());\n\nvar intParBy2 = intPar * 2;\n\nexecutor.outFields.put(\"intPar\", intParBy2);\n\nexecutor.logger.debug(\"outgoing value: \" + executor.outFields.get(\"intPar\").toString());\n\ntrue;" } } } ] |