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.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.plugins.executor.test.script.engine.TestApexActionListener;
49 import org.onap.policy.apex.test.common.model.SampleDomainModelFactory;
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;
68 public void beforeTest() {
69 schemaParameters = new SchemaParameters();
71 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
72 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
74 ParameterService.register(schemaParameters);
76 contextParameters = new ContextParameters();
78 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
79 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
80 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
81 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
83 ParameterService.register(contextParameters);
84 ParameterService.register(contextParameters.getDistributorParameters());
85 ParameterService.register(contextParameters.getLockManagerParameters());
86 ParameterService.register(contextParameters.getPersistorParameters());
88 engineParameters = new EngineParameters();
89 engineParameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
90 ParameterService.register(engineParameters);
94 public void afterTest() {
95 ParameterService.deregister(engineParameters);
97 ParameterService.deregister(contextParameters.getDistributorParameters());
98 ParameterService.deregister(contextParameters.getLockManagerParameters());
99 ParameterService.deregister(contextParameters.getPersistorParameters());
100 ParameterService.deregister(contextParameters);
102 ParameterService.deregister(schemaParameters);
106 public void testContextUpdateModel() throws ApexException, InterruptedException, IOException {
107 final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
109 final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
110 final TestApexActionListener listener = new TestApexActionListener("Test");
111 apexEngine.addEventListener("listener", listener);
113 final AxPolicyModel model1 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
114 assertNotNull(model1);
115 assertEquals(2, model1.getPolicies().getPolicyMap().size());
117 apexEngine.updateModel(model1);
119 sendEvent(apexEngine, listener, "Event0000", true);
120 sendEvent(apexEngine, listener, "Event0100", true);
123 final AxPolicyModel model2 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
124 assertNotNull(model2);
125 model2.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy0", "0.0.1"));
126 assertEquals(1, model2.getPolicies().getPolicyMap().size());
127 apexEngine.updateModel(model2);
129 sendEvent(apexEngine, listener, "Event0000", false);
130 sendEvent(apexEngine, listener, "Event0100", true);
133 final AxPolicyModel model3 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
134 assertNotNull(model3);
135 model3.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy1", "0.0.1"));
136 assertEquals(1, model3.getPolicies().getPolicyMap().size());
137 apexEngine.updateModel(model3);
139 sendEvent(apexEngine, listener, "Event0000", true);
140 sendEvent(apexEngine, listener, "Event0100", false);
143 final AxPolicyModel model4 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
144 assertNotNull(model4);
145 assertEquals(2, model4.getPolicies().getPolicyMap().size());
146 apexEngine.updateModel(model4);
148 sendEvent(apexEngine, listener, "Event0100", true);
149 sendEvent(apexEngine, listener, "Event0000", true);
156 public void testContextUpdateModelAfter() {}
158 private void sendEvent(final ApexEngine apexEngine, final TestApexActionListener listener, final String eventName,
159 final boolean shouldWork) throws ContextException {
160 final Date aDate = new Date(1433453067123L);
161 final Map<String, Object> eventDataMap = new HashMap<String, Object>();
162 eventDataMap.put("TestSlogan", "This is a test slogan");
163 eventDataMap.put("TestMatchCase", new Byte((byte) 123));
164 eventDataMap.put("TestTimestamp", aDate.getTime());
165 eventDataMap.put("TestTemperature", 34.5445667);
167 final EnEvent event0 = apexEngine.createEvent(new AxArtifactKey(eventName, "0.0.1"));
168 event0.putAll(eventDataMap);
169 apexEngine.handleEvent(event0);
171 final EnEvent result = listener.getResult(true);
172 logger.debug("result 1 is:" + result);
173 checkResult(result, shouldWork);
176 private void checkResult(final EnEvent result, final boolean shouldWork) {
178 assertNotNull(result.getExceptionMessage());
182 assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
184 if (result.getName().equals("Event0004")) {
185 assertEquals("This is a test slogan", result.get("TestSlogan"));
186 assertEquals((byte) 123, result.get("TestMatchCase"));
187 assertEquals(34.5445667, result.get("TestTemperature"));
188 assertEquals((byte) 2, result.get("TestMatchCaseSelected"));
189 assertEquals((byte) 0, result.get("TestEstablishCaseSelected"));
190 assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
191 assertEquals((byte) 3, result.get("TestActCaseSelected"));
193 assertEquals("This is a test slogan", result.get("TestSlogan"));
194 assertEquals((byte) 123, result.get("TestMatchCase"));
195 assertEquals(34.5445667, result.get("TestTemperature"));
196 assertEquals((byte) 1, result.get("TestMatchCaseSelected"));
197 assertEquals((byte) 3, result.get("TestEstablishCaseSelected"));
198 assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
199 assertEquals((byte) 2, result.get("TestActCaseSelected"));