9cfc3ead0ee85ba9417d029921714c0f33348c18
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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.ccsdk.features.sdnr.wt.dataprovider.test.issues;
23
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.SerializationFeature;
27 import java.io.IOException;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.opendaylight.yangtools.yang.common.Uint32;
31
32 public class TestIssue227 extends Mockito {
33
34     static String inputJsonString = "{\"value1\":\"forty-two\", \"value2\":\"forty-three\"}";
35     static String inputJsonNumber = "{\"value1\":42, \"value2\":43}";
36
37     @Test
38     public void testWithException() {
39         String inputJson = inputJsonNumber;
40         System.out.println("Input " + inputJson);
41
42         ObjectMapper mapper = new ObjectMapper();
43         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
44
45         doMapping(mapper, inputJson);
46     }
47
48     @Test
49     public void testWithMixin() {
50         String inputJson = inputJsonNumber;
51         System.out.println("Input " + inputJson);
52
53         ObjectMapper mapper = new ObjectMapper();
54         mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
55         mapper.addMixIn(TestBuilder.class, IgnoreFooSetValueIntMixIn.class);
56
57         doMapping(mapper, inputJson);
58     }
59
60
61     private void doMapping(ObjectMapper mapper, String json) {
62         TestBuilder foo;
63         try {
64             foo = mapper.readValue(json.getBytes(), TestBuilder.class);
65             System.out.println("Foo " + foo);
66             System.out.println(mapper.writeValueAsString(foo));
67         } catch (IOException e) {
68             e.printStackTrace();
69         }
70     }
71
72     static class TestBuilder {
73
74         private String value1;
75         private String value2;
76
77         String getValue1() {
78             return value1;
79         }
80
81         public void setValue1(String value) {
82             this.value1 = value;
83         }
84
85         public void setValue1(int value) {
86             this.value1 = String.valueOf(value);
87         }
88
89         public void setValue1(long value) {
90             this.value1 = String.valueOf(value);
91         }
92
93         public void setValue1(Uint32 value) {
94             this.value1 = String.valueOf(value);
95         }
96
97         public String getValue2() {
98             return value2;
99         }
100
101         public void setValue2(String value) {
102             this.value2 = value;
103         }
104
105         public void setValue2(int value) {
106             this.value2 = String.valueOf(value);
107         }
108
109         @Override
110         public String toString() {
111             return "Foo [value1=" + value1 + ", value2=" + value2 + "]";
112         }
113
114     }
115
116
117     private abstract class IgnoreFooSetValueIntMixIn {
118         @JsonProperty
119         public abstract void setValue1(String value);
120
121         @JsonProperty
122         public abstract void setValue2(String value);
123     }
124 }
125
126