f68a4872b91f4ea885a282cafde54005faa392e4
[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.service.engine.runtime.impl;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31
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.model.utilities.TextFileUtils;
52 import org.onap.policy.apex.service.engine.event.ApexEvent;
53 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
54 import org.onap.policy.common.parameters.ParameterService;
55
56 /**
57  * Test the engine service implementation.
58  */
59 public class EngineServiceImplTest {
60
61     private static String simpleModelString;
62     private static String mfpModelString;
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
74                         .getTextFileAsString("src/test/resources/policymodels/SamplePolicyModelJAVASCRIPT.json");
75
76         mfpModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/MyFirstPolicyModel.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.setTaskSelectionExecutorPluginClass(
113                         "org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
114         jsExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
115         jsExecutorParameters.setStateFinalizerExecutorPluginClass(
116                         "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.setTaskSelectionExecutorPluginClass(
121                         "org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
122         mvvelExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
123         mvvelExecutorParameters.setStateFinalizerExecutorPluginClass(
124                         "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         assertEquals("{\n   \"apexEngineModel\" :", status.substring(0, 24));
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(), mfpModelString, false);
429             fail("test should throw an exception");
430         } catch (ApexException apEx) {
431             assertEquals("apex model update failed, supplied model with key \"MyFirstPolicyModel:0.0.1\" is not a "
432                             + "compatible model update "
433                             + "from the existing engine model with key \"SamplePolicyModelJAVASCRIPT:0.0.1\"",
434                             apEx.getMessage());
435         }
436
437         try {
438             esImpl.updateModel(config.getEngineKey(), mfpModelString, true);
439         } catch (ApexException apEx) {
440             fail("test should not throw an exception");
441         }
442
443         try {
444             esImpl.updateModel(config.getEngineKey(), simpleModelString, true);
445         } catch (ApexException apEx) {
446             fail("test should not throw an exception");
447         }
448
449         String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
450         assertEquals("{\n  \"TimeStamp\":", runtimeInfo.substring(0, 16));
451
452         assertEquals(AxEngineState.EXECUTING, esImpl.getState());
453
454         String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
455         assertEquals("{\n   \"apexEngineModel\" :", status.substring(0, 24));
456
457         assertTrue(esImpl.isStarted());
458         assertTrue(esImpl.isStarted(esImpl.getEngineKeys().iterator().next()));
459         assertFalse(esImpl.isStopped());
460         assertFalse(esImpl.isStopped(esImpl.getEngineKeys().iterator().next()));
461
462         try {
463             esImpl.start(esImpl.getEngineKeys().iterator().next());
464             fail("test should throw an exception");
465         } catch (ApexException apEx) {
466             assertEquals("apex engine for engine key Engine-0:0.0.1 is already running with state READY",
467                             apEx.getMessage());
468         }
469
470         try {
471             esImpl.startAll();
472             fail("test should throw an exception");
473         } catch (ApexException apEx) {
474             assertEquals("apex engine for engine key Engine-0:0.0.1 is already running with state READY",
475                             apEx.getMessage());
476         }
477
478         try {
479             esImpl.stop(esImpl.getEngineKeys().iterator().next());
480         } catch (ApexException apEx) {
481             fail("test should not throw an exception");
482         }
483
484         try {
485             esImpl.start(esImpl.getEngineKeys().iterator().next());
486         } catch (ApexException apEx) {
487             fail("test should not throw an exception");
488         }
489
490         try {
491             esImpl.stop();
492         } catch (ApexException apEx) {
493             fail("test should not throw an exception");
494         }
495
496         try {
497             esImpl.startAll();
498         } catch (ApexException apEx) {
499             fail("test should not throw an exception");
500         }
501
502         try {
503             esImpl.sendEvent(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget"));
504         } catch (ApexException apEx) {
505             fail("test should not throw an exception");
506         }
507
508         esImpl.startPeriodicEvents(100000);
509         esImpl.stop();
510         esImpl.startAll();
511         esImpl.stopPeriodicEvents();
512
513         esImpl.startPeriodicEvents(100000);
514         try {
515             esImpl.startPeriodicEvents(100000);
516             fail("test should throw an exception");
517         } catch (ApexException apEx) {
518             assertEquals("Peiodic event geneation already running on engine Engine:0.0.1, ApexPeriodicEventGenerator "
519                             + "[period=100000, firstEventTime=0, lastEventTime=0, eventCount=0]", apEx.getMessage());
520         }
521
522         esImpl.stopPeriodicEvents();
523         try {
524             esImpl.stopPeriodicEvents();
525             fail("test should throw an exception");
526         } catch (ApexException apEx) {
527             assertEquals("Peiodic event geneation not running on engine Engine:0.0.1", apEx.getMessage());
528         }
529
530         try {
531             esImpl.clear(esImpl.getEngineKeys().iterator().next());
532         } catch (ApexException apEx) {
533             fail("test should not throw an exception");
534         }
535
536         try {
537             esImpl.clear();
538         } catch (ApexException apEx) {
539             fail("test should not throw an exception");
540         }
541     }
542 }