Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / runtime / impl / EngineServiceImplTest.java
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.service.engine.runtime.impl;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
36 import org.onap.policy.apex.context.parameters.ContextParameters;
37 import org.onap.policy.apex.context.parameters.DistributorParameters;
38 import org.onap.policy.apex.context.parameters.LockManagerParameters;
39 import org.onap.policy.apex.context.parameters.PersistorParameters;
40 import org.onap.policy.apex.context.parameters.SchemaParameters;
41 import org.onap.policy.apex.core.engine.EngineParameterConstants;
42 import org.onap.policy.apex.core.engine.EngineParameters;
43 import org.onap.policy.apex.core.engine.ExecutorParameters;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
46 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
47 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
48 import org.onap.policy.apex.model.basicmodel.service.ModelService;
49 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
50 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
51 import org.onap.policy.apex.service.engine.event.ApexEvent;
52 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
53 import org.onap.policy.common.parameters.ParameterService;
54 import org.onap.policy.common.utils.resources.TextFileUtils;
55
56 /**
57  * Test the engine service implementation.
58  */
59 public class EngineServiceImplTest {
60
61     private static String simpleModelString;
62     private static String differentModelString;
63     private static AxPolicyModel simpleModel;
64
65     /**
66      * Read the models into strings.
67      *
68      * @throws IOException on model reading errors
69      * @throws ApexModelException on model reading exceptions
70      */
71     @BeforeClass
72     public static void readSimpleModel() throws IOException, ApexModelException {
73         simpleModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SmallModel.json");
74
75         differentModelString =
76                 TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SmallModelDifferent.json");
77
78         final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
79         simpleModel = modelReader.read(new ByteArrayInputStream(simpleModelString.getBytes()));
80     }
81
82     /**
83      * Initialize default parameters.
84      */
85     @BeforeClass
86     public static void initializeDefaultParameters() {
87         ParameterService.clear();
88         final SchemaParameters schemaParameters = new SchemaParameters();
89         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
90         ParameterService.register(schemaParameters);
91
92         final ContextParameters contextParameters = new ContextParameters();
93         contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
94         ParameterService.register(contextParameters);
95
96         final DistributorParameters distributorParameters = new DistributorParameters();
97         distributorParameters.setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
98         ParameterService.register(distributorParameters);
99
100         final LockManagerParameters lockManagerParameters = new LockManagerParameters();
101         lockManagerParameters.setName(ContextParameterConstants.LOCKING_GROUP_NAME);
102         ParameterService.register(lockManagerParameters);
103
104         final PersistorParameters persistorParameters = new PersistorParameters();
105         persistorParameters.setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
106         ParameterService.register(persistorParameters);
107
108         final EngineParameters engineParameters = new EngineParameters();
109         engineParameters.setName(EngineParameterConstants.MAIN_GROUP_NAME);
110         ExecutorParameters jsExecutorParameters = new ExecutorParameters();
111         jsExecutorParameters.setName("JAVASCRIPT");
112         jsExecutorParameters
113                 .setTaskSelectionExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
114         jsExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
115         jsExecutorParameters
116                 .setStateFinalizerExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
117         engineParameters.getExecutorParameterMap().put("JAVASCRIPT", jsExecutorParameters);
118         ExecutorParameters mvvelExecutorParameters = new ExecutorParameters();
119         mvvelExecutorParameters.setName("MVEL");
120         mvvelExecutorParameters
121                 .setTaskSelectionExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
122         mvvelExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
123         mvvelExecutorParameters
124                 .setStateFinalizerExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
125         engineParameters.getExecutorParameterMap().put("MVEL", jsExecutorParameters);
126         ParameterService.register(engineParameters);
127     }
128
129     /**
130      * Teardown default parameters.
131      */
132     @AfterClass
133     public static void teardownDefaultParameters() {
134         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
135         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
136         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
137         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
138         ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
139         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
140         ModelService.clear();
141     }
142
143     @Test
144     public void testEngineServiceImplSanity() throws ApexException {
145         try {
146             EngineServiceImpl.create(null);
147             fail("test should throw an exception");
148         } catch (ApexException apEx) {
149             assertEquals("engine service configuration parameters are null", apEx.getMessage());
150         }
151
152         EngineServiceParameters config = new EngineServiceParameters();
153         config.setInstanceCount(0);
154
155         try {
156             EngineServiceImpl.create(config);
157             fail("test should throw an exception");
158         } catch (ApexException apEx) {
159             assertEquals("Invalid engine service configuration parameters:", apEx.getMessage().substring(0, 48));
160         }
161
162         config.setId(123);
163         config.setEngineKey(new AxArtifactKey("Engine", "0.0.1"));
164         config.setInstanceCount(1);
165
166         EngineServiceImpl esImpl = EngineServiceImpl.create(config);
167         assertEquals("Engine:0.0.1", esImpl.getKey().getId());
168
169         esImpl.registerActionListener(null, null);
170         esImpl.registerActionListener("DummyListener", null);
171         esImpl.registerActionListener(null, new DummyApexEventListener());
172
173         esImpl.registerActionListener("DummyListener", new DummyApexEventListener());
174         try {
175             esImpl.deregisterActionListener(null);
176             fail("test should throw an exception");
177         } catch (Exception apEx) {
178             assertEquals("removeEventListener()<-Engine-0:0.0.1,STOPPED, listenerName is null", apEx.getMessage());
179         }
180
181         esImpl.deregisterActionListener("DummyListener");
182
183         assertEquals(esImpl, esImpl.getEngineServiceEventInterface());
184         assertEquals(1, esImpl.getEngineKeys().size());
185
186         assertNull(esImpl.getApexModelKey());
187
188         try {
189             esImpl.getRuntimeInfo(null);
190             fail("test should throw an exception");
191         } catch (ApexException apEx) {
192             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
193         }
194
195         try {
196             esImpl.getRuntimeInfo(new AxArtifactKey("DummyKey", "0.0.1"));
197             fail("test should throw an exception");
198         } catch (ApexException apEx) {
199             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
200         }
201
202         String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
203         assertEquals("{\n  \"TimeStamp\":", runtimeInfo.substring(0, 16));
204
205         assertEquals(AxEngineState.STOPPED, esImpl.getState());
206
207         try {
208             esImpl.getStatus(null);
209             fail("test should throw an exception");
210         } catch (ApexException apEx) {
211             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
212         }
213
214         try {
215             esImpl.getStatus(new AxArtifactKey("DummyKey", "0.0.1"));
216             fail("test should throw an exception");
217         } catch (ApexException apEx) {
218             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
219         }
220
221         String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
222         assertTrue(status.contains("\n   \"apexEngineModel\" :"));
223
224         assertFalse(esImpl.isStarted());
225         assertFalse(esImpl.isStarted(null));
226         assertFalse(esImpl.isStarted(new AxArtifactKey("DummyKey", "0.0.1")));
227         assertFalse(esImpl.isStarted(esImpl.getEngineKeys().iterator().next()));
228         assertTrue(esImpl.isStopped());
229         assertTrue(esImpl.isStopped(null));
230         assertTrue(esImpl.isStopped(new AxArtifactKey("DummyKey", "0.0.1")));
231         assertTrue(esImpl.isStopped(esImpl.getEngineKeys().iterator().next()));
232
233         try {
234             esImpl.start(null);
235             fail("test should throw an exception");
236         } catch (ApexException apEx) {
237             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
238         }
239
240         try {
241             esImpl.start(new AxArtifactKey("DummyKey", "0.0.1"));
242             fail("test should throw an exception");
243         } catch (ApexException apEx) {
244             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
245         }
246
247         try {
248             esImpl.start(esImpl.getEngineKeys().iterator().next());
249             fail("test should throw an exception");
250         } catch (ApexException apEx) {
251             assertEquals("start()<-Engine-0:0.0.1,STOPPED,  cannot start engine, "
252                     + "engine has not been initialized, its model is not loaded", apEx.getMessage());
253         }
254
255         try {
256             esImpl.startAll();
257             fail("test should throw an exception");
258         } catch (ApexException apEx) {
259             assertEquals("start()<-Engine-0:0.0.1,STOPPED,  cannot start engine, "
260                     + "engine has not been initialized, its model is not loaded", apEx.getMessage());
261         }
262
263         try {
264             esImpl.stop(null);
265             fail("test should throw an exception");
266         } catch (ApexException apEx) {
267             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
268         }
269
270         try {
271             esImpl.stop(new AxArtifactKey("DummyKey", "0.0.1"));
272             fail("test should throw an exception");
273         } catch (ApexException apEx) {
274             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
275         }
276
277         try {
278             esImpl.stop(esImpl.getEngineKeys().iterator().next());
279         } catch (ApexException apEx) {
280             fail("test should not throw an exception");
281         }
282
283         try {
284             esImpl.stop();
285         } catch (ApexException apEx) {
286             fail("test should not throw an exception");
287         }
288
289         try {
290             esImpl.sendEvent(null);
291         } catch (Exception apEx) {
292             fail("test should not throw an exception");
293         }
294
295         try {
296             esImpl.sendEvent(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget"));
297         } catch (ApexException apEx) {
298             fail("test should not throw an exception");
299         }
300
301         esImpl.startPeriodicEvents(100000);
302
303         try {
304             esImpl.startPeriodicEvents(100000);
305             fail("test should throw an exception");
306         } catch (ApexException apEx) {
307             assertEquals("Peiodic event geneation already running on engine Engine:0.0.1, ApexPeriodicEventGenerator "
308                     + "[period=100000, firstEventTime=0, lastEventTime=0, eventCount=0]", apEx.getMessage());
309         }
310
311         esImpl.stopPeriodicEvents();
312         try {
313             esImpl.stopPeriodicEvents();
314             fail("test should throw an exception");
315         } catch (ApexException apEx) {
316             assertEquals("Peiodic event geneation not running on engine Engine:0.0.1", apEx.getMessage());
317         }
318
319         try {
320             esImpl.clear(null);
321             fail("test should throw an exception");
322         } catch (ApexException apEx) {
323             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
324         }
325
326         try {
327             esImpl.clear(new AxArtifactKey("DummyKey", "0.0.1"));
328             fail("test should throw an exception");
329         } catch (ApexException apEx) {
330             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
331         }
332
333         try {
334             esImpl.clear(esImpl.getEngineKeys().iterator().next());
335         } catch (ApexException apEx) {
336             fail("test should not throw an exception");
337         }
338
339         try {
340             esImpl.clear();
341         } catch (ApexException apEx) {
342             fail("test should not throw an exception");
343         }
344
345         try {
346             esImpl.updateModel(null, (String) null, true);
347             fail("test should throw an exception");
348         } catch (ApexException apEx) {
349             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
350         }
351
352         try {
353             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (String) null, true);
354             fail("test should throw an exception");
355         } catch (ApexException apEx) {
356             assertEquals("model for updating engine service with key DummyKey:0.0.1 is empty", apEx.getMessage());
357         }
358
359         try {
360             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "", true);
361             fail("test should throw an exception");
362         } catch (ApexException apEx) {
363             assertEquals("model for updating engine service with key DummyKey:0.0.1 is empty", apEx.getMessage());
364         }
365
366         try {
367             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "I am not an Apex model", true);
368             fail("test should throw an exception");
369         } catch (ApexException apEx) {
370             assertEquals("failed to unmarshal the apex model on engine service DummyKey:0.0.1", apEx.getMessage());
371         }
372
373         try {
374             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModelString, true);
375             fail("test should throw an exception");
376         } catch (ApexException apEx) {
377             assertEquals("engine service key DummyKey:0.0.1 does not match the keyEngine:0.0.1 of this engine service",
378                     apEx.getMessage());
379         }
380
381         try {
382             esImpl.updateModel(null, simpleModelString, true);
383             fail("test should throw an exception");
384         } catch (ApexException apEx) {
385             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
386         }
387
388         try {
389             esImpl.updateModel(null, (AxPolicyModel) null, true);
390             fail("test should throw an exception");
391         } catch (ApexException apEx) {
392             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
393         }
394
395         try {
396             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (AxPolicyModel) null, true);
397             fail("test should throw an exception");
398         } catch (ApexException apEx) {
399             assertEquals("model for updating on engine service with key DummyKey:0.0.1 is null", apEx.getMessage());
400         }
401
402         try {
403             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModel, true);
404             fail("test should throw an exception");
405         } catch (ApexException apEx) {
406             assertEquals("engine service key DummyKey:0.0.1 does not match the keyEngine:0.0.1 of this engine service",
407                     apEx.getMessage());
408         }
409     }
410
411     @Test
412     public void testApexImplModelWIthModel() throws ApexException {
413         EngineServiceParameters config = new EngineServiceParameters();
414         config.setId(123);
415         config.setEngineKey(new AxArtifactKey("Engine", "0.0.1"));
416         config.setInstanceCount(1);
417
418         EngineServiceImpl esImpl = EngineServiceImpl.create(config);
419         assertEquals("Engine:0.0.1", esImpl.getKey().getId());
420
421         try {
422             esImpl.updateModel(config.getEngineKey(), simpleModelString, false);
423         } catch (ApexException apEx) {
424             fail("test should not throw an exception");
425         }
426
427         try {
428             esImpl.updateModel(config.getEngineKey(), differentModelString, false);
429             fail("test should throw an exception");
430         } catch (ApexException apEx) {
431             assertEquals("apex model update failed, supplied model with key \"SmallModelDifferent:0.0.1\" is not a "
432                     + "compatible model update " + "from the existing engine model with key \"SmallModel:0.0.1\"",
433                     apEx.getMessage());
434         }
435
436         try {
437             esImpl.updateModel(config.getEngineKey(), differentModelString, true);
438         } catch (ApexException apEx) {
439             fail("test should not throw an exception");
440         }
441
442         try {
443             esImpl.updateModel(config.getEngineKey(), simpleModelString, true);
444         } catch (ApexException apEx) {
445             fail("test should not throw an exception");
446         }
447
448         String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
449         assertEquals("{\n  \"TimeStamp\":", runtimeInfo.substring(0, 16));
450
451         assertEquals(AxEngineState.EXECUTING, esImpl.getState());
452
453         String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
454         assertTrue(status.contains("\n   \"apexEngineModel\" :"));
455         assertTrue(esImpl.isStarted());
456         assertTrue(esImpl.isStarted(esImpl.getEngineKeys().iterator().next()));
457         assertFalse(esImpl.isStopped());
458         assertFalse(esImpl.isStopped(esImpl.getEngineKeys().iterator().next()));
459
460         try {
461             esImpl.start(esImpl.getEngineKeys().iterator().next());
462             fail("test should throw an exception");
463         } catch (ApexException apEx) {
464             assertEquals("apex engine for engine key Engine-0:0.0.1 is already running with state READY",
465                     apEx.getMessage());
466         }
467
468         try {
469             esImpl.startAll();
470             fail("test should throw an exception");
471         } catch (ApexException apEx) {
472             assertEquals("apex engine for engine key Engine-0:0.0.1 is already running with state READY",
473                     apEx.getMessage());
474         }
475
476         try {
477             esImpl.stop(esImpl.getEngineKeys().iterator().next());
478         } catch (ApexException apEx) {
479             fail("test should not throw an exception");
480         }
481
482         try {
483             esImpl.start(esImpl.getEngineKeys().iterator().next());
484         } catch (ApexException apEx) {
485             fail("test should not throw an exception");
486         }
487
488         try {
489             esImpl.stop();
490         } catch (ApexException apEx) {
491             fail("test should not throw an exception");
492         }
493
494         try {
495             esImpl.startAll();
496         } catch (ApexException apEx) {
497             fail("test should not throw an exception");
498         }
499
500         try {
501             esImpl.sendEvent(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget"));
502         } catch (ApexException apEx) {
503             fail("test should not throw an exception");
504         }
505
506         esImpl.startPeriodicEvents(100000);
507         esImpl.stop();
508         esImpl.startAll();
509         esImpl.stopPeriodicEvents();
510
511         esImpl.startPeriodicEvents(100000);
512         try {
513             esImpl.startPeriodicEvents(100000);
514             fail("test should throw an exception");
515         } catch (ApexException apEx) {
516             assertEquals("Peiodic event geneation already running on engine Engine:0.0.1, ApexPeriodicEventGenerator "
517                     + "[period=100000, firstEventTime=0, lastEventTime=0, eventCount=0]", apEx.getMessage());
518         }
519
520         esImpl.stopPeriodicEvents();
521         try {
522             esImpl.stopPeriodicEvents();
523             fail("test should throw an exception");
524         } catch (ApexException apEx) {
525             assertEquals("Peiodic event geneation not running on engine Engine:0.0.1", apEx.getMessage());
526         }
527
528         try {
529             esImpl.clear(esImpl.getEngineKeys().iterator().next());
530         } catch (ApexException apEx) {
531             fail("test should not throw an exception");
532         }
533
534         try {
535             esImpl.clear();
536         } catch (ApexException apEx) {
537             fail("test should not throw an exception");
538         }
539     }
540 }