7fe394758813a8d71b4ad0b9d8dde912c2efd82e
[policy/apex-pdp.git] /
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.core.engine.executor.impl;
22
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;
27
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;
45
46 /**
47  * Test the executor factory implementation.
48  *
49  */
50 public class ExceutorFactoryImplTest {
51     @Mock
52     private ApexInternalContext internalContextMock;
53
54     @Mock
55     private AxState stateMock;
56
57     @Mock
58     private AxTaskSelectionLogic tslMock;
59
60     @Mock
61     private AxTask taskMock;
62
63     @Mock
64     private AxTaskLogic tlMock;
65
66     @Mock
67     private AxStateFinalizerLogic sflMock;
68
69     @Mock
70     private Executor<?, ?, ?, ?> parentMock;
71
72     private ExecutorParameters executorPars;
73
74     /**
75      * Set up mocking.
76      */
77     @Before
78     public void startMocking() {
79         MockitoAnnotations.initMocks(this);
80
81         Mockito.doReturn(tslMock).when(stateMock).getTaskSelectionLogic();
82         Mockito.doReturn("Dummy").when(tslMock).getLogicFlavour();
83
84         Mockito.doReturn(tlMock).when(taskMock).getTaskLogic();
85         Mockito.doReturn("Dummy").when(tlMock).getLogicFlavour();
86
87         Mockito.doReturn("Dummy").when(sflMock).getLogicFlavour();
88     }
89
90     @After
91     public void clearPars() {
92         ParameterService.clear();
93     }
94
95     @Test
96     public void testExecutorFactoryImplGood() {
97         setGoodPars();
98
99         ExecutorFactoryImpl factory = null;
100
101         try {
102             factory = new ExecutorFactoryImpl();
103         } catch (StateMachineException e) {
104             fail("test should not throw an exception");
105         }
106
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));
111
112         assertNotNull(factory.getTaskExecutor(null, taskMock, internalContextMock));
113
114         assertNotNull(factory.getStateFinalizerExecutor(parentMock, sflMock, internalContextMock));
115     }
116
117     @Test
118     public void testExecutorFactoryImplNonExistant() {
119         setNonExistantPars();
120
121         try {
122             new ExecutorFactoryImpl();
123             fail("test should throw an exception");
124
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());
128         }
129
130         executorPars.setTaskExecutorPluginClass(null);
131
132         try {
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());
138         }
139
140         executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor");
141
142         try {
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());
148         }
149
150         executorPars.setTaskSelectionExecutorPluginClass(
151                         "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor");
152
153         try {
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\"",
159                             ex.getMessage());
160         }
161     }
162
163     @Test
164     public void testExecutorFactoryImplBad() {
165         setBadPars();
166
167         try {
168             new ExecutorFactoryImpl();
169             fail("test should throw an exception");
170
171         } catch (StateMachineException ex) {
172             assertEquals("Specified Apex executor plugin class \"java.lang.String\" "
173                             + "does not implment the Executor interface", ex.getMessage());
174         }
175
176         executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor");
177
178         try {
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());
184         }
185
186         executorPars.setTaskSelectionExecutorPluginClass(
187                         "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor");
188
189         try {
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());
195         }
196     }
197
198     @Test
199     public void testExecutorFactoryCreateErrors() {
200         setGoodPars();
201
202         executorPars.setTaskExecutorPluginClass(null);
203
204         ExecutorFactoryImpl factory = null;
205
206         try {
207             factory = new ExecutorFactoryImpl();
208         } catch (StateMachineException e) {
209             fail("test should not throw an exception");
210         }
211
212         Mockito.doReturn(true).when(stateMock).checkSetTaskSelectionLogic();
213
214         try {
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());
220         }
221
222         executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor");
223
224         try {
225             factory = new ExecutorFactoryImpl();
226         } catch (StateMachineException e) {
227             fail("test should not throw an exception");
228         }
229
230         try {
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\"",
236                             ex.getMessage());
237         }
238
239         executorPars.setTaskExecutorPluginClass(
240                         "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor");
241
242         try {
243             factory = new ExecutorFactoryImpl();
244         } catch (StateMachineException e) {
245             fail("test should not throw an exception");
246         }
247
248         try {
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\"",
255                             ex.getMessage());
256         }
257     }
258
259     /**
260      * Set up good parameters.
261      */
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");
269
270         EngineParameters enginePars = new EngineParameters();
271         enginePars.getExecutorParameterMap().put("Dummy", executorPars);
272
273         ParameterService.register(enginePars);
274         ParameterService.register(executorPars);
275     }
276
277     /**
278      * Set up non existant parameters.
279      */
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");
287
288         EngineParameters enginePars = new EngineParameters();
289         enginePars.getExecutorParameterMap().put("Dummy", executorPars);
290
291         ParameterService.register(enginePars, true);
292         ParameterService.register(executorPars, true);
293     }
294
295     /**
296      * Set up bad parameters.
297      */
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");
303
304         EngineParameters enginePars = new EngineParameters();
305         enginePars.getExecutorParameterMap().put("Dummy", executorPars);
306
307         ParameterService.register(enginePars, true);
308         ParameterService.register(executorPars, true);
309     }
310 }