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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.protocol.xml;
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;
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;
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 verifyNull("testGarbageTextLine", "hello there");
52 * Test partial event line.
54 * @throws IOException Signals that an I/O exception has occurred.
57 public void testPartialEventLine() throws IOException {
58 verifyNull("testPartialEventLine", "1469781869268</TestTimestamp></MainTag>");
62 * Test full event line.
64 * @throws IOException Signals that an I/O exception has occurred.
67 public void testFullEventLine() throws IOException {
68 verifyLine("testFullEventLine", "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>",
69 "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>");
73 * Test full event garbage before line.
75 * @throws IOException Signals that an I/O exception has occurred.
78 public void testFullEventGarbageBeforeLine() throws IOException {
79 verifyEnd("testFullEventGarbageBeforeLine",
80 "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>");
84 * Test full event garbage before after line.
86 * @throws IOException Signals that an I/O exception has occurred.
89 public void testFullEventGarbageBeforeAfterLine() throws IOException {
90 verifyEnd("testFullEventGarbageBeforeAfterLine",
91 "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>Rubbish");
95 * Test full event garbage after line.
97 * @throws IOException Signals that an I/O exception has occurred.
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");
107 * Test garbage text multi line.
109 * @throws IOException Signals that an I/O exception has occurred.
112 public void testGarbageTextMultiLine() throws IOException {
113 verifyEnd("testGarbageTextMultiLine", "hello\nthere");
117 * Test partial event multi line.
119 * @throws IOException Signals that an I/O exception has occurred.
122 public void testPartialEventMultiLine() throws IOException {
123 verifyEnd("testPartialEventMultiLine", "1469781869268\n</TestTimestamp>\n</MainTag>");
127 * Test full event multi line.
129 * @throws IOException Signals that an I/O exception has occurred.
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>");
139 * Test full event garbage before multi line.
141 * @throws IOException Signals that an I/O exception has occurred.
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>");
151 * Test full event garbage before after multi line.
153 * @throws IOException Signals that an I/O exception has occurred.
156 public void testFullEventGarbageBeforeAfterMultiLine() throws IOException {
157 String garbageString = "Garbage\n<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>"
158 + "\n</MainTag>\nRubbish\n\n";
160 verifyLine("testFullEventsGarbageAfterLine", garbageString,
161 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>\nRubbish");
165 * Test full event garbage after multi line.
167 * @throws IOException Signals that an I/O exception has occurred.
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");
177 * Test partial events line.
179 * @throws IOException Signals that an I/O exception has occurred.
182 public void testPartialEventsLine() throws IOException {
183 verifyEnd("testPartialEventsLine", "1469781869268</TestTimestamp></MainTag><?xml><MainTag>"
184 + "<TestTimestamp>1469781869268</TestTimestamp>");
188 * Test full events garbage before line.
190 * @throws IOException Signals that an I/O exception has occurred.
193 public void testFullEventsGarbageBeforeLine() throws IOException {
194 verifyEnd("testPartialEventsLine",
195 "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp></MainTag>"
196 + "<?xml><MainTag><TestTimestamp>");
200 * Test full events garbage before after line.
202 * @throws IOException Signals that an I/O exception has occurred.
205 public void testFullEventsGarbageBeforeAfterLine() throws IOException {
206 verifyEnd("testFullEventGarbageBeforeLine", "Garbage<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp>"
207 + "</MainTag>Rubbish<?xml><MainTag><TestTimestamp>\nRefuse");
211 * Test full events garbage after line.
213 * @throws IOException Signals that an I/O exception has occurred.
216 public void testFullEventsGarbageAfterLine() throws IOException {
217 String garbageString = "<?xml><MainTag><TestTimestamp>1469781869268</TestTimestamp>"
218 + "</MainTag>Rubbish<?xml><MainTag><TestTimestamp>Refuse";
220 verifyLine("testFullEventsGarbageAfterLine", garbageString, garbageString);
224 * Test partial events multi line.
226 * @throws IOException Signals that an I/O exception has occurred.
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>");
236 * Test full events multi line.
238 * @throws IOException Signals that an I/O exception has occurred.
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";
245 verifyLines("testFullEventsMultiLine", garbageString,
246 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
247 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>");
251 * Test full events garbage before multi line.
253 * @throws IOException Signals that an I/O exception has occurred.
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";
260 verifyLines("testFullEventsGarbageBeforeMultiLine", garbageString,
261 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>",
262 "<?xml>\n<MainTag>\n<TestTimestamp>1469781869268</TestTimestamp>\n</MainTag>");
266 * Test full events garbage before after multi line.
268 * @throws IOException Signals that an I/O exception has occurred.
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";
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");
282 * Test full events garbage after multi line.
284 * @throws IOException Signals that an I/O exception has occurred.
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");
293 private void verifyNull(String testName, String xml) throws IOException {
294 final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes());
296 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
297 xmlTaggedReader.init(xmlInputStream);
299 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
300 assertNull(testName, textBlock.getText());
301 assertTrue(testName, textBlock.isEndOfText());
304 private void verifyLine(String testName, String xml, String expected) throws IOException {
305 final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes());
307 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
308 xmlTaggedReader.init(xmlInputStream);
310 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
311 assertEquals(testName, expected, textBlock.getText());
312 assertTrue(testName, textBlock.isEndOfText());
315 private void verifyLines(String testName, String xml, String expected, String expected2) throws IOException {
316 final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes());
318 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
319 xmlTaggedReader.init(xmlInputStream);
321 TextBlock textBlock = xmlTaggedReader.readTextBlock();
322 assertEquals(testName, expected, textBlock.getText());
323 assertFalse(testName, textBlock.isEndOfText());
325 textBlock = xmlTaggedReader.readTextBlock();
326 assertEquals(testName, expected2, textBlock.getText());
327 assertTrue(testName, textBlock.isEndOfText());
330 private void verifyEnd(String testName, String xml) throws IOException {
331 final InputStream xmlInputStream = new ByteArrayInputStream(xml.getBytes());
333 final HeaderDelimitedTextBlockReader xmlTaggedReader = new HeaderDelimitedTextBlockReader("<?xml", null, true);
334 xmlTaggedReader.init(xmlInputStream);
336 final TextBlock textBlock = xmlTaggedReader.readTextBlock();
337 assertTrue(testName, textBlock.isEndOfText());