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.service.engine.event;
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.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 verifyNoEvent("testGarbageText", "hello there");
48 public void testPartialEvent() throws IOException {
49 verifyNoEvent("testGarbageText", "\"TestTimestamp\": 1469781869268}");
53 public void testFullEvent() throws IOException {
54 verifyMulti("testFullEvent", "{TestTimestamp\": 1469781869268}", "{TestTimestamp\": 1469781869268}");
58 public void testFullEventGarbageBefore() throws IOException {
59 verifyMulti("testFullEventGarbageBefore", "Garbage{TestTimestamp\": 1469781869268}",
60 "{TestTimestamp\": 1469781869268}");
64 public void testFullEventGarbageBeforeAfter() throws IOException {
65 verifyMulti("testFullEventGarbageBeforeAfter", "Garbage{TestTimestamp\": 1469781869268}Rubbish",
66 "{TestTimestamp\": 1469781869268}");
70 public void testFullEventGarbageAfter() throws IOException {
71 verifyMulti("testFullEventGarbageAfter", "{TestTimestamp\": 1469781869268}Rubbish",
72 "{TestTimestamp\": 1469781869268}");
75 private void verifyNoEvent(String testName, String input) throws IOException {
76 final InputStream jsonInputStream = new ByteArrayInputStream(input.getBytes());
78 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
79 taggedReader.init(jsonInputStream);
81 final TextBlock textBlock = taggedReader.readTextBlock();
82 assertNull(testName, textBlock.getText());
83 assertTrue(testName, textBlock.isEndOfText());
86 private void verifyMulti(String testName, String input, String expected) throws IOException {
87 final InputStream jsonInputStream = new ByteArrayInputStream(input.getBytes());
89 final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
90 taggedReader.init(jsonInputStream);
92 TextBlock textBlock = taggedReader.readTextBlock();
93 assertEquals(testName, expected, textBlock.getText());
94 assertFalse(testName, textBlock.isEndOfText());
96 textBlock = taggedReader.readTextBlock();
97 assertNull(testName, textBlock.getText());
98 assertTrue(testName, textBlock.isEndOfText());