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.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.fail;
29 import java.util.Properties;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
36 import org.onap.policy.apex.context.parameters.DistributorParameters;
37 import org.onap.policy.apex.context.parameters.LockManagerParameters;
38 import org.onap.policy.apex.context.parameters.PersistorParameters;
39 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
40 import org.onap.policy.apex.core.engine.event.EnEvent;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
43 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
44 import org.onap.policy.apex.model.policymodel.concepts.AxState;
45 import org.onap.policy.common.parameters.ParameterService;
48 * Test the JavaTaskSelectExecutor class.
51 public class JavascriptTaskSelectExecutorTest {
53 * Initiate Parameters.
56 public void initiateParameters() {
57 ParameterService.register(new DistributorParameters());
58 ParameterService.register(new LockManagerParameters());
59 ParameterService.register(new PersistorParameters());
66 public void clearParameters() {
67 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
68 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
69 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
74 public void testJavascriptTaskSelectExecutor() throws Exception {
75 JavascriptTaskSelectExecutor jtse = new JavascriptTaskSelectExecutor();
78 assertThatThrownBy(() -> {
80 fail("test should throw an exception here");
81 }).isInstanceOf(NullPointerException.class);
83 AxState state = new AxState();
84 ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
85 jtse.setContext(null, state, internalContext);
87 assertThatThrownBy(() -> {
89 }).hasMessage("no logic specified for NULL:0.0.0:NULL:NULL");
91 AxEvent axEvent1 = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
92 EnEvent event1 = new EnEvent(axEvent1);
94 assertThatThrownBy(() -> {
95 jtse.execute(-1, new Properties(), event1);
96 }).hasMessage("execution failed, executor NULL:0.0.0:NULL:NULL is not running");
98 state.getTaskSelectionLogic().setLogic("java.lang.String");
101 assertThatThrownBy(() -> {
102 jtse.execute(-1, new Properties(), null);
103 }).isInstanceOf(NullPointerException.class);
105 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
106 EnEvent event = new EnEvent(axEvent);
108 assertThatThrownBy(() -> {
109 jtse.execute(-1, new Properties(), event);
111 "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]");
113 state.getTaskSelectionLogic().setLogic("var x=1;\n" + "false;");
115 assertThatThrownBy(() -> {
116 jtse.execute(-1, new Properties(), event);
118 "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]");
120 assertThatThrownBy(() -> {
122 }).hasMessage("initiation failed, executor NULL:0.0.0:NULL:NULL is already running");
124 assertThatCode(() -> {
126 }).doesNotThrowAnyException();
128 JavascriptTaskSelectExecutor jtse1 = new JavascriptTaskSelectExecutor();
129 jtse1.setContext(null, state, internalContext);
130 state.getTaskSelectionLogic().setLogic("var x = 1\n" + "true;");
132 assertThatCode(() -> {
134 AxArtifactKey taskKey = jtse1.execute(0, new Properties(), event);
135 assertEquals("NULL:0.0.0", taskKey.getId());
137 }).doesNotThrowAnyException();