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 mfpModelString;
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/SamplePolicyModelJAVASCRIPT.json");
76 mfpModelString = TextFileUtils.getTextFileAsString("src/test/resources/policymodels/MyFirstPolicyModel.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");
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);
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());
174 esImpl.deregisterActionListener(null);
175 esImpl.deregisterActionListener("DummyListener");
177 assertEquals(esImpl, esImpl.getEngineServiceEventInterface());
178 assertEquals(1, esImpl.getEngineKeys().size());
180 assertNull(esImpl.getApexModelKey());
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());
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());
196 String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
197 assertEquals("{\n \"TimeStamp\":", runtimeInfo.substring(0, 16));
199 assertEquals(AxEngineState.STOPPED, esImpl.getState());
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());
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());
215 String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
216 assertEquals("{\n \"apexEngineModel\" :", status.substring(0, 24));
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()));
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());
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());
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());
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());
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());
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());
272 esImpl.stop(esImpl.getEngineKeys().iterator().next());
273 } catch (ApexException apEx) {
274 fail("test should not throw an exception");
279 } catch (ApexException apEx) {
280 fail("test should not throw an exception");
284 esImpl.sendEvent(null);
285 } catch (Exception apEx) {
286 fail("test should not throw an exception");
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");
295 esImpl.startPeriodicEvents(100000);
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());
305 esImpl.stopPeriodicEvents();
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());
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());
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());
328 esImpl.clear(esImpl.getEngineKeys().iterator().next());
329 } catch (ApexException apEx) {
330 fail("test should not throw an exception");
335 } catch (ApexException apEx) {
336 fail("test should not throw an exception");
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());
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());
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());
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());
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",
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());
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());
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());
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",
406 public void testApexImplModelWIthModel() throws ApexException {
407 EngineServiceParameters config = new EngineServiceParameters();
409 config.setEngineKey(new AxArtifactKey("Engine", "0.0.1"));
410 config.setInstanceCount(1);
412 EngineServiceImpl esImpl = EngineServiceImpl.create(config);
413 assertEquals("Engine:0.0.1", esImpl.getKey().getId());
416 esImpl.updateModel(config.getEngineKey(), simpleModelString, false);
417 } catch (ApexException apEx) {
418 fail("test should not throw an exception");
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\"",
432 esImpl.updateModel(config.getEngineKey(), mfpModelString, true);
433 } catch (ApexException apEx) {
434 fail("test should not throw an exception");
438 esImpl.updateModel(config.getEngineKey(), simpleModelString, true);
439 } catch (ApexException apEx) {
440 fail("test should not throw an exception");
443 String runtimeInfo = esImpl.getRuntimeInfo(esImpl.getEngineKeys().iterator().next());
444 assertEquals("{\n \"TimeStamp\":", runtimeInfo.substring(0, 16));
446 assertEquals(AxEngineState.EXECUTING, esImpl.getState());
448 String status = esImpl.getStatus(esImpl.getEngineKeys().iterator().next());
449 assertEquals("{\n \"apexEngineModel\" :", status.substring(0, 24));
451 assertTrue(esImpl.isStarted());
452 assertTrue(esImpl.isStarted(esImpl.getEngineKeys().iterator().next()));
453 assertFalse(esImpl.isStopped());
454 assertFalse(esImpl.isStopped(esImpl.getEngineKeys().iterator().next()));
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",
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",
473 esImpl.stop(esImpl.getEngineKeys().iterator().next());
474 } catch (ApexException apEx) {
475 fail("test should not throw an exception");
479 esImpl.start(esImpl.getEngineKeys().iterator().next());
480 } catch (ApexException apEx) {
481 fail("test should not throw an exception");
486 } catch (ApexException apEx) {
487 fail("test should not throw an exception");
492 } catch (ApexException apEx) {
493 fail("test should not throw an exception");
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");
502 esImpl.startPeriodicEvents(100000);
505 esImpl.stopPeriodicEvents();
507 esImpl.startPeriodicEvents(100000);
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());
516 esImpl.stopPeriodicEvents();
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());
525 esImpl.clear(esImpl.getEngineKeys().iterator().next());
526 } catch (ApexException apEx) {
527 fail("test should not throw an exception");
532 } catch (ApexException apEx) {
533 fail("test should not throw an exception");