2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.common.endpoints.http.server.test;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import com.google.gson.JsonObject;
29 import jakarta.ws.rs.core.MediaType;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.nio.charset.StandardCharsets;
33 import java.util.HashMap;
35 import java.util.TreeMap;
36 import lombok.ToString;
37 import org.junit.Test;
38 import org.onap.policy.common.endpoints.http.server.YamlJacksonHandler;
39 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
40 import org.onap.policy.common.gson.annotation.GsonJsonAnyGetter;
41 import org.onap.policy.common.gson.annotation.GsonJsonAnySetter;
43 public class YamlJacksonHandlerTest {
46 public void test() throws Exception {
47 YamlJacksonHandler hdlr = new YamlJacksonHandler();
49 assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf(YamlMessageBodyHandler.APPLICATION_YAML)));
50 assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_PLAIN_TYPE));
52 JsonObject expected = new JsonObject();
53 expected.addProperty("myId", 100);
54 expected.addProperty("value", "a value");
55 expected.addProperty("abc", "def");
56 expected.addProperty("hello", "world");
58 Data data = new Data();
60 data.value = "a value";
61 data.props = new HashMap<>();
62 data.props.put("abc", "def");
63 data.props.put("hello", "world");
66 * Ensure everything serializes as expected.
68 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
69 hdlr.writeTo(data, Data.class, Data.class, null, null, null, outstr);
71 assertEquals("abc: def\nhello: world\nmyId: 100\nvalue: a value\n", outstr.toString(StandardCharsets.UTF_8));
74 * Ensure everything deserializes as expected.
76 Data data2 = (Data) hdlr.readFrom(Object.class, Data.class, null, null, null,
77 new ByteArrayInputStream(outstr.toByteArray()));
79 // id is not serialized, so we must copy it manually before comparing
82 assertEquals(data.toString(), data2.toString());
86 public void testMapDouble() throws Exception {
87 MyMap map = new MyMap();
88 map.props = new HashMap<>();
89 map.props.put("plainString", "def");
90 map.props.put("negInt", -10);
91 map.props.put("doubleVal", 12.5);
92 map.props.put("posLong", 100000000000L);
94 YamlJacksonHandler hdlr = new YamlJacksonHandler();
95 ByteArrayOutputStream outstr = new ByteArrayOutputStream();
96 hdlr.writeTo(map, map.getClass(), map.getClass(), null, null, null, outstr);
98 Object obj2 = hdlr.readFrom(Object.class, map.getClass(), null, null, null,
99 new ByteArrayInputStream(outstr.toByteArray()));
100 assertEquals(map.toString(), obj2.toString());
104 assertEquals(-10, map.props.get("negInt"));
105 assertEquals(100000000000L, map.props.get("posLong"));
106 assertEquals(12.5, map.props.get("doubleVal"));
110 * This class includes all policy-specific gson annotations.
113 public static class Data {
116 protected String value;
118 protected Map<String, String> props;
120 public int getMyId() {
124 public String getValue() {
128 public void setValue(String value) {
133 public Map<String, String> getProps() {
140 * @param name property name
141 * @param value new value
144 public void setProperty(String name, String value) {
146 props = new TreeMap<>();
149 props.put(name, value);
153 private static class MyMap {
154 private Map<String, Object> props;
157 public String toString() {
158 return props.toString();
161 @SuppressWarnings("unused")
162 public Map<String, Object> getProps() {
166 @SuppressWarnings("unused")
167 public void setProps(Map<String, Object> props) {