| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class JCardDeserializerTest { |
| 53 |
|
| 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 |
|
| 65 |
String json = |
| 66 |
"[\"vcard\"," + |
| 67 |
"[" + |
| 68 |
"[\"version\", {}, \"text\", \"4.0\"]," + |
| 69 |
"[\"fn\", {}, \"text\", \"John Doe\"]" + |
| 70 |
"]" + |
| 71 |
"]"; |
| 72 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 128 |
String json = |
| 129 |
"{" + |
| 130 |
"\"contact\": [\"vcard\"," + |
| 131 |
"[" + |
| 132 |
"[\"version\", {}, \"text\", \"4.0\"]," + |
| 133 |
"[\"fn\", {}, \"text\", \"John Doe\"]" + |
| 134 |
"]" + |
| 135 |
"]" + |
| 136 |
"}"; |
| 137 |
|
| 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 |
|
| 152 |
String json = |
| 153 |
"{" + |
| 154 |
"\"contact\": null" + |
| 155 |
"}"; |
| 156 |
|
| 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 |
} |