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.service.engine.event;
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.CharacterDelimitedTextBlockReader;
34 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock;
37 * Test JSON Tagged Event Consumer.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class JsonTaggedEventConsumerTest {
43 public void testGarbageText() throws IOException {
44 final InputStream jsonInputStream = new ByteArrayInputStream("hello there".getBytes());
46 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
47 taggedReader.init(jsonInputStream);
49 final TextBlock textBlock = taggedReader.readTextBlock();
50 assertNull(textBlock.getText());
51 assertTrue(textBlock.isEndOfText());
55 public void testPartialEvent() throws IOException {
56 final InputStream jsonInputStream = new ByteArrayInputStream("\"TestTimestamp\": 1469781869268}".getBytes());
58 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
59 taggedReader.init(jsonInputStream);
61 final TextBlock textBlock = taggedReader.readTextBlock();
62 assertNull(textBlock.getText());
63 assertTrue(textBlock.isEndOfText());
67 public void testFullEvent() throws IOException {
68 final InputStream jsonInputStream = new ByteArrayInputStream("{TestTimestamp\": 1469781869268}".getBytes());
70 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
71 taggedReader.init(jsonInputStream);
73 TextBlock textBlock = taggedReader.readTextBlock();
74 assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
76 textBlock = taggedReader.readTextBlock();
77 assertNull(textBlock.getText());
78 assertTrue(textBlock.isEndOfText());
82 public void testFullEventGarbageBefore() throws IOException {
83 final InputStream jsonInputStream =
84 new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}".getBytes());
86 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
87 taggedReader.init(jsonInputStream);
89 TextBlock textBlock = taggedReader.readTextBlock();
90 assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
91 assertFalse(textBlock.isEndOfText());
93 textBlock = taggedReader.readTextBlock();
94 assertNull(textBlock.getText());
95 assertTrue(textBlock.isEndOfText());
99 public void testFullEventGarbageBeforeAfter() throws IOException {
100 final InputStream jsonInputStream =
101 new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}Rubbish".getBytes());
103 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
104 taggedReader.init(jsonInputStream);
106 TextBlock textBlock = taggedReader.readTextBlock();
107 assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
108 assertFalse(textBlock.isEndOfText());
110 textBlock = taggedReader.readTextBlock();
111 assertNull(textBlock.getText());
112 assertTrue(textBlock.isEndOfText());
116 public void testFullEventGarbageAfter() throws IOException {
117 final InputStream jsonInputStream =
118 new ByteArrayInputStream("{TestTimestamp\": 1469781869268}Rubbish".getBytes());
120 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
121 taggedReader.init(jsonInputStream);
123 TextBlock textBlock = taggedReader.readTextBlock();
124 assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
125 assertFalse(textBlock.isEndOfText());
127 textBlock = taggedReader.readTextBlock();
128 assertNull(textBlock.getText());
129 assertTrue(textBlock.isEndOfText());