5d9186c75bbd993204eba5ad21a0ae9d706a53fd
[policy/models.git] / models-sim / models-sim-dmaap / src / test / java / org / onap / policy / models / sim / dmaap / rest / CambriaMessageBodyHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.sim.dmaap.rest;
22
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;
27
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;
37
38 public class CambriaMessageBodyHandlerTest {
39     private static final String STD_INPUT = "1.3.XAbc";
40     private static final String EXPECTED_OUTPUT = "[Abc]";
41
42     private CambriaMessageBodyHandler hdlr;
43
44     @Before
45     public void setUp() {
46         hdlr = new CambriaMessageBodyHandler();
47     }
48
49     @Test
50     public void testIsReadable() {
51         assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf("application/cambria")));
52
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")));
56     }
57
58     @Test
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());
62
63         // empty stream
64         lst = readStream();
65         assertEquals("[]", lst.toString());
66     }
67
68     @Test
69     public void testReadMessage_InvalidPartitionLength() {
70         assertThatThrownBy(() -> readStream("100000000.3.")).isInstanceOf(IOException.class)
71                         .hasMessage("invalid partition length");
72     }
73
74     @Test
75     public void testReadMessage_InvalidMessageLength() {
76         assertThatThrownBy(() -> readStream("3.100000000.ABC")).isInstanceOf(IOException.class)
77                         .hasMessage("invalid message length");
78     }
79
80     @Test
81     public void testSkipWhitespace() throws IOException {
82         // no white space
83         assertEquals(EXPECTED_OUTPUT, readStream(STD_INPUT).toString());
84
85         // single white space
86         assertEquals(EXPECTED_OUTPUT, readStream(" " + STD_INPUT).toString());
87
88         // multiple white spaces
89         assertEquals(EXPECTED_OUTPUT, readStream("\n\n\t" + STD_INPUT).toString());
90     }
91
92     @Test
93     public void testReadLength_NoDigits() throws IOException {
94         assertEquals("[]", readStream("..").toString());
95     }
96
97     @Test
98     public void testReadLength_NoDot() {
99         assertThatThrownBy(() -> readStream("3.2")).isInstanceOf(EOFException.class)
100                         .hasMessage("missing '.' in 'length' field");
101     }
102
103     @Test
104     public void testReadLength_NonDigit() {
105         assertThatThrownBy(() -> readStream("3.2x.ABCde")).isInstanceOf(IOException.class)
106                         .hasMessage("invalid character in 'length' field");
107     }
108
109     @Test
110     public void testReadLength_TooManyDigits() {
111         assertThatThrownBy(() -> readStream("3.12345678901234567890.ABCde")).isInstanceOf(IOException.class)
112                         .hasMessage("too many digits in 'length' field");
113     }
114
115     @Test
116     public void testReadString_ZeroLength() throws IOException {
117         assertEquals("[]", readStream("1..X").toString());
118     }
119
120     @Test
121     public void testReadString_TooShort() {
122         assertThatThrownBy(() -> readStream(".5.me")).isInstanceOf(EOFException.class).hasMessageContaining("actual");
123     }
124
125     /**
126      * Reads a stream via the handler.
127      *
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
131      */
132     private List<Object> readStream(String... text) throws IOException {
133         return hdlr.readFrom(null, null, null, null, null, makeStream(text));
134     }
135
136     /**
137      * Creates an input stream from lines of text.
138      *
139      * @param text lines of text
140      * @return an input stream
141      */
142     private InputStream makeStream(String... text) {
143         return new ByteArrayInputStream(String.join("\n", text).getBytes(StandardCharsets.UTF_8));
144     }
145 }