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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.protocol.xml;
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;
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 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;
37 * The Class TestXmlTaggedEventConsumer.
39 public class XmlTaggedEventConsumerTest {
42 * Test garbage text line.
44 * @throws IOException Signals that an I/O exception has occurred.
47 public void testGarbageTextLine() throws IOException {
48 final InputStream xmlInputStream = new ByteArrayInputStream("hello there".getBytes());
50 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
51 xmlTaggedReader.init(xmlInputStream);
53 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
54 assertNull(textBlock.getText());
55 assertTrue(textBlock.isEndOfText());
59 * Test partial event line.
61 * @throws IOException Signals that an I/O exception has occurred.
64 public void testPartialEventLine() throws IOException {
65 final InputStream xmlInputStream = new ByteArrayInputStream(
66 "1469781869268</TestTimestamp></MainTag>".getBytes());
68 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
69 xmlTaggedReader.init(xmlInputStream);
71 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
72 assertNull(textBlock.getText());
73 assertTrue(textBlock.isEndOfText());
77 * Test full event line.
79 * @throws IOException Signals that an I/O exception has occurred.
82 public void testFullEventLine() throws IOException {
83 final InputStream xmlInputStream = new ByteArrayInputStream(
84 "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>".getBytes());
86 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
87 xmlTaggedReader.init(xmlInputStream);
89 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
90 assertEquals("<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>", textBlock.getText());
91 assertTrue(textBlock.isEndOfText());
95 * Test full event garbage before line.
97 * @throws IOException Signals that an I/O exception has occurred.
100 public void testFullEventGarbageBeforeLine() throws IOException {
101 final InputStream xmlInputStream = new ByteArrayInputStream(
102 "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>".getBytes());
104 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
105 xmlTaggedReader.init(xmlInputStream);
107 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
108 assertTrue(textBlock.isEndOfText());
112 * Test full event garbage before after line.
114 * @throws IOException Signals that an I/O exception has occurred.
117 public void testFullEventGarbageBeforeAfterLine() throws IOException {
118 final InputStream xmlInputStream = new ByteArrayInputStream(
119 "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish"
122 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
123 xmlTaggedReader.init(xmlInputStream);
125 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
126 assertTrue(textBlock.isEndOfText());
130 * Test full event garbage after line.
132 * @throws IOException Signals that an I/O exception has occurred.
135 public void testFullEventGarbageAfterLine() throws IOException {
136 final InputStream xmlInputStream = new ByteArrayInputStream(
137 "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish".getBytes());
139 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
140 xmlTaggedReader.init(xmlInputStream);
142 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
143 assertEquals("<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish",
144 textBlock.getText());
145 assertTrue(textBlock.isEndOfText());
149 * Test garbage text multi line.
151 * @throws IOException Signals that an I/O exception has occurred.
154 public void testGarbageTextMultiLine() throws IOException {
155 final InputStream xmlInputStream = new ByteArrayInputStream("hello\nthere".getBytes());
157 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
158 xmlTaggedReader.init(xmlInputStream);
160 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
161 assertTrue(textBlock.isEndOfText());
165 * Test partial event multi line.
167 * @throws IOException Signals that an I/O exception has occurred.
170 public void testPartialEventMultiLine() throws IOException {
171 final InputStream xmlInputStream = new ByteArrayInputStream(
172 "1469781869268\n</TestTimestamp>\n</MainTag>".getBytes());
174 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
175 xmlTaggedReader.init(xmlInputStream);
177 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
178 assertTrue(textBlock.isEndOfText());
182 * Test full event multi line.
184 * @throws IOException Signals that an I/O exception has occurred.
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());
191 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
192 xmlTaggedReader.init(xmlInputStream);
194 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
195 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
196 textBlock.getText());
197 assertTrue(textBlock.isEndOfText());
201 * Test full event garbage before multi line.
203 * @throws IOException Signals that an I/O exception has occurred.
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"
211 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
212 xmlTaggedReader.init(xmlInputStream);
214 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
215 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
216 textBlock.getText());
217 assertTrue(textBlock.isEndOfText());
221 * Test full event garbage before after multi line.
223 * @throws IOException Signals that an I/O exception has occurred.
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());
231 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
232 xmlTaggedReader.init(xmlInputStream);
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());
241 * Test full event garbage after multi line.
243 * @throws IOException Signals that an I/O exception has occurred.
246 public void testFullEventGarbageAfterMultiLine() throws IOException {
247 final InputStream xmlInputStream = new ByteArrayInputStream(
248 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish"
251 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
252 xmlTaggedReader.init(xmlInputStream);
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());
261 * Test partial events line.
263 * @throws IOException Signals that an I/O exception has occurred.
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());
271 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
272 xmlTaggedReader.init(xmlInputStream);
274 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
275 assertTrue(textBlock.isEndOfText());
279 * Test full events garbage before line.
281 * @throws IOException Signals that an I/O exception has occurred.
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());
289 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
290 xmlTaggedReader.init(xmlInputStream);
292 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
293 assertTrue(textBlock.isEndOfText());
297 * Test full events garbage before after line.
299 * @throws IOException Signals that an I/O exception has occurred.
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());
307 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
308 xmlTaggedReader.init(xmlInputStream);
310 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
311 assertTrue(textBlock.isEndOfText());
315 * Test full events garbage after line.
317 * @throws IOException Signals that an I/O exception has occurred.
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());
325 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
326 xmlTaggedReader.init(xmlInputStream);
328 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
329 assertEquals(textBlock.getText(), garbageString);
330 assertTrue(textBlock.isEndOfText());
334 * Test partial events multi line.
336 * @throws IOException Signals that an I/O exception has occurred.
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());
343 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
344 xmlTaggedReader.init(xmlInputStream);
346 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
347 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>", textBlock.getText());
348 assertTrue(textBlock.isEndOfText());
352 * Test full events multi line.
354 * @throws IOException Signals that an I/O exception has occurred.
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());
362 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
363 xmlTaggedReader.init(xmlInputStream);
365 TextBlock textBlock = xmlTaggedReader.readTextBlock();
366 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
367 textBlock.getText());
368 assertFalse(textBlock.isEndOfText());
370 textBlock = xmlTaggedReader.readTextBlock();
371 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
372 textBlock.getText());
373 assertTrue(textBlock.isEndOfText());
377 * Test full events garbage before multi line.
379 * @throws IOException Signals that an I/O exception has occurred.
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());
387 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
388 xmlTaggedReader.init(xmlInputStream);
390 TextBlock textBlock = xmlTaggedReader.readTextBlock();
391 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
392 textBlock.getText());
393 assertFalse(textBlock.isEndOfText());
395 textBlock = xmlTaggedReader.readTextBlock();
396 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
397 textBlock.getText());
398 assertTrue(textBlock.isEndOfText());
402 * Test full events garbage before after multi line.
404 * @throws IOException Signals that an I/O exception has occurred.
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());
413 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
414 xmlTaggedReader.init(xmlInputStream);
416 TextBlock textBlock = xmlTaggedReader.readTextBlock();
417 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
418 textBlock.getText());
419 assertFalse(textBlock.isEndOfText());
421 textBlock = xmlTaggedReader.readTextBlock();
422 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRefuse",
423 textBlock.getText());
424 assertTrue(textBlock.isEndOfText());
428 * Test full events garbage after multi line.
430 * @throws IOException Signals that an I/O exception has occurred.
433 public void testFullEventsGarbageAfterMultiLine() throws IOException {
434 final InputStream xmlInputStream = new ByteArrayInputStream(
435 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish"
438 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
439 xmlTaggedReader.init(xmlInputStream);
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());