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.io.IOException;
29 import java.util.Date;
30 import java.util.HashMap;
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;
55 * The Class ApexServiceTest.
57 * @author Liam Fallon (liam.fallon@ericsson.com)
59 public class ApexServiceModelUpdateTest {
60 // Logger for this class
61 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServiceModelUpdateTest.class);
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;
69 private AxPolicyModel apexSamplePolicyModel = null;
70 private String apexSampleModelString;
73 * Sets up the test by creating an engine and reading in the test policy.
75 * @throws ApexException if something goes wrong
76 * @throws IOException on IO exceptions
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);
88 LOGGER.debug("Running TestApexEngine. . .");
90 apexSamplePolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
91 assertNotNull(apexSamplePolicyModel);
93 apexSampleModelString = Utils.getModelString(apexSamplePolicyModel);
96 listener = new TestListener();
97 service.registerActionListener("MyListener", listener);
101 * Tear down the the test infrastructure.
103 * @throws ApexException if there is an error
106 public void tearDown() throws Exception {
107 if (service != null) {
114 * Test start with no model.
117 public void testNoModelStart() {
120 fail("Engine should not start with no model");
121 } catch (final Exception e) {
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());
129 * Test model update with string model without force.
131 * @throws ApexException if there is an error
134 public void testModelUpdateStringNewNoForce() throws ApexException {
135 service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
137 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
141 * Test model update with string model with force.
143 * @throws ApexException if there is an error
146 public void testModelUpdateStringNewForce() throws ApexException {
147 service.updateModel(parameters.getEngineKey(), apexSampleModelString, true);
149 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
153 * Test model update with a new string model without force.
155 * @throws ApexException if there is an error
158 public void testModelUpdateStringNewNewNoForce() throws ApexException {
159 service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
161 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
165 service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
166 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
172 * Test incompatible model update with a model object without force.
174 * @throws ApexException if there is an error
177 public void testModelUpdateIncoNoForce() throws ApexException {
178 service.updateModel(parameters.getEngineKey(), apexSamplePolicyModel, false);
180 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
182 // Different model name, incompatible
183 final AxPolicyModel incoPolicyModel0 = new AxPolicyModel(apexSamplePolicyModel);
184 incoPolicyModel0.getKey().setName("INCOMPATIBLE");
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\"",
196 // Still on old model
199 // Different major version, incompatible
200 final AxPolicyModel incoPolicyModel1 = new AxPolicyModel(apexSamplePolicyModel);
201 incoPolicyModel1.getKey().setVersion("1.0.1");
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());
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());
214 // Still on old model
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);
222 // On new compatible model
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);
230 // On new compatible model
236 * Test incompatible model update with a model object with force.
238 * @throws ApexException if there is an error
241 public void testModelUpdateIncoForce() throws ApexException {
242 service.updateModel(parameters.getEngineKey(), apexSamplePolicyModel, false);
244 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
246 // Different model name, incompatible
247 final AxPolicyModel incoPolicyModel0 = new AxPolicyModel(apexSamplePolicyModel);
248 incoPolicyModel0.getKey().setName("INCOMPATIBLE");
249 service.updateModel(parameters.getEngineKey(), incoPolicyModel0, true);
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);
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);
267 // On new compatible model
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);
275 // On new compatible model
281 * Utility method to send some events into the test engine.
283 * @throws ApexEventException if there is an error
285 private void sendEvents() throws ApexEventException {
286 final EngineServiceEventInterface engineServiceEventInterface = service.getEngineServiceEventInterface();
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);
296 final ApexEvent event = new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events",
298 event.putAll(eventDataMap);
299 engineServiceEventInterface.sendEvent(event);
301 final ApexEvent event2 = new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events",
303 event2.putAll(eventDataMap);
304 engineServiceEventInterface.sendEvent(event2);
307 while (actionEventsReceived < 2) {
308 ThreadUtilities.sleep(100);
310 ThreadUtilities.sleep(500);
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
321 private final class TestListener implements ApexEventListener {
326 * @see org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEvent(org.onap.policy
327 * .apex.service.engine.event.ApexEvent)
330 public synchronized void onApexEvent(final ApexEvent event) {
331 LOGGER.debug("result 1 is:" + event);
333 actionEventsReceived++;
335 final Date testStartTime = new Date((Long) event.get("TestTimestamp"));
336 final Date testEndTime = new Date();
338 LOGGER.info("policy execution time: " + (testEndTime.getTime() - testStartTime.getTime()) + "ms");
344 * @param result the result
346 private void checkResult(final ApexEvent result) {
347 assertTrue(result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104"));
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));