d5605ea4c2e191564ccf5632aa88f9cc5bf608c5
[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.plugins.executor.test.script.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.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;
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     @Before
68     public void beforeTest() {
69         schemaParameters = new SchemaParameters();
70         
71         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
72         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
73
74         ParameterService.register(schemaParameters);
75         
76         contextParameters = new ContextParameters();
77
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);
82
83         ParameterService.register(contextParameters);
84         ParameterService.register(contextParameters.getDistributorParameters());
85         ParameterService.register(contextParameters.getLockManagerParameters());
86         ParameterService.register(contextParameters.getPersistorParameters());
87         
88         engineParameters = new EngineParameters();
89         engineParameters.getExecutorParameterMap().put("MVEL", new MvelExecutorParameters());
90         ParameterService.register(engineParameters);
91     }
92
93     @After
94     public void afterTest() {
95         ParameterService.deregister(engineParameters);
96         
97         ParameterService.deregister(contextParameters.getDistributorParameters());
98         ParameterService.deregister(contextParameters.getLockManagerParameters());
99         ParameterService.deregister(contextParameters.getPersistorParameters());
100         ParameterService.deregister(contextParameters);
101
102         ParameterService.deregister(schemaParameters);
103     }
104
105     @Test
106     public void testContextUpdateModel() throws ApexException, InterruptedException, IOException {
107         final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
108
109         final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
110         final TestApexActionListener listener = new TestApexActionListener("Test");
111         apexEngine.addEventListener("listener", listener);
112
113         final AxPolicyModel model1 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
114         assertNotNull(model1);
115         assertEquals(2, model1.getPolicies().getPolicyMap().size());
116
117         apexEngine.updateModel(model1);
118         apexEngine.start();
119         sendEvent(apexEngine, listener, "Event0000", true);
120         sendEvent(apexEngine, listener, "Event0100", true);
121         apexEngine.stop();
122
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);
128         apexEngine.start();
129         sendEvent(apexEngine, listener, "Event0000", false);
130         sendEvent(apexEngine, listener, "Event0100", true);
131         apexEngine.stop();
132
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);
138         apexEngine.start();
139         sendEvent(apexEngine, listener, "Event0000", true);
140         sendEvent(apexEngine, listener, "Event0100", false);
141         apexEngine.stop();
142
143         final AxPolicyModel model4 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
144         assertNotNull(model4);
145         assertEquals(2, model4.getPolicies().getPolicyMap().size());
146         apexEngine.updateModel(model4);
147         apexEngine.start();
148         sendEvent(apexEngine, listener, "Event0100", true);
149         sendEvent(apexEngine, listener, "Event0000", true);
150         apexEngine.stop();
151
152         apexEngine.clear();
153     }
154
155     @After
156     public void testContextUpdateModelAfter() {}
157
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);
166
167         final EnEvent event0 = apexEngine.createEvent(new AxArtifactKey(eventName, "0.0.1"));
168         event0.putAll(eventDataMap);
169         apexEngine.handleEvent(event0);
170
171         final EnEvent result = listener.getResult(true);
172         logger.debug("result 1 is:" + result);
173         checkResult(result, shouldWork);
174     }
175
176     private void checkResult(final EnEvent result, final boolean shouldWork) {
177         if (!shouldWork) {
178             assertNotNull(result.getExceptionMessage());
179             return;
180         }
181
182         assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
183
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"));
192         } else {
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"));
200         }
201     }
202 }