582e61948847ad4e5c4e6a91043705b805786084
[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         esImpl.deregisterActionListener(null);
175         esImpl.deregisterActionListener("DummyListener");
176
177         assertEquals(esImpl, esImpl.getEngineServiceEventInterface());
178         assertEquals(1, esImpl.getEngineKeys().size());
179
180         assertNull(esImpl.getApexModelKey());
181
182         try {
183             esImpl.getRuntimeInfo(null);
184             fail("test should throw an exception");
185         } catch (ApexException apEx) {
186             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
187         }
188
189         try {
190             esImpl.getRuntimeInfo(new AxArtifactKey("DummyKey", "0.0.1"));
191             fail("test should throw an exception");
192         } catch (ApexException apEx) {
193             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
194         }
195
196         String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
197         assertEquals("{\n  \"TimeStamp\":", runtimeInfo.substring(0, 16));
198
199         assertEquals(AxEngineState.STOPPED, esImpl.getState());
200
201         try {
202             esImpl.getStatus(null);
203             fail("test should throw an exception");
204         } catch (ApexException apEx) {
205             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
206         }
207
208         try {
209             esImpl.getStatus(new AxArtifactKey("DummyKey", "0.0.1"));
210             fail("test should throw an exception");
211         } catch (ApexException apEx) {
212             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
213         }
214
215         String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
216         assertEquals("{\n   \"apexEngineModel\" :", status.substring(0, 24));
217
218         assertFalse(esImpl.isStarted());
219         assertFalse(esImpl.isStarted(null));
220         assertFalse(esImpl.isStarted(new AxArtifactKey("DummyKey", "0.0.1")));
221         assertFalse(esImpl.isStarted(esImpl.getEngineKeys().iterator().next()));
222         assertTrue(esImpl.isStopped());
223         assertTrue(esImpl.isStopped(null));
224         assertTrue(esImpl.isStopped(new AxArtifactKey("DummyKey", "0.0.1")));
225         assertTrue(esImpl.isStopped(esImpl.getEngineKeys().iterator().next()));
226
227         try {
228             esImpl.start(null);
229             fail("test should throw an exception");
230         } catch (ApexException apEx) {
231             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
232         }
233
234         try {
235             esImpl.start(new AxArtifactKey("DummyKey", "0.0.1"));
236             fail("test should throw an exception");
237         } catch (ApexException apEx) {
238             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
239         }
240
241         try {
242             esImpl.start(esImpl.getEngineKeys().iterator().next());
243             fail("test should throw an exception");
244         } catch (ApexException apEx) {
245             assertEquals("start()<-Engine-0:0.0.1,STOPPED,  cannot start engine, "
246                             + "engine has not been initialized, its model is not loaded", apEx.getMessage());
247         }
248
249         try {
250             esImpl.startAll();
251             fail("test should throw an exception");
252         } catch (ApexException apEx) {
253             assertEquals("start()<-Engine-0:0.0.1,STOPPED,  cannot start engine, "
254                             + "engine has not been initialized, its model is not loaded", apEx.getMessage());
255         }
256
257         try {
258             esImpl.stop(null);
259             fail("test should throw an exception");
260         } catch (ApexException apEx) {
261             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
262         }
263
264         try {
265             esImpl.stop(new AxArtifactKey("DummyKey", "0.0.1"));
266             fail("test should throw an exception");
267         } catch (ApexException apEx) {
268             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
269         }
270
271         try {
272             esImpl.stop(esImpl.getEngineKeys().iterator().next());
273         } catch (ApexException apEx) {
274             fail("test should not throw an exception");
275         }
276
277         try {
278             esImpl.stop();
279         } catch (ApexException apEx) {
280             fail("test should not throw an exception");
281         }
282
283         try {
284             esImpl.sendEvent(null);
285         } catch (Exception apEx) {
286             fail("test should not throw an exception");
287         }
288
289         try {
290             esImpl.sendEvent(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget"));
291         } catch (ApexException apEx) {
292             fail("test should not throw an exception");
293         }
294
295         esImpl.startPeriodicEvents(100000);
296
297         try {
298             esImpl.startPeriodicEvents(100000);
299             fail("test should throw an exception");
300         } catch (ApexException apEx) {
301             assertEquals("Peiodic event geneation already running on engine Engine:0.0.1, ApexPeriodicEventGenerator "
302                             + "[period=100000, firstEventTime=0, lastEventTime=0, eventCount=0]", apEx.getMessage());
303         }
304
305         esImpl.stopPeriodicEvents();
306         try {
307             esImpl.stopPeriodicEvents();
308             fail("test should throw an exception");
309         } catch (ApexException apEx) {
310             assertEquals("Peiodic event geneation not running on engine Engine:0.0.1", apEx.getMessage());
311         }
312
313         try {
314             esImpl.clear(null);
315             fail("test should throw an exception");
316         } catch (ApexException apEx) {
317             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
318         }
319
320         try {
321             esImpl.clear(new AxArtifactKey("DummyKey", "0.0.1"));
322             fail("test should throw an exception");
323         } catch (ApexException apEx) {
324             assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
325         }
326
327         try {
328             esImpl.clear(esImpl.getEngineKeys().iterator().next());
329         } catch (ApexException apEx) {
330             fail("test should not throw an exception");
331         }
332
333         try {
334             esImpl.clear();
335         } catch (ApexException apEx) {
336             fail("test should not throw an exception");
337         }
338
339         try {
340             esImpl.updateModel(null, (String) null, true);
341             fail("test should throw an exception");
342         } catch (ApexException apEx) {
343             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
344         }
345
346         try {
347             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (String) null, true);
348             fail("test should throw an exception");
349         } catch (ApexException apEx) {
350             assertEquals("model for updating engine service with key DummyKey:0.0.1 is empty", apEx.getMessage());
351         }
352
353         try {
354             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "", true);
355             fail("test should throw an exception");
356         } catch (ApexException apEx) {
357             assertEquals("model for updating engine service with key DummyKey:0.0.1 is empty", apEx.getMessage());
358         }
359
360         try {
361             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "I am not an Apex model", true);
362             fail("test should throw an exception");
363         } catch (ApexException apEx) {
364             assertEquals("failed to unmarshal the apex model on engine service DummyKey:0.0.1", apEx.getMessage());
365         }
366
367         try {
368             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModelString, true);
369             fail("test should throw an exception");
370         } catch (ApexException apEx) {
371             assertEquals("engine service key DummyKey:0.0.1 does not match the keyEngine:0.0.1 of this engine service",
372                             apEx.getMessage());
373         }
374
375         try {
376             esImpl.updateModel(null, simpleModelString, true);
377             fail("test should throw an exception");
378         } catch (ApexException apEx) {
379             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
380         }
381
382         try {
383             esImpl.updateModel(null, (AxPolicyModel) null, true);
384             fail("test should throw an exception");
385         } catch (ApexException apEx) {
386             assertEquals("engine key must be specified and may not be null", apEx.getMessage());
387         }
388
389         try {
390             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (AxPolicyModel) null, true);
391             fail("test should throw an exception");
392         } catch (ApexException apEx) {
393             assertEquals("model for updating on engine service with key DummyKey:0.0.1 is null", apEx.getMessage());
394         }
395
396         try {
397             esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModel, true);
398             fail("test should throw an exception");
399         } catch (ApexException apEx) {
400             assertEquals("engine service key DummyKey:0.0.1 does not match the keyEngine:0.0.1 of this engine service",
401                             apEx.getMessage());
402         }
403     }
404
405     @Test
406     public void testApexImplModelWIthModel() throws ApexException {
407         EngineServiceParameters config = new EngineServiceParameters();
408         config.setId(123);
409         config.setEngineKey(new AxArtifactKey("Engine", "0.0.1"));
410         config.setInstanceCount(1);
411
412         EngineServiceImpl esImpl = EngineServiceImpl.create(config);
413         assertEquals("Engine:0.0.1", esImpl.getKey().getId());
414
415         try {
416             esImpl.updateModel(config.getEngineKey(), simpleModelString, false);
417         } catch (ApexException apEx) {
418             fail("test should not throw an exception");
419         }
420
421         try {
422             esImpl.updateModel(config.getEngineKey(), mfpModelString, false);
423             fail("test should throw an exception");
424         } catch (ApexException apEx) {
425             assertEquals("apex model update failed, supplied model with key \"MyFirstPolicyModel:0.0.1\" is not a "
426                             + "compatible model update "
427                             + "from the existing engine model with key \"SamplePolicyModelJAVASCRIPT:0.0.1\"",
428                             apEx.getMessage());
429         }
430
431         try {
432             esImpl.updateModel(config.getEngineKey(), mfpModelString, true);
433         } catch (ApexException apEx) {
434             fail("test should not throw an exception");
435         }
436
437         try {
438             esImpl.updateModel(config.getEngineKey(), simpleModelString, true);
439         } catch (ApexException apEx) {
440             fail("test should not throw an exception");
441         }
442
443         String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
444         assertEquals("{\n  \"TimeStamp\":", runtimeInfo.substring(0, 16));
445
446         assertEquals(AxEngineState.EXECUTING, esImpl.getState());
447
448         String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
449         assertEquals("{\n   \"apexEngineModel\" :", status.substring(0, 24));
450
451         assertTrue(esImpl.isStarted());
452         assertTrue(esImpl.isStarted(esImpl.getEngineKeys().iterator().next()));
453         assertFalse(esImpl.isStopped());
454         assertFalse(esImpl.isStopped(esImpl.getEngineKeys().iterator().next()));
455
456         try {
457             esImpl.start(esImpl.getEngineKeys().iterator().next());
458             fail("test should throw an exception");
459         } catch (ApexException apEx) {
460             assertEquals("apex engine for engine key Engine-0:0.0.1 is already running with state READY",
461                             apEx.getMessage());
462         }
463
464         try {
465             esImpl.startAll();
466             fail("test should throw an exception");
467         } catch (ApexException apEx) {
468             assertEquals("apex engine for engine key Engine-0:0.0.1 is already running with state READY",
469                             apEx.getMessage());
470         }
471
472         try {
473             esImpl.stop(esImpl.getEngineKeys().iterator().next());
474         } catch (ApexException apEx) {
475             fail("test should not throw an exception");
476         }
477
478         try {
479             esImpl.start(esImpl.getEngineKeys().iterator().next());
480         } catch (ApexException apEx) {
481             fail("test should not throw an exception");
482         }
483
484         try {
485             esImpl.stop();
486         } catch (ApexException apEx) {
487             fail("test should not throw an exception");
488         }
489
490         try {
491             esImpl.startAll();
492         } catch (ApexException apEx) {
493             fail("test should not throw an exception");
494         }
495
496         try {
497             esImpl.sendEvent(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget"));
498         } catch (ApexException apEx) {
499             fail("test should not throw an exception");
500         }
501
502         esImpl.startPeriodicEvents(100000);
503         esImpl.stop();
504         esImpl.startAll();
505         esImpl.stopPeriodicEvents();
506
507         esImpl.startPeriodicEvents(100000);
508         try {
509             esImpl.startPeriodicEvents(100000);
510             fail("test should throw an exception");
511         } catch (ApexException apEx) {
512             assertEquals("Peiodic event geneation already running on engine Engine:0.0.1, ApexPeriodicEventGenerator "
513                             + "[period=100000, firstEventTime=0, lastEventTime=0, eventCount=0]", apEx.getMessage());
514         }
515
516         esImpl.stopPeriodicEvents();
517         try {
518             esImpl.stopPeriodicEvents();
519             fail("test should throw an exception");
520         } catch (ApexException apEx) {
521             assertEquals("Peiodic event geneation not running on engine Engine:0.0.1", apEx.getMessage());
522         }
523
524         try {
525             esImpl.clear(esImpl.getEngineKeys().iterator().next());
526         } catch (ApexException apEx) {
527             fail("test should not throw an exception");
528         }
529
530         try {
531             esImpl.clear();
532         } catch (ApexException apEx) {
533             fail("test should not throw an exception");
534         }
535     }
536 }