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 com.google.gson.JsonObject;
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.util.HashMap;
32 import java.util.TreeMap;
33 import javax.ws.rs.core.MediaType;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.http.server.YamlJacksonHandler;
36 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
37 import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
38 import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
40 public class YamlJacksonHandlerTest {
43 public void test() throws Exception {
44 YamlJacksonHandler hdlr = new YamlJacksonHandler();
46 assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf(YamlMessageBodyHandler.APPLICATION_YAML)));
47 assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_PLAIN_TYPE));
49 JsonObject expected = new JsonObject();
50 expected.addProperty("myId", 100);
51 expected.addProperty("value", "a value");
52 expected.addProperty("abc", "def");
53 expected.addProperty("hello", "world");
55 Data data = new Data();
57 data.value = "a value";
58 data.props = new HashMap<>();
59 data.props.put("abc", "def");
60 data.props.put("hello", "world");
63 * Ensure everything serializes as expected.
65 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
66 hdlr.writeTo(data, Data.class, Data.class, null, null, null, outstr);
68 assertEquals("abc: def\nhello: world\nmyId: 100\nvalue: a value\n", outstr.toString("UTF-8"));
71 * Ensure everything deserializes as expected.
73 Data data2 = (Data) hdlr.readFrom(Object.class, Data.class, null, null, null,
74 new ByteArrayInputStream(outstr.toByteArray()));
76 // id is not serialized, so we must copy it manually before comparing
79 assertEquals(data.toString(), data2.toString());
83 public void testMapDouble() throws Exception {
84 MyMap map = new MyMap();
85 map.props = new HashMap<>();
86 map.props.put("plainString", "def");
87 map.props.put("negInt", -10);
88 map.props.put("doubleVal", 12.5);
89 map.props.put("posLong", 100000000000L);
91 YamlJacksonHandler hdlr = new YamlJacksonHandler();
92 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
93 hdlr.writeTo(map, map.getClass(), map.getClass(), null, null, null, outstr);
95 Object obj2 = hdlr.readFrom(Object.class, map.getClass(), null, null, null,
96 new ByteArrayInputStream(outstr.toByteArray()));
97 assertEquals(map.toString(), obj2.toString());
101 assertEquals(-10, map.props.get("negInt"));
102 assertEquals(100000000000L, map.props.get("posLong"));
103 assertEquals(12.5, map.props.get("doubleVal"));
107 * This class includes all policy-specific gson annotations.
109 public static class Data {
112 protected String value;
114 protected Map<String, String> props;
116 public int getMyId() {
120 public String getValue() {
124 public void setValue(String value) {
129 public Map<String, String> getProps() {
136 * @param name property name
137 * @param value new value
140 public void setProperty(String name, String value) {
142 props = new TreeMap<>();
145 props.put(name, value);
149 public String toString() {
150 return "Data [id=" + id + ", value=" + value + ", props=" + props + "]";
154 private static class MyMap {
155 private Map<String, Object> props;
158 public String toString() {
159 return props.toString();
162 @SuppressWarnings("unused")
163 public Map<String, Object> getProps() {
167 @SuppressWarnings("unused")
168 public void setProps(Map<String, Object> props) {