2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.common.endpoints.http.server.test;
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.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.util.HashMap;
35 import javax.ws.rs.core.MediaType;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
39 import org.yaml.snakeyaml.error.YAMLException;
41 public class YamlMessageBodyHandlerTest {
42 private static final String EXPECTED_EXCEPTION = "expected exception";
44 private static final String GEN_TYPE = "some-type";
45 private static final String[] subtypes = {"yaml"};
47 @SuppressWarnings("rawtypes")
48 private static final Class GEN_CLASS = MyObject.class;
50 @SuppressWarnings("unchecked")
51 private static final Class<Object> CLASS_OBJ = GEN_CLASS;
53 private YamlMessageBodyHandler hdlr;
57 hdlr = new YamlMessageBodyHandler();
61 public void testIsWriteable() {
62 for (String subtype : subtypes) {
63 assertTrue("writeable " + subtype, hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, subtype)));
67 // the remaining should be FALSE
70 assertFalse(hdlr.isWriteable(null, null, null, null));
73 assertFalse(hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, null)));
76 assertFalse(hdlr.isWriteable(null, null, null, MediaType.TEXT_HTML_TYPE));
80 public void testGetSize() {
81 assertEquals(-1, hdlr.getSize(null, null, null, null, null));
85 public void testWriteTo_testReadFrom() throws Exception {
86 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
87 MyObject obj1 = new MyObject(10);
88 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
90 Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
91 new ByteArrayInputStream(outstr.toByteArray()));
92 assertEquals(obj1.toString(), obj2.toString());
96 public void testWriteTo_DifferentTypes() throws Exception {
97 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
99 // use a derived type, but specify the base type when writing
100 MyObject obj1 = new DerivedObject(10);
101 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
103 Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
104 new ByteArrayInputStream(outstr.toByteArray()));
105 assertEquals(obj1.toString(), obj2.toString());
109 public void testWriteTo_Ex() throws Exception {
110 OutputStream outstr = new OutputStream() {
112 public void write(int value) throws IOException {
113 throw new IOException(EXPECTED_EXCEPTION);
117 MyObject obj1 = new MyObject(10);
118 assertThatThrownBy(() -> hdlr.writeTo(obj1, MyObject.class, CLASS_OBJ, null, null, null, outstr))
119 .isInstanceOf(YAMLException.class);
125 public void testIsReadable() {
126 for (String subtype : subtypes) {
127 assertTrue("readable " + subtype, hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, subtype)));
131 // the remaining should be FALSE
134 assertFalse(hdlr.isReadable(null, null, null, null));
137 assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
140 assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_HTML_TYPE));
144 public void testReadFrom_DifferentTypes() throws Exception {
145 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
146 MyObject obj1 = new MyObject(10);
147 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
149 // use a derived type, but specify the base type when reading
150 @SuppressWarnings("rawtypes")
151 Class clazz = DerivedObject.class;
153 @SuppressWarnings("unchecked")
154 Class<Object> objclazz = clazz;
156 Object obj2 = hdlr.readFrom(objclazz, CLASS_OBJ, null, null, null,
157 new ByteArrayInputStream(outstr.toByteArray()));
158 assertEquals(obj1.toString(), obj2.toString());
162 public void testReadFrom_Ex() throws Exception {
163 InputStream inpstr = new InputStream() {
165 public int read() throws IOException {
166 throw new IOException(EXPECTED_EXCEPTION);
170 assertThatThrownBy(() -> hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null, inpstr))
171 .isInstanceOf(YAMLException.class);
177 public void testReadFrom_Invalid() throws Exception {
178 InputStream inpstr = new ByteArrayInputStream("plain text".getBytes());
180 assertThatThrownBy(() -> hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null, inpstr))
181 .isInstanceOf(YAMLException.class);
187 public void testMapDouble() throws Exception {
188 MyMap map = new MyMap();
189 map.props = new HashMap<>();
190 map.props.put("plainString", "def");
191 map.props.put("negInt", -10);
192 map.props.put("doubleVal", 12.5);
193 map.props.put("posLong", 100000000000L);
195 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
196 hdlr.writeTo(map, map.getClass(), map.getClass(), null, null, null, outstr);
198 Object obj2 = hdlr.readFrom(Object.class, map.getClass(), null, null, null,
199 new ByteArrayInputStream(outstr.toByteArray()));
200 assertEquals(map.toString(), obj2.toString());
204 assertEquals(-10, map.props.get("negInt"));
205 assertEquals(100000000000L, map.props.get("posLong"));
206 assertEquals(12.5, map.props.get("doubleVal"));
209 public static class DerivedObject extends MyObject {
210 public DerivedObject(int id) {
215 public static class MyObject {
222 public MyObject(int id) {
227 public String toString() {
228 return "MyObject [id=" + id + "]";
232 private static class MyMap {
233 private Map<String, Object> props;
236 public String toString() {
237 return props.toString();