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.plugins.executor.test.script.handling;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import java.io.IOException;
28 import java.util.Date;
29 import java.util.HashMap;
32 import org.junit.After;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.ContextException;
35 import org.onap.policy.apex.core.engine.EngineParameters;
36 import org.onap.policy.apex.core.engine.engine.ApexEngine;
37 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
42 import org.onap.policy.apex.plugins.executor.mvel.MVELExecutorParameters;
43 import org.onap.policy.apex.plugins.executor.test.script.engine.TestApexActionListener;
44 import org.onap.policy.apex.test.common.model.SampleDomainModelFactory;
45 import org.slf4j.ext.XLogger;
46 import org.slf4j.ext.XLoggerFactory;
49 * The Class TestApexEngine.
51 * @author Liam Fallon (liam.fallon@ericsson.com)
53 public class TestContextUpdateModel {
54 // Logger for this class
55 private static final XLogger logger = XLoggerFactory.getXLogger(TestContextUpdateModel.class);
59 public void testContextUpdateModel() throws ApexException, InterruptedException, IOException {
60 final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
62 final EngineParameters parameters = new EngineParameters();
63 parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters());
65 final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
66 final TestApexActionListener listener = new TestApexActionListener("Test");
67 apexEngine.addEventListener("listener", listener);
69 final AxPolicyModel model1 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
70 assertNotNull(model1);
71 assertEquals(2, model1.getPolicies().getPolicyMap().size());
73 apexEngine.updateModel(model1);
75 sendEvent(apexEngine, listener, "Event0000", true);
76 sendEvent(apexEngine, listener, "Event0100", true);
79 final AxPolicyModel model2 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
80 assertNotNull(model2);
81 model2.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy0", "0.0.1"));
82 assertEquals(1, model2.getPolicies().getPolicyMap().size());
83 apexEngine.updateModel(model2);
85 sendEvent(apexEngine, listener, "Event0000", false);
86 sendEvent(apexEngine, listener, "Event0100", true);
89 final AxPolicyModel model3 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
90 assertNotNull(model3);
91 model3.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy1", "0.0.1"));
92 assertEquals(1, model3.getPolicies().getPolicyMap().size());
93 apexEngine.updateModel(model3);
95 sendEvent(apexEngine, listener, "Event0000", true);
96 sendEvent(apexEngine, listener, "Event0100", false);
99 final AxPolicyModel model4 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
100 assertNotNull(model4);
101 assertEquals(2, model4.getPolicies().getPolicyMap().size());
102 apexEngine.updateModel(model4);
104 sendEvent(apexEngine, listener, "Event0100", true);
105 sendEvent(apexEngine, listener, "Event0000", true);
112 public void testContextUpdateModelAfter() {}
114 private void sendEvent(final ApexEngine apexEngine, final TestApexActionListener listener, final String eventName,
115 final boolean shouldWork) throws ContextException {
116 final Date aDate = new Date(1433453067123L);
117 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
118 eventDataMap.put("TestSlogan", "This is a test slogan");
119 eventDataMap.put("TestMatchCase", new Byte((byte) 123));
120 eventDataMap.put("TestTimestamp", aDate.getTime());
121 eventDataMap.put("TestTemperature", 34.5445667);
123 final EnEvent event0 = apexEngine.createEvent(new AxArtifactKey(eventName, "0.0.1"));
124 event0.putAll(eventDataMap);
125 apexEngine.handleEvent(event0);
127 final EnEvent result = listener.getResult(true);
128 logger.debug("result 1 is:" + result);
129 checkResult(result, shouldWork);
132 private void checkResult(final EnEvent result, final boolean shouldWork) {
134 assertNotNull(result.getExceptionMessage());
138 assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
140 if (result.getName().equals("Event0004")) {
141 assertEquals("This is a test slogan", result.get("TestSlogan"));
142 assertEquals((byte) 123, result.get("TestMatchCase"));
143 assertEquals(34.5445667, result.get("TestTemperature"));
144 assertEquals((byte) 2, result.get("TestMatchCaseSelected"));
145 assertEquals((byte) 0, result.get("TestEstablishCaseSelected"));
146 assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
147 assertEquals((byte) 3, result.get("TestActCaseSelected"));
149 assertEquals("This is a test slogan", result.get("TestSlogan"));
150 assertEquals((byte) 123, result.get("TestMatchCase"));
151 assertEquals(34.5445667, result.get("TestTemperature"));
152 assertEquals((byte) 1, result.get("TestMatchCaseSelected"));
153 assertEquals((byte) 3, result.get("TestEstablishCaseSelected"));
154 assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
155 assertEquals((byte) 2, result.get("TestActCaseSelected"));