9c6ec80d90332017c6618e005e92a17b0d263f71
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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.common.endpoints.http.server.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
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;
33
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"};
37
38     @SuppressWarnings("rawtypes")
39     private static final Class GEN_CLASS = MyObject.class;
40
41     @SuppressWarnings("unchecked")
42     private static final Class<Object> CLASS_OBJ = GEN_CLASS;
43
44     private GsonMessageBodyHandler hdlr;
45
46     @Before
47     public void setUp() {
48         hdlr = new GsonMessageBodyHandler();
49     }
50
51     @Test
52     public void testIsWriteable() {
53         // null media type
54         assertTrue(hdlr.isWriteable(null, null, null, null));
55
56         for (String subtype : subtypes) {
57             assertTrue("writeable " + subtype, hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, subtype)));
58
59         }
60
61         // the remaining should be FALSE
62
63         // null subtype
64         assertFalse(hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, null)));
65
66         // text subtype
67         assertFalse(hdlr.isWriteable(null, null, null, MediaType.TEXT_HTML_TYPE));
68     }
69
70     @Test
71     public void testGetSize() {
72         assertEquals(-1, hdlr.getSize(null, null, null, null, null));
73     }
74
75     @Test
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);
80
81         Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
82                         new ByteArrayInputStream(outstr.toByteArray()));
83         assertEquals(obj1.toString(), obj2.toString());
84     }
85
86     @Test
87     public void testWriteTo_DifferentTypes() throws Exception {
88         ByteArrayOutputStream outstr = new ByteArrayOutputStream();
89
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);
93
94         Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
95                         new ByteArrayInputStream(outstr.toByteArray()));
96         assertEquals(obj1.toString(), obj2.toString());
97     }
98
99     @Test
100     public void testIsReadable() {
101         // null media type
102         assertTrue(hdlr.isReadable(null, null, null, null));
103
104         // null subtype
105         assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
106
107         for (String subtype : subtypes) {
108             assertTrue("readable " + subtype, hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, subtype)));
109
110         }
111
112         // the remaining should be FALSE
113
114         // null subtype
115         assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
116
117         // text subtype
118         assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_HTML_TYPE));
119     }
120
121     @Test
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);
126
127         // use a derived type, but specify the base type when reading
128         @SuppressWarnings("rawtypes")
129         Class clazz = new MyObject() {}.getClass();
130
131         @SuppressWarnings("unchecked")
132         Class<Object> objclazz = clazz;
133
134         Object obj2 = hdlr.readFrom(objclazz, CLASS_OBJ, null, null, null,
135                         new ByteArrayInputStream(outstr.toByteArray()));
136         assertEquals(obj1.toString(), obj2.toString());
137     }
138
139     public static class MyObject {
140         private int id;
141
142         public MyObject() {
143             super();
144         }
145
146         public MyObject(int id) {
147             this.id = id;
148         }
149
150         @Override
151         public String toString() {
152             return "MyObject [id=" + id + "]";
153         }
154     }
155
156 }