2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.uservice.engine;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.util.Date;
33 import java.util.HashMap;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
41 import org.onap.policy.apex.context.parameters.ContextParameters;
42 import org.onap.policy.apex.context.parameters.DistributorParameters;
43 import org.onap.policy.apex.context.parameters.LockManagerParameters;
44 import org.onap.policy.apex.context.parameters.PersistorParameters;
45 import org.onap.policy.apex.context.parameters.SchemaParameters;
46 import org.onap.policy.apex.core.engine.EngineParameterConstants;
47 import org.onap.policy.apex.core.engine.EngineParameters;
48 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
49 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
51 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
52 import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
53 import org.onap.policy.apex.model.basicmodel.service.ModelService;
54 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
55 import org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters;
56 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
57 import org.onap.policy.apex.service.engine.event.ApexEvent;
58 import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
59 import org.onap.policy.apex.service.engine.runtime.EngineService;
60 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
61 import org.onap.policy.apex.service.engine.runtime.impl.EngineServiceImpl;
62 import org.onap.policy.apex.service.parameters.ApexParameterConstants;
63 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
64 import org.onap.policy.apex.testsuites.integration.common.model.SampleDomainModelFactory;
65 import org.onap.policy.common.parameters.ParameterService;
66 import org.slf4j.ext.XLogger;
67 import org.slf4j.ext.XLoggerFactory;
70 * The Class ApexServiceTest.
72 * @author Liam Fallon (liam.fallon@ericsson.com)
74 public class ApexServiceTest {
75 // Logger for this class
76 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServiceTest.class);
78 private static final long MAX_STOP_WAIT = 5000; // 5 sec
79 private static final long MAX_START_WAIT = 5000; // 5 sec
80 private static final long MAX_RECV_WAIT = 5000; // 5 sec
82 private static final AxArtifactKey engineServiceKey = new AxArtifactKey("Machine-1_process-1_engine-1", "0.0.0");
83 private static final EngineServiceParameters parameters = new EngineServiceParameters();
84 private static EngineService service = null;
85 private static TestListener listener = null;
86 private static AxPolicyModel apexPolicyModel = null;
87 private static int actionEventsReceived = 0;
89 private static String apexModelString;
91 private boolean waitFlag = true;
94 public static void beforeSetUp() throws Exception {
95 // create engine with 3 threads
96 parameters.setInstanceCount(3);
97 parameters.setName(engineServiceKey.getName());
98 parameters.setVersion(engineServiceKey.getVersion());
99 parameters.setId(100);
100 parameters.setPolicyModel("policy model impl");
101 parameters.getEngineParameters().getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
102 service = EngineServiceImpl.create(parameters);
104 LOGGER.debug("Running TestApexEngine. . .");
106 apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("JAVASCRIPT");
107 assertNotNull(apexPolicyModel);
109 apexModelString = getModelString(apexPolicyModel);
112 listener = new TestListener();
113 service.registerActionListener("Listener", listener);
117 public static void afterCleardown() throws Exception {
118 ModelService.clear();
125 public void setupParameters() {
126 ParameterService.register(new SchemaParameters());
127 ParameterService.register(new ContextParameters());
128 ParameterService.register(new DistributorParameters());
129 ParameterService.register(new LockManagerParameters());
130 ParameterService.register(new PersistorParameters());
131 ParameterService.register(new EngineServiceParameters());
133 EngineParameters engineParameters = new EngineParameters();
134 engineParameters.getExecutorParameterMap().put("JAVASCRIPT", new JavascriptExecutorParameters());
135 ParameterService.register(engineParameters);
139 * Clear down parameters.
142 public void teardownParameters() {
143 ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
144 ParameterService.deregister(ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME);
145 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
146 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
147 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
148 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
149 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
153 * Update the engine then test the engine with 2 sample events.
155 * @throws ApexException if there is a problem
158 public void testExecutionSet1() throws ApexException {
159 service.updateModel(parameters.getEngineKey(), apexModelString, true);
161 final long starttime = System.currentTimeMillis();
162 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
163 LOGGER.debug("{}", service.getStatus(engineKey));
165 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
166 ThreadUtilities.sleep(200);
168 if (!service.isStarted()) {
169 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
172 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
175 final Date testStartTime = new Date();
176 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
177 eventDataMap.put("TestSlogan", "This is a test slogan");
178 eventDataMap.put("TestMatchCase", (byte) 123);
179 eventDataMap.put("TestTimestamp", testStartTime.getTime());
180 eventDataMap.put("TestTemperature", 34.5445667);
182 final ApexEvent event =
183 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
184 event.setExecutionId(System.nanoTime());
185 event.putAll(eventDataMap);
186 engineServiceEventInterface.sendEvent(event);
188 final ApexEvent event2 =
189 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
190 event2.setExecutionId(System.nanoTime());
191 event2.putAll(eventDataMap);
192 engineServiceEventInterface.sendEvent(event2);
195 final long recvtime = System.currentTimeMillis();
196 while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
197 ThreadUtilities.sleep(100);
199 ThreadUtilities.sleep(500);
200 assertEquals(2, actionEventsReceived);
201 actionEventsReceived = 0;
203 // Stop all engines on this engine service
204 final long stoptime = System.currentTimeMillis();
206 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
207 ThreadUtilities.sleep(200);
209 if (!service.isStopped()) {
210 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
215 * Update the engine then test the engine with 2 sample events.
217 * @throws ApexException if there is a problem
220 public void testExecutionSet1Sync() throws ApexException {
221 service.updateModel(parameters.getEngineKey(), apexModelString, true);
223 final long starttime = System.currentTimeMillis();
224 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
225 LOGGER.debug("{}", service.getStatus(engineKey));
227 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
228 ThreadUtilities.sleep(200);
230 if (!service.isStarted()) {
231 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
235 final Date testStartTime = new Date();
236 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
237 eventDataMap.put("TestSlogan", "This is a test slogan");
238 eventDataMap.put("TestMatchCase", (byte) 123);
239 eventDataMap.put("TestTimestamp", testStartTime.getTime());
240 eventDataMap.put("TestTemperature", 34.5445667);
242 final ApexEvent event1 =
243 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
244 event1.putAll(eventDataMap);
245 event1.setExecutionId(System.nanoTime());
247 final ApexEventListener myEventListener1 = new ApexEventListener() {
249 public void onApexEvent(final ApexEvent responseEvent) {
250 assertNotNull("Synchronous sendEventWait failed", responseEvent);
251 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
257 service.registerActionListener("Listener1", myEventListener1);
258 service.getEngineServiceEventInterface().sendEvent(event1);
261 ThreadUtilities.sleep(100);
264 final ApexEvent event2 =
265 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
266 event2.setExecutionId(System.nanoTime());
267 event2.putAll(eventDataMap);
269 final ApexEventListener myEventListener2 = new ApexEventListener() {
271 public void onApexEvent(final ApexEvent responseEvent) {
272 assertNotNull("Synchronous sendEventWait failed", responseEvent);
273 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
274 assertEquals(2, actionEventsReceived);
280 service.deregisterActionListener("Listener1");
281 service.registerActionListener("Listener2", myEventListener2);
282 service.getEngineServiceEventInterface().sendEvent(event2);
285 ThreadUtilities.sleep(100);
287 service.deregisterActionListener("Listener2");
289 actionEventsReceived = 0;
291 // Stop all engines on this engine service
292 final long stoptime = System.currentTimeMillis();
294 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
295 ThreadUtilities.sleep(200);
297 if (!service.isStopped()) {
298 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
303 * Update the engine then test the engine with 2 sample events - again.
305 * @throws ApexException if there is a problem
308 public void testExecutionSet2() throws ApexException {
309 service.updateModel(parameters.getEngineKey(), apexModelString, true);
311 final long starttime = System.currentTimeMillis();
312 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
313 LOGGER.debug("{}", service.getStatus(engineKey));
315 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
316 ThreadUtilities.sleep(200);
318 if (!service.isStarted()) {
319 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
322 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
325 final Date testStartTime = new Date();
326 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
327 eventDataMap.put("TestSlogan", "This is a test slogan");
328 eventDataMap.put("TestMatchCase", (byte) 123);
329 eventDataMap.put("TestTimestamp", testStartTime.getTime());
330 eventDataMap.put("TestTemperature", 34.5445667);
332 final ApexEvent event =
333 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
334 event.setExecutionId(System.nanoTime());
335 event.putAll(eventDataMap);
336 engineServiceEventInterface.sendEvent(event);
338 final ApexEvent event2 =
339 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
340 event2.setExecutionId(System.nanoTime());
341 event2.putAll(eventDataMap);
342 engineServiceEventInterface.sendEvent(event2);
345 final long recvtime = System.currentTimeMillis();
346 while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
347 ThreadUtilities.sleep(100);
349 ThreadUtilities.sleep(500);
350 assertEquals(2, actionEventsReceived);
351 actionEventsReceived = 0;
353 // Stop all engines on this engine service
354 final long stoptime = System.currentTimeMillis();
356 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
357 ThreadUtilities.sleep(200);
359 if (!service.isStopped()) {
360 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
365 * Update the engine then test the engine with 2 sample events - again.
367 * @throws ApexException if there is a problem
370 public void testExecutionSet2Sync() throws ApexException {
371 service.updateModel(parameters.getEngineKey(), apexModelString, true);
373 final long starttime = System.currentTimeMillis();
374 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
375 LOGGER.debug("{}", service.getStatus(engineKey));
377 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
378 ThreadUtilities.sleep(200);
380 if (!service.isStarted()) {
381 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
385 final Date testStartTime = new Date();
386 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
387 eventDataMap.put("TestSlogan", "This is a test slogan");
388 eventDataMap.put("TestMatchCase", (byte) 123);
389 eventDataMap.put("TestTimestamp", testStartTime.getTime());
390 eventDataMap.put("TestTemperature", 34.5445667);
392 final ApexEvent event1 =
393 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
394 event1.putAll(eventDataMap);
396 final ApexEventListener myEventListener1 = new ApexEventListener() {
398 public void onApexEvent(final ApexEvent responseEvent) {
399 assertNotNull("Synchronous sendEventWait failed", responseEvent);
400 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
406 service.registerActionListener("Listener1", myEventListener1);
407 service.getEngineServiceEventInterface().sendEvent(event1);
410 ThreadUtilities.sleep(100);
413 final ApexEvent event2 =
414 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
415 event2.putAll(eventDataMap);
417 final ApexEventListener myEventListener2 = new ApexEventListener() {
419 public void onApexEvent(final ApexEvent responseEvent) {
420 assertNotNull("Synchronous sendEventWait failed", responseEvent);
421 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
427 service.registerActionListener("Listener2", myEventListener2);
428 service.deregisterActionListener("Listener1");
429 service.getEngineServiceEventInterface().sendEvent(event2);
432 ThreadUtilities.sleep(100);
435 service.deregisterActionListener("Listener2");
437 assertEquals(2, actionEventsReceived);
439 actionEventsReceived = 0;
441 // Stop all engines on this engine service
442 final long stoptime = System.currentTimeMillis();
444 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
445 ThreadUtilities.sleep(200);
447 if (!service.isStopped()) {
448 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
453 * Tear down the the test infrastructure.
455 * @throws ApexException if there is an error
458 public static void tearDown() throws Exception {
459 // Stop all engines on this engine service
460 final long stoptime = System.currentTimeMillis();
462 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
463 ThreadUtilities.sleep(200);
465 if (!service.isStopped()) {
466 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
472 * The listener interface for receiving test events. The class that is interested in processing a test event
473 * implements this interface, and the object created with that class is registered with a component using the
474 * component's <code>addTestListener</code> method. When the test event occurs, that object's appropriate method is
477 private static final class TestListener implements ApexEventListener {
483 public synchronized void onApexEvent(final ApexEvent event) {
484 LOGGER.debug("result 1 is:" + event);
486 actionEventsReceived++;
488 final Date testStartTime = new Date((Long) event.get("TestTimestamp"));
489 final Date testEndTime = new Date();
491 LOGGER.debug("policy execution time: " + (testEndTime.getTime() - testStartTime.getTime()) + "ms");
497 * @param result the result
499 private void checkResult(final ApexEvent result) {
500 assertTrue(result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104"));
502 assertEquals("This is a test slogan", result.get("TestSlogan"));
503 assertEquals((byte) 123, result.get("TestMatchCase"));
504 assertEquals(34.5445667, result.get("TestTemperature"));
505 assertTrue(((byte) result.get("TestMatchCaseSelected")) >= 0
506 && ((byte) result.get("TestMatchCaseSelected") <= 3));
507 assertTrue(((byte) result.get("TestEstablishCaseSelected")) >= 0
508 && ((byte) result.get("TestEstablishCaseSelected") <= 3));
509 assertTrue(((byte) result.get("TestDecideCaseSelected")) >= 0
510 && ((byte) result.get("TestDecideCaseSelected") <= 3));
512 ((byte) result.get("TestActCaseSelected")) >= 0 && ((byte) result.get("TestActCaseSelected") <= 3));
517 * Gets the model string.
519 * @param policyModel the eca policy model
520 * @return the model string
521 * @throws ApexModelException the apex model exception
522 * @throws IOException Signals that an I/O exception has occurred.
524 private static String getModelString(final AxPolicyModel policyModel) throws ApexModelException, IOException {
525 try (final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream()) {
526 new ApexModelWriter<AxPolicyModel>(AxPolicyModel.class).write(policyModel, baOutputStream);
527 return baOutputStream.toString();