2 * ============LICENSE_START=======================================================
\r
3 * Copyright (C) 2019 Nordix Foundation.
\r
4 * ================================================================================
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
17 * SPDX-License-Identifier: Apache-2.0
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.onap.policy.apex.plugins.executor.jruby;
\r
23 import static org.junit.Assert.assertEquals;
\r
24 import static org.junit.Assert.assertNotNull;
\r
25 import static org.junit.Assert.fail;
\r
27 import java.lang.reflect.Field;
\r
28 import java.util.Properties;
\r
29 import org.junit.After;
\r
30 import org.junit.Before;
\r
31 import org.junit.Test;
\r
32 import org.onap.policy.apex.context.ContextException;
\r
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
\r
34 import org.onap.policy.apex.context.parameters.DistributorParameters;
\r
35 import org.onap.policy.apex.context.parameters.LockManagerParameters;
\r
36 import org.onap.policy.apex.context.parameters.PersistorParameters;
\r
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
\r
38 import org.onap.policy.apex.core.engine.event.EnEvent;
\r
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
\r
40 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
\r
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
\r
42 import org.onap.policy.apex.model.policymodel.concepts.AxState;
\r
43 import org.onap.policy.common.parameters.ParameterService;
\r
46 * Test the JrubyTaskSelectExecutor class.
\r
49 public class JrubyTaskSelectExecutorTest {
\r
51 * Initiate Parameters.
\r
54 public void initiateParameters() {
\r
55 ParameterService.register(new DistributorParameters());
\r
56 ParameterService.register(new LockManagerParameters());
\r
57 ParameterService.register(new PersistorParameters());
\r
64 public void clearParameters() {
\r
65 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
\r
66 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
\r
67 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
\r
71 public void testJrubyTaskSelectExecutor() {
\r
72 JrubyTaskSelectExecutor jtse = new JrubyTaskSelectExecutor();
\r
73 assertNotNull(jtse);
\r
74 assertNotNull(jtse.getOutputEventSet());
\r
76 Field fieldContainer = JrubyTaskSelectExecutor.class.getDeclaredField("container");
\r
77 fieldContainer.setAccessible(true);
\r
78 fieldContainer.set(jtse, null);
\r
80 fail("test should throw an exception here");
\r
81 } catch (Exception jtseException) {
\r
82 assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
\r
85 AxState state = new AxState();
\r
86 ApexInternalContext internalContext = null;
\r
88 internalContext = new ApexInternalContext(new AxPolicyModel());
\r
89 } catch (ContextException e) {
\r
90 fail("test should not throw an exception here");
\r
92 jtse.setContext(null, state, internalContext);
\r
97 } catch (Exception jtseException) {
\r
98 fail("test should not throw an exception here");
\r
100 AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
\r
101 EnEvent event = new EnEvent(axEvent);
\r
103 jtse.execute(-1, new Properties(), event);
\r
104 fail("test should throw an exception here");
\r
105 } catch (Exception jtseException) {
\r
106 assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",
\r
107 jtseException.getMessage());
\r
110 final String jrubyLogic =
\r
111 "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true" + "\n end";
\r
112 state.getTaskSelectionLogic().setLogic(jrubyLogic);
\r
116 AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
\r
117 assertEquals("NULL:0.0.0", taskKey.getId());
\r
119 } catch (Exception jtseException) {
\r
120 fail("test should not throw an exception here");
\r