2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-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.testsuites.integration.uservice.engine;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.util.Date;
32 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.getEngineParameters().getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
101 service = EngineServiceImpl.create(parameters);
103 LOGGER.debug("Running TestApexEngine. . .");
105 apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("JAVASCRIPT");
106 assertNotNull(apexPolicyModel);
108 apexModelString = getModelString(apexPolicyModel);
111 listener = new TestListener();
112 service.registerActionListener("Listener", listener);
116 public static void afterCleardown() throws Exception {
117 ModelService.clear();
124 public void setupParameters() {
125 ParameterService.register(new SchemaParameters());
126 ParameterService.register(new ContextParameters());
127 ParameterService.register(new DistributorParameters());
128 ParameterService.register(new LockManagerParameters());
129 ParameterService.register(new PersistorParameters());
130 ParameterService.register(new EngineServiceParameters());
132 EngineParameters engineParameters = new EngineParameters();
133 engineParameters.getExecutorParameterMap().put("JAVASCRIPT", new JavascriptExecutorParameters());
134 ParameterService.register(engineParameters);
138 * Clear down parameters.
141 public void teardownParameters() {
142 ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
143 ParameterService.deregister(ApexParameterConstants.ENGINE_SERVICE_GROUP_NAME);
144 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
145 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
146 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
147 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
148 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
152 * Update the engine then test the engine with 2 sample events.
154 * @throws ApexException if there is a problem
157 public void testExecutionSet1() throws ApexException {
158 service.updateModel(parameters.getEngineKey(), apexModelString, true);
160 final long starttime = System.currentTimeMillis();
161 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
162 LOGGER.debug("{}", service.getStatus(engineKey));
164 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
165 ThreadUtilities.sleep(200);
167 if (!service.isStarted()) {
168 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
171 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
174 final Date testStartTime = new Date();
175 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
176 eventDataMap.put("TestSlogan", "This is a test slogan");
177 eventDataMap.put("TestMatchCase", (byte) 123);
178 eventDataMap.put("TestTimestamp", testStartTime.getTime());
179 eventDataMap.put("TestTemperature", 34.5445667);
181 final ApexEvent event =
182 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
183 event.setExecutionId(System.nanoTime());
184 event.putAll(eventDataMap);
185 engineServiceEventInterface.sendEvent(event);
187 final ApexEvent event2 =
188 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
189 event2.setExecutionId(System.nanoTime());
190 event2.putAll(eventDataMap);
191 engineServiceEventInterface.sendEvent(event2);
194 final long recvtime = System.currentTimeMillis();
195 while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
196 ThreadUtilities.sleep(100);
198 ThreadUtilities.sleep(500);
199 assertEquals(2, actionEventsReceived);
200 actionEventsReceived = 0;
202 // Stop all engines on this engine service
203 final long stoptime = System.currentTimeMillis();
205 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
206 ThreadUtilities.sleep(200);
208 if (!service.isStopped()) {
209 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
214 * Update the engine then test the engine with 2 sample events.
216 * @throws ApexException if there is a problem
219 public void testExecutionSet1Sync() throws ApexException {
220 service.updateModel(parameters.getEngineKey(), apexModelString, true);
222 final long starttime = System.currentTimeMillis();
223 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
224 LOGGER.debug("{}", service.getStatus(engineKey));
226 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
227 ThreadUtilities.sleep(200);
229 if (!service.isStarted()) {
230 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
234 final Date testStartTime = new Date();
235 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
236 eventDataMap.put("TestSlogan", "This is a test slogan");
237 eventDataMap.put("TestMatchCase", (byte) 123);
238 eventDataMap.put("TestTimestamp", testStartTime.getTime());
239 eventDataMap.put("TestTemperature", 34.5445667);
241 final ApexEvent event1 =
242 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
243 event1.putAll(eventDataMap);
244 event1.setExecutionId(System.nanoTime());
246 final ApexEventListener myEventListener1 = new ApexEventListener() {
248 public void onApexEvent(final ApexEvent responseEvent) {
249 assertNotNull("Synchronous sendEventWait failed", responseEvent);
250 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
256 service.registerActionListener("Listener1", myEventListener1);
257 service.getEngineServiceEventInterface().sendEvent(event1);
260 ThreadUtilities.sleep(100);
263 final ApexEvent event2 =
264 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
265 event2.setExecutionId(System.nanoTime());
266 event2.putAll(eventDataMap);
268 final ApexEventListener myEventListener2 = new ApexEventListener() {
270 public void onApexEvent(final ApexEvent responseEvent) {
271 assertNotNull("Synchronous sendEventWait failed", responseEvent);
272 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
273 assertEquals(2, actionEventsReceived);
279 service.deregisterActionListener("Listener1");
280 service.registerActionListener("Listener2", myEventListener2);
281 service.getEngineServiceEventInterface().sendEvent(event2);
284 ThreadUtilities.sleep(100);
286 service.deregisterActionListener("Listener2");
288 actionEventsReceived = 0;
290 // Stop all engines on this engine service
291 final long stoptime = System.currentTimeMillis();
293 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
294 ThreadUtilities.sleep(200);
296 if (!service.isStopped()) {
297 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
302 * Update the engine then test the engine with 2 sample events - again.
304 * @throws ApexException if there is a problem
307 public void testExecutionSet2() throws ApexException {
308 service.updateModel(parameters.getEngineKey(), apexModelString, true);
310 final long starttime = System.currentTimeMillis();
311 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
312 LOGGER.debug("{}", service.getStatus(engineKey));
314 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
315 ThreadUtilities.sleep(200);
317 if (!service.isStarted()) {
318 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
321 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
324 final Date testStartTime = new Date();
325 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
326 eventDataMap.put("TestSlogan", "This is a test slogan");
327 eventDataMap.put("TestMatchCase", (byte) 123);
328 eventDataMap.put("TestTimestamp", testStartTime.getTime());
329 eventDataMap.put("TestTemperature", 34.5445667);
331 final ApexEvent event =
332 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
333 event.setExecutionId(System.nanoTime());
334 event.putAll(eventDataMap);
335 engineServiceEventInterface.sendEvent(event);
337 final ApexEvent event2 =
338 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
339 event2.setExecutionId(System.nanoTime());
340 event2.putAll(eventDataMap);
341 engineServiceEventInterface.sendEvent(event2);
344 final long recvtime = System.currentTimeMillis();
345 while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
346 ThreadUtilities.sleep(100);
348 ThreadUtilities.sleep(500);
349 assertEquals(2, actionEventsReceived);
350 actionEventsReceived = 0;
352 // Stop all engines on this engine service
353 final long stoptime = System.currentTimeMillis();
355 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
356 ThreadUtilities.sleep(200);
358 if (!service.isStopped()) {
359 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
364 * Update the engine then test the engine with 2 sample events - again.
366 * @throws ApexException if there is a problem
369 public void testExecutionSet2Sync() throws ApexException {
370 service.updateModel(parameters.getEngineKey(), apexModelString, true);
372 final long starttime = System.currentTimeMillis();
373 for (final AxArtifactKey engineKey : service.getEngineKeys()) {
374 LOGGER.debug("{}", service.getStatus(engineKey));
376 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
377 ThreadUtilities.sleep(200);
379 if (!service.isStarted()) {
380 fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
384 final Date testStartTime = new Date();
385 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
386 eventDataMap.put("TestSlogan", "This is a test slogan");
387 eventDataMap.put("TestMatchCase", (byte) 123);
388 eventDataMap.put("TestTimestamp", testStartTime.getTime());
389 eventDataMap.put("TestTemperature", 34.5445667);
391 final ApexEvent event1 =
392 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
393 event1.putAll(eventDataMap);
395 final ApexEventListener myEventListener1 = new ApexEventListener() {
397 public void onApexEvent(final ApexEvent responseEvent) {
398 assertNotNull("Synchronous sendEventWait failed", responseEvent);
399 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
405 service.registerActionListener("Listener1", myEventListener1);
406 service.getEngineServiceEventInterface().sendEvent(event1);
409 ThreadUtilities.sleep(100);
412 final ApexEvent event2 =
413 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
414 event2.putAll(eventDataMap);
416 final ApexEventListener myEventListener2 = new ApexEventListener() {
418 public void onApexEvent(final ApexEvent responseEvent) {
419 assertNotNull("Synchronous sendEventWait failed", responseEvent);
420 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
426 service.registerActionListener("Listener2", myEventListener2);
427 service.deregisterActionListener("Listener1");
428 service.getEngineServiceEventInterface().sendEvent(event2);
431 ThreadUtilities.sleep(100);
434 service.deregisterActionListener("Listener2");
436 assertEquals(2, actionEventsReceived);
438 actionEventsReceived = 0;
440 // Stop all engines on this engine service
441 final long stoptime = System.currentTimeMillis();
443 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
444 ThreadUtilities.sleep(200);
446 if (!service.isStopped()) {
447 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
452 * Tear down the the test infrastructure.
454 * @throws ApexException if there is an error
457 public static void tearDown() throws Exception {
458 // Stop all engines on this engine service
459 final long stoptime = System.currentTimeMillis();
461 while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
462 ThreadUtilities.sleep(200);
464 if (!service.isStopped()) {
465 fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
471 * The listener interface for receiving test events. The class that is interested in processing a test event
472 * implements this interface, and the object created with that class is registered with a component using the
473 * component's <code>addTestListener</code> method. When the test event occurs, that object's appropriate method is
478 private static final class TestListener implements ApexEventListener {
484 public synchronized void onApexEvent(final ApexEvent event) {
485 LOGGER.debug("result 1 is:" + event);
487 actionEventsReceived++;
489 final Date testStartTime = new Date((Long) event.get("TestTimestamp"));
490 final Date testEndTime = new Date();
492 LOGGER.debug("policy execution time: " + (testEndTime.getTime() - testStartTime.getTime()) + "ms");
498 * @param result the result
500 private void checkResult(final ApexEvent result) {
501 assertTrue(result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104"));
503 assertTrue(result.get("TestSlogan").equals("This is a test slogan"));
504 assertTrue(result.get("TestMatchCase").equals((byte) 123));
505 assertTrue(result.get("TestTemperature").equals(34.5445667));
506 assertTrue(((byte) result.get("TestMatchCaseSelected")) >= 0
507 && ((byte) result.get("TestMatchCaseSelected") <= 3));
508 assertTrue(((byte) result.get("TestEstablishCaseSelected")) >= 0
509 && ((byte) result.get("TestEstablishCaseSelected") <= 3));
510 assertTrue(((byte) result.get("TestDecideCaseSelected")) >= 0
511 && ((byte) result.get("TestDecideCaseSelected") <= 3));
513 ((byte) result.get("TestActCaseSelected")) >= 0 && ((byte) result.get("TestActCaseSelected") <= 3));
518 * Gets the model string.
520 * @param policyModel the eca policy model
521 * @return the model string
522 * @throws ApexModelException the apex model exception
523 * @throws IOException Signals that an I/O exception has occurred.
525 private static String getModelString(final AxPolicyModel policyModel) throws ApexModelException, IOException {
526 try (final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream()) {
527 new ApexModelWriter<AxPolicyModel>(AxPolicyModel.class).write(policyModel, baOutputStream);
528 return baOutputStream.toString();