summaryrefslogtreecommitdiffstats
path: root/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-xml/src/test/java/org/onap/policy/apex/plugins/event/protocol/xml/XmlEventHandlerTest.java
blob: 609b0471b7ed75a52e457d0a85ecba443d9f74a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.plugins.event.protocol.xml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.service.engine.event.ApexEvent;
import org.onap.policy.apex.service.engine.event.ApexEventException;
import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * The Class TestApexXMLEventHandlerURL.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class XmlEventHandlerTest {
    private static final XLogger logger = XLoggerFactory.getXLogger(XmlEventHandlerTest.class);

    /**
     * Test XML to apex event. Null value is passed as parameter.
     *
     * @throws ApexException on Apex event handling errors
     */
    @Test(expected = ApexException.class)
    public void testApexEventToApexNullObject() throws ApexException {
        final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
        // This is only for code coverage stats. This method does nothing
        xmlEventConverter.init(null);

        xmlEventConverter.toApexEvent("XMLEventName", null);
    }

    /**
     * Test XML to apex event. There is no string passed as parameter.
     *
     * @throws ApexException on Apex event handling errors
     */
    @Test(expected = ApexEventRuntimeException.class)
    public void testApexEventToApexNotString() throws ApexException {
        final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();

        xmlEventConverter.toApexEvent("XMLEventName", new Random().nextInt());
    }

    /**
     * Test not valid XML to apex event.
     *
     * @throws ApexException on Apex event handling errors
     */
    @Test(expected = ApexException.class)
    public void testApexEventToApexNotXml() throws ApexException {
        final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();

        xmlEventConverter.toApexEvent("XMLEventName", RandomStringUtils.randomAlphabetic(25));
    }

    /**
     * Test XML to apex event.
     *
     * @throws ApexException on Apex event handling errors
     */
    @Test
    public void testXmltoApexEvent() throws ApexException {
        try {
            final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
            assertNotNull(xmlEventConverter);

            final String apexEventXmlStringIn = XmlEventGenerator.xmlEvent();

            logger.debug("input event\n" + apexEventXmlStringIn);

            for (final ApexEvent apexEvent : xmlEventConverter.toApexEvent("XMLEventName", apexEventXmlStringIn)) {
                assertNotNull(apexEvent);

                logger.debug(apexEvent.toString());

                assertTrue(apexEvent.getName().equals("Event0000") || apexEvent.getName().equals("Event0100"));
                assertEquals("0.0.1", apexEvent.getVersion());
                assertEquals("org.onap.policy.apex.sample.events", apexEvent.getNameSpace());
                assertEquals("test", apexEvent.getSource());
                assertEquals("apex", apexEvent.getTarget());
                assertTrue(apexEvent.get("TestSlogan").toString().startsWith("Test slogan for External Event"));

                final Object testMatchCaseSelected = apexEvent.get("TestMatchCaseSelected");
                assertNull(testMatchCaseSelected);
            }
        } catch (final Exception e) {
            e.printStackTrace();
            throw new ApexException("Exception reading Apex event xml file", e);
        }
    }

    /**
     * Test null as apex event to xml.
     *
     * @throws ApexEventException on Apex event handling errors
     */
    @Test(expected = ApexEventException.class)
    public void testApexEventToXmlNullEvent() throws ApexEventException {
        final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
        xmlEventConverter.fromApexEvent(null);
    }

    /**
     * Test apex event to xml.
     *
     * @throws ApexException on Apex event handling errors
     */
    @Test
    public void testApexEventToXml() throws ApexException {
        try {
            final Apex2XmlEventConverter xmlEventConverter = new Apex2XmlEventConverter();
            assertNotNull(xmlEventConverter);

            final Date event0000StartTime = new Date();
            final Map<String, Object> event0000DataMap = new HashMap<String, Object>();
            event0000DataMap.put("TestSlogan", "This is a test slogan");
            event0000DataMap.put("TestMatchCase", 12345);
            event0000DataMap.put("TestTimestamp", event0000StartTime.getTime());
            event0000DataMap.put("TestTemperature", 34.5445667);
            event0000DataMap.put("NullValue", null);

            final ApexEvent apexEvent0000 =
                    new ApexEvent("Event0000", "0.0.1", "org.onap.policy.apex.sample.events", "test", "apex");
            apexEvent0000.putAll(event0000DataMap);

            final String apexEvent0000XmlString = xmlEventConverter.fromApexEvent(apexEvent0000);

            logger.debug(apexEvent0000XmlString);

            assertTrue(apexEvent0000XmlString.contains("<name>Event0000</name>"));
            assertTrue(apexEvent0000XmlString.contains("<version>0.0.1</version>"));
            assertTrue(apexEvent0000XmlString.contains("<value>This is a test slogan</value>"));
            assertTrue(apexEvent0000XmlString.contains("<value>12345</value>"));
            assertTrue(apexEvent0000XmlString.contains("<value></value>"));
            assertTrue(apexEvent0000XmlString.contains("<value>" + event0000StartTime.getTime() + "</value>"));
            assertTrue(apexEvent0000XmlString.contains("<value>34.5445667</value>"));

            final Date event0004StartTime = new Date(1434363272000L);
            final Map<String, Object> event0004DataMap = new HashMap<String, Object>();
            event0004DataMap.put("TestSlogan", "Test slogan for External Event");
            event0004DataMap.put("TestMatchCase", Integer.valueOf(2));
            event0004DataMap.put("TestTimestamp", Long.valueOf(event0004StartTime.getTime()));
            event0004DataMap.put("TestTemperature", Double.valueOf(1064.43));
            event0004DataMap.put("TestMatchCaseSelected", Integer.valueOf(2));
            event0004DataMap.put("TestMatchStateTime", Long.valueOf(1434370506078L));
            event0004DataMap.put("TestEstablishCaseSelected", Integer.valueOf(0));
            event0004DataMap.put("TestEstablishStateTime", Long.valueOf(1434370506085L));
            event0004DataMap.put("TestDecideCaseSelected", Integer.valueOf(3));
            event0004DataMap.put("TestDecideStateTime", Long.valueOf(1434370506092L));
            event0004DataMap.put("TestActCaseSelected", Integer.valueOf(2));
            event0004DataMap.put("TestActStateTime", Long.valueOf(1434370506095L));

            final ApexEvent apexEvent0004 =
                    new ApexEvent("Event0004", "0.0.1", "org.onap.policy.apex.domains.sample.events", "test", "apex");
            apexEvent0004.putAll(event0004DataMap);

            final String apexEvent0004XmlString = xmlEventConverter.fromApexEvent(apexEvent0004);

            logger.debug(apexEvent0004XmlString);

            assertTrue(apexEvent0004XmlString.contains("<name>Event0004</name>"));
            assertTrue(apexEvent0004XmlString.contains("<version>0.0.1</version>"));
            assertTrue(apexEvent0004XmlString.contains("<value>Test slogan for External Event</value>"));
            assertTrue(apexEvent0004XmlString.contains("<value>1434370506078</value>"));
            assertTrue(apexEvent0004XmlString.contains("<value>" + event0004StartTime.getTime() + "</value>"));
            assertTrue(apexEvent0004XmlString.contains("<value>1064.43</value>"));
        } catch (final Exception e) {
            e.printStackTrace();
            throw new ApexException("Exception reading Apex event xml file", e);
        }
    }
}