Java 17 Upgrade
[policy/models.git] / models-sim / models-sim-dmaap / src / test / java / org / onap / policy / models / sim / dmaap / rest / CambriaMessageBodyHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.sim.dmaap.rest;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29 import jakarta.ws.rs.core.MediaType;
30 import java.io.ByteArrayInputStream;
31 import java.io.EOFException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.util.List;
36 import org.junit.Before;
37 import org.junit.Test;
38
39 public class CambriaMessageBodyHandlerTest {
40     private static final String STD_INPUT = "1.3.XAbc";
41     private static final String EXPECTED_OUTPUT = "[Abc]";
42
43     private CambriaMessageBodyHandler hdlr;
44
45     @Before
46     public void setUp() {
47         hdlr = new CambriaMessageBodyHandler();
48     }
49
50     @Test
51     public void testIsReadable() {
52         assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf("application/cambria")));
53
54         assertFalse(hdlr.isReadable(null, null, null, null));
55         assertFalse(hdlr.isReadable(null, null, null, MediaType.valueOf("application/other")));
56         assertFalse(hdlr.isReadable(null, null, null, MediaType.valueOf("other/cambria")));
57     }
58
59     @Test
60     public void testReadFrom() throws IOException {
61         List<Object> lst = readStream("1.11.AMessageBody", "3.3.123Foo3.3.123Bar", "0.16.You can do that..8.Or that.");
62         assertEquals("[MessageBody, Foo, Bar, You can do that., Or that.]", lst.toString());
63
64         // empty stream
65         lst = readStream();
66         assertEquals("[]", lst.toString());
67     }
68
69     @Test
70     public void testReadMessage_InvalidPartitionLength() {
71         assertThatThrownBy(() -> readStream("100000000.3.")).isInstanceOf(IOException.class)
72                         .hasMessage("invalid partition length");
73     }
74
75     @Test
76     public void testReadMessage_InvalidMessageLength() {
77         assertThatThrownBy(() -> readStream("3.100000000.ABC")).isInstanceOf(IOException.class)
78                         .hasMessage("invalid message length");
79     }
80
81     @Test
82     public void testSkipWhitespace() throws IOException {
83         // no white space
84         assertEquals(EXPECTED_OUTPUT, readStream(STD_INPUT).toString());
85
86         // single white space
87         assertEquals(EXPECTED_OUTPUT, readStream(" " + STD_INPUT).toString());
88
89         // multiple white spaces
90         assertEquals(EXPECTED_OUTPUT, readStream("\n\n\t" + STD_INPUT).toString());
91     }
92
93     @Test
94     public void testReadLength_NoDigits() throws IOException {
95         assertEquals("[]", readStream("..").toString());
96     }
97
98     @Test
99     public void testReadLength_NoDot() {
100         assertThatThrownBy(() -> readStream("3.2")).isInstanceOf(EOFException.class)
101                         .hasMessage("missing '.' in 'length' field");
102     }
103
104     @Test
105     public void testReadLength_NonDigit() {
106         assertThatThrownBy(() -> readStream("3.2x.ABCde")).isInstanceOf(IOException.class)
107                         .hasMessage("invalid character in 'length' field");
108     }
109
110     @Test
111     public void testReadLength_TooManyDigits() {
112         assertThatThrownBy(() -> readStream("3.12345678901234567890.ABCde")).isInstanceOf(IOException.class)
113                         .hasMessage("too many digits in 'length' field");
114     }
115
116     @Test
117     public void testReadString_ZeroLength() throws IOException {
118         assertEquals("[]", readStream("1..X").toString());
119     }
120
121     @Test
122     public void testReadString_TooShort() {
123         assertThatThrownBy(() -> readStream(".5.me")).isInstanceOf(EOFException.class).hasMessageContaining("actual");
124     }
125
126     /**
127      * Reads a stream via the handler.
128      *
129      * @param text lines of text to be read
130      * @return the list of objects that were decoded from the stream
131      * @throws IOException if an error occurs
132      */
133     private List<Object> readStream(String... text) throws IOException {
134         return hdlr.readFrom(null, null, null, null, null, makeStream(text));
135     }
136
137     /**
138      * Creates an input stream from lines of text.
139      *
140      * @param text lines of text
141      * @return an input stream
142      */
143     private InputStream makeStream(String... text) {
144         return new ByteArrayInputStream(String.join("\n", text).getBytes(StandardCharsets.UTF_8));
145     }
146 }