2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.sim.dmaap.rest;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.io.ByteArrayInputStream;
29 import java.io.EOFException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
33 import java.util.List;
34 import javax.ws.rs.core.MediaType;
35 import org.junit.Before;
36 import org.junit.Test;
38 public class CambriaMessageBodyHandlerTest {
39 private static final String STD_INPUT = "1.3.XAbc";
40 private static final String EXPECTED_OUTPUT = "[Abc]";
42 private CambriaMessageBodyHandler hdlr;
46 hdlr = new CambriaMessageBodyHandler();
50 public void testIsReadable() {
51 assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf("application/cambria")));
53 assertFalse(hdlr.isReadable(null, null, null, null));
54 assertFalse(hdlr.isReadable(null, null, null, MediaType.valueOf("application/other")));
55 assertFalse(hdlr.isReadable(null, null, null, MediaType.valueOf("other/cambria")));
59 public void testReadFrom() throws IOException {
60 List<Object> lst = readStream("1.11.AMessageBody", "3.3.123Foo3.3.123Bar", "0.16.You can do that..8.Or that.");
61 assertEquals("[MessageBody, Foo, Bar, You can do that., Or that.]", lst.toString());
65 assertEquals("[]", lst.toString());
69 public void testReadMessage_InvalidPartitionLength() {
70 assertThatThrownBy(() -> readStream("100000000.3.")).isInstanceOf(IOException.class)
71 .hasMessage("invalid partition length");
75 public void testReadMessage_InvalidMessageLength() {
76 assertThatThrownBy(() -> readStream("3.100000000.ABC")).isInstanceOf(IOException.class)
77 .hasMessage("invalid message length");
81 public void testSkipWhitespace() throws IOException {
83 assertEquals(EXPECTED_OUTPUT, readStream(STD_INPUT).toString());
86 assertEquals(EXPECTED_OUTPUT, readStream(" " + STD_INPUT).toString());
88 // multiple white spaces
89 assertEquals(EXPECTED_OUTPUT, readStream("\n\n\t" + STD_INPUT).toString());
93 public void testReadLength_NoDigits() throws IOException {
94 assertEquals("[]", readStream("..").toString());
98 public void testReadLength_NoDot() {
99 assertThatThrownBy(() -> readStream("3.2")).isInstanceOf(EOFException.class)
100 .hasMessage("missing '.' in 'length' field");
104 public void testReadLength_NonDigit() {
105 assertThatThrownBy(() -> readStream("3.2x.ABCde")).isInstanceOf(IOException.class)
106 .hasMessage("invalid character in 'length' field");
110 public void testReadLength_TooManyDigits() {
111 assertThatThrownBy(() -> readStream("3.12345678901234567890.ABCde")).isInstanceOf(IOException.class)
112 .hasMessage("too many digits in 'length' field");
116 public void testReadString_ZeroLength() throws IOException {
117 assertEquals("[]", readStream("1..X").toString());
121 public void testReadString_TooShort() {
122 assertThatThrownBy(() -> readStream(".5.me")).isInstanceOf(EOFException.class).hasMessageContaining("actual");
126 * Reads a stream via the handler.
128 * @param text lines of text to be read
129 * @return the list of objects that were decoded from the stream
130 * @throws IOException if an error occurs
132 private List<Object> readStream(String... text) throws IOException {
133 return hdlr.readFrom(null, null, null, null, null, makeStream(text));
137 * Creates an input stream from lines of text.
139 * @param text lines of text
140 * @return an input stream
142 private InputStream makeStream(String... text) {
143 return new ByteArrayInputStream(String.join("\n", text).getBytes(StandardCharsets.UTF_8));