96bc6bb3d4de3edb104ef9924e95bfe4f32184e2
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.executor.java;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Properties;
32 import java.util.TreeMap;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.apex.context.ContextException;
37 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
38 import org.onap.policy.apex.context.parameters.DistributorParameters;
39 import org.onap.policy.apex.context.parameters.LockManagerParameters;
40 import org.onap.policy.apex.context.parameters.PersistorParameters;
41 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
42 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
44 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
45 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
46 import org.onap.policy.common.parameters.ParameterService;
47
48 /**
49  * Test the JavaTaskExecutor class.
50  *
51  */
52 public class JavaTaskExecutorTest {
53     /**
54      * Initiate Parameters.
55      */
56     @Before
57     public void initiateParameters() {
58         ParameterService.register(new DistributorParameters());
59         ParameterService.register(new LockManagerParameters());
60         ParameterService.register(new PersistorParameters());
61     }
62
63     /**
64      * Clear Parameters.
65      */
66     @After
67     public void clearParameters() {
68         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
69         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
70         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
71     }
72
73     @Test
74     public void testJavaTaskExecutor() throws ContextException, StateMachineException {
75         JavaTaskExecutor jte = new JavaTaskExecutor();
76         assertNotNull(jte);
77
78         assertThatThrownBy(jte::prepare)
79             .isInstanceOf(java.lang.NullPointerException.class);
80         AxTask task = new AxTask();
81         ApexInternalContext internalContext = null;
82         internalContext = new ApexInternalContext(new AxPolicyModel());
83
84         jte.setContext(null, task, internalContext);
85
86         assertThatThrownBy(jte::prepare)
87             .hasMessage("instantiation error on Java class \"\"");
88         task.getTaskLogic().setLogic("java.lang.String");
89         task.setInputEvent(new AxEvent());
90         task.setOutputEvents(new TreeMap<>());
91         jte.prepare();
92
93         assertThatThrownBy(() -> jte.execute(-1, new Properties(), null))
94             .isInstanceOf(java.lang.NullPointerException.class);
95         Map<String, Object> incomingParameters = new HashMap<>();
96         assertThatThrownBy(() -> jte.execute(-1, new Properties(), incomingParameters))
97             .hasMessage("task logic failed to run for task  \"NULL:0.0.0\"");
98         task.getTaskLogic().setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaTaskLogic");
99         jte.prepare();
100         assertThatThrownBy(() -> jte.execute(-1, new Properties(), incomingParameters))
101             .hasMessage("execute-post: task logic execution failure on task \"NULL\" in model NULL:0.0.0");
102         jte.prepare();
103         Map<String, Map<String, Object>> returnMap = jte.execute(0, new Properties(), incomingParameters);
104         assertEquals(0, returnMap.size());
105         jte.cleanUp();
106
107     }
108 }