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;
31 import org.junit.Test;
32 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.CharacterDelimitedTextBlockReader;
33 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock;
36 * Test JSON Tagged Event Consumer.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class JsonTaggedEventConsumerTest {
42 public void testGarbageText() throws IOException {
43 final InputStream jsonInputStream = new ByteArrayInputStream("hello there".getBytes());
45 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
46 taggedReader.init(jsonInputStream);
48 final TextBlock textBlock = taggedReader.readTextBlock();
49 assertNull(textBlock.getText());
50 assertTrue(textBlock.isEndOfText());
54 public void testPartialEvent() throws IOException {
55 final InputStream jsonInputStream = new ByteArrayInputStream("\"TestTimestamp\": 1469781869268}".getBytes());
57 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
58 taggedReader.init(jsonInputStream);
60 final TextBlock textBlock = taggedReader.readTextBlock();
61 assertNull(textBlock.getText());
62 assertTrue(textBlock.isEndOfText());
66 public void testFullEvent() throws IOException {
67 final InputStream jsonInputStream = new ByteArrayInputStream("{TestTimestamp\": 1469781869268}".getBytes());
69 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
70 taggedReader.init(jsonInputStream);
72 TextBlock textBlock = taggedReader.readTextBlock();
73 assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
75 textBlock = taggedReader.readTextBlock();
76 assertNull(textBlock.getText());
77 assertTrue(textBlock.isEndOfText());
81 public void testFullEventGarbageBefore() throws IOException {
82 final InputStream jsonInputStream =
83 new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}".getBytes());
85 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
86 taggedReader.init(jsonInputStream);
88 TextBlock textBlock = taggedReader.readTextBlock();
89 assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
90 assertFalse(textBlock.isEndOfText());
92 textBlock = taggedReader.readTextBlock();
93 assertNull(textBlock.getText());
94 assertTrue(textBlock.isEndOfText());
98 public void testFullEventGarbageBeforeAfter() throws IOException {
99 final InputStream jsonInputStream =
100 new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}Rubbish".getBytes());
102 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
103 taggedReader.init(jsonInputStream);
105 TextBlock textBlock = taggedReader.readTextBlock();
106 assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
107 assertFalse(textBlock.isEndOfText());
109 textBlock = taggedReader.readTextBlock();
110 assertNull(textBlock.getText());
111 assertTrue(textBlock.isEndOfText());
115 public void testFullEventGarbageAfter() throws IOException {
116 final InputStream jsonInputStream =
117 new ByteArrayInputStream("{TestTimestamp\": 1469781869268}Rubbish".getBytes());
119 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
120 taggedReader.init(jsonInputStream);
122 TextBlock textBlock = taggedReader.readTextBlock();
123 assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
124 assertFalse(textBlock.isEndOfText());
126 textBlock = taggedReader.readTextBlock();
127 assertNull(textBlock.getText());
128 assertTrue(textBlock.isEndOfText());