f313f1fd4f4af148167ec448a311390d3cb0bca8
[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
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         final InputStream jsonInputStream = new ByteArrayInputStream("hello there".getBytes());
45
46         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
47         taggedReader.init(jsonInputStream);
48
49         final TextBlock textBlock = taggedReader.readTextBlock();
50         assertNull(textBlock.getText());
51         assertTrue(textBlock.isEndOfText());
52     }
53
54     @Test
55     public void testPartialEvent() throws IOException {
56         final InputStream jsonInputStream = new ByteArrayInputStream("\"TestTimestamp\": 1469781869268}".getBytes());
57
58         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
59         taggedReader.init(jsonInputStream);
60
61         final TextBlock textBlock = taggedReader.readTextBlock();
62         assertNull(textBlock.getText());
63         assertTrue(textBlock.isEndOfText());
64     }
65
66     @Test
67     public void testFullEvent() throws IOException {
68         final InputStream jsonInputStream = new ByteArrayInputStream("{TestTimestamp\": 1469781869268}".getBytes());
69
70         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
71         taggedReader.init(jsonInputStream);
72
73         TextBlock textBlock = taggedReader.readTextBlock();
74         assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
75
76         textBlock = taggedReader.readTextBlock();
77         assertNull(textBlock.getText());
78         assertTrue(textBlock.isEndOfText());
79     }
80
81     @Test
82     public void testFullEventGarbageBefore() throws IOException {
83         final InputStream jsonInputStream =
84                 new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}".getBytes());
85
86         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
87         taggedReader.init(jsonInputStream);
88
89         TextBlock textBlock = taggedReader.readTextBlock();
90         assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
91         assertFalse(textBlock.isEndOfText());
92
93         textBlock = taggedReader.readTextBlock();
94         assertNull(textBlock.getText());
95         assertTrue(textBlock.isEndOfText());
96     }
97
98     @Test
99     public void testFullEventGarbageBeforeAfter() throws IOException {
100         final InputStream jsonInputStream =
101                 new ByteArrayInputStream("Garbage{TestTimestamp\": 1469781869268}Rubbish".getBytes());
102
103         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
104         taggedReader.init(jsonInputStream);
105
106         TextBlock textBlock = taggedReader.readTextBlock();
107         assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
108         assertFalse(textBlock.isEndOfText());
109
110         textBlock = taggedReader.readTextBlock();
111         assertNull(textBlock.getText());
112         assertTrue(textBlock.isEndOfText());
113     }
114
115     @Test
116     public void testFullEventGarbageAfter() throws IOException {
117         final InputStream jsonInputStream =
118                 new ByteArrayInputStream("{TestTimestamp\": 1469781869268}Rubbish".getBytes());
119
120         final CharacterDelimitedTextBlockReader taggedReader = new CharacterDelimitedTextBlockReader('{', '}');
121         taggedReader.init(jsonInputStream);
122
123         TextBlock textBlock = taggedReader.readTextBlock();
124         assertEquals("{TestTimestamp\": 1469781869268}", textBlock.getText());
125         assertFalse(textBlock.isEndOfText());
126
127         textBlock = taggedReader.readTextBlock();
128         assertNull(textBlock.getText());
129         assertTrue(textBlock.isEndOfText());
130     }
131 }