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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.models.sim.dmaap.rest;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
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;
35 public class TextMessageBodyHandlerTest {
36 private TextMessageBodyHandler hdlr;
40 hdlr = new TextMessageBodyHandler();
44 public void testIsReadable() {
45 assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf("text/plain")));
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")));
53 public void testReadFrom() throws IOException {
54 List<Object> lst = readStream("hello", "world");
55 assertEquals("[hello, world]", lst.toString());
59 assertEquals("[]", lst.toString());
63 * Reads a stream via the handler.
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
69 private List<Object> readStream(String... text) throws IOException {
70 return hdlr.readFrom(null, null, null, null, null, makeStream(text));
74 * Creates an input stream from lines of text.
76 * @param text lines of text
77 * @return an input stream
79 private InputStream makeStream(String... text) {
80 return new ByteArrayInputStream(String.join("\n", text).getBytes(StandardCharsets.UTF_8));