f8caf8c2f1fb2d2c2e862b6e115ce818e1b0ec37
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
5  *  Modifications Copyright (C) 2022 Bell Canada.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.event.protocol.xml;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Random;
34 import org.apache.commons.lang3.RandomStringUtils;
35 import org.junit.Test;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.service.engine.event.ApexEvent;
38 import org.onap.policy.apex.service.engine.event.ApexEventException;
39 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
40 import org.slf4j.ext.XLogger;
41 import org.slf4j.ext.XLoggerFactory;
42
43 /**
44  * The Class TestApexXMLEventHandlerURL.
45  *
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  */
48 public class XmlEventHandlerTest {
49     private static final XLogger logger = XLoggerFactory.getXLogger(XmlEventHandlerTest.class);
50
51     /**
52      * Test XML to apex event. Null value is passed as parameter.
53      *
54      * @throws ApexException on Apex event handling errors
55      */
56     @Test(expected = ApexException.class)
57     public void testApexEventToApexNullObject() throws ApexException {
58         final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
59         // This is only for code coverage stats. This method does nothing
60         xmlEventConverter.init(null);
61
62         xmlEventConverter.toApexEvent("XMLEventName", null);
63     }
64
65     /**
66      * Test XML to apex event. There is no string passed as parameter.
67      *
68      * @throws ApexException on Apex event handling errors
69      */
70     @Test(expected = ApexEventRuntimeException.class)
71     public void testApexEventToApexNotString() throws ApexException {
72         final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
73
74         xmlEventConverter.toApexEvent("XMLEventName", new Random().nextInt());
75     }
76
77     /**
78      * Test not valid XML to apex event.
79      *
80      * @throws ApexException on Apex event handling errors
81      */
82     @Test(expected = ApexException.class)
83     public void testApexEventToApexNotXml() throws ApexException {
84         final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
85
86         xmlEventConverter.toApexEvent("XMLEventName", RandomStringUtils.randomAlphabetic(25));
87     }
88
89     /**
90      * Test XML to apex event.
91      *
92      * @throws ApexException on Apex event handling errors
93      */
94     @Test
95     public void testXmltoApexEvent() throws ApexException {
96         try {
97             final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
98             assertNotNull(xmlEventConverter);
99
100             final String apexEventXmlStringIn = XmlEventGenerator.xmlEvent();
101
102             logger.debug("input event\n" + apexEventXmlStringIn);
103
104             for (final ApexEvent apexEvent : xmlEventConverter.toApexEvent("XMLEventName", apexEventXmlStringIn)) {
105                 assertNotNull(apexEvent);
106
107                 logger.debug(apexEvent.toString());
108
109                 assertTrue(apexEvent.getName().equals("Event0000") || apexEvent.getName().equals("Event0100"));
110                 assertEquals("0.0.1", apexEvent.getVersion());
111                 assertEquals("org.onap.policy.apex.sample.events", apexEvent.getNameSpace());
112                 assertEquals("test", apexEvent.getSource());
113                 assertEquals("apex", apexEvent.getTarget());
114                 assertTrue(apexEvent.get("TestSlogan").toString().startsWith("Test slogan for External Event"));
115
116                 final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected");
117                 assertNull(testMatchCaseSelected);
118             }
119         } catch (final Exception e) {
120             e.printStackTrace();
121             throw new ApexException("Exception reading Apex event xml file", e);
122         }
123     }
124
125     /**
126      * Test null as apex event to xml.
127      *
128      * @throws ApexEventException on Apex event handling errors
129      */
130     @Test(expected = ApexEventException.class)
131     public void testApexEventToXmlNullEvent() throws ApexEventException {
132         final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
133         xmlEventConverter.fromApexEvent(null);
134     }
135
136     /**
137      * Test apex event to xml.
138      *
139      * @throws ApexException on Apex event handling errors
140      */
141     @Test
142     public void testApexEventToXml() throws ApexException {
143         try {
144             final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
145             assertNotNull(xmlEventConverter);
146
147             final Date event0000StartTime = new Date();
148             final Map<String, Object> event0000DataMap = new HashMap<String, Object>();
149             event0000DataMap.put("TestSlogan", "This is a test slogan");
150             event0000DataMap.put("TestMatchCase", 12345);
151             event0000DataMap.put("TestTimestamp", event0000StartTime.getTime());
152             event0000DataMap.put("TestTemperature", 34.5445667);
153             event0000DataMap.put("NullValue", null);
154
155             final ApexEvent apexEvent0000 =
156                     new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex", "");
157             apexEvent0000.putAll(event0000DataMap);
158
159             final String apexEvent0000XmlString = xmlEventConverter.fromApexEvent(apexEvent0000);
160
161             logger.debug(apexEvent0000XmlString);
162
163             assertTrue(apexEvent0000XmlString.contains("<name>Event0000</name>"));
164             assertTrue(apexEvent0000XmlString.contains("<version>0.0.1</version>"));
165             assertTrue(apexEvent0000XmlString.contains("<value>This is a test slogan</value>"));
166             assertTrue(apexEvent0000XmlString.contains("<value>12345</value>"));
167             assertTrue(apexEvent0000XmlString.contains("<value></value>"));
168             assertTrue(apexEvent0000XmlString.contains("<value>" + event0000StartTime.getTime() + "</value>"));
169             assertTrue(apexEvent0000XmlString.contains("<value>34.5445667</value>"));
170
171             final Date event0004StartTime = new Date(1434363272000L);
172             final Map<String, Object> event0004DataMap = new HashMap<String, Object>();
173             event0004DataMap.put("TestSlogan", "Test slogan for External Event");
174             event0004DataMap.put("TestMatchCase", Integer.valueOf(2));
175             event0004DataMap.put("TestTimestamp", Long.valueOf(event0004StartTime.getTime()));
176             event0004DataMap.put("TestTemperature", Double.valueOf(1064.43));
177             event0004DataMap.put("TestMatchCaseSelected", Integer.valueOf(2));
178             event0004DataMap.put("TestMatchStateTime", Long.valueOf(1434370506078L));
179             event0004DataMap.put("TestEstablishCaseSelected", Integer.valueOf(0));
180             event0004DataMap.put("TestEstablishStateTime", Long.valueOf(1434370506085L));
181             event0004DataMap.put("TestDecideCaseSelected", Integer.valueOf(3));
182             event0004DataMap.put("TestDecideStateTime", Long.valueOf(1434370506092L));
183             event0004DataMap.put("TestActCaseSelected", Integer.valueOf(2));
184             event0004DataMap.put("TestActStateTime", Long.valueOf(1434370506095L));
185
186             final ApexEvent apexEvent0004 = new ApexEvent("Event0004", "0.0.1",
187                     "org.onap.policy.apex.domains.sample.events", "test", "apex", "");
188             apexEvent0004.putAll(event0004DataMap);
189
190             final String apexEvent0004XmlString = xmlEventConverter.fromApexEvent(apexEvent0004);
191
192             logger.debug(apexEvent0004XmlString);
193
194             assertTrue(apexEvent0004XmlString.contains("<name>Event0004</name>"));
195             assertTrue(apexEvent0004XmlString.contains("<version>0.0.1</version>"));
196             assertTrue(apexEvent0004XmlString.contains("<value>Test slogan for External Event</value>"));
197             assertTrue(apexEvent0004XmlString.contains("<value>1434370506078</value>"));
198             assertTrue(apexEvent0004XmlString.contains("<value>" + event0004StartTime.getTime() + "</value>"));
199             assertTrue(apexEvent0004XmlString.contains("<value>1064.43</value>"));
200         } catch (final Exception e) {
201             e.printStackTrace();
202             throw new ApexException("Exception reading Apex event xml file", e);
203         }
204     }
205 }