2 * ============LICENSE_START=======================================================
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
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 lombok.ToString;
35 import org.junit.Test;
36 import org.onap.policy.common.endpoints.http.server.YamlJacksonHandler;
37 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
38 import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
39 import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
41 public class YamlJacksonHandlerTest {
44 public void test() throws Exception {
45 YamlJacksonHandler hdlr = new YamlJacksonHandler();
47 assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf(YamlMessageBodyHandler.APPLICATION_YAML)));
48 assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_PLAIN_TYPE));
50 JsonObject expected = new JsonObject();
51 expected.addProperty("myId", 100);
52 expected.addProperty("value", "a value");
53 expected.addProperty("abc", "def");
54 expected.addProperty("hello", "world");
56 Data data = new Data();
58 data.value = "a value";
59 data.props = new HashMap<>();
60 data.props.put("abc", "def");
61 data.props.put("hello", "world");
64 * Ensure everything serializes as expected.
66 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
67 hdlr.writeTo(data, Data.class, Data.class, null, null, null, outstr);
69 assertEquals("abc: def\nhello: world\nmyId: 100\nvalue: a value\n", outstr.toString("UTF-8"));
72 * Ensure everything deserializes as expected.
74 Data data2 = (Data) hdlr.readFrom(Object.class, Data.class, null, null, null,
75 new ByteArrayInputStream(outstr.toByteArray()));
77 // id is not serialized, so we must copy it manually before comparing
80 assertEquals(data.toString(), data2.toString());
84 public void testMapDouble() throws Exception {
85 MyMap map = new MyMap();
86 map.props = new HashMap<>();
87 map.props.put("plainString", "def");
88 map.props.put("negInt", -10);
89 map.props.put("doubleVal", 12.5);
90 map.props.put("posLong", 100000000000L);
92 YamlJacksonHandler hdlr = new YamlJacksonHandler();
93 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
94 hdlr.writeTo(map, map.getClass(), map.getClass(), null, null, null, outstr);
96 Object obj2 = hdlr.readFrom(Object.class, map.getClass(), null, null, null,
97 new ByteArrayInputStream(outstr.toByteArray()));
98 assertEquals(map.toString(), obj2.toString());
102 assertEquals(-10, map.props.get("negInt"));
103 assertEquals(100000000000L, map.props.get("posLong"));
104 assertEquals(12.5, map.props.get("doubleVal"));
108 * This class includes all policy-specific gson annotations.
111 public static class Data {
114 protected String value;
116 protected Map<String, String> props;
118 public int getMyId() {
122 public String getValue() {
126 public void setValue(String value) {
131 public Map<String, String> getProps() {
138 * @param name property name
139 * @param value new value
142 public void setProperty(String name, String value) {
144 props = new TreeMap<>();
147 props.put(name, value);
151 private static class MyMap {
152 private Map<String, Object> props;
155 public String toString() {
156 return props.toString();
159 @SuppressWarnings("unused")
160 public Map<String, Object> getProps() {
164 @SuppressWarnings("unused")
165 public void setProps(Map<String, Object> props) {