summaryrefslogtreecommitdiffstats
path: root/plugins/plugins-executor/plugins-executor-javascript/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/plugins-executor/plugins-executor-javascript/src/test/java')
-rw-r--r--plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutorFullApexTest.java65
-rw-r--r--plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java79
-rw-r--r--plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java196
-rw-r--r--plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java85
4 files changed, 235 insertions, 190 deletions
diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutorFullApexTest.java b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutorFullApexTest.java
new file mode 100644
index 000000000..1ac052456
--- /dev/null
+++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptExecutorFullApexTest.java
@@ -0,0 +1,65 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * 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.executor.javascript;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Test;
+import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
+import org.onap.policy.apex.service.engine.main.ApexMain;
+import org.onap.policy.common.utils.resources.TextFileUtils;
+
+public class JavascriptExecutorFullApexTest {
+
+ @Test
+ public void testFullApexPolicy() throws ApexException {
+ final String[] args = {"src/test/resources/prodcons/File2File.json"};
+
+ final File outFile0 = new File("src/test/resources/events/EventsOut0.json");
+ final File outFile1 = new File("src/test/resources/events/EventsOut1.json");
+ outFile0.deleteOnExit();
+ outFile1.deleteOnExit();
+
+ final ApexMain apexMain = new ApexMain(args);
+ assertNotNull(apexMain);
+
+ await().atMost(10, TimeUnit.SECONDS).until(() -> outFile0.exists());
+ await().atMost(10, TimeUnit.SECONDS).until(() -> outFile1.exists());
+
+ await().atMost(10, TimeUnit.SECONDS).until(() -> fileHasOccurencesOf(outFile0, "BasicEventOut0", 50));
+ await().atMost(10, TimeUnit.SECONDS).until(() -> fileHasOccurencesOf(outFile1, "BasicEventOut1", 50));
+
+ apexMain.shutdown();
+ }
+
+ private boolean fileHasOccurencesOf(final File file, final String token, final int occurenceCount)
+ throws IOException {
+
+ return occurenceCount == StringUtils.countMatches(TextFileUtils.getTextFileAsString(file.getAbsolutePath()),
+ token);
+ }
+}
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 724c70bd2..5ccbd255f 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
@@ -1,6 +1,6 @@
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-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.
@@ -20,6 +20,7 @@
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;
@@ -31,7 +32,6 @@ import java.util.Properties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.onap.policy.apex.context.ContextException;
import org.onap.policy.apex.context.parameters.ContextParameterConstants;
import org.onap.policy.apex.context.parameters.DistributorParameters;
import org.onap.policy.apex.context.parameters.LockManagerParameters;
@@ -41,7 +41,6 @@ import org.onap.policy.apex.core.engine.EngineParameters;
import org.onap.policy.apex.core.engine.context.ApexInternalContext;
import org.onap.policy.apex.core.engine.event.EnEvent;
import org.onap.policy.apex.core.engine.executor.StateExecutor;
-import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
@@ -78,30 +77,16 @@ public class JavascriptStateFinalizerExecutorTest {
}
@Test
- public void testJavaStateFinalizerExecutor() {
+ public void testJavaStateFinalizerExecutor() throws Exception {
JavascriptStateFinalizerExecutor jsfe = new JavascriptStateFinalizerExecutor();
assertNotNull(jsfe);
- try {
+ assertThatThrownBy(() -> {
jsfe.prepare();
- fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
- }
-
- ApexInternalContext internalContext = null;
- try {
- internalContext = new ApexInternalContext(new AxPolicyModel());
- } catch (ContextException e) {
- fail("test should not throw an exception here");
- }
-
- StateExecutor parentStateExcutor = null;
- try {
- parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
- } catch (StateMachineException e) {
- fail("test should not throw an exception here");
- }
+ }).isInstanceOf(java.lang.NullPointerException.class);
+
+ ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
+ StateExecutor parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
AxState state = new AxState();
parentStateExcutor.setContext(null, state, internalContext);
@@ -109,29 +94,16 @@ public class JavascriptStateFinalizerExecutorTest {
jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
stateFinalizerLogic.setLogic("return false");
- try {
- jsfe.prepare();
- fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals("state finalizer logic failed to compile for state finalizer \"NULL:0.0.0:NULL:NULL\"",
- jtseException.getMessage());
- }
+ jsfe.prepare();
Map<String, Object> incomingParameters1 = new HashMap<>();
- try {
+ assertThatThrownBy(() -> {
jsfe.execute(-1, new Properties(), incomingParameters1);
fail("test should throw an exception here");
- } catch (Exception jteException) {
- assertEquals("state finalizer logic failed to run for state finalizer \"NULL:0.0.0:NULL:NULL\"",
- jteException.getMessage());
- }
+ }).hasMessage("execute: logic failed to run for \"NULL:0.0.0:NULL:NULL\"");
stateFinalizerLogic.setLogic("java.lang.String");
- try {
- jsfe.prepare();
- } catch (Exception jtseException) {
- fail("test should not throw an exception here");
- }
+ jsfe.prepare();
AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
EnEvent event = new EnEvent(axEvent);
@@ -141,26 +113,19 @@ public class JavascriptStateFinalizerExecutorTest {
+ "executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n"
+ "var returnValueType = Java.type(\"java.lang.Boolean\");\n" + "\n"
+ "var returnValue = new returnValueType(true);}");
- try {
+
+ assertThatThrownBy(() -> {
jsfe.prepare();
jsfe.execute(-1, new Properties(), event);
- fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals(
- "execute-post: state finalizer logic execution failure on state \"NULL:0.0.0:NULL:NULL\" "
- + "on finalizer logic NULL:0.0.0:NULL:NULL",
- jtseException.getMessage());
- }
+ }).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");
state.getStateOutputs().put("SelectedOutputIsMe", null);
- try {
- jsfe.prepare();
- String stateOutput = jsfe.execute(0, new Properties(), event);
- assertEquals("SelectedOutputIsMe", stateOutput);
- jsfe.cleanUp();
- } catch (Exception jtseException) {
- jtseException.printStackTrace();
- fail("test should not throw an exception here");
- }
+
+ jsfe.prepare();
+ String stateOutput = jsfe.execute(0, new Properties(), event);
+ assertEquals("SelectedOutputIsMe", stateOutput);
+
+ jsfe.cleanUp();
}
}
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 fed293921..786cebcc3 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
@@ -1,6 +1,6 @@
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-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.
@@ -20,26 +20,37 @@
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.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
-import org.junit.After;
-import org.junit.Before;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
+import org.onap.policy.apex.context.ContextAlbum;
import org.onap.policy.apex.context.ContextException;
+import org.onap.policy.apex.context.Distributor;
+import org.onap.policy.apex.context.impl.ContextAlbumImpl;
+import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
+import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
import org.onap.policy.apex.context.parameters.ContextParameterConstants;
-import org.onap.policy.apex.context.parameters.DistributorParameters;
-import org.onap.policy.apex.context.parameters.LockManagerParameters;
-import org.onap.policy.apex.context.parameters.PersistorParameters;
+import org.onap.policy.apex.context.parameters.ContextParameters;
+import org.onap.policy.apex.context.parameters.SchemaParameters;
import org.onap.policy.apex.core.engine.context.ApexInternalContext;
+import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
+import org.onap.policy.apex.model.basicmodel.service.ModelService;
+import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
+import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
+import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
import org.onap.policy.apex.model.policymodel.concepts.AxTask;
import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.common.utils.resources.TextFileUtils;
/**
* Test the JavaTaskExecutor class.
@@ -47,106 +58,147 @@ import org.onap.policy.common.parameters.ParameterService;
*/
public class JavascriptTaskExecutorTest {
/**
- * Initiate Parameters.
+ * Set ups everything for the test.
*/
- @Before
- public void initiateParameters() {
- ParameterService.register(new DistributorParameters());
- ParameterService.register(new LockManagerParameters());
- ParameterService.register(new PersistorParameters());
+ @BeforeClass
+ public static void prepareForTest() {
+ final ContextParameters contextParameters = new ContextParameters();
+ contextParameters.getLockManagerParameters()
+ .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");
+
+ contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
+ contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
+ contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
+ contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
+
+ ParameterService.register(contextParameters);
+ ParameterService.register(contextParameters.getDistributorParameters());
+ ParameterService.register(contextParameters.getLockManagerParameters());
+ ParameterService.register(contextParameters.getPersistorParameters());
+
+ final SchemaParameters schemaParameters = new SchemaParameters();
+ schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
+ schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
+
+ ParameterService.register(schemaParameters);
}
/**
- * Clear Parameters.
+ * Clear down the test data.
*/
- @After
- public void clearParameters() {
+ @AfterClass
+ public static void cleanUpAfterTest() {
ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
+ ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
+ ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
+ ParameterService.clear();
}
@Test
- public void testJavascriptTaskExecutor() {
+ public void testJavascriptTaskExecutor() throws Exception {
JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
assertNotNull(jte);
- try {
+ assertThatThrownBy(() -> {
jte.prepare();
- fail("test should throw an exception here");
- } catch (Exception jteException) {
- assertEquals(java.lang.NullPointerException.class, jteException.getClass());
- }
+ }).isInstanceOf(NullPointerException.class);
AxTask task = new AxTask();
- ApexInternalContext internalContext = null;
- try {
- internalContext = new ApexInternalContext(new AxPolicyModel());
- } catch (ContextException e) {
- fail("test should not throw an exception here");
- }
+ final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
+
jte.setContext(null, task, internalContext);
- task.getTaskLogic().setLogic("return boolean");
- try {
- jte.prepare();
- fail("test should throw an exception here");
- } catch (Exception jteException) {
- assertEquals("task logic failed to compile for task \"NULL:0.0.0\"", jteException.getMessage());
- }
+ task.getTaskLogic().setLogic("return boolean;");
+ jte.prepare();
Map<String, Object> incomingParameters2 = new HashMap<>();
- try {
+ assertThatThrownBy(() -> {
jte.execute(-1, new Properties(), incomingParameters2);
- fail("test should throw an exception here");
- } catch (Exception jteException) {
- assertEquals("task logic failed to run for task \"NULL:0.0.0\"", jteException.getMessage());
- }
+ }).hasMessage("execute: logic failed to run for \"NULL:0.0.0\"");
- task.getTaskLogic().setLogic("java.lang.String");
-
- try {
- jte.prepare();
- } catch (Exception jteException) {
- fail("test should not throw an exception here");
- }
+ task.getTaskLogic().setLogic("var x = 5;");
+ jte.prepare();
- try {
+ assertThatThrownBy(() -> {
jte.execute(-1, new Properties(), null);
- fail("test should throw an exception here");
- } catch (Exception jteException) {
- assertEquals(java.lang.NullPointerException.class, jteException.getClass());
- }
+ }).isInstanceOf(NullPointerException.class);
Map<String, Object> incomingParameters = new HashMap<>();
- try {
+ assertThatThrownBy(() -> {
jte.execute(-1, new Properties(), incomingParameters);
- fail("test should throw an exception here");
- } catch (Exception jteException) {
- assertEquals("execute: task logic failed to set a return value for task \"NULL:0.0.0\"",
- jteException.getMessage());
- }
+ }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0\"");
- task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
+ task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\n"
+ "var returnValue = new returnValueType(false); ");
- try {
+
+ assertThatThrownBy(() -> {
jte.prepare();
jte.execute(-1, new Properties(), incomingParameters);
- fail("test should throw an exception here");
- } catch (Exception jteException) {
- assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0",
- jteException.getMessage());
- }
+ }).hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0");
task.getTaskLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
+ "var returnValue = new returnValueType(true); ");
- try {
+
+ jte.prepare();
+ Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);
+ assertEquals(0, returnMap.size());
+ jte.cleanUp();
+ }
+
+ @Test
+ public void testJavascriptTaskExecutorLogic() throws Exception {
+ JavascriptTaskExecutor jte = new JavascriptTaskExecutor();
+ assertNotNull(jte);
+
+ assertThatThrownBy(() -> {
jte.prepare();
- Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);
- assertEquals(0, returnMap.size());
- jte.cleanUp();
- } catch (Exception jteException) {
- fail("test should not throw an exception here");
- }
+ }).isInstanceOf(NullPointerException.class);
+
+ AxTask task = new AxTask(new AxArtifactKey("TestTask:0.0.1"));
+
+ ContextAlbum contextAlbum = createTestContextAlbum();
+
+ final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
+ internalContext.getContextAlbums().put(contextAlbum.getKey(), contextAlbum);
+
+ task.getContextAlbumReferences().add(contextAlbum.getKey());
+ task.getOutputFields().put("par0", null);
+ task.getOutputFields().put("par1", null);
+
+ jte.setContext(null, task, internalContext);
+
+ Map<String, Object> incomingParameters = new HashMap<>();
+ incomingParameters.put("par0", "value0");
+
+ task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic00.js"));
+
+ jte.prepare();
+ jte.execute(-1, new Properties(), incomingParameters);
+
+ task.getTaskLogic().setLogic(TextFileUtils.getTextFileAsString("src/test/resources/javascript/TestLogic01.js"));
+ jte.prepare();
+
+ Map<String, Object> outcomingParameters = jte.execute(-1, new Properties(), incomingParameters);
+
+ assertEquals("returnVal0", outcomingParameters.get("par0"));
+ assertEquals("returnVal1", outcomingParameters.get("par1"));
+ }
+
+ private ContextAlbum createTestContextAlbum() throws ContextException {
+ AxContextSchemas schemas = new AxContextSchemas();
+ AxContextSchema simpleStringSchema =
+ new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String");
+ schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
+ ModelService.registerModel(AxContextSchemas.class, schemas);
+
+ AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
+ true, AxArtifactKey.getNullKey());
+
+ axContextAlbum.setItemSchema(simpleStringSchema.getKey());
+ Distributor distributor = new JvmLocalDistributor();
+ distributor.init(axContextAlbum.getKey());
+ return new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
}
}
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 9a705020c..abbcd19b7 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
@@ -1,6 +1,6 @@
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-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.
@@ -20,6 +20,7 @@
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;
@@ -29,7 +30,6 @@ import java.util.Properties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.onap.policy.apex.context.ContextException;
import org.onap.policy.apex.context.parameters.ContextParameterConstants;
import org.onap.policy.apex.context.parameters.DistributorParameters;
import org.onap.policy.apex.context.parameters.LockManagerParameters;
@@ -68,92 +68,55 @@ public class JavascriptTaskSelectExecutorTest {
}
@Test
- public void testJavascriptTaskSelectExecutor() {
+ public void testJavascriptTaskSelectExecutor() throws Exception {
JavascriptTaskSelectExecutor jtse = new JavascriptTaskSelectExecutor();
assertNotNull(jtse);
- try {
+ assertThatThrownBy(() -> {
jtse.prepare();
fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
- }
+ }).isInstanceOf(NullPointerException.class);
AxState state = new AxState();
- ApexInternalContext internalContext = null;
- try {
- internalContext = new ApexInternalContext(new AxPolicyModel());
- } catch (ContextException e) {
- fail("test should not throw an exception here");
- }
+ ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
jtse.setContext(null, state, internalContext);
-
- state.getTaskSelectionLogic().setLogic("x!0");
- try {
- jtse.prepare();
- fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals("task selection logic failed to compile for state \"NULL:0.0.0:NULL:NULL\"",
- jtseException.getMessage());
- }
+ jtse.prepare();
AxEvent axEvent1 = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
EnEvent event1 = new EnEvent(axEvent1);
- try {
+
+ assertThatThrownBy(() -> {
jtse.execute(-1, new Properties(), event1);
- fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals(
- "task selection logic failed to run for state \"NULL:0.0.0:NULL:NULL\"",
- jtseException.getMessage());
- }
+ }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0:NULL:NULL\"");
state.getTaskSelectionLogic().setLogic("java.lang.String");
+ jtse.prepare();
- try {
- jtse.prepare();
- } catch (Exception jtseException) {
- fail("test should not throw an exception here");
- }
-
- try {
+ assertThatThrownBy(() -> {
jtse.execute(-1, new Properties(), null);
- fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
- }
+ }).isInstanceOf(NullPointerException.class);
AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
EnEvent event = new EnEvent(axEvent);
- try {
+
+ assertThatThrownBy(() -> {
jtse.execute(-1, new Properties(), event);
- fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals(
- "execute: task selection logic failed to set a return value for state \"NULL:0.0.0:NULL:NULL\"",
- jtseException.getMessage());
- }
+ }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0:NULL:NULL\"");
state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
+ "var returnValue = new returnValueType(false); ");
- try {
+
+ assertThatThrownBy(() -> {
jtse.prepare();
jtse.execute(-1, new Properties(), event);
- fail("test should throw an exception here");
- } catch (Exception jtseException) {
- assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",
- jtseException.getMessage());
- }
+ }).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); ");
- try {
- jtse.prepare();
- AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
- assertEquals("NULL:0.0.0", taskKey.getId());
- jtse.cleanUp();
- } catch (Exception jtseException) {
- fail("test should not throw an exception here");
- }
+
+ jtse.prepare();
+ AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
+ assertEquals("NULL:0.0.0", taskKey.getId());
+ jtse.cleanUp();
}
}