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;
26 import static org.junit.Assert.fail;
28 import java.util.Properties;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
34 import org.onap.policy.apex.context.parameters.DistributorParameters;
35 import org.onap.policy.apex.context.parameters.LockManagerParameters;
36 import org.onap.policy.apex.context.parameters.PersistorParameters;
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
42 import org.onap.policy.apex.model.policymodel.concepts.AxState;
43 import org.onap.policy.common.parameters.ParameterService;
46 * Test the JavaTaskSelectExecutor class.
49 public class JavascriptTaskSelectExecutorTest {
51 * Initiate Parameters.
54 public void initiateParameters() {
55 ParameterService.register(new DistributorParameters());
56 ParameterService.register(new LockManagerParameters());
57 ParameterService.register(new PersistorParameters());
64 public void clearParameters() {
65 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
66 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
67 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
71 public void testJavascriptTaskSelectExecutor() throws Exception {
72 JavascriptTaskSelectExecutor jtse = new JavascriptTaskSelectExecutor();
75 assertThatThrownBy(() -> {
77 fail("test should throw an exception here");
78 }).isInstanceOf(NullPointerException.class);
80 AxState state = new AxState();
81 ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
82 jtse.setContext(null, state, internalContext);
85 AxEvent axEvent1 = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
86 EnEvent event1 = new EnEvent(axEvent1);
88 assertThatThrownBy(() -> {
89 jtse.execute(-1, new Properties(), event1);
90 }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0:NULL:NULL\"");
92 state.getTaskSelectionLogic().setLogic("java.lang.String");
95 assertThatThrownBy(() -> {
96 jtse.execute(-1, new Properties(), null);
97 }).isInstanceOf(NullPointerException.class);
99 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
100 EnEvent event = new EnEvent(axEvent);
102 assertThatThrownBy(() -> {
103 jtse.execute(-1, new Properties(), event);
104 }).hasMessage("execute: logic failed to set a return value for \"NULL:0.0.0:NULL:NULL\"");
106 state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
107 + "var returnValue = new returnValueType(false); ");
109 assertThatThrownBy(() -> {
111 jtse.execute(-1, new Properties(), event);
112 }).hasMessage("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"");
114 state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
115 + "var returnValue = new returnValueType(true); ");
118 AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
119 assertEquals("NULL:0.0.0", taskKey.getId());