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