Changes for checkstyle 8.32
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / event / JsonTaggedEventConsumerTest.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event;
22
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;
27
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;
34
35 /**
36  * Test JSON Tagged Event Consumer.
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class JsonTaggedEventConsumerTest {
40
41     @Test
42     public void testGarbageText() throws IOException {
43         final InputStream jsonInputStream = new ByteArrayInputStream("hello there".getBytes());
44
45         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
46         taggedReader.init(jsonInputStream);
47
48         final TextBlock textBlock = taggedReader.readTextBlock();
49         assertNull(textBlock.getText());
50         assertTrue(textBlock.isEndOfText());
51     }
52
53     @Test
54     public void testPartialEvent() throws IOException {
55         final InputStream jsonInputStream = new ByteArrayInputStream("\"TestTimestamp\": 1469781869268}".getBytes());
56
57         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
58         taggedReader.init(jsonInputStream);
59
60         final TextBlock textBlock = taggedReader.readTextBlock();
61         assertNull(textBlock.getText());
62         assertTrue(textBlock.isEndOfText());
63     }
64
65     @Test
66     public void testFullEvent() throws IOException {
67         final InputStream jsonInputStream = new ByteArrayInputStream("{TestTimestamp\": 1469781869268}".getBytes());
68
69         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
70         taggedReader.init(jsonInputStream);
71
72         TextBlock textBlock = taggedReader.readTextBlock();
73         assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
74
75         textBlock = taggedReader.readTextBlock();
76         assertNull(textBlock.getText());
77         assertTrue(textBlock.isEndOfText());
78     }
79
80     @Test
81     public void testFullEventGarbageBefore() throws IOException {
82         final InputStream jsonInputStream =
83                 new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}".getBytes());
84
85         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
86         taggedReader.init(jsonInputStream);
87
88         TextBlock textBlock = taggedReader.readTextBlock();
89         assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
90         assertFalse(textBlock.isEndOfText());
91
92         textBlock = taggedReader.readTextBlock();
93         assertNull(textBlock.getText());
94         assertTrue(textBlock.isEndOfText());
95     }
96
97     @Test
98     public void testFullEventGarbageBeforeAfter() throws IOException {
99         final InputStream jsonInputStream =
100                 new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}Rubbish".getBytes());
101
102         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
103         taggedReader.init(jsonInputStream);
104
105         TextBlock textBlock = taggedReader.readTextBlock();
106         assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
107         assertFalse(textBlock.isEndOfText());
108
109         textBlock = taggedReader.readTextBlock();
110         assertNull(textBlock.getText());
111         assertTrue(textBlock.isEndOfText());
112     }
113
114     @Test
115     public void testFullEventGarbageAfter() throws IOException {
116         final InputStream jsonInputStream =
117                 new ByteArrayInputStream("{TestTimestamp\": 1469781869268}Rubbish".getBytes());
118
119         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
120         taggedReader.init(jsonInputStream);
121
122         TextBlock textBlock = taggedReader.readTextBlock();
123         assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
124         assertFalse(textBlock.isEndOfText());
125
126         textBlock = taggedReader.readTextBlock();
127         assertNull(textBlock.getText());
128         assertTrue(textBlock.isEndOfText());
129     }
130 }