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