2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-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;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.util.Date;
29 import java.util.HashMap;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
39 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
40 import org.onap.policy.apex.service.engine.event.ApexEvent;
41 import org.onap.policy.apex.service.engine.runtime.impl.EngineServiceImpl;
42 import org.onap.policy.apex.service.engine.utils.Utils;
43 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
44 import org.onap.policy.apex.test.common.model.SampleDomainModelFactory;
45 import org.slf4j.ext.XLogger;
46 import org.slf4j.ext.XLoggerFactory;
49 * The Class ApexServiceTest.
51 * @author Liam Fallon (liam.fallon@ericsson.com)
53 public class ApexServiceTest {
54 // Logger for this class
55 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServiceTest.class);
57 private static final long MAX_STOP_WAIT = 5000; // 5 sec
58 private static final long MAX_START_WAIT = 5000; // 5 sec
59 private static final long MAX_RECV_WAIT = 5000; // 5 sec
61 private static final AxArtifactKey engineServiceKey = new AxArtifactKey("Machine-1_process-1_engine-1", "0.0.0");
62 private static final EngineServiceParameters parameters = new EngineServiceParameters();
63 private static EngineService service = null;
64 private static TestListener listener = null;
65 private static AxPolicyModel apexPolicyModel = null;
66 private static int actionEventsReceived = 0;
68 private static String apexModelString;
70 private boolean waitFlag = true;
75 * @throws Exception the exception
78 public static void setUp() throws Exception {
79 // create engine with 3 threads
80 parameters.setInstanceCount(3);
81 parameters.setName(engineServiceKey.getName());
82 parameters.setVersion(engineServiceKey.getVersion());
83 parameters.setId(100);
84 parameters.getEngineParameters().getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
85 service = EngineServiceImpl.create(parameters);
88 LOGGER.debug("Running TestApexEngine. . .");
90 apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
91 assertNotNull(apexPolicyModel);
93 apexModelString = Utils.getModelString(apexPolicyModel);
96 listener = new TestListener();
97 service.registerActionListener("Listener", listener);
101 * Update the engine then test the engine with 2 sample events.
103 * @throws ApexException if there is a problem
106 public void testExecutionSet1() throws ApexException {
107 service.updateModel(parameters.getEngineKey(), apexModelString, true);
110 final long starttime = System.currentTimeMillis();
111 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
112 LOGGER.info("{}", service.getStatus(engineKey));
114 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
115 ThreadUtilities.sleep(200);
117 if (!service.isStarted()) {
118 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
121 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
124 final Date testStartTime = new Date();
125 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
126 eventDataMap.put("TestSlogan", "This is a test slogan");
127 eventDataMap.put("TestMatchCase", (byte) 123);
128 eventDataMap.put("TestTimestamp", testStartTime.getTime());
129 eventDataMap.put("TestTemperature", 34.5445667);
131 final ApexEvent event =
132 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
133 event.setExecutionId(System.nanoTime());
134 event.putAll(eventDataMap);
135 engineServiceEventInterface.sendEvent(event);
137 final ApexEvent event2 =
138 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
139 event2.setExecutionId(System.nanoTime());
140 event2.putAll(eventDataMap);
141 engineServiceEventInterface.sendEvent(event2);
144 final long recvtime = System.currentTimeMillis();
145 while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
146 ThreadUtilities.sleep(100);
148 ThreadUtilities.sleep(500);
149 assertEquals(2, actionEventsReceived);
150 actionEventsReceived = 0;
153 // Stop all engines on this engine service
154 final long stoptime = System.currentTimeMillis();
156 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
157 ThreadUtilities.sleep(200);
159 if (!service.isStopped()) {
160 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
165 * Update the engine then test the engine with 2 sample events.
167 * @throws ApexException if there is a problem
170 public void testExecutionSet1Sync() throws ApexException {
171 service.updateModel(parameters.getEngineKey(), apexModelString, true);
174 final long starttime = System.currentTimeMillis();
175 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
176 LOGGER.info("{}", service.getStatus(engineKey));
178 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
179 ThreadUtilities.sleep(200);
181 if (!service.isStarted()) {
182 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
186 final Date testStartTime = new Date();
187 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
188 eventDataMap.put("TestSlogan", "This is a test slogan");
189 eventDataMap.put("TestMatchCase", (byte) 123);
190 eventDataMap.put("TestTimestamp", testStartTime.getTime());
191 eventDataMap.put("TestTemperature", 34.5445667);
193 final ApexEvent event1 =
194 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
195 event1.putAll(eventDataMap);
196 event1.setExecutionId(System.nanoTime());
198 final ApexEventListener myEventListener1 = new ApexEventListener() {
200 public void onApexEvent(final ApexEvent responseEvent) {
201 assertNotNull("Synchronous sendEventWait failed", responseEvent);
202 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
208 service.registerActionListener("Listener1", myEventListener1);
209 service.getEngineServiceEventInterface().sendEvent(event1);
212 ThreadUtilities.sleep(100);
215 final ApexEvent event2 =
216 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
217 event2.setExecutionId(System.nanoTime());
218 event2.putAll(eventDataMap);
220 final ApexEventListener myEventListener2 = new ApexEventListener() {
222 public void onApexEvent(final ApexEvent responseEvent) {
223 assertNotNull("Synchronous sendEventWait failed", responseEvent);
224 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
225 assertEquals(2, actionEventsReceived);
231 service.deregisterActionListener("Listener1");
232 service.registerActionListener("Listener2", myEventListener2);
233 service.getEngineServiceEventInterface().sendEvent(event2);
236 ThreadUtilities.sleep(100);
238 service.deregisterActionListener("Listener2");
240 actionEventsReceived = 0;
242 // Stop all engines on this engine service
243 final long stoptime = System.currentTimeMillis();
245 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
246 ThreadUtilities.sleep(200);
248 if (!service.isStopped()) {
249 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
254 * Update the engine then test the engine with 2 sample events - again.
256 * @throws ApexException if there is a problem
259 public void testExecutionSet2() throws ApexException {
260 service.updateModel(parameters.getEngineKey(), apexModelString, true);
263 final long starttime = System.currentTimeMillis();
264 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
265 LOGGER.info("{}", service.getStatus(engineKey));
267 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
268 ThreadUtilities.sleep(200);
270 if (!service.isStarted()) {
271 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
274 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
277 final Date testStartTime = new Date();
278 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
279 eventDataMap.put("TestSlogan", "This is a test slogan");
280 eventDataMap.put("TestMatchCase", (byte) 123);
281 eventDataMap.put("TestTimestamp", testStartTime.getTime());
282 eventDataMap.put("TestTemperature", 34.5445667);
284 final ApexEvent event =
285 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
286 event.setExecutionId(System.nanoTime());
287 event.putAll(eventDataMap);
288 engineServiceEventInterface.sendEvent(event);
290 final ApexEvent event2 =
291 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
292 event2.setExecutionId(System.nanoTime());
293 event2.putAll(eventDataMap);
294 engineServiceEventInterface.sendEvent(event2);
297 final long recvtime = System.currentTimeMillis();
298 while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
299 ThreadUtilities.sleep(100);
301 ThreadUtilities.sleep(500);
302 assertEquals(2, actionEventsReceived);
303 actionEventsReceived = 0;
305 // Stop all engines on this engine service
306 final long stoptime = System.currentTimeMillis();
308 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
309 ThreadUtilities.sleep(200);
311 if (!service.isStopped()) {
312 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
317 * Update the engine then test the engine with 2 sample events - again.
319 * @throws ApexException if there is a problem
322 public void testExecutionSet2Sync() throws ApexException {
323 service.updateModel(parameters.getEngineKey(), apexModelString, true);
326 final long starttime = System.currentTimeMillis();
327 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
328 LOGGER.info("{}", service.getStatus(engineKey));
330 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
331 ThreadUtilities.sleep(200);
333 if (!service.isStarted()) {
334 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
338 final Date testStartTime = new Date();
339 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
340 eventDataMap.put("TestSlogan", "This is a test slogan");
341 eventDataMap.put("TestMatchCase", (byte) 123);
342 eventDataMap.put("TestTimestamp", testStartTime.getTime());
343 eventDataMap.put("TestTemperature", 34.5445667);
345 final ApexEvent event1 =
346 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
347 event1.putAll(eventDataMap);
349 final ApexEventListener myEventListener1 = new ApexEventListener() {
351 public void onApexEvent(final ApexEvent responseEvent) {
352 assertNotNull("Synchronous sendEventWait failed", responseEvent);
353 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
359 service.registerActionListener("Listener1", myEventListener1);
360 service.getEngineServiceEventInterface().sendEvent(event1);
363 ThreadUtilities.sleep(100);
366 final ApexEvent event2 =
367 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
368 event2.putAll(eventDataMap);
370 final ApexEventListener myEventListener2 = new ApexEventListener() {
372 public void onApexEvent(final ApexEvent responseEvent) {
373 assertNotNull("Synchronous sendEventWait failed", responseEvent);
374 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
380 service.registerActionListener("Listener2", myEventListener2);
381 service.deregisterActionListener("Listener1");
382 service.getEngineServiceEventInterface().sendEvent(event2);
385 ThreadUtilities.sleep(100);
388 service.deregisterActionListener("Listener2");
390 assertEquals(2, actionEventsReceived);
392 actionEventsReceived = 0;
394 // Stop all engines on this engine service
395 final long stoptime = System.currentTimeMillis();
397 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
398 ThreadUtilities.sleep(200);
400 if (!service.isStopped()) {
401 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
406 * Tear down the the test infrastructure.
408 * @throws ApexException if there is an error
411 public static void tearDown() throws Exception {
412 // Stop all engines on this engine service
413 final long stoptime = System.currentTimeMillis();
415 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
416 ThreadUtilities.sleep(200);
418 if (!service.isStopped()) {
419 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
425 * The listener interface for receiving test events. The class that is interested in processing
426 * a test event implements this interface, and the object created with that class is registered
427 * with a component using the component's <code>addTestListener</code> method. When the test
428 * event occurs, that object's appropriate method is invoked.
432 private static final class TestListener implements ApexEventListener {
438 * org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEvent(org.onap.policy
439 * .apex.service.engine.event.ApexEvent)
442 public synchronized void onApexEvent(final ApexEvent event) {
443 LOGGER.debug("result 1 is:" + event);
445 actionEventsReceived++;
447 final Date testStartTime = new Date((Long) event.get("TestTimestamp"));
448 final Date testEndTime = new Date();
450 LOGGER.info("policy execution time: " + (testEndTime.getTime() - testStartTime.getTime()) + "ms");
456 * @param result the result
458 private void checkResult(final ApexEvent result) {
459 assertTrue(result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104"));
461 assertTrue(result.get("TestSlogan").equals("This is a test slogan"));
462 assertTrue(result.get("TestMatchCase").equals(new Byte((byte) 123)));
463 assertTrue(result.get("TestTemperature").equals(34.5445667));
464 assertTrue(((byte) result.get("TestMatchCaseSelected")) >= 0
465 && ((byte) result.get("TestMatchCaseSelected") <= 3));
466 assertTrue(((byte) result.get("TestEstablishCaseSelected")) >= 0
467 && ((byte) result.get("TestEstablishCaseSelected") <= 3));
468 assertTrue(((byte) result.get("TestDecideCaseSelected")) >= 0
469 && ((byte) result.get("TestDecideCaseSelected") <= 3));
471 ((byte) result.get("TestActCaseSelected")) >= 0 && ((byte) result.get("TestActCaseSelected") <= 3));