cf930150e25877302e15e72d34b41fb1550ad149
[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.io.IOException;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.service.ModelService;
40 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
41 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
42 import org.onap.policy.apex.service.engine.event.ApexEvent;
43 import org.onap.policy.apex.service.engine.event.ApexEventException;
44 import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
45 import org.onap.policy.apex.service.engine.runtime.EngineService;
46 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
47 import org.onap.policy.apex.service.engine.runtime.impl.EngineServiceImpl;
48 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
49 import org.onap.policy.apex.testsuites.integration.common.model.SampleDomainModelFactory;
50 import org.onap.policy.apex.testsuites.performance.benchmark.engine.utils.Utils;
51 import org.slf4j.ext.XLogger;
52 import org.slf4j.ext.XLoggerFactory;
53
54 /**
55  * The Class ApexServiceTest.
56  *
57  * @author Liam Fallon (liam.fallon@ericsson.com)
58  */
59 public class ApexServiceModelUpdateTest {
60     // Logger for this class
61     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServiceModelUpdateTest.class);
62
63     private final AxArtifactKey engineServiceKey = new AxArtifactKey("Machine-1_process-1_engine-1", "0.0.0");
64     private final EngineServiceParameters parameters = new EngineServiceParameters();
65     private EngineService service = null;
66     private TestListener listener = null;
67     private int actionEventsReceived = 0;
68
69     private AxPolicyModel apexSamplePolicyModel = null;
70     private String apexSampleModelString;
71
72     /**
73      * Sets up the test by creating an engine and reading in the test policy.
74      *
75      * @throws ApexException if something goes wrong
76      * @throws IOException on IO exceptions
77      */
78     @Before
79     public void setUp() throws ApexException, IOException {
80         // create engine with 3 threads
81         parameters.setInstanceCount(3);
82         parameters.setName(engineServiceKey.getName());
83         parameters.setVersion(engineServiceKey.getVersion());
84         parameters.setId(100);
85         parameters.getEngineParameters().getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
86         service = EngineServiceImpl.create(parameters);
87
88         LOGGER.debug("Running TestApexEngine. . .");
89
90         apexSamplePolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
91         assertNotNull(apexSamplePolicyModel);
92
93         apexSampleModelString = Utils.getModelString(apexSamplePolicyModel);
94
95         // create engine
96         listener = new TestListener();
97         service.registerActionListener("MyListener", listener);
98     }
99
100     /**
101      * Tear down the the test infrastructure.
102      *
103      * @throws ApexException if there is an error
104      */
105     @After
106     public void tearDown() throws Exception {
107         if (service != null) {
108             service.stop();
109         }
110         service = null;
111     }
112
113     /**
114      * Test start with no model.
115      */
116     @Test
117     public void testNoModelStart() {
118         try {
119             service.startAll();
120             fail("Engine should not start with no model");
121         } catch (final Exception e) {
122             e.printStackTrace();
123             assertEquals("start()<-Machine-1_process-1_engine-1-0:0.0.0,STOPPED,  cannot start engine, "
124                             + "engine has not been initialized, its model is not loaded", e.getMessage());
125         }
126     }
127
128     /**
129      * Test model update with string model without force.
130      *
131      * @throws ApexException if there is an error
132      */
133     @Test
134     public void testModelUpdateStringNewNoForce() throws ApexException {
135         service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
136         service.startAll();
137         assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
138     }
139
140     /**
141      * Test model update with string model with force.
142      *
143      * @throws ApexException if there is an error
144      */
145     @Test
146     public void testModelUpdateStringNewForce() throws ApexException {
147         service.updateModel(parameters.getEngineKey(), apexSampleModelString, true);
148         service.startAll();
149         assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
150     }
151
152     /**
153      * Test model update with a new string model without force.
154      *
155      * @throws ApexException if there is an error
156      */
157     @Test
158     public void testModelUpdateStringNewNewNoForce() throws ApexException {
159         service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
160         service.startAll();
161         assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
162
163         sendEvents();
164
165         service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
166         assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
167
168         sendEvents();
169     }
170
171     /**
172      * Test incompatible model update with a model object without force.
173      *
174      * @throws ApexException if there is an error
175      */
176     @Test
177     public void testModelUpdateIncoNoForce() throws ApexException {
178         service.updateModel(parameters.getEngineKey(), apexSamplePolicyModel, false);
179         service.startAll();
180         assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
181
182         // Different model name, incompatible
183         final AxPolicyModel incoPolicyModel0 = new AxPolicyModel(apexSamplePolicyModel);
184         incoPolicyModel0.getKey().setName("INCOMPATIBLE");
185
186         try {
187             service.updateModel(parameters.getEngineKey(), incoPolicyModel0, false);
188             fail("model update should fail on incompatible model without force being true");
189         } catch (final Exception e) {
190             System.err.println(e.getMessage());
191             assertEquals("apex model update failed, supplied model with key \"INCOMPATIBLE:0.0.1\" is not a compatible "
192                             + "model update from the existing engine model with key \"SamplePolicyModelMVEL:0.0.1\"",
193                             e.getMessage());
194         }
195
196         // Still on old model
197         sendEvents();
198
199         // Different major version, incompatible
200         final AxPolicyModel incoPolicyModel1 = new AxPolicyModel(apexSamplePolicyModel);
201         incoPolicyModel1.getKey().setVersion("1.0.1");
202
203         try {
204             service.updateModel(parameters.getEngineKey(), incoPolicyModel1, false);
205             fail("model update should fail on incompatible model without force being true");
206         } catch (final Exception e) {
207             System.err.println(e.getMessage());
208             e.printStackTrace();
209             assertEquals("apex model update failed, supplied model with key \"SamplePolicyModelMVEL:1.0.1\" is not "
210                             + "a compatible model update from the existing engine model with key "
211                             + "\"SamplePolicyModelMVEL:0.0.1\"", e.getMessage());
212         }
213
214         // Still on old model
215         sendEvents();
216
217         // Different minor version, compatible
218         final AxPolicyModel coPolicyModel0 = new AxPolicyModel(apexSamplePolicyModel);
219         coPolicyModel0.getKey().setVersion("0.1.0");
220         service.updateModel(parameters.getEngineKey(), coPolicyModel0, false);
221
222         // On new compatible model
223         sendEvents();
224
225         // Different patch version, compatible
226         final AxPolicyModel coPolicyModel1 = new AxPolicyModel(apexSamplePolicyModel);
227         coPolicyModel1.getKey().setVersion("0.0.2");
228         service.updateModel(parameters.getEngineKey(), coPolicyModel1, false);
229
230         // On new compatible model
231         sendEvents();
232
233     }
234
235     /**
236      * Test incompatible model update with a model object with force.
237      *
238      * @throws ApexException if there is an error
239      */
240     @Test
241     public void testModelUpdateIncoForce() throws ApexException {
242         service.updateModel(parameters.getEngineKey(), apexSamplePolicyModel, false);
243         service.startAll();
244         assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
245
246         // Different model name, incompatible
247         final AxPolicyModel incoPolicyModel0 = new AxPolicyModel(apexSamplePolicyModel);
248         incoPolicyModel0.getKey().setName("INCOMPATIBLE");
249         service.updateModel(parameters.getEngineKey(), incoPolicyModel0, true);
250
251         // On updated model
252         sendEvents();
253
254         // Different major version, incompatible
255         final AxPolicyModel incoPolicyModel1 = new AxPolicyModel(apexSamplePolicyModel);
256         incoPolicyModel1.getKey().setVersion("1.0.1");
257         service.updateModel(parameters.getEngineKey(), incoPolicyModel1, true);
258
259         // On updated model
260         sendEvents();
261
262         // Different minor version, compatible
263         final AxPolicyModel coPolicyModel0 = new AxPolicyModel(apexSamplePolicyModel);
264         coPolicyModel0.getKey().setVersion("0.1.0");
265         service.updateModel(parameters.getEngineKey(), coPolicyModel0, true);
266
267         // On new compatible model
268         sendEvents();
269
270         // Different patch version, compatible
271         final AxPolicyModel coPolicyModel1 = new AxPolicyModel(apexSamplePolicyModel);
272         coPolicyModel1.getKey().setVersion("0.0.2");
273         service.updateModel(parameters.getEngineKey(), coPolicyModel1, true);
274
275         // On new compatible model
276         sendEvents();
277
278     }
279
280     /**
281      * Utility method to send some events into the test engine.
282      * 
283      * @throws ApexEventException if there is an error
284      */
285     private void sendEvents() throws ApexEventException {
286         final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
287
288         // Send some events
289         final Date testStartTime = new Date();
290         final Map<String, Object> eventDataMap = new HashMap<String, Object>();
291         eventDataMap.put("TestSlogan", "This is a test slogan");
292         eventDataMap.put("TestMatchCase", (byte) 123);
293         eventDataMap.put("TestTimestamp", testStartTime.getTime());
294         eventDataMap.put("TestTemperature", 34.5445667);
295
296         final ApexEvent event = new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events",
297                         "test", "apex");
298         event.putAll(eventDataMap);
299         engineServiceEventInterface.sendEvent(event);
300
301         final ApexEvent event2 = new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events",
302                         "test", "apex");
303         event2.putAll(eventDataMap);
304         engineServiceEventInterface.sendEvent(event2);
305
306         // Wait for results
307         while (actionEventsReceived < 2) {
308             ThreadUtilities.sleep(100);
309         }
310         ThreadUtilities.sleep(500);
311     }
312
313     /**
314      * The listener interface for receiving test events. The class that is interested in processing a test event
315      * implements this interface, and the object created with that class is registered with a component using the
316      * component's <code>addTestListener</code> method. When the test event occurs, that object's appropriate method is
317      * invoked.
318      *
319      * @see TestEvent
320      */
321     private final class TestListener implements ApexEventListener {
322
323         /*
324          * (non-Javadoc)
325          *
326          * @see org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEvent(org.onap.policy
327          * .apex.service.engine.event.ApexEvent)
328          */
329         @Override
330         public synchronized void onApexEvent(final ApexEvent event) {
331             LOGGER.debug("result 1 is:" + event);
332             checkResult(event);
333             actionEventsReceived++;
334
335             final Date testStartTime = new Date((Long) event.get("TestTimestamp"));
336             final Date testEndTime = new Date();
337
338             LOGGER.info("policy execution time: " + (testEndTime.getTime() - testStartTime.getTime()) + "ms");
339         }
340
341         /**
342          * Check result.
343          *
344          * @param result the result
345          */
346         private void checkResult(final ApexEvent result) {
347             assertTrue(result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104"));
348
349             assertTrue(result.get("TestSlogan").equals("This is a test slogan"));
350             assertTrue(result.get("TestMatchCase").equals(new Byte((byte) 123)));
351             assertTrue(result.get("TestTemperature").equals(34.5445667));
352             assertTrue(((byte) result.get("TestMatchCaseSelected")) >= 0
353                             && ((byte) result.get("TestMatchCaseSelected") <= 3));
354             assertTrue(((byte) result.get("TestEstablishCaseSelected")) >= 0
355                             && ((byte) result.get("TestEstablishCaseSelected") <= 3));
356             assertTrue(((byte) result.get("TestDecideCaseSelected")) >= 0
357                             && ((byte) result.get("TestDecideCaseSelected") <= 3));
358             assertTrue(((byte) result.get("TestActCaseSelected")) >= 0
359                             && ((byte) result.get("TestActCaseSelected") <= 3));
360         }
361     }
362 }