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