3bdcf53d9fe35267cea433cee98cd975dfa1e1cd
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.service.engine.event;
23
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;
28
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;
35
36 /**
37  * Test JSON Tagged Event Consumer.
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class JsonTaggedEventConsumerTest {
41
42     @Test
43     public void testGarbageText() throws IOException {
44         verifyNoEvent("testGarbageText", "hello there");
45     }
46
47     @Test
48     public void testPartialEvent() throws IOException {
49         verifyNoEvent("testGarbageText", "\"TestTimestamp\": 1469781869268}");
50     }
51
52     @Test
53     public void testFullEvent() throws IOException {
54         verifyMulti("testFullEvent", "{TestTimestamp\": 1469781869268}", "{TestTimestamp\": 1469781869268}");
55     }
56
57     @Test
58     public void testFullEventGarbageBefore() throws IOException {
59         verifyMulti("testFullEventGarbageBefore", "Garbage{TestTimestamp\": 1469781869268}",
60                         "{TestTimestamp\": 1469781869268}");
61     }
62
63     @Test
64     public void testFullEventGarbageBeforeAfter() throws IOException {
65         verifyMulti("testFullEventGarbageBeforeAfter", "Garbage{TestTimestamp\": 1469781869268}Rubbish",
66                         "{TestTimestamp\": 1469781869268}");
67     }
68
69     @Test
70     public void testFullEventGarbageAfter() throws IOException {
71         verifyMulti("testFullEventGarbageAfter", "{TestTimestamp\": 1469781869268}Rubbish",
72                         "{TestTimestamp\": 1469781869268}");
73     }
74
75     private void verifyNoEvent(String testName, String input) throws IOException {
76         final InputStream jsonInputStream = new ByteArrayInputStream(input.getBytes());
77
78         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
79         taggedReader.init(jsonInputStream);
80
81         final TextBlock textBlock = taggedReader.readTextBlock();
82         assertNull(testName, textBlock.getText());
83         assertTrue(testName, textBlock.isEndOfText());
84     }
85
86     private void verifyMulti(String testName, String input, String expected) throws IOException {
87         final InputStream jsonInputStream = new ByteArrayInputStream(input.getBytes());
88
89         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
90         taggedReader.init(jsonInputStream);
91
92         TextBlock textBlock = taggedReader.readTextBlock();
93         assertEquals(testName, expected, textBlock.getText());
94         assertFalse(testName, textBlock.isEndOfText());
95
96         textBlock = taggedReader.readTextBlock();
97         assertNull(testName, textBlock.getText());
98         assertTrue(testName, textBlock.isEndOfText());
99     }
100 }