Debian Codegraph @

src/test/java/ezvcard/io/json/JCardDeserializerTest.java

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.junit.Before;
10 import org.junit.Test;
11
12 import com.fasterxml.jackson.core.type.TypeReference;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
15
16 import ezvcard.VCard;
17 import ezvcard.VCardVersion;
18
19 /*
20 Copyright (c) 2012-2023, Michael Angstadt
21 All rights reserved.
22
23 Redistribution and use in source and binary forms, with or without
24 modification, are permitted provided that the following conditions are met:
25
26 1. Redistributions of source code must retain the above copyright notice, this
27 list of conditions and the following disclaimer.
28 2. Redistributions in binary form must reproduce the above copyright notice,
29 this list of conditions and the following disclaimer in the documentation
30 and/or other materials provided with the distribution.
31
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
33 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
36 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43 The views and conclusions contained in the software and documentation are those
44 of the authors and should not be interpreted as representing official policies,
45 either expressed or implied, of the FreeBSD Project.
46 */
47
48 /**
49 * @author Buddy Gorven
50 * @author Michael Angstadt
51 */
52 public class JCardDeserializerTest {
53 //TODO test setScribe()
54
55 private ObjectMapper mapper;
56
57 @Before
58 public void before() {
59 mapper = new ObjectMapper();
60 }
61
62 @Test
63 public void deserialize_single() throws Exception {
64 //@formatter:off
65 String json =
66 "[\"vcard\"," +
67 "[" +
68 "[\"version\", {}, \"text\", \"4.0\"]," +
69 "[\"fn\", {}, \"text\", \"John Doe\"]" +
70 "]" +
71 "]";
72 //@formatter:on
73
74 JCardModule module = new JCardModule();
75 mapper.registerModule(module);
76
77 VCard expected = new VCard();
78 expected.setVersion(VCardVersion.V4_0);
79 expected.setFormattedName("John Doe");
80
81 VCard actual = mapper.readValue(json, VCard.class);
82 assertEquals(expected, actual);
83 }
84
85 @Test
86 public void deserialize_multiple() throws Exception {
87 //@formatter:off
88 String json =
89 "[" +
90 "[\"vcard\"," +
91 "[" +
92 "[\"version\", {}, \"text\", \"4.0\"]," +
93 "[\"fn\", {}, \"text\", \"John Doe\"]" +
94 "]" +
95 "]," +
96 "[\"vcard\"," +
97 "[" +
98 "[\"version\", {}, \"text\", \"4.0\"]," +
99 "[\"fn\", {}, \"text\", \"Jane Doe\"]" +
100 "]" +
101 "]" +
102 "]";
103 //@formatter:on
104
105 JCardModule module = new JCardModule();
106 mapper.registerModule(module);
107
108 List<VCard> expected = new ArrayList<>();
109
110 VCard vcard = new VCard();
111 vcard.setVersion(VCardVersion.V4_0);
112 vcard.setFormattedName("John Doe");
113 expected.add(vcard);
114
115 vcard = new VCard();
116 vcard.setVersion(VCardVersion.V4_0);
117 vcard.setFormattedName("Jane Doe");
118 expected.add(vcard);
119
120 List<VCard> actual = mapper.readValue(json, new TypeReference<List<VCard>>() {
121 });
122 assertEquals(expected, actual);
123 }
124
125 @Test
126 public void container() throws Exception {
127 //@formatter:off
128 String json =
129 "{" +
130 "\"contact\": [\"vcard\"," +
131 "[" +
132 "[\"version\", {}, \"text\", \"4.0\"]," +
133 "[\"fn\", {}, \"text\", \"John Doe\"]" +
134 "]" +
135 "]" +
136 "}";
137 //@formatter:on
138
139 Container container = mapper.readValue(json, Container.class);
140
141 VCard expected = new VCard();
142 expected.setVersion(VCardVersion.V4_0);
143 expected.setFormattedName("John Doe");
144
145 VCard actual = container.contact;
146 assertEquals(expected, actual);
147 }
148
149 @Test
150 public void container_null() throws Exception {
151 //@formatter:off
152 String json =
153 "{" +
154 "\"contact\": null" +
155 "}";
156 //@formatter:on
157
158 Container container = mapper.readValue(json, Container.class);
159
160 VCard actual = container.contact;
161 assertNull(actual);
162 }
163
164 private static class Container {
165 private VCard contact;
166
167 @JsonDeserialize(using = JCardDeserializer.class)
168 public void setContact(VCard contact) {
169 this.contact = contact;
170 }
171 }
172 }