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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.engine.executor.impl;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.policy.apex.core.engine.EngineParameters;
35 import org.onap.policy.apex.core.engine.ExecutorParameters;
36 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
37 import org.onap.policy.apex.core.engine.executor.Executor;
38 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
39 import org.onap.policy.apex.model.policymodel.concepts.AxState;
40 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
41 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
42 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
43 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
44 import org.onap.policy.common.parameters.ParameterService;
47 * Test the executor factory implementation.
50 public class ExceutorFactoryImplTest {
52 private ApexInternalContext internalContextMock;
55 private AxState stateMock;
58 private AxTaskSelectionLogic tslMock;
61 private AxTask taskMock;
64 private AxTaskLogic tlMock;
67 private AxStateFinalizerLogic sflMock;
70 private Executor<?, ?, ?, ?> parentMock;
72 private ExecutorParameters executorPars;
78 public void startMocking() {
79 MockitoAnnotations.initMocks(this);
81 Mockito.doReturn(tslMock).when(stateMock).getTaskSelectionLogic();
82 Mockito.doReturn("Dummy").when(tslMock).getLogicFlavour();
84 Mockito.doReturn(tlMock).when(taskMock).getTaskLogic();
85 Mockito.doReturn("Dummy").when(tlMock).getLogicFlavour();
87 Mockito.doReturn("Dummy").when(sflMock).getLogicFlavour();
91 public void clearPars() {
92 ParameterService.clear();
96 public void testExecutorFactoryImplGood() {
99 ExecutorFactoryImpl factory = null;
102 factory = new ExecutorFactoryImpl();
103 } catch (StateMachineException e) {
104 fail("test should not throw an exception");
107 Mockito.doReturn(true).when(stateMock).checkSetTaskSelectionLogic();
108 assertNotNull(factory.getTaskSelectionExecutor(null, stateMock, internalContextMock));
109 Mockito.doReturn(false).when(stateMock).checkSetTaskSelectionLogic();
110 assertNull(factory.getTaskSelectionExecutor(null, stateMock, internalContextMock));
112 assertNotNull(factory.getTaskExecutor(null, taskMock, internalContextMock));
114 assertNotNull(factory.getStateFinalizerExecutor(parentMock, sflMock, internalContextMock));
118 public void testExecutorFactoryImplNonExistant() {
119 setNonExistantPars();
122 new ExecutorFactoryImpl();
123 fail("test should throw an exception");
125 } catch (StateMachineException ex) {
126 assertEquals("Apex executor class not found for executor plugin "
127 + "\"org.onap.policy.apex.core.engine.executor.BadTaskExecutor\"", ex.getMessage());
130 executorPars.setTaskExecutorPluginClass(null);
133 new ExecutorFactoryImpl();
134 fail("test should throw an exception");
135 } catch (StateMachineException ex) {
136 assertEquals("Apex executor class not found for executor plugin "
137 + "\"org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor\"", ex.getMessage());
140 executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor");
143 new ExecutorFactoryImpl();
144 fail("test should throw an exception");
145 } catch (StateMachineException ex) {
146 assertEquals("Apex executor class not found for executor plugin "
147 + "\"org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor\"", ex.getMessage());
150 executorPars.setTaskSelectionExecutorPluginClass(
151 "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor");
154 new ExecutorFactoryImpl();
155 fail("test should throw an exception");
156 } catch (StateMachineException ex) {
157 assertEquals("Apex executor class not found for executor plugin "
158 + "\"org.onap.policy.apex.core.engine.executor.BadStateFinalizerExecutor\"",
164 public void testExecutorFactoryImplBad() {
168 new ExecutorFactoryImpl();
169 fail("test should throw an exception");
171 } catch (StateMachineException ex) {
172 assertEquals("Specified Apex executor plugin class \"java.lang.String\" "
173 + "does not implment the Executor interface", ex.getMessage());
176 executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor");
179 new ExecutorFactoryImpl();
180 fail("test should throw an exception");
181 } catch (StateMachineException ex) {
182 assertEquals("Specified Apex executor plugin class \"java.lang.String\" "
183 + "does not implment the Executor interface", ex.getMessage());
186 executorPars.setTaskSelectionExecutorPluginClass(
187 "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor");
190 new ExecutorFactoryImpl();
191 fail("test should throw an exception");
192 } catch (StateMachineException ex) {
193 assertEquals("Specified Apex executor plugin class \"java.lang.String\" "
194 + "does not implment the Executor interface", ex.getMessage());
199 public void testExecutorFactoryCreateErrors() {
202 executorPars.setTaskExecutorPluginClass(null);
204 ExecutorFactoryImpl factory = null;
207 factory = new ExecutorFactoryImpl();
208 } catch (StateMachineException e) {
209 fail("test should not throw an exception");
212 Mockito.doReturn(true).when(stateMock).checkSetTaskSelectionLogic();
215 factory.getTaskExecutor(null, taskMock, internalContextMock);
216 fail("test should throw an exception");
217 } catch (Exception ex) {
218 assertEquals("Executor plugin class not defined for \"Dummy\" executor of type "
219 + "\"org.onap.policy.apex.core.engine.executor.TaskExecutor\"", ex.getMessage());
222 executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor");
225 factory = new ExecutorFactoryImpl();
226 } catch (StateMachineException e) {
227 fail("test should not throw an exception");
231 factory.getTaskExecutor(null, taskMock, internalContextMock);
232 fail("test should throw an exception");
233 } catch (Exception ex) {
234 assertEquals("Instantiation error on \"Dummy\" executor of type "
235 + "\"org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor\"",
239 executorPars.setTaskExecutorPluginClass(
240 "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor");
243 factory = new ExecutorFactoryImpl();
244 } catch (StateMachineException e) {
245 fail("test should not throw an exception");
249 factory.getTaskExecutor(null, taskMock, internalContextMock);
250 fail("test should throw an exception");
251 } catch (Exception ex) {
252 assertEquals("Executor on \"Dummy\" "
253 + "of type \"class org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor\""
254 + " is not an instance of \"org.onap.policy.apex.core.engine.executor.TaskExecutor\"",
260 * Set up good parameters.
262 private void setGoodPars() {
263 executorPars = new ExecutorParameters();
264 executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor");
265 executorPars.setTaskSelectionExecutorPluginClass(
266 "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor");
267 executorPars.setStateFinalizerExecutorPluginClass(
268 "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor");
270 EngineParameters enginePars = new EngineParameters();
271 enginePars.getExecutorParameterMap().put("Dummy", executorPars);
273 ParameterService.register(enginePars);
274 ParameterService.register(executorPars);
278 * Set up non existant parameters.
280 private void setNonExistantPars() {
281 executorPars = new ExecutorParameters();
282 executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.BadTaskExecutor");
283 executorPars.setTaskSelectionExecutorPluginClass(
284 "org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor");
285 executorPars.setStateFinalizerExecutorPluginClass(
286 "org.onap.policy.apex.core.engine.executor.BadStateFinalizerExecutor");
288 EngineParameters enginePars = new EngineParameters();
289 enginePars.getExecutorParameterMap().put("Dummy", executorPars);
291 ParameterService.register(enginePars, true);
292 ParameterService.register(executorPars, true);
296 * Set up bad parameters.
298 private void setBadPars() {
299 executorPars = new ExecutorParameters();
300 executorPars.setTaskExecutorPluginClass("java.lang.String");
301 executorPars.setTaskSelectionExecutorPluginClass("java.lang.String");
302 executorPars.setStateFinalizerExecutorPluginClass("java.lang.String");
304 EngineParameters enginePars = new EngineParameters();
305 enginePars.getExecutorParameterMap().put("Dummy", executorPars);
307 ParameterService.register(enginePars, true);
308 ParameterService.register(executorPars, true);