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.common.endpoints.http.server.test;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import javax.ws.rs.core.MediaType;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.endpoints.http.server.internal.GsonMessageBodyHandler;
34 public class GsonMessageBodyHandlerTest {
35 private static final String GEN_TYPE = "some-type";
36 private static final String[] subtypes = {"json", "jSoN", "hello+json", "javascript", "x-javascript", "x-json"};
38 @SuppressWarnings("rawtypes")
39 private static final Class GEN_CLASS = MyObject.class;
41 @SuppressWarnings("unchecked")
42 private static final Class<Object> CLASS_OBJ = GEN_CLASS;
44 private GsonMessageBodyHandler hdlr;
48 hdlr = new GsonMessageBodyHandler();
52 public void testIsWriteable() {
54 assertTrue(hdlr.isWriteable(null, null, null, null));
56 for (String subtype : subtypes) {
57 assertTrue("writeable " + subtype, hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, subtype)));
61 // the remaining should be FALSE
64 assertFalse(hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, null)));
67 assertFalse(hdlr.isWriteable(null, null, null, MediaType.TEXT_HTML_TYPE));
71 public void testGetSize() {
72 assertEquals(-1, hdlr.getSize(null, null, null, null, null));
76 public void testWriteTo_testReadFrom() throws Exception {
77 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
78 MyObject obj1 = new MyObject(10);
79 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
81 Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
82 new ByteArrayInputStream(outstr.toByteArray()));
83 assertEquals(obj1.toString(), obj2.toString());
87 public void testWriteTo_DifferentTypes() throws Exception {
88 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
90 // use a derived type, but specify the base type when writing
91 MyObject obj1 = new MyObject(10) {};
92 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
94 Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
95 new ByteArrayInputStream(outstr.toByteArray()));
96 assertEquals(obj1.toString(), obj2.toString());
100 public void testIsReadable() {
102 assertTrue(hdlr.isReadable(null, null, null, null));
105 assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
107 for (String subtype : subtypes) {
108 assertTrue("readable " + subtype, hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, subtype)));
112 // the remaining should be FALSE
115 assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
118 assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_HTML_TYPE));
122 public void testReadFrom_DifferentTypes() throws Exception {
123 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
124 MyObject obj1 = new MyObject(10);
125 hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
127 // use a derived type, but specify the base type when reading
128 @SuppressWarnings("rawtypes")
129 Class clazz = new MyObject() {}.getClass();
131 @SuppressWarnings("unchecked")
132 Class<Object> objclazz = clazz;
134 Object obj2 = hdlr.readFrom(objclazz, CLASS_OBJ, null, null, null,
135 new ByteArrayInputStream(outstr.toByteArray()));
136 assertEquals(obj1.toString(), obj2.toString());
139 public static class MyObject {
146 public MyObject(int id) {
151 public String toString() {
152 return "MyObject [id=" + id + "]";