0ed78a13f72a1d43cb3ea7241695a1627144bc44
[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.event;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
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.Test;
34 import org.onap.policy.apex.core.engine.EngineParameters;
35 import org.onap.policy.apex.core.engine.engine.ApexEngine;
36 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
37 import org.onap.policy.apex.core.engine.event.EnEvent;
38 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
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.test.common.model.SampleDomainModelFactory;
44 import org.slf4j.ext.XLogger;
45 import org.slf4j.ext.XLoggerFactory;
46
47 /**
48  * The Class TestEventInstantiation.
49  *
50  * @author Liam Fallon (liam.fallon@ericsson.com)
51  */
52 public class TestEventInstantiation {
53     // Logger for this class
54     private static final XLogger logger = XLoggerFactory.getXLogger(TestEventInstantiation.class);
55
56     /**
57      * Test event instantiation.
58      *
59      * @throws ApexModelException on errors in handling Apex models
60      * @throws IOException Signals that an I/O exception has occurred.
61      * @throws ApexException the apex exception
62      */
63     @Test
64     public void testEventInstantiation() throws ApexModelException, IOException, ApexException {
65         final String xmlFileName = "xml/ApexModel_MVEL.xml";
66
67         logger.debug("Running TestEventInstantiation test  on file {} . . .", xmlFileName);
68
69         final AxPolicyModel apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
70         assertNotNull(apexPolicyModel);
71
72         final EngineParameters parameters = new EngineParameters();
73         parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters());
74
75         final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(apexPolicyModel.getKey());
76         apexEngine.updateModel(apexPolicyModel);
77         apexEngine.start();
78
79         final EnEvent event = apexEngine.createEvent(new AxArtifactKey("Event0000", "0.0.1"));
80
81         Object slogan1 = event.put("TestSlogan", "This is a slogan");
82         assertNull(slogan1);
83         slogan1 = event.get("TestSlogan");
84         assertNotNull(slogan1);
85         assertEquals("This is a slogan", slogan1);
86
87         Object mc1 = event.put("TestMatchCase", new Byte("4"));
88         assertNull(mc1);
89         mc1 = event.get("TestMatchCase");
90         assertNotNull(mc1);
91         assertEquals((byte) 4, mc1);
92
93         Object mc2 = event.put("TestMatchCase", new Byte("16"));
94         assertNotNull(mc2);
95         assertEquals((byte) 4, mc2);
96         mc2 = event.get("TestMatchCase");
97         assertNotNull(mc2);
98         assertEquals((byte) 16, mc2);
99
100         final Date timeNow = new Date();
101         Object timestamp1 = event.put("TestTimestamp", timeNow.getTime());
102         assertNull(timestamp1);
103         timestamp1 = event.get("TestTimestamp");
104         assertNotNull(timestamp1);
105         assertEquals(timeNow.getTime(), timestamp1);
106
107         final double temperature = 123.456789;
108         Object temp1 = event.put("TestTemperature", temperature);
109         assertNull(temp1);
110         temp1 = event.get("TestTemperature");
111         assertNotNull(temp1);
112         assertEquals(temperature, temp1);
113
114         Object value = event.put("TestMatchCase", null);
115         assertEquals(16, ((Byte) value).intValue());
116         value = event.get("TestMatchCase");
117         assertNull(value);
118
119         try {
120             event.put("TestMatchCase", "Hello");
121         } catch (final Exception e) {
122             assertEquals(
123                     "Event0000:0.0.1:NULL:TestMatchCase: object \"Hello\" of class \"java.lang.String\" not compatible with class \"java.lang.Byte\"",
124                     e.getMessage());
125         }
126
127         try {
128             event.put("TestMatchCase", 123.45);
129         } catch (final Exception e) {
130             assertEquals(
131                     "Event0000:0.0.1:NULL:TestMatchCase: object \"123.45\" of class \"java.lang.Double\" not compatible with class \"java.lang.Byte\"",
132                     e.getMessage());
133         }
134
135         event.put("TestMatchCase", new Byte("16"));
136
137         final String slogan2 = (String) event.get("TestSlogan");
138         assertNotNull(slogan2);
139         assertEquals("This is a slogan", slogan2);
140
141         final byte mc21 = (byte) event.get("TestMatchCase");
142         assertNotNull(mc21);
143         assertEquals(16, mc21);
144
145         final byte mc22 = (byte) event.get("TestMatchCase");
146         assertNotNull(mc22);
147         assertEquals((byte) 16, mc22);
148
149         final long timestamp2 = (Long) event.get("TestTimestamp");
150         assertNotNull(timestamp2);
151         assertEquals(timestamp2, timestamp1);
152
153         final double temp2 = (double) event.get("TestTemperature");
154         assertNotNull(temp2);
155         assertTrue(temp2 == 123.456789);
156
157         final Double temp3 = (Double) event.get("TestTemperature");
158         assertNotNull(temp3);
159         assertTrue(temp3 == 123.456789);
160
161         final Date aDate = new Date(1433453067123L);
162         final Map<String, Object> eventDataList = new HashMap<String, Object>();
163         eventDataList.put("TestSlogan", "This is a test slogan");
164         eventDataList.put("TestMatchCase", new Byte("123"));
165         eventDataList.put("TestTimestamp", aDate.getTime());
166         eventDataList.put("TestTemperature", 34.5445667);
167
168         event.putAll(eventDataList);
169
170         final String slogan3 = (String) event.get("TestSlogan");
171         assertNotNull(slogan3);
172         assertEquals("This is a test slogan", slogan3);
173
174         final byte mc31 = (byte) event.get("TestMatchCase");
175         assertNotNull(mc31);
176         assertEquals((byte) 123, mc31);
177
178         final long timestamp3 = (Long) event.get("TestTimestamp");
179         assertNotNull(timestamp3);
180         assertEquals(timestamp3, aDate.getTime());
181
182         final double temp4 = (double) event.get("TestTemperature");
183         assertNotNull(temp4);
184         assertTrue(temp4 == 34.5445667);
185
186         logger.debug(event.toString());
187     }
188 }