b8963e9baee4641aa7a81beb02955839ba28fcd5
[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.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.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.util.HashMap;
34 import java.util.Map;
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
40 public class YamlMessageBodyHandlerTest {
41     private static final String EXPECTED_EXCEPTION = "expected exception";
42
43     private static final String GEN_TYPE = "some-type";
44     private static final String[] subtypes = {"yaml"};
45
46     @SuppressWarnings("rawtypes")
47     private static final Class GEN_CLASS = MyObject.class;
48
49     @SuppressWarnings("unchecked")
50     private static final Class<Object> CLASS_OBJ = GEN_CLASS;
51
52     private YamlMessageBodyHandler hdlr;
53
54     @Before
55     public void setUp() {
56         hdlr = new YamlMessageBodyHandler();
57     }
58
59     @Test
60     public void testIsWriteable() {
61         for (String subtype : subtypes) {
62             assertTrue("writeable " + subtype, hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, subtype)));
63
64         }
65
66         // the remaining should be FALSE
67
68         // null media type
69         assertFalse(hdlr.isWriteable(null, null, null, null));
70
71         // null subtype
72         assertFalse(hdlr.isWriteable(null, null, null, new MediaType(GEN_TYPE, null)));
73
74         // text subtype
75         assertFalse(hdlr.isWriteable(null, null, null, MediaType.TEXT_HTML_TYPE));
76     }
77
78     @Test
79     public void testGetSize() {
80         assertEquals(-1, hdlr.getSize(null, null, null, null, null));
81     }
82
83     @Test
84     public void testWriteTo_testReadFrom() throws Exception {
85         ByteArrayOutputStream outstr = new ByteArrayOutputStream();
86         MyObject obj1 = new MyObject(10);
87         hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
88
89         Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
90                         new ByteArrayInputStream(outstr.toByteArray()));
91         assertEquals(obj1.toString(), obj2.toString());
92     }
93
94     @Test
95     public void testWriteTo_DifferentTypes() throws Exception {
96         ByteArrayOutputStream outstr = new ByteArrayOutputStream();
97
98         // use a derived type, but specify the base type when writing
99         MyObject obj1 = new DerivedObject(10);
100         hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr);
101
102         Object obj2 = hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null,
103                         new ByteArrayInputStream(outstr.toByteArray()));
104         assertEquals(obj1.toString(), obj2.toString());
105     }
106
107     @Test
108     public void testWriteTo_Ex() throws Exception {
109         OutputStream outstr = new OutputStream() {
110             @Override
111             public void write(int value) throws IOException {
112                 throw new IOException(EXPECTED_EXCEPTION);
113             }
114         };
115
116         MyObject obj1 = new MyObject(10);
117
118         assertThatThrownBy(() -> hdlr.writeTo(obj1, obj1.getClass(), CLASS_OBJ, null, null, null, outstr))
119                         .isInstanceOf(IOException.class);
120
121         outstr.close();
122     }
123
124     @Test
125     public void testIsReadable() {
126         for (String subtype : subtypes) {
127             assertTrue("readable " + subtype, hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, subtype)));
128
129         }
130
131         // the remaining should be FALSE
132
133         // null media type
134         assertFalse(hdlr.isReadable(null, null, null, null));
135
136         // null subtype
137         assertFalse(hdlr.isReadable(null, null, null, new MediaType(GEN_TYPE, null)));
138
139         // text subtype
140         assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_HTML_TYPE));
141     }
142
143     @Test
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);
148
149         // use a derived type, but specify the base type when reading
150         @SuppressWarnings("rawtypes")
151         Class clazz = DerivedObject.class;
152
153         @SuppressWarnings("unchecked")
154         Class<Object> objclazz = clazz;
155
156         Object obj2 = hdlr.readFrom(objclazz, CLASS_OBJ, null, null, null,
157                         new ByteArrayInputStream(outstr.toByteArray()));
158         assertEquals(obj1.toString(), obj2.toString());
159     }
160
161     @Test
162     public void testReadFrom_Ex() throws Exception {
163         InputStream inpstr = new InputStream() {
164             @Override
165             public int read() throws IOException {
166                 throw new IOException(EXPECTED_EXCEPTION);
167             }
168         };
169
170         assertThatThrownBy(() -> hdlr.readFrom(CLASS_OBJ, CLASS_OBJ, null, null, null, inpstr))
171                         .isInstanceOf(IOException.class);
172
173         inpstr.close();
174     }
175
176     @Test
177     public void testMapDouble() throws Exception {
178         MyMap map = new MyMap();
179         map.props = new HashMap<>();
180         map.props.put("plainString", "def");
181         map.props.put("negInt", -10);
182         map.props.put("doubleVal", 12.5);
183         map.props.put("posLong", 100000000000L);
184
185         ByteArrayOutputStream outstr = new ByteArrayOutputStream();
186         hdlr.writeTo(map, map.getClass(), map.getClass(), null, null, null, outstr);
187
188         Object obj2 = hdlr.readFrom(Object.class, map.getClass(), null, null, null,
189                         new ByteArrayInputStream(outstr.toByteArray()));
190         assertEquals(map.toString(), obj2.toString());
191
192         map = (MyMap) obj2;
193
194         assertEquals(-10, map.props.get("negInt"));
195         assertEquals(100000000000L, map.props.get("posLong"));
196         assertEquals(12.5, map.props.get("doubleVal"));
197     }
198
199     public static class DerivedObject extends MyObject {
200         public DerivedObject(int id) {
201             super(id);
202         }
203     }
204
205     public static class MyObject {
206         private int id;
207
208         public MyObject() {
209             super();
210         }
211
212         public MyObject(int id) {
213             this.id = id;
214         }
215
216         @Override
217         public String toString() {
218             return "MyObject [id=" + id + "]";
219         }
220     }
221
222     private static class MyMap {
223         private Map<String, Object> props;
224
225         @Override
226         public String toString() {
227             return props.toString();
228         }
229     }
230 }