e66d1b7918b76de6de2faf128ddb65cc69b7a06e
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.performance.benchmark.engine.runtime;
22
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;
27
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.Map;
31
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;
50
51 /**
52  * The Class ApexServiceTest.
53  *
54  * @author Liam Fallon (liam.fallon@ericsson.com)
55  */
56 public class ApexServiceTest {
57     // Logger for this class
58     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServiceTest.class);
59
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
63
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;
70
71     private static String apexModelString;
72
73     private boolean waitFlag = true;
74
75     /**
76      * Sets the up.
77      *
78      * @throws Exception the exception
79      */
80     @BeforeClass
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);
89
90
91         LOGGER.debug("Running TestApexEngine. . .");
92
93         apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
94         assertNotNull(apexPolicyModel);
95
96         apexModelString = Utils.getModelString(apexPolicyModel);
97
98         // create engine
99         listener = new TestListener();
100         service.registerActionListener("Listener", listener);
101     }
102
103     /**
104      * Update the engine then test the engine with 2 sample events.
105      *
106      * @throws ApexException if there is a problem
107      */
108     @Test
109     public void testExecutionSet1() throws ApexException {
110         service.updateModel(parameters.getEngineKey(), apexModelString, true);
111         // Start the service
112         service.startAll();
113         final long starttime = System.currentTimeMillis();
114         for (final AxArtifactKey engineKey : service.getEngineKeys()) {
115             LOGGER.info("{}", service.getStatus(engineKey));
116         }
117         while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
118             ThreadUtilities.sleep(200);
119         }
120         if (!service.isStarted()) {
121             fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
122         }
123
124         final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
125
126         // Send some events
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);
133
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);
139
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);
145
146         // Wait for results
147         final long recvtime = System.currentTimeMillis();
148         while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
149             ThreadUtilities.sleep(100);
150         }
151         ThreadUtilities.sleep(500);
152         assertEquals(2, actionEventsReceived);
153         actionEventsReceived = 0;
154
155
156         // Stop all engines on this engine service
157         final long stoptime = System.currentTimeMillis();
158         service.stop();
159         while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
160             ThreadUtilities.sleep(200);
161         }
162         if (!service.isStopped()) {
163             fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
164         }
165     }
166
167     /**
168      * Update the engine then test the engine with 2 sample events.
169      *
170      * @throws ApexException if there is a problem
171      */
172     @Test
173     public void testExecutionSet1Sync() throws ApexException {
174         service.updateModel(parameters.getEngineKey(), apexModelString, true);
175         // Start the service
176         service.startAll();
177         final long starttime = System.currentTimeMillis();
178         for (final AxArtifactKey engineKey : service.getEngineKeys()) {
179             LOGGER.info("{}", service.getStatus(engineKey));
180         }
181         while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
182             ThreadUtilities.sleep(200);
183         }
184         if (!service.isStarted()) {
185             fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
186         }
187
188         // Send some events
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);
195
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());
200
201         final ApexEventListener myEventListener1 = new ApexEventListener() {
202             @Override
203             public void onApexEvent(final ApexEvent responseEvent) {
204                 assertNotNull("Synchronous sendEventWait failed", responseEvent);
205                 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
206                 waitFlag = false;
207             }
208         };
209
210         waitFlag = true;
211         service.registerActionListener("Listener1", myEventListener1);
212         service.getEngineServiceEventInterface().sendEvent(event1);
213
214         while (waitFlag) {
215             ThreadUtilities.sleep(100);
216         }
217
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);
222
223         final ApexEventListener myEventListener2 = new ApexEventListener() {
224             @Override
225             public void onApexEvent(final ApexEvent responseEvent) {
226                 assertNotNull("Synchronous sendEventWait failed", responseEvent);
227                 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
228                 assertEquals(2, actionEventsReceived);
229                 waitFlag = false;
230             }
231         };
232
233         waitFlag = true;
234         service.deregisterActionListener("Listener1");
235         service.registerActionListener("Listener2", myEventListener2);
236         service.getEngineServiceEventInterface().sendEvent(event2);
237
238         while (waitFlag) {
239             ThreadUtilities.sleep(100);
240         }
241         service.deregisterActionListener("Listener2");
242
243         actionEventsReceived = 0;
244
245         // Stop all engines on this engine service
246         final long stoptime = System.currentTimeMillis();
247         service.stop();
248         while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
249             ThreadUtilities.sleep(200);
250         }
251         if (!service.isStopped()) {
252             fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
253         }
254     }
255
256     /**
257      * Update the engine then test the engine with 2 sample events - again.
258      *
259      * @throws ApexException if there is a problem
260      */
261     @Test
262     public void testExecutionSet2() throws ApexException {
263         service.updateModel(parameters.getEngineKey(), apexModelString, true);
264         // Start the service
265         service.startAll();
266         final long starttime = System.currentTimeMillis();
267         for (final AxArtifactKey engineKey : service.getEngineKeys()) {
268             LOGGER.info("{}", service.getStatus(engineKey));
269         }
270         while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
271             ThreadUtilities.sleep(200);
272         }
273         if (!service.isStarted()) {
274             fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
275         }
276
277         final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
278
279         // Send some events
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);
286
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);
292
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);
298
299         // Wait for results
300         final long recvtime = System.currentTimeMillis();
301         while (actionEventsReceived < 2 && System.currentTimeMillis() - recvtime < MAX_RECV_WAIT) {
302             ThreadUtilities.sleep(100);
303         }
304         ThreadUtilities.sleep(500);
305         assertEquals(2, actionEventsReceived);
306         actionEventsReceived = 0;
307
308         // Stop all engines on this engine service
309         final long stoptime = System.currentTimeMillis();
310         service.stop();
311         while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
312             ThreadUtilities.sleep(200);
313         }
314         if (!service.isStopped()) {
315             fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
316         }
317     }
318
319     /**
320      * Update the engine then test the engine with 2 sample events - again.
321      *
322      * @throws ApexException if there is a problem
323      */
324     @Test
325     public void testExecutionSet2Sync() throws ApexException {
326         service.updateModel(parameters.getEngineKey(), apexModelString, true);
327         // Start the service
328         service.startAll();
329         final long starttime = System.currentTimeMillis();
330         for (final AxArtifactKey engineKey : service.getEngineKeys()) {
331             LOGGER.info("{}", service.getStatus(engineKey));
332         }
333         while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
334             ThreadUtilities.sleep(200);
335         }
336         if (!service.isStarted()) {
337             fail("Apex Service " + service.getKey() + " failed to start after " + MAX_START_WAIT + " ms");
338         }
339
340         // Send some events
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);
347
348         final ApexEvent event1 =
349                 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
350         event1.putAll(eventDataMap);
351
352         final ApexEventListener myEventListener1 = new ApexEventListener() {
353             @Override
354             public void onApexEvent(final ApexEvent responseEvent) {
355                 assertNotNull("Synchronous sendEventWait failed", responseEvent);
356                 assertEquals(event1.getExecutionId(), responseEvent.getExecutionId());
357                 waitFlag = false;
358             }
359         };
360
361         waitFlag = true;
362         service.registerActionListener("Listener1", myEventListener1);
363         service.getEngineServiceEventInterface().sendEvent(event1);
364
365         while (waitFlag) {
366             ThreadUtilities.sleep(100);
367         }
368
369         final ApexEvent event2 =
370                 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
371         event2.putAll(eventDataMap);
372
373         final ApexEventListener myEventListener2 = new ApexEventListener() {
374             @Override
375             public void onApexEvent(final ApexEvent responseEvent) {
376                 assertNotNull("Synchronous sendEventWait failed", responseEvent);
377                 assertEquals(event2.getExecutionId(), responseEvent.getExecutionId());
378                 waitFlag = false;
379             }
380         };
381
382         waitFlag = true;
383         service.registerActionListener("Listener2", myEventListener2);
384         service.deregisterActionListener("Listener1");
385         service.getEngineServiceEventInterface().sendEvent(event2);
386
387         while (waitFlag) {
388             ThreadUtilities.sleep(100);
389         }
390
391         service.deregisterActionListener("Listener2");
392
393         assertEquals(2, actionEventsReceived);
394
395         actionEventsReceived = 0;
396
397         // Stop all engines on this engine service
398         final long stoptime = System.currentTimeMillis();
399         service.stop();
400         while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
401             ThreadUtilities.sleep(200);
402         }
403         if (!service.isStopped()) {
404             fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
405         }
406     }
407
408     /**
409      * Tear down the the test infrastructure.
410      *
411      * @throws ApexException if there is an error
412      */
413     @AfterClass
414     public static void tearDown() throws Exception {
415         // Stop all engines on this engine service
416         final long stoptime = System.currentTimeMillis();
417         service.stop();
418         while (!service.isStopped() && System.currentTimeMillis() - stoptime < MAX_STOP_WAIT) {
419             ThreadUtilities.sleep(200);
420         }
421         if (!service.isStopped()) {
422             fail("Apex Service " + service.getKey() + " failed to stop after " + MAX_STOP_WAIT + " ms");
423         }
424         service = null;
425     }
426
427     /**
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.
432      *
433      * @see TestEvent
434      */
435     private static final class TestListener implements ApexEventListener {
436
437         /*
438          * (non-Javadoc)
439          *
440          * @see
441          * org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEvent(org.onap.policy
442          * .apex.service.engine.event.ApexEvent)
443          */
444         @Override
445         public synchronized void onApexEvent(final ApexEvent event) {
446             LOGGER.debug("result 1 is:" + event);
447             checkResult(event);
448             actionEventsReceived++;
449
450             final Date testStartTime = new Date((Long) event.get("TestTimestamp"));
451             final Date testEndTime = new Date();
452
453             LOGGER.info("policy execution time: " + (testEndTime.getTime() - testStartTime.getTime()) + "ms");
454         }
455
456         /**
457          * Check result.
458          *
459          * @param result the result
460          */
461         private void checkResult(final ApexEvent result) {
462             assertTrue(result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104"));
463
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));
473             assertTrue(
474                     ((byte) result.get("TestActCaseSelected")) >= 0 && ((byte) result.get("TestActCaseSelected") <= 3));
475         }
476     }
477 }