Java 17 Upgrade
[policy/models.git] / models-sim / models-sim-dmaap / src / test / java / org / onap / policy / models / sim / dmaap / rest / TextMessageBodyHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.models.sim.dmaap.rest;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25
26 import jakarta.ws.rs.core.MediaType;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
31 import java.util.List;
32 import org.junit.Before;
33 import org.junit.Test;
34
35 public class TextMessageBodyHandlerTest {
36     private TextMessageBodyHandler hdlr;
37
38     @Before
39     public void setUp() {
40         hdlr = new TextMessageBodyHandler();
41     }
42
43     @Test
44     public void testIsReadable() {
45         assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf("text/plain")));
46
47         assertFalse(hdlr.isReadable(null, null, null, null));
48         assertFalse(hdlr.isReadable(null, null, null, MediaType.valueOf("text/other")));
49         assertFalse(hdlr.isReadable(null, null, null, MediaType.valueOf("other/plain")));
50     }
51
52     @Test
53     public void testReadFrom() throws IOException {
54         List<Object> lst = readStream("hello", "world");
55         assertEquals("[hello, world]", lst.toString());
56
57         // empty stream
58         lst = readStream();
59         assertEquals("[]", lst.toString());
60     }
61
62     /**
63      * Reads a stream via the handler.
64      *
65      * @param text lines of text to be read
66      * @return the list of objects that were decoded from the stream
67      * @throws IOException if an error occurs
68      */
69     private List<Object> readStream(String... text) throws IOException {
70         return hdlr.readFrom(null, null, null, null, null, makeStream(text));
71     }
72
73     /**
74      * Creates an input stream from lines of text.
75      *
76      * @param text lines of text
77      * @return an input stream
78      */
79     private InputStream makeStream(String... text) {
80         return new ByteArrayInputStream(String.join("\n", text).getBytes(StandardCharsets.UTF_8));
81     }
82 }