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
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.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.models.sim.dmaap.rest;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
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;
34 public class TextMessageBodyHandlerTest {
35 private TextMessageBodyHandler hdlr;
39 hdlr = new TextMessageBodyHandler();
43 public void testIsReadable() {
44 assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf("text/plain")));
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")));
52 public void testReadFrom() throws IOException {
53 List<Object> lst = readStream("hello", "world");
54 assertEquals("[hello, world]", lst.toString());
58 assertEquals("[]", lst.toString());
62 * Reads a stream via the handler.
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
68 private List<Object> readStream(String... text) throws IOException {
69 return hdlr.readFrom(null, null, null, null, null, makeStream(text));
73 * Creates an input stream from lines of text.
75 * @param text lines of text
76 * @return an input stream
78 private InputStream makeStream(String... text) {
79 return new ByteArrayInputStream(String.join("\n", text).getBytes(StandardCharsets.UTF_8));