320d7ba224919b046081677703af04394b781fe6
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import org.junit.Test;
33 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.HeaderDelimitedTextBlockReader;
34 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock;
35
36 /**
37  * The Class TestXmlTaggedEventConsumer.
38  */
39 public class XmlTaggedEventConsumerTest {
40
41     /**
42      * Test garbage text line.
43      *
44      * @throws IOException Signals that an I/O exception has occurred.
45      */
46     @Test
47     public void testGarbageTextLine() throws IOException {
48         verifyNull("testGarbageTextLine", "hello there");
49     }
50
51     /**
52      * Test partial event line.
53      *
54      * @throws IOException Signals that an I/O exception has occurred.
55      */
56     @Test
57     public void testPartialEventLine() throws IOException {
58         verifyNull("testPartialEventLine", "1469781869268</TestTimestamp></MainTag>");
59     }
60
61     /**
62      * Test full event line.
63      *
64      * @throws IOException Signals that an I/O exception has occurred.
65      */
66     @Test
67     public void testFullEventLine() throws IOException {
68         verifyLine("testFullEventLine", "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>",
69                         "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>");
70     }
71
72     /**
73      * Test full event garbage before line.
74      *
75      * @throws IOException Signals that an I/O exception has occurred.
76      */
77     @Test
78     public void testFullEventGarbageBeforeLine() throws IOException {
79         verifyEnd("testFullEventGarbageBeforeLine",
80                         "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>");
81     }
82
83     /**
84      * Test full event garbage before after line.
85      *
86      * @throws IOException Signals that an I/O exception has occurred.
87      */
88     @Test
89     public void testFullEventGarbageBeforeAfterLine() throws IOException {
90         verifyEnd("testFullEventGarbageBeforeAfterLine",
91                         "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish");
92     }
93
94     /**
95      * Test full event garbage after line.
96      *
97      * @throws IOException Signals that an I/O exception has occurred.
98      */
99     @Test
100     public void testFullEventGarbageAfterLine() throws IOException {
101         verifyLine("testFullEventGarbageAfterLine",
102                         "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish",
103                         "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish");
104     }
105
106     /**
107      * Test garbage text multi line.
108      *
109      * @throws IOException Signals that an I/O exception has occurred.
110      */
111     @Test
112     public void testGarbageTextMultiLine() throws IOException {
113         verifyEnd("testGarbageTextMultiLine", "hello\nthere");
114     }
115
116     /**
117      * Test partial event multi line.
118      *
119      * @throws IOException Signals that an I/O exception has occurred.
120      */
121     @Test
122     public void testPartialEventMultiLine() throws IOException {
123         verifyEnd("testPartialEventMultiLine", "1469781869268\n</TestTimestamp>\n</MainTag>");
124     }
125
126     /**
127      * Test full event multi line.
128      *
129      * @throws IOException Signals that an I/O exception has occurred.
130      */
131     @Test
132     public void testFullEventMultiLine() throws IOException {
133         verifyLine("testFullEventMultiLine",
134                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\n\n",
135                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>");
136     }
137
138     /**
139      * Test full event garbage before multi line.
140      *
141      * @throws IOException Signals that an I/O exception has occurred.
142      */
143     @Test
144     public void testFullEventGarbageBeforeMultiLine() throws IOException {
145         verifyLine("testFullEventGarbageBeforeMultiLine",
146                 "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\n\n",
147                 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>");
148     }
149
150     /**
151      * Test full event garbage before after multi line.
152      *
153      * @throws IOException Signals that an I/O exception has occurred.
154      */
155     @Test
156     public void testFullEventGarbageBeforeAfterMultiLine() throws IOException {
157         String garbageString = "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>"
158                         + "\n</MainTag>\nRubbish\n\n";
159
160         verifyLine("testFullEventsGarbageAfterLine", garbageString,
161                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish");
162     }
163
164     /**
165      * Test full event garbage after multi line.
166      *
167      * @throws IOException Signals that an I/O exception has occurred.
168      */
169     @Test
170     public void testFullEventGarbageAfterMultiLine() throws IOException {
171         verifyLine("testFullEventGarbageAfterMultiLine",
172                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
173                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish");
174     }
175
176     /**
177      * Test partial events line.
178      *
179      * @throws IOException Signals that an I/O exception has occurred.
180      */
181     @Test
182     public void testPartialEventsLine() throws IOException {
183         verifyEnd("testPartialEventsLine", "1469781869268</TestTimestamp></MainTag><?xml><MainTag>"
184                         + "<TestTimestamp>1469781869268</TestTimestamp>");
185     }
186
187     /**
188      * Test full events garbage before line.
189      *
190      * @throws IOException Signals that an I/O exception has occurred.
191      */
192     @Test
193     public void testFullEventsGarbageBeforeLine() throws IOException {
194         verifyEnd("testPartialEventsLine",
195                         "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>"
196                                         + "<?xml><MainTag><TestTimestamp>");
197     }
198
199     /**
200      * Test full events garbage before after line.
201      *
202      * @throws IOException Signals that an I/O exception has occurred.
203      */
204     @Test
205     public void testFullEventsGarbageBeforeAfterLine() throws IOException {
206         verifyEnd("testFullEventGarbageBeforeLine", "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp>"
207                         + "</MainTag>Rubbish<?xml><MainTag><TestTimestamp>\nRefuse");
208     }
209
210     /**
211      * Test full events garbage after line.
212      *
213      * @throws IOException Signals that an I/O exception has occurred.
214      */
215     @Test
216     public void testFullEventsGarbageAfterLine() throws IOException {
217         String garbageString = "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp>"
218                         + "</MainTag>Rubbish<?xml><MainTag><TestTimestamp>Refuse";
219
220         verifyLine("testFullEventsGarbageAfterLine", garbageString, garbageString);
221     }
222
223     /**
224      * Test partial events multi line.
225      *
226      * @throws IOException Signals that an I/O exception has occurred.
227      */
228     @Test
229     public void testPartialEventsMultiLine() throws IOException {
230         verifyLine("testPartialEventsMultiLine",
231                         "1469781869268\n</TestTimestamp>\n</MainTag>\n<?xml>\n<MainTag>\n<TestTimestamp>",
232                         "<?xml>\n<MainTag>\n<TestTimestamp>");
233     }
234
235     /**
236      * Test full events multi line.
237      *
238      * @throws IOException Signals that an I/O exception has occurred.
239      */
240     @Test
241     public void testFullEventsMultiLine() throws IOException {
242         String garbageString = "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n"
243                         + "</MainTag>\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\n";
244
245         verifyLines("testFullEventsMultiLine", garbageString,
246                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
247                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>");
248     }
249
250     /**
251      * Test full events garbage before multi line.
252      *
253      * @throws IOException Signals that an I/O exception has occurred.
254      */
255     @Test
256     public void testFullEventsGarbageBeforeMultiLine() throws IOException {
257         String garbageString = "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n"
258                         + "</MainTag>\n\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\n";
259
260         verifyLines("testFullEventsGarbageBeforeMultiLine", garbageString,
261                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
262                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>");
263     }
264
265     /**
266      * Test full events garbage before after multi line.
267      *
268      * @throws IOException Signals that an I/O exception has occurred.
269      */
270     @Test
271     public void testFullEventsGarbageBeforeAfterMultiLine() throws IOException {
272         String garbageString = "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n"
273                         + "</MainTag>\nRubbish\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n"
274                         + "</MainTag>\nRefuse\n";
275
276         verifyLines("testFullEventsGarbageBeforeAfterMultiLine", garbageString,
277                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
278                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRefuse");
279     }
280
281     /**
282      * Test full events garbage after multi line.
283      *
284      * @throws IOException Signals that an I/O exception has occurred.
285      */
286     @Test
287     public void testFullEventsGarbageAfterMultiLine() throws IOException {
288         verifyLine("testFullEventsGarbageAfterMultiLine",
289                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
290                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish");
291     }
292
293     private void verifyNull(String testName, String xml) throws IOException {
294         final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes());
295
296         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
297         xmlTaggedReader.init(xmlInputStream);
298
299         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
300         assertNull(testName, textBlock.getText());
301         assertTrue(testName, textBlock.isEndOfText());
302     }
303
304     private void verifyLine(String testName, String xml, String expected) throws IOException {
305         final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes());
306
307         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
308         xmlTaggedReader.init(xmlInputStream);
309
310         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
311         assertEquals(testName, expected, textBlock.getText());
312         assertTrue(testName, textBlock.isEndOfText());
313     }
314
315     private void verifyLines(String testName, String xml, String expected, String expected2) throws IOException {
316         final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes());
317
318         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
319         xmlTaggedReader.init(xmlInputStream);
320
321         TextBlock textBlock = xmlTaggedReader.readTextBlock();
322         assertEquals(testName, expected, textBlock.getText());
323         assertFalse(testName, textBlock.isEndOfText());
324
325         textBlock = xmlTaggedReader.readTextBlock();
326         assertEquals(testName, expected2, textBlock.getText());
327         assertTrue(testName, textBlock.isEndOfText());
328     }
329
330     private void verifyEnd(String testName, String xml) throws IOException {
331         final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes());
332
333         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
334         xmlTaggedReader.init(xmlInputStream);
335
336         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
337         assertTrue(testName, textBlock.isEndOfText());
338     }
339 }