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