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