4fa42a7275a18df61e767a0f35e5944f49e46a87
[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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.InputStream;
27 import java.nio.charset.StandardCharsets;
28 import org.apache.commons.lang3.RandomStringUtils;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.service.engine.event.ApexEventException;
32 import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters;
33 import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters;
34 import org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperTokenDelimitedEventProtocolParameters;
35 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
36
37 public class TextBlockReaderFactoryTest {
38     private TextBlockReaderFactory factory;
39
40     @Before
41     public void setUp() throws Exception {
42         factory = new TextBlockReaderFactory();
43     }
44
45     @Test
46     public void getTaggedReaderTextCharDelimitedParametersParams() throws ApexEventException {
47         final String text = RandomStringUtils.randomAlphanumeric(22);
48         final InputStream inputStream = prepareInputStream(text);
49         final EventProtocolParameters parameters = new JsonEventProtocolParameters();
50
51         final TextBlockReader actual = factory.getTaggedReader(inputStream, parameters);
52         assertThat(actual).isNotInstanceOf(HeaderDelimitedTextBlockReader.class);
53     }
54
55     @Test
56     public void getTaggedReaderTextTokenDelimitedParams() throws ApexEventException {
57         final String text = RandomStringUtils.randomAlphanumeric(22);
58         final InputStream inputStream = prepareInputStream(text);
59         new ApexEventProtocolParameters();
60         final EventProtocolParameters parameters = new SuperTokenDelimitedEventProtocolParameters();
61
62         final TextBlockReader actual = factory.getTaggedReader(inputStream, parameters);
63         assertThat(actual).isInstanceOf(HeaderDelimitedTextBlockReader.class);
64     }
65
66     @Test
67     public void getTaggedReaderNotSupportedParams() {
68         final String text = RandomStringUtils.randomAlphanumeric(22);
69         final InputStream inputStream = prepareInputStream(text);
70         final EventProtocolParameters parameters = new ApexEventProtocolParameters();
71         assertThatThrownBy(() -> factory.getTaggedReader(inputStream, parameters))
72             .isInstanceOf(ApexEventException.class);
73     }
74
75
76     private InputStream prepareInputStream(String text) {
77         return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
78     }
79 }