1d33d3f21d85b2a53c668881a171695eef75219c
[policy/apex-pdp.git] / testsuites / integration / integration-executor-test / src / test / java / org / onap / policy / apex / testsuites / integration / executor / handling / TestContextUpdateModel.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.executor.handling;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.Map;
32
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;
54
55 /**
56  * The Class TestApexEngine.
57  *
58  * @author Liam Fallon (liam.fallon@ericsson.com)
59  */
60 public class TestContextUpdateModel {
61     // Logger for this class
62     private static final XLogger logger = XLoggerFactory.getXLogger(TestContextUpdateModel.class);
63
64     private SchemaParameters schemaParameters;
65     private ContextParameters contextParameters;
66     private EngineParameters engineParameters;
67
68     /**
69      * Before test.
70      */
71     @Before
72     public void beforeTest() {
73         schemaParameters = new SchemaParameters();
74
75         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
76         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
77
78         ParameterService.register(schemaParameters);
79
80         contextParameters = new ContextParameters();
81
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);
86
87         ParameterService.register(contextParameters);
88         ParameterService.register(contextParameters.getDistributorParameters());
89         ParameterService.register(contextParameters.getLockManagerParameters());
90         ParameterService.register(contextParameters.getPersistorParameters());
91
92         engineParameters = new EngineParameters();
93         engineParameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
94         ParameterService.register(engineParameters);
95     }
96
97     /**
98      * After test.
99      */
100     @After
101     public void afterTest() {
102         ParameterService.deregister(engineParameters);
103
104         ParameterService.deregister(contextParameters.getDistributorParameters());
105         ParameterService.deregister(contextParameters.getLockManagerParameters());
106         ParameterService.deregister(contextParameters.getPersistorParameters());
107         ParameterService.deregister(contextParameters);
108
109         ParameterService.deregister(schemaParameters);
110     }
111
112     /**
113      * Test context update model.
114      *
115      * @throws ApexException the apex exception
116      * @throws InterruptedException the interrupted exception
117      * @throws IOException Signals that an I/O exception has occurred.
118      */
119     @Test
120     public void testContextUpdateModel() throws ApexException, InterruptedException, IOException {
121         final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
122
123         final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
124         final TestApexActionListener listener = new TestApexActionListener("Test");
125         apexEngine.addEventListener("listener", listener);
126
127         final AxPolicyModel model1 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
128         assertNotNull(model1);
129         assertEquals(2, model1.getPolicies().getPolicyMap().size());
130
131         apexEngine.updateModel(model1, false);
132         apexEngine.start();
133         sendEvent(apexEngine, listener, "Event0000", true);
134         sendEvent(apexEngine, listener, "Event0100", true);
135         apexEngine.stop();
136
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);
142         apexEngine.start();
143         sendEvent(apexEngine, listener, "Event0000", false);
144         sendEvent(apexEngine, listener, "Event0100", true);
145         apexEngine.stop();
146
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);
152         apexEngine.start();
153         sendEvent(apexEngine, listener, "Event0000", true);
154         sendEvent(apexEngine, listener, "Event0100", false);
155         apexEngine.stop();
156
157         final AxPolicyModel model4 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
158         assertNotNull(model4);
159         assertEquals(2, model4.getPolicies().getPolicyMap().size());
160         apexEngine.updateModel(model4, false);
161         apexEngine.start();
162         sendEvent(apexEngine, listener, "Event0100", true);
163         sendEvent(apexEngine, listener, "Event0000", true);
164         apexEngine.stop();
165
166         apexEngine.clear();
167     }
168
169     /**
170      * Test context update model after.
171      */
172     @After
173     public void testContextUpdateModelAfter() {
174         // Not used
175     }
176
177     /**
178      * Send event.
179      *
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
185      */
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);
194
195         final EnEvent event0 = apexEngine.createEvent(new AxArtifactKey(eventName, "0.0.1"));
196         event0.putAll(eventDataMap);
197         apexEngine.handleEvent(event0);
198
199         final EnEvent result = listener.getResult(true);
200         logger.debug("result 1 is:" + result);
201         checkResult(result, shouldWork);
202     }
203
204     /**
205      * Check result.
206      *
207      * @param result the result
208      * @param shouldWork the should work
209      */
210     private void checkResult(final EnEvent result, final boolean shouldWork) {
211         if (!shouldWork) {
212             assertNotNull(result.getExceptionMessage());
213             return;
214         }
215
216         assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
217
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"));
226         } else {
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"));
234         }
235     }
236 }