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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.runtime.impl;
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;
29 import java.io.ByteArrayInputStream;
30 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.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;
57 * Test the engine service implementation.
59 public class EngineServiceImplTest {
61 private static String simpleModelString;
62 private static String differentModelString;
63 private static AxPolicyModel simpleModel;
66 * Read the models into strings.
68 * @throws IOException on model reading errors
69 * @throws ApexModelException on model reading exceptions
72 public static void readSimpleModel() throws IOException, ApexModelException {
73 simpleModelString = TextFileUtils
74 .getTextFileAsString("src/test/resources/policymodels/SmallModel.json");
76 differentModelString = TextFileUtils
77 .getTextFileAsString("src/test/resources/policymodels/SmallModelDifferent.json");
79 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
80 simpleModel = modelReader.read(new ByteArrayInputStream(simpleModelString.getBytes()));
84 * Initialize default parameters.
87 public static void initializeDefaultParameters() {
88 ParameterService.clear();
89 final SchemaParameters schemaParameters = new SchemaParameters();
90 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
91 ParameterService.register(schemaParameters);
93 final ContextParameters contextParameters = new ContextParameters();
94 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
95 ParameterService.register(contextParameters);
97 final DistributorParameters distributorParameters = new DistributorParameters();
98 distributorParameters.setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
99 ParameterService.register(distributorParameters);
101 final LockManagerParameters lockManagerParameters = new LockManagerParameters();
102 lockManagerParameters.setName(ContextParameterConstants.LOCKING_GROUP_NAME);
103 ParameterService.register(lockManagerParameters);
105 final PersistorParameters persistorParameters = new PersistorParameters();
106 persistorParameters.setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
107 ParameterService.register(persistorParameters);
109 final EngineParameters engineParameters = new EngineParameters();
110 engineParameters.setName(EngineParameterConstants.MAIN_GROUP_NAME);
111 ExecutorParameters jsExecutorParameters = new ExecutorParameters();
112 jsExecutorParameters.setName("JAVASCRIPT");
113 jsExecutorParameters.setTaskSelectionExecutorPluginClass(
114 "org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
115 jsExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
116 jsExecutorParameters.setStateFinalizerExecutorPluginClass(
117 "org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
118 engineParameters.getExecutorParameterMap().put("JAVASCRIPT", jsExecutorParameters);
119 ExecutorParameters mvvelExecutorParameters = new ExecutorParameters();
120 mvvelExecutorParameters.setName("MVEL");
121 mvvelExecutorParameters.setTaskSelectionExecutorPluginClass(
122 "org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
123 mvvelExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
124 mvvelExecutorParameters.setStateFinalizerExecutorPluginClass(
125 "org.onap.policy.apex.service.engine.runtime.impl.DummySfe");
126 engineParameters.getExecutorParameterMap().put("MVEL", jsExecutorParameters);
127 ParameterService.register(engineParameters);
131 * Teardown default parameters.
134 public static void teardownDefaultParameters() {
135 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
136 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
137 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
138 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
139 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
140 ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
141 ModelService.clear();
145 public void testEngineServiceImplSanity() throws ApexException {
147 EngineServiceImpl.create(null);
148 fail("test should throw an exception");
149 } catch (ApexException apEx) {
150 assertEquals("engine service configuration parameters are null", apEx.getMessage());
153 EngineServiceParameters config = new EngineServiceParameters();
154 config.setInstanceCount(0);
157 EngineServiceImpl.create(config);
158 fail("test should throw an exception");
159 } catch (ApexException apEx) {
160 assertEquals("Invalid engine service configuration parameters:", apEx.getMessage().substring(0, 48));
164 config.setEngineKey(new AxArtifactKey("Engine", "0.0.1"));
165 config.setInstanceCount(1);
167 EngineServiceImpl esImpl = EngineServiceImpl.create(config);
168 assertEquals("Engine:0.0.1", esImpl.getKey().getId());
170 esImpl.registerActionListener(null, null);
171 esImpl.registerActionListener("DummyListener", null);
172 esImpl.registerActionListener(null, new DummyApexEventListener());
174 esImpl.registerActionListener("DummyListener", new DummyApexEventListener());
176 esImpl.deregisterActionListener(null);
177 fail("test should throw an exception");
178 } catch (Exception apEx) {
179 assertEquals("removeEventListener()<-Engine-0:0.0.1,STOPPED, listenerName is null", apEx.getMessage());
182 esImpl.deregisterActionListener("DummyListener");
184 assertEquals(esImpl, esImpl.getEngineServiceEventInterface());
185 assertEquals(1, esImpl.getEngineKeys().size());
187 assertNull(esImpl.getApexModelKey());
190 esImpl.getRuntimeInfo(null);
191 fail("test should throw an exception");
192 } catch (ApexException apEx) {
193 assertEquals("engine key must be specified and may not be null", apEx.getMessage());
197 esImpl.getRuntimeInfo(new AxArtifactKey("DummyKey", "0.0.1"));
198 fail("test should throw an exception");
199 } catch (ApexException apEx) {
200 assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
203 String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
204 assertEquals("{\n \"TimeStamp\":", runtimeInfo.substring(0, 16));
206 assertEquals(AxEngineState.STOPPED, esImpl.getState());
209 esImpl.getStatus(null);
210 fail("test should throw an exception");
211 } catch (ApexException apEx) {
212 assertEquals("engine key must be specified and may not be null", apEx.getMessage());
216 esImpl.getStatus(new AxArtifactKey("DummyKey", "0.0.1"));
217 fail("test should throw an exception");
218 } catch (ApexException apEx) {
219 assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
222 String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
223 assertTrue(status.contains("\n \"apexEngineModel\" :"));
225 assertFalse(esImpl.isStarted());
226 assertFalse(esImpl.isStarted(null));
227 assertFalse(esImpl.isStarted(new AxArtifactKey("DummyKey", "0.0.1")));
228 assertFalse(esImpl.isStarted(esImpl.getEngineKeys().iterator().next()));
229 assertTrue(esImpl.isStopped());
230 assertTrue(esImpl.isStopped(null));
231 assertTrue(esImpl.isStopped(new AxArtifactKey("DummyKey", "0.0.1")));
232 assertTrue(esImpl.isStopped(esImpl.getEngineKeys().iterator().next()));
236 fail("test should throw an exception");
237 } catch (ApexException apEx) {
238 assertEquals("engine key must be specified and may not be null", apEx.getMessage());
242 esImpl.start(new AxArtifactKey("DummyKey", "0.0.1"));
243 fail("test should throw an exception");
244 } catch (ApexException apEx) {
245 assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
249 esImpl.start(esImpl.getEngineKeys().iterator().next());
250 fail("test should throw an exception");
251 } catch (ApexException apEx) {
252 assertEquals("start()<-Engine-0:0.0.1,STOPPED, cannot start engine, "
253 + "engine has not been initialized, its model is not loaded", apEx.getMessage());
258 fail("test should throw an exception");
259 } catch (ApexException apEx) {
260 assertEquals("start()<-Engine-0:0.0.1,STOPPED, cannot start engine, "
261 + "engine has not been initialized, its model is not loaded", apEx.getMessage());
266 fail("test should throw an exception");
267 } catch (ApexException apEx) {
268 assertEquals("engine key must be specified and may not be null", apEx.getMessage());
272 esImpl.stop(new AxArtifactKey("DummyKey", "0.0.1"));
273 fail("test should throw an exception");
274 } catch (ApexException apEx) {
275 assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
279 esImpl.stop(esImpl.getEngineKeys().iterator().next());
280 } catch (ApexException apEx) {
281 fail("test should not throw an exception");
286 } catch (ApexException apEx) {
287 fail("test should not throw an exception");
291 esImpl.sendEvent(null);
292 } catch (Exception apEx) {
293 fail("test should not throw an exception");
297 esImpl.sendEvent(new ApexEvent("SomeEvent", "0.0.1", "the.event.namespace", "EventSource", "EventTarget"));
298 } catch (ApexException apEx) {
299 fail("test should not throw an exception");
302 esImpl.startPeriodicEvents(100000);
305 esImpl.startPeriodicEvents(100000);
306 fail("test should throw an exception");
307 } catch (ApexException apEx) {
308 assertEquals("Peiodic event geneation already running on engine Engine:0.0.1, ApexPeriodicEventGenerator "
309 + "[period=100000, firstEventTime=0, lastEventTime=0, eventCount=0]", apEx.getMessage());
312 esImpl.stopPeriodicEvents();
314 esImpl.stopPeriodicEvents();
315 fail("test should throw an exception");
316 } catch (ApexException apEx) {
317 assertEquals("Peiodic event geneation not running on engine Engine:0.0.1", apEx.getMessage());
322 fail("test should throw an exception");
323 } catch (ApexException apEx) {
324 assertEquals("engine key must be specified and may not be null", apEx.getMessage());
328 esImpl.clear(new AxArtifactKey("DummyKey", "0.0.1"));
329 fail("test should throw an exception");
330 } catch (ApexException apEx) {
331 assertEquals("engine with key DummyKey:0.0.1 not found in engine service", apEx.getMessage());
335 esImpl.clear(esImpl.getEngineKeys().iterator().next());
336 } catch (ApexException apEx) {
337 fail("test should not throw an exception");
342 } catch (ApexException apEx) {
343 fail("test should not throw an exception");
347 esImpl.updateModel(null, (String) null, true);
348 fail("test should throw an exception");
349 } catch (ApexException apEx) {
350 assertEquals("engine key must be specified and may not be null", apEx.getMessage());
354 esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (String) null, 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());
361 esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "", true);
362 fail("test should throw an exception");
363 } catch (ApexException apEx) {
364 assertEquals("model for updating engine service with key DummyKey:0.0.1 is empty", apEx.getMessage());
368 esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), "I am not an Apex model", true);
369 fail("test should throw an exception");
370 } catch (ApexException apEx) {
371 assertEquals("failed to unmarshal the apex model on engine service DummyKey:0.0.1", apEx.getMessage());
375 esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModelString, true);
376 fail("test should throw an exception");
377 } catch (ApexException apEx) {
378 assertEquals("engine service key DummyKey:0.0.1 does not match the keyEngine:0.0.1 of this engine service",
383 esImpl.updateModel(null, simpleModelString, 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());
390 esImpl.updateModel(null, (AxPolicyModel) null, true);
391 fail("test should throw an exception");
392 } catch (ApexException apEx) {
393 assertEquals("engine key must be specified and may not be null", apEx.getMessage());
397 esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), (AxPolicyModel) null, true);
398 fail("test should throw an exception");
399 } catch (ApexException apEx) {
400 assertEquals("model for updating on engine service with key DummyKey:0.0.1 is null", apEx.getMessage());
404 esImpl.updateModel(new AxArtifactKey("DummyKey", "0.0.1"), simpleModel, true);
405 fail("test should throw an exception");
406 } catch (ApexException apEx) {
407 assertEquals("engine service key DummyKey:0.0.1 does not match the keyEngine:0.0.1 of this engine service",
413 public void testApexImplModelWIthModel() throws ApexException {
414 EngineServiceParameters config = new EngineServiceParameters();
416 config.setEngineKey(new AxArtifactKey("Engine", "0.0.1"));
417 config.setInstanceCount(1);
419 EngineServiceImpl esImpl = EngineServiceImpl.create(config);
420 assertEquals("Engine:0.0.1", esImpl.getKey().getId());
423 esImpl.updateModel(config.getEngineKey(), simpleModelString, false);
424 } catch (ApexException apEx) {
425 fail("test should not throw an exception");
429 esImpl.updateModel(config.getEngineKey(), differentModelString, false);
430 fail("test should throw an exception");
431 } catch (ApexException apEx) {
432 assertEquals("apex model update failed, supplied model with key \"SmallModelDifferent:0.0.1\" is not a "
433 + "compatible model update "
434 + "from the existing engine model with key \"SmallModel:0.0.1\"",
439 esImpl.updateModel(config.getEngineKey(), differentModelString, true);
440 } catch (ApexException apEx) {
441 fail("test should not throw an exception");
445 esImpl.updateModel(config.getEngineKey(), simpleModelString, true);
446 } catch (ApexException apEx) {
447 fail("test should not throw an exception");
450 String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
451 assertEquals("{\n \"TimeStamp\":", runtimeInfo.substring(0, 16));
453 assertEquals(AxEngineState.EXECUTING, esImpl.getState());
455 String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
456 assertTrue(status.contains("\n \"apexEngineModel\" :"));
457 assertTrue(esImpl.isStarted());
458 assertTrue(esImpl.isStarted(esImpl.getEngineKeys().iterator().next()));
459 assertFalse(esImpl.isStopped());
460 assertFalse(esImpl.isStopped(esImpl.getEngineKeys().iterator().next()));
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",
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",
479 esImpl.stop(esImpl.getEngineKeys().iterator().next());
480 } catch (ApexException apEx) {
481 fail("test should not throw an exception");
485 esImpl.start(esImpl.getEngineKeys().iterator().next());
486 } catch (ApexException apEx) {
487 fail("test should not throw an exception");
492 } catch (ApexException apEx) {
493 fail("test should not throw an exception");
498 } catch (ApexException apEx) {
499 fail("test should not throw an exception");
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");
508 esImpl.startPeriodicEvents(100000);
511 esImpl.stopPeriodicEvents();
513 esImpl.startPeriodicEvents(100000);
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());
522 esImpl.stopPeriodicEvents();
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());
531 esImpl.clear(esImpl.getEngineKeys().iterator().next());
532 } catch (ApexException apEx) {
533 fail("test should not throw an exception");
538 } catch (ApexException apEx) {
539 fail("test should not throw an exception");