2 * ============LICENSE_START=======================================================
\r
3 * Copyright (C) 2019 Nordix Foundation.
\r
4 * Modifications Copyright (C) 2020 Nordix Foundation
\r
5 * ================================================================================
\r
6 * Licensed under the Apache License, Version 2.0 (the "License");
\r
7 * you may not use this file except in compliance with the License.
\r
8 * You may obtain a copy of the License at
\r
10 * http://www.apache.org/licenses/LICENSE-2.0
\r
12 * Unless required by applicable law or agreed to in writing, software
\r
13 * distributed under the License is distributed on an "AS IS" BASIS,
\r
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
15 * See the License for the specific language governing permissions and
\r
16 * limitations under the License.
\r
18 * SPDX-License-Identifier: Apache-2.0
\r
19 * ============LICENSE_END=========================================================
\r
22 package org.onap.policy.apex.plugins.executor.jruby;
\r
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
\r
25 import static org.junit.Assert.assertEquals;
\r
26 import static org.junit.Assert.assertNotNull;
\r
28 import java.lang.reflect.Field;
\r
29 import java.util.Properties;
\r
30 import org.junit.After;
\r
31 import org.junit.Before;
\r
32 import org.junit.Test;
\r
33 import org.onap.policy.apex.context.ContextException;
\r
34 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
\r
35 import org.onap.policy.apex.context.parameters.DistributorParameters;
\r
36 import org.onap.policy.apex.context.parameters.LockManagerParameters;
\r
37 import org.onap.policy.apex.context.parameters.PersistorParameters;
\r
38 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
\r
39 import org.onap.policy.apex.core.engine.event.EnEvent;
\r
40 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
\r
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
\r
42 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
\r
43 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
\r
44 import org.onap.policy.apex.model.policymodel.concepts.AxState;
\r
45 import org.onap.policy.common.parameters.ParameterService;
\r
48 * Test the JrubyTaskSelectExecutor class.
\r
51 public class JrubyTaskSelectExecutorTest {
\r
53 * Initiate Parameters.
\r
56 public void initiateParameters() {
\r
57 ParameterService.register(new DistributorParameters());
\r
58 ParameterService.register(new LockManagerParameters());
\r
59 ParameterService.register(new PersistorParameters());
\r
66 public void clearParameters() {
\r
67 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
\r
68 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
\r
69 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
\r
73 public void testJrubyTaskSelectExecutor() throws StateMachineException, ContextException,
\r
74 NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
\r
75 JrubyTaskSelectExecutor jtse = new JrubyTaskSelectExecutor();
\r
76 assertNotNull(jtse);
\r
77 assertNotNull(jtse.getOutputEventSet());
\r
79 Field fieldContainer = JrubyTaskSelectExecutor.class.getDeclaredField("container");
\r
80 fieldContainer.setAccessible(true);
\r
81 fieldContainer.set(jtse, null);
\r
83 assertThatThrownBy(jtse::prepare).isInstanceOf(java.lang.NullPointerException.class);
\r
84 AxState state = new AxState();
\r
85 ApexInternalContext internalContext = null;
\r
86 internalContext = new ApexInternalContext(new AxPolicyModel());
\r
88 jtse.setContext(null, state, internalContext);
\r
92 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
\r
93 EnEvent event = new EnEvent(axEvent);
\r
94 assertThatThrownBy(() -> jtse.execute(-1, new Properties(), event))
\r
95 .hasMessage("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"");
\r
96 final String jrubyLogic =
\r
97 "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true" + "\n end";
\r
98 state.getTaskSelectionLogic().setLogic(jrubyLogic);
\r
101 AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
\r
102 assertEquals("NULL:0.0.0", taskKey.getId());
\r