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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer;
23 import static org.assertj.core.api.Assertions.assertThat;
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;
33 public class HeaderDelimitedTextBlockReaderTest {
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;
43 final InputStream stream = new ByteArrayInputStream(expected.getBytes(StandardCharsets.UTF_8));
45 final HeaderDelimitedTextBlockReader reader =
46 new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter);
49 final TextBlock textBlock = reader.readTextBlock();
50 assertThat(textBlock.getText()).isEqualTo(expected);
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;
62 final InputStream stream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
64 final HeaderDelimitedTextBlockReader reader =
65 new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter);
68 final TextBlock textBlock = reader.readTextBlock();
69 assertThat(textBlock.getText()).isEqualTo(expected);
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);
79 final InputStream stream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
81 final HeaderDelimitedTextBlockReader reader =
82 new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter);
85 final TextBlock textBlock = reader.readTextBlock();
86 assertThat(textBlock.getText()).isEqualTo(text);
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;
98 final InputStream stream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
100 final HeaderDelimitedTextBlockReader reader =
101 new HeaderDelimitedTextBlockReader(startToken, endToken, delimeter);
104 final TextBlock textBlock = reader.readTextBlock();
105 assertThat(textBlock.getText()).isEqualTo(expected);