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.service.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.impl.EngineServiceImpl;
45 import org.onap.policy.apex.service.engine.utils.Utils;
46 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
47 import org.onap.policy.apex.test.common.model.SampleDomainModelFactory;
48 import org.slf4j.ext.XLogger;
49 import org.slf4j.ext.XLoggerFactory;
53 * The Class ApexServiceTest.
55 * @author Liam Fallon (liam.fallon@ericsson.com)
57 public class ApexServiceModelUpdateTest {
58 // Logger for this class
59 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServiceModelUpdateTest.class);
61 private final AxArtifactKey engineServiceKey = new AxArtifactKey("Machine-1_process-1_engine-1", "0.0.0");
62 private final EngineServiceParameters parameters = new EngineServiceParameters();
63 private EngineService service = null;
64 private TestListener listener = null;
65 private int actionEventsReceived = 0;
67 private AxPolicyModel apexSamplePolicyModel = null;
68 private String apexSampleModelString;
71 * Sets up the test by creating an engine and reading in the test policy.
73 * @throws ApexException if something goes wron
77 public void setUp() throws ApexException, IOException {
78 // create engine with 3 threads
79 parameters.setInstanceCount(3);
80 parameters.setName(engineServiceKey.getName());
81 parameters.setVersion(engineServiceKey.getVersion());
82 parameters.setId(100);
83 parameters.getEngineParameters().getExecutorParameterMap().put("MVEL", new MVELExecutorParameters());
84 service = EngineServiceImpl.create(parameters);
86 LOGGER.debug("Running TestApexEngine. . .");
88 apexSamplePolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
89 assertNotNull(apexSamplePolicyModel);
91 apexSampleModelString = Utils.getModelString(apexSamplePolicyModel);
94 listener = new TestListener();
95 service.registerActionListener("MyListener", listener);
99 * Tear down the the test infrastructure.
101 * @throws ApexException if there is an error
104 public void tearDown() throws Exception {
105 if (service != null) {
112 * Test start with no model.
115 public void testNoModelStart() {
118 fail("Engine should not start with no model");
119 } catch (final Exception e) {
121 assertEquals("start()<-Machine-1_process-1_engine-1-0:0.0.0,STOPPED, cannot start engine, "
122 + "engine has not been initialized, its model is not loaded", e.getMessage());
127 * Test model update with string model without force.
129 * @throws ApexException if there is an error
132 public void testModelUpdateStringNewNoForce() throws ApexException {
133 service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
135 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
139 * Test model update with string model with force.
141 * @throws ApexException if there is an error
144 public void testModelUpdateStringNewForce() throws ApexException {
145 service.updateModel(parameters.getEngineKey(), apexSampleModelString, true);
147 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
151 * Test model update with a new string model without force.
153 * @throws ApexException if there is an error
156 public void testModelUpdateStringNewNewNoForce() throws ApexException {
157 service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
159 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
163 service.updateModel(parameters.getEngineKey(), apexSampleModelString, false);
164 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
170 * Test incompatible model update with a model object without force.
172 * @throws ApexException if there is an error
175 public void testModelUpdateIncoNoForce() throws ApexException {
176 service.updateModel(parameters.getEngineKey(), apexSamplePolicyModel, false);
178 assertEquals(apexSamplePolicyModel.getKey(), ModelService.getModel(AxPolicyModel.class).getKey());
180 // Different model name, incompatible
181 final AxPolicyModel incoPolicyModel0 = new AxPolicyModel(apexSamplePolicyModel);
182 incoPolicyModel0.getKey().setName("INCOMPATIBLE");
185 service.updateModel(parameters.getEngineKey(), incoPolicyModel0, false);
186 fail("model update should fail on incompatible model without force being true");
187 } catch (final Exception e) {
188 System.err.println(e.getMessage());
190 "apex model update failed, supplied model with key \"INCOMPATIBLE:0.0.1\" is not a compatible "
191 + "model update from the existing engine model with key \"SamplePolicyModelMVEL:0.0.1\"",
195 // Still on old model
198 // Different major version, incompatible
199 final AxPolicyModel incoPolicyModel1 = new AxPolicyModel(apexSamplePolicyModel);
200 incoPolicyModel1.getKey().setVersion("1.0.1");
203 service.updateModel(parameters.getEngineKey(), incoPolicyModel1, false);
204 fail("model update should fail on incompatible model without force being true");
205 } catch (final Exception e) {
206 System.err.println(e.getMessage());
209 "apex model update failed, supplied model with key \"SamplePolicyModelMVEL:1.0.1\" is not a compatible "
210 + "model update from the existing engine model with key \"SamplePolicyModelMVEL:0.0.1\"",
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 =
297 new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
298 event.putAll(eventDataMap);
299 engineServiceEventInterface.sendEvent(event);
301 final ApexEvent event2 =
302 new ApexEvent("Event0100", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
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
315 * a test event implements this interface, and the object created with that class is registered
316 * with a component using the component's <code>addTestListener</code> method. When the test
317 * event occurs, that object's appropriate method is invoked.
321 private final class TestListener implements ApexEventListener {
327 * org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEvent(org.onap.policy
328 * .apex.service.engine.event.ApexEvent)
331 public synchronized void onApexEvent(final ApexEvent event) {
332 LOGGER.debug("result 1 is:" + event);
334 actionEventsReceived++;
336 final Date testStartTime = new Date((Long) event.get("TestTimestamp"));
337 final Date testEndTime = new Date();
339 LOGGER.info("policy execution time: " + (testEndTime.getTime() - testStartTime.getTime()) + "ms");
345 * @param result the result
347 private void checkResult(final ApexEvent result) {
348 assertTrue(result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104"));
350 assertTrue(result.get("TestSlogan").equals("This is a test slogan"));
351 assertTrue(result.get("TestMatchCase").equals(new Byte((byte) 123)));
352 assertTrue(result.get("TestTemperature").equals(34.5445667));
353 assertTrue(((byte) result.get("TestMatchCaseSelected")) >= 0
354 && ((byte) result.get("TestMatchCaseSelected") <= 3));
355 assertTrue(((byte) result.get("TestEstablishCaseSelected")) >= 0
356 && ((byte) result.get("TestEstablishCaseSelected") <= 3));
357 assertTrue(((byte) result.get("TestDecideCaseSelected")) >= 0
358 && ((byte) result.get("TestDecideCaseSelected") <= 3));
360 ((byte) result.get("TestActCaseSelected")) >= 0 && ((byte) result.get("TestActCaseSelected") <= 3));