2 * ============LICENSE_START=======================================================
\r
3 * Copyright (C) 2019 Nordix Foundation.
\r
4 * Modifications Copyright (C) 2019 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.junit.Assert.assertEquals;
\r
25 import static org.junit.Assert.assertNotNull;
\r
26 import static org.junit.Assert.fail;
\r
28 import java.lang.reflect.Field;
\r
29 import java.util.HashMap;
\r
30 import java.util.Map;
\r
31 import java.util.Properties;
\r
32 import org.junit.After;
\r
33 import org.junit.Before;
\r
34 import org.junit.Test;
\r
35 import org.onap.policy.apex.context.ContextException;
\r
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
\r
37 import org.onap.policy.apex.context.parameters.DistributorParameters;
\r
38 import org.onap.policy.apex.context.parameters.LockManagerParameters;
\r
39 import org.onap.policy.apex.context.parameters.PersistorParameters;
\r
40 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
\r
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
\r
42 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
\r
43 import org.onap.policy.common.parameters.ParameterService;
\r
46 * Test the JrubyTaskExecutor class.
\r
49 public class JrubyTaskExecutorTest {
\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 testJrubyTaskExecutor() {
\r
72 // Run test twice to check for incorrect shutdown activity
\r
73 jrubyExecutorTest();
\r
74 jrubyExecutorTest();
\r
78 * Test the JRuby executor.
\r
80 private void jrubyExecutorTest() {
\r
81 JrubyTaskExecutor jte = new JrubyTaskExecutor();
\r
85 Field fieldContainer = JrubyTaskExecutor.class.getDeclaredField("container");
\r
86 fieldContainer.setAccessible(true);
\r
87 fieldContainer.set(jte, null);
\r
89 fail("test should throw an exception here");
\r
90 } catch (Exception jtseException) {
\r
91 assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
\r
94 AxTask task = new AxTask();
\r
95 ApexInternalContext internalContext = null;
\r
97 internalContext = new ApexInternalContext(new AxPolicyModel());
\r
98 } catch (ContextException e) {
\r
99 fail("test should not throw an exception here");
\r
101 jte.setContext(null, task, internalContext);
\r
104 } catch (Exception jtseException) {
\r
105 fail("test should not throw an exception here");
\r
108 Map<String, Object> incomingParameters = new HashMap<>();
\r
110 jte.execute(-1, new Properties(), incomingParameters);
\r
111 fail("test should throw an exception here");
\r
112 } catch (Exception jteException) {
\r
113 assertEquals("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0",
\r
114 jteException.getMessage());
\r
117 final String jrubyLogic = "if executor.executionId == -1" + "\n return false" + "\n else " + "\n return true"
\r
119 task.getTaskLogic().setLogic(jrubyLogic);
\r
123 Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);
\r
124 assertEquals(0, returnMap.size());
\r
126 } catch (Exception jteException) {
\r
127 fail("test should not throw an exception here");
\r
132 Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters);
\r
133 assertEquals(0, returnMap.size());
\r
135 } catch (Exception jteException) {
\r
136 fail("test should not throw an exception here");
\r