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.testsuites.performance.benchmark.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.ApexEventListener;
42 import org.onap.policy.apex.service.engine.runtime.EngineService;
43 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
44 import org.onap.policy.apex.service.engine.runtime.impl.EngineServiceImpl;
45 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
46 import org.onap.policy.apex.testsuites.integration.common.model.SampleDomainModelFactory;
47 import org.onap.policy.apex.testsuites.performance.benchmark.engine.utils.Utils;
48 import org.slf4j.ext.XLogger;
49 import org.slf4j.ext.XLoggerFactory;
52 * The Class ApexServiceTest.
54 * @author Liam Fallon (liam.fallon@ericsson.com)
56 public class ApexServiceTest {
57 // Logger for this class
58 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServiceTest.class);
60 private static final long MAX_STOP_WAIT = 5000; // 5 sec
61 private static final long MAX_START_WAIT = 5000; // 5 sec
62 private static final long MAX_RECV_WAIT = 5000; // 5 sec
64 private static final AxArtifactKey engineServiceKey = new AxArtifactKey("Machine-1_process-1_engine-1", "0.0.0");
65 private static final EngineServiceParameters parameters = new EngineServiceParameters();
66 private static EngineService service = null;
67 private static TestListener listener = null;
68 private static AxPolicyModel apexPolicyModel = null;
69 private static int actionEventsReceived = 0;
71 private static String apexModelString;
73 private boolean waitFlag = true;
78 * @throws Exception the exception
81 public static void setUp() throws Exception {
82 // create engine with 3 threads
83 parameters.setInstanceCount(3);
84 parameters.setName(engineServiceKey.getName());
85 parameters.setVersion(engineServiceKey.getVersion());
86 parameters.setId(100);
87 parameters.getEngineParameters().getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
88 service = EngineServiceImpl.create(parameters);
91 LOGGER.debug("Running TestApexEngine. . .");
93 apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
94 assertNotNull(apexPolicyModel);
96 apexModelString = Utils.getModelString(apexPolicyModel);
99 listener = new TestListener();
100 service.registerActionListener("Listener", listener);
104 * Update the engine then test the engine with 2 sample events.
106 * @throws ApexException if there is a problem
109 public void testExecutionSet1() throws ApexException {
110 service.updateModel(parameters.getEngineKey(), apexModelString, true);
113 final long starttime = System.currentTimeMillis();
114 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
115 LOGGER.info("{}", service.getStatus(engineKey));
117 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
118 ThreadUtilities.sleep(200);
120 if (!service.isStarted()) {
121 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
124 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
127 final Date testStartTime = new Date();
128 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
129 eventDataMap.put("TestSlogan", "This is a test slogan");
130 eventDataMap.put("TestMatchCase", (byte) 123);
131 eventDataMap.put("TestTimestamp", testStartTime.getTime());
132 eventDataMap.put("TestTemperature", 34.5445667);
134 final ApexEvent event =
135 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
136 event.setExecutionId(System.nanoTime());
137 event.putAll(eventDataMap);
138 engineServiceEventInterface.sendEvent(event);
140 final ApexEvent event2 =
141 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
142 event2.setExecutionId(System.nanoTime());
143 event2.putAll(eventDataMap);
144 engineServiceEventInterface.sendEvent(event2);
147 final long recvtime = System.currentTimeMillis();
148 while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
149 ThreadUtilities.sleep(100);
151 ThreadUtilities.sleep(500);
152 assertEquals(2, actionEventsReceived);
153 actionEventsReceived = 0;
156 // Stop all engines on this engine service
157 final long stoptime = System.currentTimeMillis();
159 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
160 ThreadUtilities.sleep(200);
162 if (!service.isStopped()) {
163 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
168 * Update the engine then test the engine with 2 sample events.
170 * @throws ApexException if there is a problem
173 public void testExecutionSet1Sync() throws ApexException {
174 service.updateModel(parameters.getEngineKey(), apexModelString, true);
177 final long starttime = System.currentTimeMillis();
178 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
179 LOGGER.info("{}", service.getStatus(engineKey));
181 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
182 ThreadUtilities.sleep(200);
184 if (!service.isStarted()) {
185 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
189 final Date testStartTime = new Date();
190 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
191 eventDataMap.put("TestSlogan", "This is a test slogan");
192 eventDataMap.put("TestMatchCase", (byte) 123);
193 eventDataMap.put("TestTimestamp", testStartTime.getTime());
194 eventDataMap.put("TestTemperature", 34.5445667);
196 final ApexEvent event1 =
197 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
198 event1.putAll(eventDataMap);
199 event1.setExecutionId(System.nanoTime());
201 final ApexEventListener myEventListener1 = new ApexEventListener() {
203 public void onApexEvent(final ApexEvent responseEvent) {
204 assertNotNull("Synchronous sendEventWait failed", responseEvent);
205 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
211 service.registerActionListener("Listener1", myEventListener1);
212 service.getEngineServiceEventInterface().sendEvent(event1);
215 ThreadUtilities.sleep(100);
218 final ApexEvent event2 =
219 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
220 event2.setExecutionId(System.nanoTime());
221 event2.putAll(eventDataMap);
223 final ApexEventListener myEventListener2 = new ApexEventListener() {
225 public void onApexEvent(final ApexEvent responseEvent) {
226 assertNotNull("Synchronous sendEventWait failed", responseEvent);
227 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
228 assertEquals(2, actionEventsReceived);
234 service.deregisterActionListener("Listener1");
235 service.registerActionListener("Listener2", myEventListener2);
236 service.getEngineServiceEventInterface().sendEvent(event2);
239 ThreadUtilities.sleep(100);
241 service.deregisterActionListener("Listener2");
243 actionEventsReceived = 0;
245 // Stop all engines on this engine service
246 final long stoptime = System.currentTimeMillis();
248 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
249 ThreadUtilities.sleep(200);
251 if (!service.isStopped()) {
252 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
257 * Update the engine then test the engine with 2 sample events - again.
259 * @throws ApexException if there is a problem
262 public void testExecutionSet2() throws ApexException {
263 service.updateModel(parameters.getEngineKey(), apexModelString, true);
266 final long starttime = System.currentTimeMillis();
267 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
268 LOGGER.info("{}", service.getStatus(engineKey));
270 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
271 ThreadUtilities.sleep(200);
273 if (!service.isStarted()) {
274 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
277 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
280 final Date testStartTime = new Date();
281 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
282 eventDataMap.put("TestSlogan", "This is a test slogan");
283 eventDataMap.put("TestMatchCase", (byte) 123);
284 eventDataMap.put("TestTimestamp", testStartTime.getTime());
285 eventDataMap.put("TestTemperature", 34.5445667);
287 final ApexEvent event =
288 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
289 event.setExecutionId(System.nanoTime());
290 event.putAll(eventDataMap);
291 engineServiceEventInterface.sendEvent(event);
293 final ApexEvent event2 =
294 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
295 event2.setExecutionId(System.nanoTime());
296 event2.putAll(eventDataMap);
297 engineServiceEventInterface.sendEvent(event2);
300 final long recvtime = System.currentTimeMillis();
301 while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
302 ThreadUtilities.sleep(100);
304 ThreadUtilities.sleep(500);
305 assertEquals(2, actionEventsReceived);
306 actionEventsReceived = 0;
308 // Stop all engines on this engine service
309 final long stoptime = System.currentTimeMillis();
311 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
312 ThreadUtilities.sleep(200);
314 if (!service.isStopped()) {
315 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
320 * Update the engine then test the engine with 2 sample events - again.
322 * @throws ApexException if there is a problem
325 public void testExecutionSet2Sync() throws ApexException {
326 service.updateModel(parameters.getEngineKey(), apexModelString, true);
329 final long starttime = System.currentTimeMillis();
330 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
331 LOGGER.info("{}", service.getStatus(engineKey));
333 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
334 ThreadUtilities.sleep(200);
336 if (!service.isStarted()) {
337 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
341 final Date testStartTime = new Date();
342 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
343 eventDataMap.put("TestSlogan", "This is a test slogan");
344 eventDataMap.put("TestMatchCase", (byte) 123);
345 eventDataMap.put("TestTimestamp", testStartTime.getTime());
346 eventDataMap.put("TestTemperature", 34.5445667);
348 final ApexEvent event1 =
349 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
350 event1.putAll(eventDataMap);
352 final ApexEventListener myEventListener1 = new ApexEventListener() {
354 public void onApexEvent(final ApexEvent responseEvent) {
355 assertNotNull("Synchronous sendEventWait failed", responseEvent);
356 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
362 service.registerActionListener("Listener1", myEventListener1);
363 service.getEngineServiceEventInterface().sendEvent(event1);
366 ThreadUtilities.sleep(100);
369 final ApexEvent event2 =
370 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
371 event2.putAll(eventDataMap);
373 final ApexEventListener myEventListener2 = new ApexEventListener() {
375 public void onApexEvent(final ApexEvent responseEvent) {
376 assertNotNull("Synchronous sendEventWait failed", responseEvent);
377 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
383 service.registerActionListener("Listener2", myEventListener2);
384 service.deregisterActionListener("Listener1");
385 service.getEngineServiceEventInterface().sendEvent(event2);
388 ThreadUtilities.sleep(100);
391 service.deregisterActionListener("Listener2");
393 assertEquals(2, actionEventsReceived);
395 actionEventsReceived = 0;
397 // Stop all engines on this engine service
398 final long stoptime = System.currentTimeMillis();
400 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
401 ThreadUtilities.sleep(200);
403 if (!service.isStopped()) {
404 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
409 * Tear down the the test infrastructure.
411 * @throws ApexException if there is an error
414 public static void tearDown() throws Exception {
415 // Stop all engines on this engine service
416 final long stoptime = System.currentTimeMillis();
418 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
419 ThreadUtilities.sleep(200);
421 if (!service.isStopped()) {
422 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
428 * The listener interface for receiving test events. The class that is interested in processing
429 * a test event implements this interface, and the object created with that class is registered
430 * with a component using the component's <code>addTestListener</code> method. When the test
431 * event occurs, that object's appropriate method is invoked.
435 private static final class TestListener implements ApexEventListener {
441 * org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEvent(org.onap.policy
442 * .apex.service.engine.event.ApexEvent)
445 public synchronized void onApexEvent(final ApexEvent event) {
446 LOGGER.debug("result 1 is:" + event);
448 actionEventsReceived++;
450 final Date testStartTime = new Date((Long) event.get("TestTimestamp"));
451 final Date testEndTime = new Date();
453 LOGGER.info("policy execution time: " + (testEndTime.getTime() - testStartTime.getTime()) + "ms");
459 * @param result the result
461 private void checkResult(final ApexEvent result) {
462 assertTrue(result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104"));
464 assertTrue(result.get("TestSlogan").equals("This is a test slogan"));
465 assertTrue(result.get("TestMatchCase").equals(new Byte((byte) 123)));
466 assertTrue(result.get("TestTemperature").equals(34.5445667));
467 assertTrue(((byte) result.get("TestMatchCaseSelected")) >= 0
468 && ((byte) result.get("TestMatchCaseSelected") <= 3));
469 assertTrue(((byte) result.get("TestEstablishCaseSelected")) >= 0
470 && ((byte) result.get("TestEstablishCaseSelected") <= 3));
471 assertTrue(((byte) result.get("TestDecideCaseSelected")) >= 0
472 && ((byte) result.get("TestDecideCaseSelected") <= 3));
474 ((byte) result.get("TestActCaseSelected")) >= 0 && ((byte) result.get("TestActCaseSelected") <= 3));