e78d4c5bb390d18e13478f040d1ae35583690a78
[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.Test;
34 import org.onap.policy.apex.context.ContextException;
35 import org.onap.policy.apex.core.engine.EngineParameters;
36 import org.onap.policy.apex.core.engine.engine.ApexEngine;
37 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
42 import org.onap.policy.apex.plugins.executor.mvel.MVELExecutorParameters;
43 import org.onap.policy.apex.plugins.executor.test.script.engine.TestApexActionListener;
44 import org.onap.policy.apex.test.common.model.SampleDomainModelFactory;
45 import org.slf4j.ext.XLogger;
46 import org.slf4j.ext.XLoggerFactory;
47
48 /**
49  * The Class TestApexEngine.
50  *
51  * @author Liam Fallon (liam.fallon@ericsson.com)
52  */
53 public class TestContextUpdateModel {
54     // Logger for this class
55     private static final XLogger logger = XLoggerFactory.getXLogger(TestContextUpdateModel.class);
56
57
58     @Test
59     public void testContextUpdateModel() throws ApexException, InterruptedException, IOException {
60         final AxArtifactKey key = new AxArtifactKey("TestApexEngine", "0.0.1");
61
62         final EngineParameters parameters = new EngineParameters();
63         parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters());
64
65         final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(key);
66         final TestApexActionListener listener = new TestApexActionListener("Test");
67         apexEngine.addEventListener("listener", listener);
68
69         final AxPolicyModel model1 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
70         assertNotNull(model1);
71         assertEquals(2, model1.getPolicies().getPolicyMap().size());
72
73         apexEngine.updateModel(model1);
74         apexEngine.start();
75         sendEvent(apexEngine, listener, "Event0000", true);
76         sendEvent(apexEngine, listener, "Event0100", true);
77         apexEngine.stop();
78
79         final AxPolicyModel model2 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
80         assertNotNull(model2);
81         model2.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy0", "0.0.1"));
82         assertEquals(1, model2.getPolicies().getPolicyMap().size());
83         apexEngine.updateModel(model2);
84         apexEngine.start();
85         sendEvent(apexEngine, listener, "Event0000", false);
86         sendEvent(apexEngine, listener, "Event0100", true);
87         apexEngine.stop();
88
89         final AxPolicyModel model3 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
90         assertNotNull(model3);
91         model3.getPolicies().getPolicyMap().remove(new AxArtifactKey("Policy1", "0.0.1"));
92         assertEquals(1, model3.getPolicies().getPolicyMap().size());
93         apexEngine.updateModel(model3);
94         apexEngine.start();
95         sendEvent(apexEngine, listener, "Event0000", true);
96         sendEvent(apexEngine, listener, "Event0100", false);
97         apexEngine.stop();
98
99         final AxPolicyModel model4 = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
100         assertNotNull(model4);
101         assertEquals(2, model4.getPolicies().getPolicyMap().size());
102         apexEngine.updateModel(model4);
103         apexEngine.start();
104         sendEvent(apexEngine, listener, "Event0100", true);
105         sendEvent(apexEngine, listener, "Event0000", true);
106         apexEngine.stop();
107
108         apexEngine.clear();
109     }
110
111     @After
112     public void testContextUpdateModelAfter() {}
113
114     private void sendEvent(final ApexEngine apexEngine, final TestApexActionListener listener, final String eventName,
115             final boolean shouldWork) throws ContextException {
116         final Date aDate = new Date(1433453067123L);
117         final Map<String, Object> eventDataMap = new HashMap<String, Object>();
118         eventDataMap.put("TestSlogan", "This is a test slogan");
119         eventDataMap.put("TestMatchCase", new Byte((byte) 123));
120         eventDataMap.put("TestTimestamp", aDate.getTime());
121         eventDataMap.put("TestTemperature", 34.5445667);
122
123         final EnEvent event0 = apexEngine.createEvent(new AxArtifactKey(eventName, "0.0.1"));
124         event0.putAll(eventDataMap);
125         apexEngine.handleEvent(event0);
126
127         final EnEvent result = listener.getResult(true);
128         logger.debug("result 1 is:" + result);
129         checkResult(result, shouldWork);
130     }
131
132     private void checkResult(final EnEvent result, final boolean shouldWork) {
133         if (!shouldWork) {
134             assertNotNull(result.getExceptionMessage());
135             return;
136         }
137
138         assertTrue(result.getName().equals("Event0004") || result.getName().equals("Event0104"));
139
140         if (result.getName().equals("Event0004")) {
141             assertEquals("This is a test slogan", result.get("TestSlogan"));
142             assertEquals((byte) 123, result.get("TestMatchCase"));
143             assertEquals(34.5445667, result.get("TestTemperature"));
144             assertEquals((byte) 2, result.get("TestMatchCaseSelected"));
145             assertEquals((byte) 0, result.get("TestEstablishCaseSelected"));
146             assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
147             assertEquals((byte) 3, result.get("TestActCaseSelected"));
148         } else {
149             assertEquals("This is a test slogan", result.get("TestSlogan"));
150             assertEquals((byte) 123, result.get("TestMatchCase"));
151             assertEquals(34.5445667, result.get("TestTemperature"));
152             assertEquals((byte) 1, result.get("TestMatchCaseSelected"));
153             assertEquals((byte) 3, result.get("TestEstablishCaseSelected"));
154             assertEquals((byte) 1, result.get("TestDecideCaseSelected"));
155             assertEquals((byte) 2, result.get("TestActCaseSelected"));
156         }
157     }
158 }