5957ff34770e0710a15146c96e9e0148830ebbfb
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.executor.impl;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
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() throws StateMachineException {
97         setGoodPars();
98
99         ExecutorFactoryImpl factory = null;
100
101         factory = new ExecutorFactoryImpl();
102
103         Mockito.doReturn(true).when(stateMock).checkSetTaskSelectionLogic();
104         assertNotNull(factory.getTaskSelectionExecutor(null, stateMock, internalContextMock));
105         Mockito.doReturn(false).when(stateMock).checkSetTaskSelectionLogic();
106         assertNull(factory.getTaskSelectionExecutor(null, stateMock, internalContextMock));
107
108         assertNotNull(factory.getTaskExecutor(null, taskMock, internalContextMock));
109
110         assertNotNull(factory.getStateFinalizerExecutor(parentMock, sflMock, internalContextMock));
111     }
112
113     @Test
114     public void testExecutorFactoryImplNonExistant() {
115         setNonExistantPars();
116
117         assertThatThrownBy(() -> new ExecutorFactoryImpl())
118             .hasMessage("Apex executor class not found for executor plugin "
119                             + "\"org.onap.policy.apex.core.engine.executor.BadTaskExecutor\"");
120         executorPars.setTaskExecutorPluginClass(null);
121
122         assertThatThrownBy(() -> new ExecutorFactoryImpl())
123             .hasMessage("Apex executor class not found for executor plugin "
124                     + "\"org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor\"");
125         executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor");
126
127         assertThatThrownBy(() -> new ExecutorFactoryImpl())
128             .hasMessage("Apex executor class not found for executor plugin "
129                     + "\"org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor\"");
130         executorPars.setTaskSelectionExecutorPluginClass(
131                         "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor");
132
133         assertThatThrownBy(() -> new ExecutorFactoryImpl())
134             .hasMessage("Apex executor class not found for executor plugin "
135                     + "\"org.onap.policy.apex.core.engine.executor.BadStateFinalizerExecutor\"");
136     }
137
138     @Test
139     public void testExecutorFactoryImplBad() {
140         setBadPars();
141
142         assertThatThrownBy(() -> new ExecutorFactoryImpl())
143             .hasMessage("Specified Apex executor plugin class \"java.lang.String\" "
144                     + "does not implment the Executor interface");
145         executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor");
146
147         assertThatThrownBy(() -> new ExecutorFactoryImpl())
148             .hasMessage("Specified Apex executor plugin class \"java.lang.String\" "
149                     + "does not implment the Executor interface");
150         executorPars.setTaskSelectionExecutorPluginClass(
151                         "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor");
152
153         assertThatThrownBy(() -> new ExecutorFactoryImpl())
154             .hasMessage("Specified Apex executor plugin class \"java.lang.String\" "
155                     + "does not implment the Executor interface");
156     }
157
158     @Test
159     public void testExecutorFactoryCreateErrors() throws StateMachineException {
160         setGoodPars();
161
162         executorPars.setTaskExecutorPluginClass(null);
163
164         final ExecutorFactoryImpl factory = new ExecutorFactoryImpl();
165
166         Mockito.doReturn(true).when(stateMock).checkSetTaskSelectionLogic();
167
168         assertThatThrownBy(() -> factory.getTaskExecutor(null, taskMock, internalContextMock))
169             .hasMessage("Executor plugin class not defined for \"Dummy\" executor of type "
170                     + "\"org.onap.policy.apex.core.engine.executor.TaskExecutor\"");
171         executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor");
172
173         ExecutorFactoryImpl factoryInitError = new ExecutorFactoryImpl();
174
175         assertThatThrownBy(() -> factoryInitError.getTaskExecutor(null, taskMock, internalContextMock))
176             .hasMessage("Instantiation error on \"Dummy\" executor of type "
177                     + "\"org.onap.policy.apex.core.engine.executor.DummyFailingTaskExecutor\"");
178         executorPars.setTaskExecutorPluginClass(
179                         "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor");
180
181         ExecutorFactoryImpl factoryDummyError = new ExecutorFactoryImpl();
182
183         assertThatThrownBy(() -> factoryDummyError.getTaskExecutor(null, taskMock, internalContextMock))
184             .hasMessage("Executor on \"Dummy\" "
185                     + "of type \"class org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor\""
186                     + " is not an instance of \"org.onap.policy.apex.core.engine.executor.TaskExecutor\"");
187     }
188
189     /**
190      * Set up good parameters.
191      */
192     private void setGoodPars() {
193         executorPars = new ExecutorParameters();
194         executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.DummyTaskExecutor");
195         executorPars.setTaskSelectionExecutorPluginClass(
196                         "org.onap.policy.apex.core.engine.executor.DummyTaskSelectExecutor");
197         executorPars.setStateFinalizerExecutorPluginClass(
198                         "org.onap.policy.apex.core.engine.executor.DummyStateFinalizerExecutor");
199
200         EngineParameters enginePars = new EngineParameters();
201         enginePars.getExecutorParameterMap().put("Dummy", executorPars);
202
203         ParameterService.register(enginePars);
204         ParameterService.register(executorPars);
205     }
206
207     /**
208      * Set up non existant parameters.
209      */
210     private void setNonExistantPars() {
211         executorPars = new ExecutorParameters();
212         executorPars.setTaskExecutorPluginClass("org.onap.policy.apex.core.engine.executor.BadTaskExecutor");
213         executorPars.setTaskSelectionExecutorPluginClass(
214                         "org.onap.policy.apex.core.engine.executor.BadTaskSelectExecutor");
215         executorPars.setStateFinalizerExecutorPluginClass(
216                         "org.onap.policy.apex.core.engine.executor.BadStateFinalizerExecutor");
217
218         EngineParameters enginePars = new EngineParameters();
219         enginePars.getExecutorParameterMap().put("Dummy", executorPars);
220
221         ParameterService.register(enginePars, true);
222         ParameterService.register(executorPars, true);
223     }
224
225     /**
226      * Set up bad parameters.
227      */
228     private void setBadPars() {
229         executorPars = new ExecutorParameters();
230         executorPars.setTaskExecutorPluginClass("java.lang.String");
231         executorPars.setTaskSelectionExecutorPluginClass("java.lang.String");
232         executorPars.setStateFinalizerExecutorPluginClass("java.lang.String");
233
234         EngineParameters enginePars = new EngineParameters();
235         enginePars.getExecutorParameterMap().put("Dummy", executorPars);
236
237         ParameterService.register(enginePars, true);
238         ParameterService.register(executorPars, true);
239     }
240 }