Changes for checkstyle 8.32
[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 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, false);
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, false);
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, false);
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, false);
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         // Not used
174     }
175
176     /**
177      * Send event.
178      *
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
184      */
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);
193
194         final EnEvent event0 = apexEngine.createEvent(new AxArtifactKey(eventName, "0.0.1"));
195         event0.putAll(eventDataMap);
196         apexEngine.handleEvent(event0);
197
198         final EnEvent result = listener.getResult(true);
199         logger.debug("result 1 is:" + result);
200         checkResult(result, shouldWork);
201     }
202
203     /**
204      * Check result.
205      *
206      * @param result the result
207      * @param shouldWork the should work
208      */
209     private void checkResult(final EnEvent result, final boolean shouldWork) {
210         if (!shouldWork) {
211             assertNotNull(result.getExceptionMessage());
212             return;
213         }
214
215         assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
216
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"));
225         } else {
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"));
233         }
234     }
235 }