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;
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;
36 * The Class TestXmlTaggedEventConsumer.
38 public class XmlTaggedEventConsumerTest {
41 * Test garbage text line.
43 * @throws IOException Signals that an I/O exception has occurred.
46 public void testGarbageTextLine() throws IOException {
47 final InputStream xmlInputStream = new ByteArrayInputStream("hello there".getBytes());
49 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
50 xmlTaggedReader.init(xmlInputStream);
52 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
53 assertNull(textBlock.getText());
54 assertTrue(textBlock.isEndOfText());
58 * Test partial event line.
60 * @throws IOException Signals that an I/O exception has occurred.
63 public void testPartialEventLine() throws IOException {
64 final InputStream xmlInputStream = new ByteArrayInputStream(
65 "1469781869268</TestTimestamp></MainTag>".getBytes());
67 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
68 xmlTaggedReader.init(xmlInputStream);
70 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
71 assertNull(textBlock.getText());
72 assertTrue(textBlock.isEndOfText());
76 * Test full event line.
78 * @throws IOException Signals that an I/O exception has occurred.
81 public void testFullEventLine() throws IOException {
82 final InputStream xmlInputStream = new ByteArrayInputStream(
83 "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>".getBytes());
85 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
86 xmlTaggedReader.init(xmlInputStream);
88 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
89 assertEquals("<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>", textBlock.getText());
90 assertTrue(textBlock.isEndOfText());
94 * Test full event garbage before line.
96 * @throws IOException Signals that an I/O exception has occurred.
99 public void testFullEventGarbageBeforeLine() throws IOException {
100 final InputStream xmlInputStream = new ByteArrayInputStream(
101 "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>".getBytes());
103 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
104 xmlTaggedReader.init(xmlInputStream);
106 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
107 assertTrue(textBlock.isEndOfText());
111 * Test full event garbage before after line.
113 * @throws IOException Signals that an I/O exception has occurred.
116 public void testFullEventGarbageBeforeAfterLine() throws IOException {
117 final InputStream xmlInputStream = new ByteArrayInputStream(
118 "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish"
121 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
122 xmlTaggedReader.init(xmlInputStream);
124 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
125 assertTrue(textBlock.isEndOfText());
129 * Test full event garbage after line.
131 * @throws IOException Signals that an I/O exception has occurred.
134 public void testFullEventGarbageAfterLine() throws IOException {
135 final InputStream xmlInputStream = new ByteArrayInputStream(
136 "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish".getBytes());
138 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
139 xmlTaggedReader.init(xmlInputStream);
141 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
142 assertEquals("<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish",
143 textBlock.getText());
144 assertTrue(textBlock.isEndOfText());
148 * Test garbage text multi line.
150 * @throws IOException Signals that an I/O exception has occurred.
153 public void testGarbageTextMultiLine() throws IOException {
154 final InputStream xmlInputStream = new ByteArrayInputStream("hello\nthere".getBytes());
156 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
157 xmlTaggedReader.init(xmlInputStream);
159 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
160 assertTrue(textBlock.isEndOfText());
164 * Test partial event multi line.
166 * @throws IOException Signals that an I/O exception has occurred.
169 public void testPartialEventMultiLine() throws IOException {
170 final InputStream xmlInputStream = new ByteArrayInputStream(
171 "1469781869268\n</TestTimestamp>\n</MainTag>".getBytes());
173 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
174 xmlTaggedReader.init(xmlInputStream);
176 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
177 assertTrue(textBlock.isEndOfText());
181 * Test full event multi line.
183 * @throws IOException Signals that an I/O exception has occurred.
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());
190 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
191 xmlTaggedReader.init(xmlInputStream);
193 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
194 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
195 textBlock.getText());
196 assertTrue(textBlock.isEndOfText());
200 * Test full event garbage before multi line.
202 * @throws IOException Signals that an I/O exception has occurred.
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"
210 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
211 xmlTaggedReader.init(xmlInputStream);
213 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
214 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
215 textBlock.getText());
216 assertTrue(textBlock.isEndOfText());
220 * Test full event garbage before after multi line.
222 * @throws IOException Signals that an I/O exception has occurred.
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());
230 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
231 xmlTaggedReader.init(xmlInputStream);
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());
240 * Test full event garbage after multi line.
242 * @throws IOException Signals that an I/O exception has occurred.
245 public void testFullEventGarbageAfterMultiLine() throws IOException {
246 final InputStream xmlInputStream = new ByteArrayInputStream(
247 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish"
250 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
251 xmlTaggedReader.init(xmlInputStream);
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());
260 * Test partial events line.
262 * @throws IOException Signals that an I/O exception has occurred.
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());
270 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
271 xmlTaggedReader.init(xmlInputStream);
273 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
274 assertTrue(textBlock.isEndOfText());
278 * Test full events garbage before line.
280 * @throws IOException Signals that an I/O exception has occurred.
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());
288 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
289 xmlTaggedReader.init(xmlInputStream);
291 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
292 assertTrue(textBlock.isEndOfText());
296 * Test full events garbage before after line.
298 * @throws IOException Signals that an I/O exception has occurred.
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());
306 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
307 xmlTaggedReader.init(xmlInputStream);
309 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
310 assertTrue(textBlock.isEndOfText());
314 * Test full events garbage after line.
316 * @throws IOException Signals that an I/O exception has occurred.
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());
324 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
325 xmlTaggedReader.init(xmlInputStream);
327 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
328 assertEquals(textBlock.getText(), garbageString);
329 assertTrue(textBlock.isEndOfText());
333 * Test partial events multi line.
335 * @throws IOException Signals that an I/O exception has occurred.
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());
342 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
343 xmlTaggedReader.init(xmlInputStream);
345 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
346 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>", textBlock.getText());
347 assertTrue(textBlock.isEndOfText());
351 * Test full events multi line.
353 * @throws IOException Signals that an I/O exception has occurred.
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());
361 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
362 xmlTaggedReader.init(xmlInputStream);
364 TextBlock textBlock = xmlTaggedReader.readTextBlock();
365 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
366 textBlock.getText());
367 assertFalse(textBlock.isEndOfText());
369 textBlock = xmlTaggedReader.readTextBlock();
370 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
371 textBlock.getText());
372 assertTrue(textBlock.isEndOfText());
376 * Test full events garbage before multi line.
378 * @throws IOException Signals that an I/O exception has occurred.
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());
386 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
387 xmlTaggedReader.init(xmlInputStream);
389 TextBlock textBlock = xmlTaggedReader.readTextBlock();
390 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
391 textBlock.getText());
392 assertFalse(textBlock.isEndOfText());
394 textBlock = xmlTaggedReader.readTextBlock();
395 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
396 textBlock.getText());
397 assertTrue(textBlock.isEndOfText());
401 * Test full events garbage before after multi line.
403 * @throws IOException Signals that an I/O exception has occurred.
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());
412 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
413 xmlTaggedReader.init(xmlInputStream);
415 TextBlock textBlock = xmlTaggedReader.readTextBlock();
416 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish",
417 textBlock.getText());
418 assertFalse(textBlock.isEndOfText());
420 textBlock = xmlTaggedReader.readTextBlock();
421 assertEquals("<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRefuse",
422 textBlock.getText());
423 assertTrue(textBlock.isEndOfText());
427 * Test full events garbage after multi line.
429 * @throws IOException Signals that an I/O exception has occurred.
432 public void testFullEventsGarbageAfterMultiLine() throws IOException {
433 final InputStream xmlInputStream = new ByteArrayInputStream(
434 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish"
437 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
438 xmlTaggedReader.init(xmlInputStream);
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());