6c595072f146d5f3c72901fe1aa8478d4ba60ac9
[policy/apex-pdp.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021. Nordix Foundation.
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.impl.filecarrierplugin.consumer;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.nio.charset.StandardCharsets;
29 import org.apache.commons.lang3.RandomStringUtils;
30 import org.junit.Test;
31
32
33 public class HeaderDelimitedTextBlockReaderTest {
34
35     @Test
36     public void readTextBlockWithDelimeter() throws IOException {
37         final String startToken = RandomStringUtils.randomAlphabetic(5);
38         final String endToken = RandomStringUtils.randomAlphabetic(6);
39         final boolean delimeter = true;
40         final String text = RandomStringUtils.randomAlphanumeric(20);
41         final String expected = startToken + text;
42         // Prepare the stream
43         final InputStream stream = new ByteArrayInputStream(expected.getBytes(StandardCharsets.UTF_8));
44
45         final HeaderDelimitedTextBlockReader reader =
46             new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter);
47         reader.init(stream);
48
49         final TextBlock textBlock = reader.readTextBlock();
50         assertThat(textBlock.getText()).isEqualTo(expected);
51     }
52
53     @Test
54     public void readTextBlockWithEndTokenDelimeter() throws IOException {
55         final String startToken = RandomStringUtils.randomAlphabetic(5);
56         final String endToken = RandomStringUtils.randomAlphabetic(6);
57         final boolean delimeter = true;
58         final String text = RandomStringUtils.randomAlphanumeric(20);
59         final String input = startToken + "\n" + text + "\n" + endToken + "\n" + text;
60         final String expected = startToken + "\n" + text + "\n" + endToken;
61         // Prepare the stream
62         final InputStream stream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
63
64         final HeaderDelimitedTextBlockReader reader =
65             new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter);
66         reader.init(stream);
67
68         final TextBlock textBlock = reader.readTextBlock();
69         assertThat(textBlock.getText()).isEqualTo(expected);
70     }
71
72     @Test
73     public void readTextBlockWithoutDelimeter() throws IOException {
74         final String startToken = RandomStringUtils.randomAlphabetic(5);
75         final String endToken = RandomStringUtils.randomAlphabetic(6);
76         final boolean delimeter = false;
77         final String text = RandomStringUtils.randomAlphanumeric(20);
78         // Prepare the stream
79         final InputStream stream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
80
81         final HeaderDelimitedTextBlockReader reader =
82             new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter);
83         reader.init(stream);
84
85         final TextBlock textBlock = reader.readTextBlock();
86         assertThat(textBlock.getText()).isEqualTo(text);
87     }
88
89     @Test
90     public void readTextBlockWithEndTokenWithoutDelimeter() throws IOException {
91         final String startToken = RandomStringUtils.randomAlphabetic(5);
92         final String endToken = RandomStringUtils.randomAlphabetic(6);
93         final boolean delimeter = false;
94         final String text = RandomStringUtils.randomAlphanumeric(20);
95         final String input = startToken + "\n" + text + "\n" + endToken + "\n" + text;
96         final String expected = startToken + "\n" + text + "\n" + endToken;
97         // Prepare the stream
98         final InputStream stream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
99
100         final HeaderDelimitedTextBlockReader reader =
101             new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter);
102         reader.init(stream);
103
104         final TextBlock textBlock = reader.readTextBlock();
105         assertThat(textBlock.getText()).isEqualTo(expected);
106     }
107 }