8d65bbd6a5e045da56ec6f59a1f2270c98ca9624
[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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import com.google.gson.JsonObject;
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.util.HashMap;
31 import java.util.Map;
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;
40
41 public class YamlJacksonHandlerTest {
42
43     @Test
44     public void test() throws Exception {
45         YamlJacksonHandler hdlr = new YamlJacksonHandler();
46
47         assertTrue(hdlr.isReadable(null, null, null, MediaType.valueOf(YamlMessageBodyHandler.APPLICATION_YAML)));
48         assertFalse(hdlr.isReadable(null, null, null, MediaType.TEXT_PLAIN_TYPE));
49
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");
55
56         Data data = new Data();
57         data.id = 10;
58         data.value = "a value";
59         data.props = new HashMap<>();
60         data.props.put("abc", "def");
61         data.props.put("hello", "world");
62
63         /*
64          * Ensure everything serializes as expected.
65          */
66         ByteArrayOutputStream outstr = new ByteArrayOutputStream();
67         hdlr.writeTo(data, Data.class, Data.class, null, null, null, outstr);
68
69         assertEquals("abc: def\nhello: world\nmyId: 100\nvalue: a value\n", outstr.toString("UTF-8"));
70
71         /*
72          * Ensure everything deserializes as expected.
73          */
74         Data data2 = (Data) hdlr.readFrom(Object.class, Data.class, null, null, null,
75                         new ByteArrayInputStream(outstr.toByteArray()));
76
77         // id is not serialized, so we must copy it manually before comparing
78         data2.id = data.id;
79
80         assertEquals(data.toString(), data2.toString());
81     }
82
83     @Test
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);
91
92         YamlJacksonHandler hdlr = new YamlJacksonHandler();
93         ByteArrayOutputStream outstr = new ByteArrayOutputStream();
94         hdlr.writeTo(map, map.getClass(), map.getClass(), null, null, null, outstr);
95
96         Object obj2 = hdlr.readFrom(Object.class, map.getClass(), null, null, null,
97                         new ByteArrayInputStream(outstr.toByteArray()));
98         assertEquals(map.toString(), obj2.toString());
99
100         map = (MyMap) obj2;
101
102         assertEquals(-10, map.props.get("negInt"));
103         assertEquals(100000000000L, map.props.get("posLong"));
104         assertEquals(12.5, map.props.get("doubleVal"));
105     }
106
107     /**
108      * This class includes all policy-specific gson annotations.
109      */
110     @ToString
111     public static class Data {
112         protected int id;
113
114         protected String value;
115
116         protected Map<String, String> props;
117
118         public int getMyId() {
119             return 100;
120         }
121
122         public String getValue() {
123             return value;
124         }
125
126         public void setValue(String value) {
127             this.value = value;
128         }
129
130         @GsonJsonAnyGetter
131         public Map<String, String> getProps() {
132             return props;
133         }
134
135         /**
136          * Sets a property.
137          *
138          * @param name property name
139          * @param value new value
140          */
141         @GsonJsonAnySetter
142         public void setProperty(String name, String value) {
143             if (props == null) {
144                 props = new TreeMap<>();
145             }
146
147             props.put(name, value);
148         }
149     }
150
151     private static class MyMap {
152         private Map<String, Object> props;
153
154         @Override
155         public String toString() {
156             return props.toString();
157         }
158
159         @SuppressWarnings("unused")
160         public Map<String, Object> getProps() {
161             return props;
162         }
163
164         @SuppressWarnings("unused")
165         public void setProps(Map<String, Object> props) {
166             this.props = props;
167         }
168     }
169 }