aeb37331ab54d272d91da13b23c35d7afdb204d2
[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 / XmlTaggedEventConsumerTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.protocol.xml;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31
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         final InputStream xmlInputStream = new ByteArrayInputStream("hello there".getBytes());
49
50         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
51         xmlTaggedReader.init(xmlInputStream);
52
53         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
54         assertNull(textBlock.getText());
55         assertTrue(textBlock.isEndOfText());
56     }
57
58     /**
59      * Test partial event line.
60      *
61      * @throws IOException Signals that an I/O exception has occurred.
62      */
63     @Test
64     public void testPartialEventLine() throws IOException {
65         final InputStream xmlInputStream = new ByteArrayInputStream(
66                         "1469781869268</TestTimestamp></MainTag>".getBytes());
67
68         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
69         xmlTaggedReader.init(xmlInputStream);
70
71         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
72         assertNull(textBlock.getText());
73         assertTrue(textBlock.isEndOfText());
74     }
75
76     /**
77      * Test full event line.
78      *
79      * @throws IOException Signals that an I/O exception has occurred.
80      */
81     @Test
82     public void testFullEventLine() throws IOException {
83         final InputStream xmlInputStream = new ByteArrayInputStream(
84                         "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>".getBytes());
85
86         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
87         xmlTaggedReader.init(xmlInputStream);
88
89         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
90         assertEquals("<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>", textBlock.getText());
91         assertTrue(textBlock.isEndOfText());
92     }
93
94     /**
95      * Test full event garbage before line.
96      *
97      * @throws IOException Signals that an I/O exception has occurred.
98      */
99     @Test
100     public void testFullEventGarbageBeforeLine() throws IOException {
101         final InputStream xmlInputStream = new ByteArrayInputStream(
102                         "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>".getBytes());
103
104         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
105         xmlTaggedReader.init(xmlInputStream);
106
107         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
108         assertTrue(textBlock.isEndOfText());
109     }
110
111     /**
112      * Test full event garbage before after line.
113      *
114      * @throws IOException Signals that an I/O exception has occurred.
115      */
116     @Test
117     public void testFullEventGarbageBeforeAfterLine() throws IOException {
118         final InputStream xmlInputStream = new ByteArrayInputStream(
119                         "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish"
120                                         .getBytes());
121
122         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
123         xmlTaggedReader.init(xmlInputStream);
124
125         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
126         assertTrue(textBlock.isEndOfText());
127     }
128
129     /**
130      * Test full event garbage after line.
131      *
132      * @throws IOException Signals that an I/O exception has occurred.
133      */
134     @Test
135     public void testFullEventGarbageAfterLine() throws IOException {
136         final InputStream xmlInputStream = new ByteArrayInputStream(
137                         "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish".getBytes());
138
139         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
140         xmlTaggedReader.init(xmlInputStream);
141
142         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
143         assertEquals("<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish",
144                         textBlock.getText());
145         assertTrue(textBlock.isEndOfText());
146     }
147
148     /**
149      * Test garbage text multi line.
150      *
151      * @throws IOException Signals that an I/O exception has occurred.
152      */
153     @Test
154     public void testGarbageTextMultiLine() throws IOException {
155         final InputStream xmlInputStream = new ByteArrayInputStream("hello\nthere".getBytes());
156
157         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
158         xmlTaggedReader.init(xmlInputStream);
159
160         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
161         assertTrue(textBlock.isEndOfText());
162     }
163
164     /**
165      * Test partial event multi line.
166      *
167      * @throws IOException Signals that an I/O exception has occurred.
168      */
169     @Test
170     public void testPartialEventMultiLine() throws IOException {
171         final InputStream xmlInputStream = new ByteArrayInputStream(
172                         "1469781869268\n</TestTimestamp>\n</MainTag>".getBytes());
173
174         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
175         xmlTaggedReader.init(xmlInputStream);
176
177         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
178         assertTrue(textBlock.isEndOfText());
179     }
180
181     /**
182      * Test full event multi line.
183      *
184      * @throws IOException Signals that an I/O exception has occurred.
185      */
186     @Test
187     public void testFullEventMultiLine() throws IOException {
188         final InputStream xmlInputStream = new ByteArrayInputStream(
189                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\n\n".getBytes());
190
191         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
192         xmlTaggedReader.init(xmlInputStream);
193
194         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
195         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
196                         textBlock.getText());
197         assertTrue(textBlock.isEndOfText());
198     }
199
200     /**
201      * Test full event garbage before multi line.
202      *
203      * @throws IOException Signals that an I/O exception has occurred.
204      */
205     @Test
206     public void testFullEventGarbageBeforeMultiLine() throws IOException {
207         final InputStream xmlInputStream = new ByteArrayInputStream(
208                         "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\n\n"
209                                         .getBytes());
210
211         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
212         xmlTaggedReader.init(xmlInputStream);
213
214         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
215         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
216                         textBlock.getText());
217         assertTrue(textBlock.isEndOfText());
218     }
219
220     /**
221      * Test full event garbage before after multi line.
222      *
223      * @throws IOException Signals that an I/O exception has occurred.
224      */
225     @Test
226     public void testFullEventGarbageBeforeAfterMultiLine() throws IOException {
227         String garbageString = "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>"
228                         + "\n</MainTag>\nRubbish\n\n";
229         final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes());
230
231         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
232         xmlTaggedReader.init(xmlInputStream);
233
234         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
235         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
236                         textBlock.getText());
237         assertTrue(textBlock.isEndOfText());
238     }
239
240     /**
241      * Test full event garbage after multi line.
242      *
243      * @throws IOException Signals that an I/O exception has occurred.
244      */
245     @Test
246     public void testFullEventGarbageAfterMultiLine() throws IOException {
247         final InputStream xmlInputStream = new ByteArrayInputStream(
248                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish"
249                                         .getBytes());
250
251         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
252         xmlTaggedReader.init(xmlInputStream);
253
254         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
255         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
256                         textBlock.getText());
257         assertTrue(textBlock.isEndOfText());
258     }
259
260     /**
261      * Test partial events line.
262      *
263      * @throws IOException Signals that an I/O exception has occurred.
264      */
265     @Test
266     public void testPartialEventsLine() throws IOException {
267         String garbageString = "1469781869268</TestTimestamp></MainTag><?xml><MainTag>"
268                         + "<TestTimestamp>1469781869268</TestTimestamp>";
269         final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes());
270
271         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
272         xmlTaggedReader.init(xmlInputStream);
273
274         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
275         assertTrue(textBlock.isEndOfText());
276     }
277
278     /**
279      * Test full events garbage before line.
280      *
281      * @throws IOException Signals that an I/O exception has occurred.
282      */
283     @Test
284     public void testFullEventsGarbageBeforeLine() throws IOException {
285         String garbageString = "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>"
286                         + "<?xml><MainTag><TestTimestamp>";
287         final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes());
288
289         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
290         xmlTaggedReader.init(xmlInputStream);
291
292         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
293         assertTrue(textBlock.isEndOfText());
294     }
295
296     /**
297      * Test full events garbage before after line.
298      *
299      * @throws IOException Signals that an I/O exception has occurred.
300      */
301     @Test
302     public void testFullEventsGarbageBeforeAfterLine() throws IOException {
303         String garbageString = "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp>"
304                         + "</MainTag>Rubbish<?xml><MainTag><TestTimestamp>\nRefuse";
305         final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes());
306
307         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
308         xmlTaggedReader.init(xmlInputStream);
309
310         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
311         assertTrue(textBlock.isEndOfText());
312     }
313
314     /**
315      * Test full events garbage after line.
316      *
317      * @throws IOException Signals that an I/O exception has occurred.
318      */
319     @Test
320     public void testFullEventsGarbageAfterLine() throws IOException {
321         String garbageString = "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp>"
322                         + "</MainTag>Rubbish<?xml><MainTag><TestTimestamp>Refuse";
323         final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes());
324
325         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
326         xmlTaggedReader.init(xmlInputStream);
327
328         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
329         assertEquals(textBlock.getText(), garbageString);
330         assertTrue(textBlock.isEndOfText());
331     }
332
333     /**
334      * Test partial events multi line.
335      *
336      * @throws IOException Signals that an I/O exception has occurred.
337      */
338     @Test
339     public void testPartialEventsMultiLine() throws IOException {
340         final InputStream xmlInputStream = new ByteArrayInputStream(
341                         "1469781869268\n</TestTimestamp>\n</MainTag>\n<?xml>\n<MainTag>\n<TestTimestamp>".getBytes());
342
343         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
344         xmlTaggedReader.init(xmlInputStream);
345
346         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
347         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>", textBlock.getText());
348         assertTrue(textBlock.isEndOfText());
349     }
350
351     /**
352      * Test full events multi line.
353      *
354      * @throws IOException Signals that an I/O exception has occurred.
355      */
356     @Test
357     public void testFullEventsMultiLine() throws IOException {
358         String garbageString = "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n"
359                         + "</MainTag>\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\n";
360         final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes());
361
362         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
363         xmlTaggedReader.init(xmlInputStream);
364
365         TextBlock textBlock = xmlTaggedReader.readTextBlock();
366         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
367                         textBlock.getText());
368         assertFalse(textBlock.isEndOfText());
369
370         textBlock = xmlTaggedReader.readTextBlock();
371         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
372                         textBlock.getText());
373         assertTrue(textBlock.isEndOfText());
374     }
375
376     /**
377      * Test full events garbage before multi line.
378      *
379      * @throws IOException Signals that an I/O exception has occurred.
380      */
381     @Test
382     public void testFullEventsGarbageBeforeMultiLine() throws IOException {
383         String garbageString = "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n"
384                         + "</MainTag>\n\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\n";
385         final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes());
386
387         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
388         xmlTaggedReader.init(xmlInputStream);
389
390         TextBlock textBlock = xmlTaggedReader.readTextBlock();
391         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
392                         textBlock.getText());
393         assertFalse(textBlock.isEndOfText());
394
395         textBlock = xmlTaggedReader.readTextBlock();
396         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
397                         textBlock.getText());
398         assertTrue(textBlock.isEndOfText());
399     }
400
401     /**
402      * Test full events garbage before after multi line.
403      *
404      * @throws IOException Signals that an I/O exception has occurred.
405      */
406     @Test
407     public void testFullEventsGarbageBeforeAfterMultiLine() throws IOException {
408         String garbageString = "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n"
409                         + "</MainTag>\nRubbish\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n"
410                         + "</MainTag>\nRefuse\n";
411         final InputStream xmlInputStream = new ByteArrayInputStream(garbageString.getBytes());
412
413         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
414         xmlTaggedReader.init(xmlInputStream);
415
416         TextBlock textBlock = xmlTaggedReader.readTextBlock();
417         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
418                         textBlock.getText());
419         assertFalse(textBlock.isEndOfText());
420
421         textBlock = xmlTaggedReader.readTextBlock();
422         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRefuse",
423                         textBlock.getText());
424         assertTrue(textBlock.isEndOfText());
425     }
426
427     /**
428      * Test full events garbage after multi line.
429      *
430      * @throws IOException Signals that an I/O exception has occurred.
431      */
432     @Test
433     public void testFullEventsGarbageAfterMultiLine() throws IOException {
434         final InputStream xmlInputStream = new ByteArrayInputStream(
435                         "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish"
436                                         .getBytes());
437
438         final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
439         xmlTaggedReader.init(xmlInputStream);
440
441         final TextBlock textBlock = xmlTaggedReader.readTextBlock();
442         assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
443                         textBlock.getText());
444         assertTrue(textBlock.isEndOfText());
445     }
446 }