2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 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.common.endpoints.http.server.test;
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.ByteArrayOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.util.HashMap;
37 import lombok.ToString;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
41 import org.yaml.snakeyaml.error.YAMLException;
43 public class YamlMessageBodyHandlerTest {
44 private static final String EXPECTED_EXCEPTION = "expected exception";
46 private static final String GEN_TYPE = "some-type";
47 private static final String[] subtypes = {"yaml"};
49 @SuppressWarnings("rawtypes")
50 private static final Class GEN_CLASS = MyObject.class;
52 @SuppressWarnings("unchecked")
53 private static final Class<Object> CLASS_OBJ = GEN_CLASS;
55 private YamlMessageBodyHandler hdlr;
59 hdlr = new YamlMessageBodyHandler();
63 public void testIsWriteable() {
64 for (String subtype : subtypes) {
65 assertTrue("writeable " + subtype, hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, subtype)));
69 // the remaining should be FALSE
72 assertFalse(hdlr.isWriteable(null, null, null, null));
75 assertFalse(hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, null)));
78 assertFalse(hdlr.isWriteable(null, null, null, MediaType.TEXT_HTML_TYPE));
82 public void testGetSize() {
83 assertEquals(-1, hdlr.getSize(null, null, null, null, null));
87 public void testWriteTo_testReadFrom() throws Exception {
88 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
89 MyObject obj1 = new MyObject(10);
90 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
92 Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
93 new ByteArrayInputStream(outstr.toByteArray()));
94 assertEquals(obj1.toString(), obj2.toString());
98 public void testWriteTo_DifferentTypes() throws Exception {
99 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
101 // use a derived type, but specify the base type when writing
102 MyObject obj1 = new DerivedObject(10);
103 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
105 Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
106 new ByteArrayInputStream(outstr.toByteArray()));
107 assertEquals(obj1.toString(), obj2.toString());
111 public void testWriteTo_Ex() throws Exception {
112 OutputStream outstr = new OutputStream() {
114 public void write(int value) throws IOException {
115 throw new IOException(EXPECTED_EXCEPTION);
119 MyObject obj1 = new MyObject(10);
120 assertThatThrownBy(() -> hdlr.writeTo(obj1, MyObject.class, CLASS_OBJ, null, null, null, outstr))
121 .isInstanceOf(YAMLException.class);
127 public void testIsReadable() {
128 for (String subtype : subtypes) {
129 assertTrue("readable " + subtype, hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, subtype)));
133 // the remaining should be FALSE
136 assertFalse(hdlr.isReadable(null, null, null, null));
139 assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
142 assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_HTML_TYPE));
146 public void testReadFrom_DifferentTypes() throws Exception {
147 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
148 MyObject obj1 = new MyObject(10);
149 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
151 // use a derived type, but specify the base type when reading
152 @SuppressWarnings("rawtypes")
153 Class clazz = DerivedObject.class;
155 @SuppressWarnings("unchecked")
156 Class<Object> objclazz = clazz;
158 Object obj2 = hdlr.readFrom(objclazz, CLASS_OBJ, null, null, null,
159 new ByteArrayInputStream(outstr.toByteArray()));
160 assertEquals(obj1.toString(), obj2.toString());
164 public void testReadFrom_Ex() throws Exception {
165 InputStream inpstr = new InputStream() {
167 public int read() throws IOException {
168 throw new IOException(EXPECTED_EXCEPTION);
172 assertThatThrownBy(() -> hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null, inpstr))
173 .isInstanceOf(YAMLException.class);
179 public void testReadFrom_Invalid() throws Exception {
180 InputStream inpstr = new ByteArrayInputStream("plain text".getBytes());
182 assertThatThrownBy(() -> hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null, inpstr))
183 .isInstanceOf(YAMLException.class);
189 public void testMapDouble() throws Exception {
190 MyMap map = new MyMap();
191 map.props = new HashMap<>();
192 map.props.put("plainString", "def");
193 map.props.put("negInt", -10);
194 map.props.put("doubleVal", 12.5);
195 map.props.put("posLong", 100000000000L);
197 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
198 hdlr.writeTo(map, map.getClass(), map.getClass(), null, null, null, outstr);
200 Object obj2 = hdlr.readFrom(Object.class, map.getClass(), null, null, null,
201 new ByteArrayInputStream(outstr.toByteArray()));
202 assertEquals(map.toString(), obj2.toString());
206 assertEquals(-10, map.props.get("negInt"));
207 assertEquals(100000000000L, map.props.get("posLong"));
208 assertEquals(12.5, map.props.get("doubleVal"));
211 public static class DerivedObject extends MyObject {
212 public DerivedObject(int id) {
218 public static class MyObject {
225 public MyObject(int id) {
230 private static class MyMap {
231 private Map<String, Object> props;
234 public String toString() {
235 return props.toString();