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 / JavaTaskSelectExecutorTest.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.Properties;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.ContextException;
32 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
33 import org.onap.policy.apex.context.parameters.DistributorParameters;
34 import org.onap.policy.apex.context.parameters.LockManagerParameters;
35 import org.onap.policy.apex.context.parameters.PersistorParameters;
36 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
37 import org.onap.policy.apex.core.engine.event.EnEvent;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
40 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
41 import org.onap.policy.apex.model.policymodel.concepts.AxState;
42 import org.onap.policy.common.parameters.ParameterService;
43
44 /**
45  * Test the JavaTaskSelectExecutor class.
46  *
47  */
48 public class JavaTaskSelectExecutorTest {
49     /**
50      * Initiate Parameters.
51      */
52     @Before
53     public void initiateParameters() {
54         ParameterService.register(new DistributorParameters());
55         ParameterService.register(new LockManagerParameters());
56         ParameterService.register(new PersistorParameters());
57     }
58
59     /**
60      * Clear Parameters.
61      */
62     @After
63     public void clearParameters() {
64         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
65         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
66         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
67     }
68
69     @Test
70     public void testJavaTaskSelectExecutor() {
71         JavaTaskSelectExecutor jtse = new JavaTaskSelectExecutor();
72         assertNotNull(jtse);
73
74         try {
75             jtse.prepare();
76             fail("test should throw an exception here");
77         } catch (Exception jtseException) {
78             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
79         }
80
81         AxState state = new AxState();
82         ApexInternalContext internalContext = null;
83         try {
84             internalContext = new ApexInternalContext(new AxPolicyModel());
85         } catch (ContextException e) {
86             fail("test should not throw an exception here");
87         }
88         jtse.setContext(null, state, internalContext);
89
90         try {
91             jtse.prepare();
92             fail("test should throw an exception here");
93         } catch (Exception jtseException) {
94             assertEquals("instantiation error on Java class \"\"", jtseException.getMessage());
95         }
96
97         state.getTaskSelectionLogic().setLogic("java.lang.String");
98
99         try {
100             jtse.prepare();
101         } catch (Exception jtseException) {
102             fail("test should not throw an exception here");
103         }
104
105         try {
106             jtse.execute(-1, new Properties(), null);
107             fail("test should throw an exception here");
108         } catch (Exception jtseException) {
109             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
110         }
111
112         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
113         EnEvent event = new EnEvent(axEvent);
114         try {
115             jtse.execute(-1, new Properties(), event);
116             fail("test should throw an exception here");
117         } catch (Exception jtseException) {
118             assertEquals("task selection logic failed to run for state  \"NULL:0.0.0:NULL:NULL\"",
119                             jtseException.getMessage());
120         }
121
122         state.getTaskSelectionLogic()
123                         .setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaTaskSelectionLogic");
124         try {
125             jtse.prepare();
126             jtse.execute(-1, new Properties(), event);
127             fail("test should throw an exception here");
128         } catch (Exception jtseException) {
129             assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",
130                             jtseException.getMessage());
131         }
132
133         try {
134             jtse.prepare();
135             AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
136             assertEquals("NULL:0.0.0", taskKey.getId());
137             jtse.cleanUp();
138         } catch (Exception jtseException) {
139             fail("test should not throw an exception here");
140         }
141     }
142 }