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