2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.executor.handling;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
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.context.ContextException;
37 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
38 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
39 import org.onap.policy.apex.context.parameters.ContextParameters;
40 import org.onap.policy.apex.context.parameters.SchemaParameters;
41 import org.onap.policy.apex.core.engine.EngineParameters;
42 import org.onap.policy.apex.core.engine.engine.ApexEngine;
43 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
44 import org.onap.policy.apex.core.engine.event.EnEvent;
45 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
47 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
48 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
49 import org.onap.policy.apex.testsuites.integration.common.model.SampleDomainModelFactory;
50 import org.onap.policy.apex.testsuites.integration.executor.engine.TestApexActionListener;
51 import org.onap.policy.common.parameters.ParameterService;
52 import org.slf4j.ext.XLogger;
53 import org.slf4j.ext.XLoggerFactory;
56 * The Class TestApexEngine.
58 * @author Liam Fallon (liam.fallon@ericsson.com)
60 public class TestContextUpdateModel {
61 // Logger for this class
62 private static final XLogger logger = XLoggerFactory.getXLogger(TestContextUpdateModel.class);
64 private SchemaParameters schemaParameters;
65 private ContextParameters contextParameters;
66 private EngineParameters engineParameters;
72 public void beforeTest() {
73 schemaParameters = new SchemaParameters();
75 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
76 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
78 ParameterService.register(schemaParameters);
80 contextParameters = new ContextParameters();
82 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
83 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
84 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
85 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
87 ParameterService.register(contextParameters);
88 ParameterService.register(contextParameters.getDistributorParameters());
89 ParameterService.register(contextParameters.getLockManagerParameters());
90 ParameterService.register(contextParameters.getPersistorParameters());
92 engineParameters = new EngineParameters();
93 engineParameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
94 ParameterService.register(engineParameters);
101 public void afterTest() {
102 ParameterService.deregister(engineParameters);
104 ParameterService.deregister(contextParameters.getDistributorParameters());
105 ParameterService.deregister(contextParameters.getLockManagerParameters());
106 ParameterService.deregister(contextParameters.getPersistorParameters());
107 ParameterService.deregister(contextParameters);
109 ParameterService.deregister(schemaParameters);
113 * Test context update model.
115 * @throws ApexException the apex exception
116 * @throws InterruptedException the interrupted exception
117 * @throws IOException Signals that an I/O exception has occurred.
120 public void testContextUpdateModel() throws ApexException, InterruptedException, IOException {
121 final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
123 final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
124 final TestApexActionListener listener = new TestApexActionListener("Test");
125 apexEngine.addEventListener("listener", listener);
127 final AxPolicyModel model1 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
128 assertNotNull(model1);
129 assertEquals(2, model1.getPolicies().getPolicyMap().size());
131 apexEngine.updateModel(model1, false);
133 sendEvent(apexEngine, listener, "Event0000", true);
134 sendEvent(apexEngine, listener, "Event0100", true);
137 final AxPolicyModel model2 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
138 assertNotNull(model2);
139 model2.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy0", "0.0.1"));
140 assertEquals(1, model2.getPolicies().getPolicyMap().size());
141 apexEngine.updateModel(model2, false);
143 sendEvent(apexEngine, listener, "Event0000", false);
144 sendEvent(apexEngine, listener, "Event0100", true);
147 final AxPolicyModel model3 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
148 assertNotNull(model3);
149 model3.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy1", "0.0.1"));
150 assertEquals(1, model3.getPolicies().getPolicyMap().size());
151 apexEngine.updateModel(model3, false);
153 sendEvent(apexEngine, listener, "Event0000", true);
154 sendEvent(apexEngine, listener, "Event0100", false);
157 final AxPolicyModel model4 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
158 assertNotNull(model4);
159 assertEquals(2, model4.getPolicies().getPolicyMap().size());
160 apexEngine.updateModel(model4, false);
162 sendEvent(apexEngine, listener, "Event0100", true);
163 sendEvent(apexEngine, listener, "Event0000", true);
170 * Test context update model after.
173 public void testContextUpdateModelAfter() {
180 * @param apexEngine the apex engine
181 * @param listener the listener
182 * @param eventName the event name
183 * @param shouldWork the should work
184 * @throws ContextException the context exception
186 private void sendEvent(final ApexEngine apexEngine, final TestApexActionListener listener, final String eventName,
187 final boolean shouldWork) throws ContextException {
188 final Date aDate = new Date(1433453067123L);
189 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
190 eventDataMap.put("TestSlogan", "This is a test slogan");
191 eventDataMap.put("TestMatchCase", (byte) 123);
192 eventDataMap.put("TestTimestamp", aDate.getTime());
193 eventDataMap.put("TestTemperature", 34.5445667);
195 final EnEvent event0 = apexEngine.createEvent(new AxArtifactKey(eventName, "0.0.1"));
196 event0.putAll(eventDataMap);
197 apexEngine.handleEvent(event0);
199 final EnEvent result = listener.getResult(true);
200 logger.debug("result 1 is:" + result);
201 checkResult(result, shouldWork);
207 * @param result the result
208 * @param shouldWork the should work
210 private void checkResult(final EnEvent result, final boolean shouldWork) {
212 assertNotNull(result.getExceptionMessage());
216 assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
218 if (result.getName().equals("Event0004")) {
219 assertEquals("This is a test slogan", result.get("TestSlogan"));
220 assertEquals((byte) 123, result.get("TestMatchCase"));
221 assertEquals(34.5445667, result.get("TestTemperature"));
222 assertEquals((byte) 2, result.get("TestMatchCaseSelected"));
223 assertEquals((byte) 0, result.get("TestEstablishCaseSelected"));
224 assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
225 assertEquals((byte) 3, result.get("TestActCaseSelected"));
227 assertEquals("This is a test slogan", result.get("TestSlogan"));
228 assertEquals((byte) 123, result.get("TestMatchCase"));
229 assertEquals(34.5445667, result.get("TestTemperature"));
230 assertEquals((byte) 1, result.get("TestMatchCaseSelected"));
231 assertEquals((byte) 3, result.get("TestEstablishCaseSelected"));
232 assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
233 assertEquals((byte) 2, result.get("TestActCaseSelected"));