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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.runtime.impl;
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;
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;
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.getTextFileAsString("src/test/resources/policymodels/SmallModel.json");
75 differentModelString =
76 TextFileUtils.getTextFileAsString("src/test/resources/policymodels/SmallModelDifferent.json");
78 final ApexModelReader<AxPolicyModel> modelReader = new ApexModelReader<>(AxPolicyModel.class);
79 simpleModel = modelReader.read(new ByteArrayInputStream(simpleModelString.getBytes()));
83 * Initialize default parameters.
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);
92 final ContextParameters contextParameters = new ContextParameters();
93 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
94 ParameterService.register(contextParameters);
96 final DistributorParameters distributorParameters = new DistributorParameters();
97 distributorParameters.setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
98 ParameterService.register(distributorParameters);
100 final LockManagerParameters lockManagerParameters = new LockManagerParameters();
101 lockManagerParameters.setName(ContextParameterConstants.LOCKING_GROUP_NAME);
102 ParameterService.register(lockManagerParameters);
104 final PersistorParameters persistorParameters = new PersistorParameters();
105 persistorParameters.setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
106 ParameterService.register(persistorParameters);
108 final EngineParameters engineParameters = new EngineParameters();
109 engineParameters.setName(EngineParameterConstants.MAIN_GROUP_NAME);
110 ExecutorParameters jsExecutorParameters = new ExecutorParameters();
111 jsExecutorParameters.setName("JAVASCRIPT");
113 .setTaskSelectionExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTse");
114 jsExecutorParameters.setTaskExecutorPluginClass("org.onap.policy.apex.service.engine.runtime.impl.DummyTe");
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);
130 * Teardown default parameters.
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();
144 public void testEngineServiceImplSanity() throws ApexException {
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());
152 EngineServiceParameters config = new EngineServiceParameters();
153 config.setInstanceCount(0);
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));
163 config.setEngineKey(new AxArtifactKey("Engine", "0.0.1"));
164 config.setInstanceCount(1);
166 EngineServiceImpl esImpl = EngineServiceImpl.create(config);
167 assertEquals("Engine:0.0.1", esImpl.getKey().getId());
169 esImpl.registerActionListener(null, null);
170 esImpl.registerActionListener("DummyListener", null);
171 esImpl.registerActionListener(null, new DummyApexEventListener());
173 esImpl.registerActionListener("DummyListener", new DummyApexEventListener());
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());
181 esImpl.deregisterActionListener("DummyListener");
183 assertEquals(esImpl, esImpl.getEngineServiceEventInterface());
184 assertEquals(1, esImpl.getEngineKeys().size());
186 assertNull(esImpl.getApexModelKey());
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());
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());
202 String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
203 assertEquals("{\n \"TimeStamp\":", runtimeInfo.substring(0, 16));
205 assertEquals(AxEngineState.STOPPED, esImpl.getState());
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());
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());
221 String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
222 assertTrue(status.contains("\n \"apexEngineModel\" :"));
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()));
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());
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());
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());
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());
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());
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());
278 esImpl.stop(esImpl.getEngineKeys().iterator().next());
279 } catch (ApexException apEx) {
280 fail("test should not throw an exception");
285 } catch (ApexException apEx) {
286 fail("test should not throw an exception");
290 esImpl.sendEvent(null);
291 } catch (Exception apEx) {
292 fail("test should not throw an exception");
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");
301 esImpl.startPeriodicEvents(100000);
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());
311 esImpl.stopPeriodicEvents();
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());
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());
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());
334 esImpl.clear(esImpl.getEngineKeys().iterator().next());
335 } catch (ApexException apEx) {
336 fail("test should not throw an exception");
341 } catch (ApexException apEx) {
342 fail("test should not throw an exception");
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());
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());
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());
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());
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",
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());
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());
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());
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",
412 public void testApexImplModelWIthModel() throws ApexException {
413 EngineServiceParameters config = new EngineServiceParameters();
415 config.setEngineKey(new AxArtifactKey("Engine", "0.0.1"));
416 config.setInstanceCount(1);
418 EngineServiceImpl esImpl = EngineServiceImpl.create(config);
419 assertEquals("Engine:0.0.1", esImpl.getKey().getId());
422 esImpl.updateModel(config.getEngineKey(), simpleModelString, false);
423 } catch (ApexException apEx) {
424 fail("test should not throw an exception");
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\"",
437 esImpl.updateModel(config.getEngineKey(), differentModelString, true);
438 } catch (ApexException apEx) {
439 fail("test should not throw an exception");
443 esImpl.updateModel(config.getEngineKey(), simpleModelString, true);
444 } catch (ApexException apEx) {
445 fail("test should not throw an exception");
448 String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
449 assertEquals("{\n \"TimeStamp\":", runtimeInfo.substring(0, 16));
451 assertEquals(AxEngineState.EXECUTING, esImpl.getState());
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()));
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",
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",
477 esImpl.stop(esImpl.getEngineKeys().iterator().next());
478 } catch (ApexException apEx) {
479 fail("test should not throw an exception");
483 esImpl.start(esImpl.getEngineKeys().iterator().next());
484 } catch (ApexException apEx) {
485 fail("test should not throw an exception");
490 } catch (ApexException apEx) {
491 fail("test should not throw an exception");
496 } catch (ApexException apEx) {
497 fail("test should not throw an exception");
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");
506 esImpl.startPeriodicEvents(100000);
509 esImpl.stopPeriodicEvents();
511 esImpl.startPeriodicEvents(100000);
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());
520 esImpl.stopPeriodicEvents();
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());
529 esImpl.clear(esImpl.getEngineKeys().iterator().next());
530 } catch (ApexException apEx) {
531 fail("test should not throw an exception");
536 } catch (ApexException apEx) {
537 fail("test should not throw an exception");