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