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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.executor.javascript;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
28 import java.util.Properties;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
33 import org.onap.policy.apex.context.parameters.DistributorParameters;
34 import org.onap.policy.apex.context.parameters.LockManagerParameters;
35 import org.onap.policy.apex.context.parameters.PersistorParameters;
36 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
37 import org.onap.policy.apex.core.engine.event.EnEvent;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
40 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
41 import org.onap.policy.apex.model.policymodel.concepts.AxState;
42 import org.onap.policy.common.parameters.ParameterService;
45 * Test the JavaTaskSelectExecutor class.
48 public class JavascriptTaskSelectExecutorTest {
50 * Initiate Parameters.
53 public void initiateParameters() {
54 ParameterService.register(new DistributorParameters());
55 ParameterService.register(new LockManagerParameters());
56 ParameterService.register(new PersistorParameters());
63 public void clearParameters() {
64 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
65 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
66 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
70 public void testJavascriptTaskSelectExecutor() throws Exception {
71 JavascriptTaskSelectExecutor jtse = new JavascriptTaskSelectExecutor();
73 assertThatThrownBy(() -> {
75 fail("test should throw an exception here");
76 }).isInstanceOf(NullPointerException.class);
78 AxState state = new AxState();
79 ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
80 jtse.setContext(null, state, internalContext);
82 assertThatThrownBy(() -> {
84 }).hasMessage("no logic specified for NULL:0.0.0:NULL:NULL");
86 state.getTaskSelectionLogic().setLogic("java.lang.String");
89 final var props = new Properties();
90 assertThatThrownBy(() -> jtse.execute(-1, props, null)).isInstanceOf(NullPointerException.class);
92 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
93 EnEvent event = new EnEvent(axEvent);
95 assertThatThrownBy(() -> {
96 jtse.execute(-1, new Properties(), event);
98 "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]");
100 state.getTaskSelectionLogic().setLogic("var x=1;\n" + "false; ");
102 assertThatThrownBy(() -> {
104 jtse.execute(-1, new Properties(), event);
105 }).hasMessage("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"");
107 state.getTaskSelectionLogic().setLogic("var x = 1\n" + "true; ");
110 AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
111 assertEquals("NULL:0.0.0", taskKey.getId());