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