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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.executor.test.script.event;
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;
28 import java.io.IOException;
29 import java.util.Date;
30 import java.util.HashMap;
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;
48 * The Class TestEventInstantiation.
50 * @author Liam Fallon (liam.fallon@ericsson.com)
52 public class TestEventInstantiation {
53 // Logger for this class
54 private static final XLogger logger = XLoggerFactory.getXLogger(TestEventInstantiation.class);
57 * Test event instantiation.
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
64 public void testEventInstantiation() throws ApexModelException, IOException, ApexException {
65 final String xmlFileName = "xml/ApexModel_MVEL.xml";
67 logger.debug("Running TestEventInstantiation test on file {} . . .", xmlFileName);
69 final AxPolicyModel apexPolicyModel = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
70 assertNotNull(apexPolicyModel);
72 final EngineParameters parameters = new EngineParameters();
73 parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters());
75 final ApexEngine apexEngine = new ApexEngineFactory().createApexEngine(apexPolicyModel.getKey());
76 apexEngine.updateModel(apexPolicyModel);
79 final EnEvent event = apexEngine.createEvent(new AxArtifactKey("Event0000", "0.0.1"));
81 Object slogan1 = event.put("TestSlogan", "This is a slogan");
83 slogan1 = event.get("TestSlogan");
84 assertNotNull(slogan1);
85 assertEquals("This is a slogan", slogan1);
87 Object mc1 = event.put("TestMatchCase", new Byte("4"));
89 mc1 = event.get("TestMatchCase");
91 assertEquals((byte) 4, mc1);
93 Object mc2 = event.put("TestMatchCase", new Byte("16"));
95 assertEquals((byte) 4, mc2);
96 mc2 = event.get("TestMatchCase");
98 assertEquals((byte) 16, mc2);
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);
107 final double temperature = 123.456789;
108 Object temp1 = event.put("TestTemperature", temperature);
110 temp1 = event.get("TestTemperature");
111 assertNotNull(temp1);
112 assertEquals(temperature, temp1);
114 Object value = event.put("TestMatchCase", null);
115 assertEquals(16, ((Byte) value).intValue());
116 value = event.get("TestMatchCase");
120 event.put("TestMatchCase", "Hello");
121 } catch (final Exception e) {
123 "Event0000:0.0.1:NULL:TestMatchCase: object \"Hello\" of class \"java.lang.String\" not compatible with class \"java.lang.Byte\"",
128 event.put("TestMatchCase", 123.45);
129 } catch (final Exception e) {
131 "Event0000:0.0.1:NULL:TestMatchCase: object \"123.45\" of class \"java.lang.Double\" not compatible with class \"java.lang.Byte\"",
135 event.put("TestMatchCase", new Byte("16"));
137 final String slogan2 = (String) event.get("TestSlogan");
138 assertNotNull(slogan2);
139 assertEquals("This is a slogan", slogan2);
141 final byte mc21 = (byte) event.get("TestMatchCase");
143 assertEquals(16, mc21);
145 final byte mc22 = (byte) event.get("TestMatchCase");
147 assertEquals((byte) 16, mc22);
149 final long timestamp2 = (Long) event.get("TestTimestamp");
150 assertNotNull(timestamp2);
151 assertEquals(timestamp2, timestamp1);
153 final double temp2 = (double) event.get("TestTemperature");
154 assertNotNull(temp2);
155 assertTrue(temp2 == 123.456789);
157 final Double temp3 = (Double) event.get("TestTemperature");
158 assertNotNull(temp3);
159 assertTrue(temp3 == 123.456789);
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);
168 event.putAll(eventDataList);
170 final String slogan3 = (String) event.get("TestSlogan");
171 assertNotNull(slogan3);
172 assertEquals("This is a test slogan", slogan3);
174 final byte mc31 = (byte) event.get("TestMatchCase");
176 assertEquals((byte) 123, mc31);
178 final long timestamp3 = (Long) event.get("TestTimestamp");
179 assertNotNull(timestamp3);
180 assertEquals(timestamp3, aDate.getTime());
182 final double temp4 = (double) event.get("TestTemperature");
183 assertNotNull(temp4);
184 assertTrue(temp4 == 34.5445667);
186 logger.debug(event.toString());