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;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.apex.context.ContextException;
36 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
37 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
38 import org.onap.policy.apex.context.parameters.ContextParameters;
39 import org.onap.policy.apex.context.parameters.SchemaParameters;
40 import org.onap.policy.apex.core.engine.EngineParameters;
41 import org.onap.policy.apex.core.engine.engine.ApexEngine;
42 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
43 import org.onap.policy.apex.core.engine.event.EnEvent;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
46 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
47 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
48 import org.onap.policy.apex.testsuites.integration.common.model.SampleDomainModelFactory;
49 import org.onap.policy.apex.testsuites.integration.executor.engine.TestApexActionListener;
50 import org.onap.policy.common.parameters.ParameterService;
51 import org.slf4j.ext.XLogger;
52 import org.slf4j.ext.XLoggerFactory;
55 * The Class TestApexEngine.
57 * @author Liam Fallon (liam.fallon@ericsson.com)
59 public class TestContextUpdateModel {
60 // Logger for this class
61 private static final XLogger logger = XLoggerFactory.getXLogger(TestContextUpdateModel.class);
63 private SchemaParameters schemaParameters;
64 private ContextParameters contextParameters;
65 private EngineParameters engineParameters;
71 public void beforeTest() {
72 schemaParameters = new SchemaParameters();
74 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
75 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
77 ParameterService.register(schemaParameters);
79 contextParameters = new ContextParameters();
81 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
82 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
83 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
84 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
86 ParameterService.register(contextParameters);
87 ParameterService.register(contextParameters.getDistributorParameters());
88 ParameterService.register(contextParameters.getLockManagerParameters());
89 ParameterService.register(contextParameters.getPersistorParameters());
91 engineParameters = new EngineParameters();
92 engineParameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
93 ParameterService.register(engineParameters);
100 public void afterTest() {
101 ParameterService.deregister(engineParameters);
103 ParameterService.deregister(contextParameters.getDistributorParameters());
104 ParameterService.deregister(contextParameters.getLockManagerParameters());
105 ParameterService.deregister(contextParameters.getPersistorParameters());
106 ParameterService.deregister(contextParameters);
108 ParameterService.deregister(schemaParameters);
112 * Test context update model.
114 * @throws ApexException the apex exception
115 * @throws InterruptedException the interrupted exception
116 * @throws IOException Signals that an I/O exception has occurred.
119 public void testContextUpdateModel() throws ApexException, InterruptedException, IOException {
120 final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
122 final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
123 final TestApexActionListener listener = new TestApexActionListener("Test");
124 apexEngine.addEventListener("listener", listener);
126 final AxPolicyModel model1 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
127 assertNotNull(model1);
128 assertEquals(2, model1.getPolicies().getPolicyMap().size());
130 apexEngine.updateModel(model1, false);
132 sendEvent(apexEngine, listener, "Event0000", true);
133 sendEvent(apexEngine, listener, "Event0100", true);
136 final AxPolicyModel model2 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
137 assertNotNull(model2);
138 model2.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy0", "0.0.1"));
139 assertEquals(1, model2.getPolicies().getPolicyMap().size());
140 apexEngine.updateModel(model2, false);
142 sendEvent(apexEngine, listener, "Event0000", false);
143 sendEvent(apexEngine, listener, "Event0100", true);
146 final AxPolicyModel model3 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
147 assertNotNull(model3);
148 model3.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy1", "0.0.1"));
149 assertEquals(1, model3.getPolicies().getPolicyMap().size());
150 apexEngine.updateModel(model3, false);
152 sendEvent(apexEngine, listener, "Event0000", true);
153 sendEvent(apexEngine, listener, "Event0100", false);
156 final AxPolicyModel model4 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
157 assertNotNull(model4);
158 assertEquals(2, model4.getPolicies().getPolicyMap().size());
159 apexEngine.updateModel(model4, false);
161 sendEvent(apexEngine, listener, "Event0100", true);
162 sendEvent(apexEngine, listener, "Event0000", true);
169 * Test context update model after.
172 public void testContextUpdateModelAfter() {
179 * @param apexEngine the apex engine
180 * @param listener the listener
181 * @param eventName the event name
182 * @param shouldWork the should work
183 * @throws ContextException the context exception
185 private void sendEvent(final ApexEngine apexEngine, final TestApexActionListener listener, final String eventName,
186 final boolean shouldWork) throws ContextException {
187 final Date aDate = new Date(1433453067123L);
188 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
189 eventDataMap.put("TestSlogan", "This is a test slogan");
190 eventDataMap.put("TestMatchCase", (byte) 123);
191 eventDataMap.put("TestTimestamp", aDate.getTime());
192 eventDataMap.put("TestTemperature", 34.5445667);
194 final EnEvent event0 = apexEngine.createEvent(new AxArtifactKey(eventName, "0.0.1"));
195 event0.putAll(eventDataMap);
196 apexEngine.handleEvent(event0);
198 final EnEvent result = listener.getResult(true);
199 logger.debug("result 1 is:" + result);
200 checkResult(result, shouldWork);
206 * @param result the result
207 * @param shouldWork the should work
209 private void checkResult(final EnEvent result, final boolean shouldWork) {
211 assertNotNull(result.getExceptionMessage());
215 assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
217 if (result.getName().equals("Event0004")) {
218 assertEquals("This is a test slogan", result.get("TestSlogan"));
219 assertEquals((byte) 123, result.get("TestMatchCase"));
220 assertEquals(34.5445667, result.get("TestTemperature"));
221 assertEquals((byte) 2, result.get("TestMatchCaseSelected"));
222 assertEquals((byte) 0, result.get("TestEstablishCaseSelected"));
223 assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
224 assertEquals((byte) 3, result.get("TestActCaseSelected"));
226 assertEquals("This is a test slogan", result.get("TestSlogan"));
227 assertEquals((byte) 123, result.get("TestMatchCase"));
228 assertEquals(34.5445667, result.get("TestTemperature"));
229 assertEquals((byte) 1, result.get("TestMatchCaseSelected"));
230 assertEquals((byte) 3, result.get("TestEstablishCaseSelected"));
231 assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
232 assertEquals((byte) 2, result.get("TestActCaseSelected"));