2dfbae98026a20e386d51a752cbb489f91c0ff6f
[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  * ================================================================================
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  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.models.sim.dmaap.rest;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.nio.charset.StandardCharsets;
29 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 public class TextMessageBodyHandlerTest {
35     private TextMessageBodyHandler hdlr;
36
37     @Before
38     public void setUp() {
39         hdlr = new TextMessageBodyHandler();
40     }
41
42     @Test
43     public void testIsReadable() {
44         assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf("text/plain")));
45
46         assertFalse(hdlr.isReadable(null, null, null, null));
47         assertFalse(hdlr.isReadable(null, null, null, MediaType.valueOf("text/other")));
48         assertFalse(hdlr.isReadable(null, null, null, MediaType.valueOf("other/plain")));
49     }
50
51     @Test
52     public void testReadFrom() throws IOException {
53         List<Object> lst = readStream("hello", "world");
54         assertEquals("[hello, world]", lst.toString());
55
56         // empty stream
57         lst = readStream();
58         assertEquals("[]", lst.toString());
59     }
60
61     /**
62      * Reads a stream via the handler.
63      *
64      * @param text lines of text to be read
65      * @return the list of objects that were decoded from the stream
66      * @throws IOException if an error occurs
67      */
68     private List<Object> readStream(String... text) throws IOException {
69         return hdlr.readFrom(null, null, null, null, null, makeStream(text));
70     }
71
72     /**
73      * Creates an input stream from lines of text.
74      *
75      * @param text lines of text
76      * @return an input stream
77      */
78     private InputStream makeStream(String... text) {
79         return new ByteArrayInputStream(String.join("\n", text).getBytes(StandardCharsets.UTF_8));
80     }
81 }