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