-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapperTest.java
More file actions
107 lines (82 loc) · 2.85 KB
/
Copy pathMapperTest.java
File metadata and controls
107 lines (82 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package tolerant.mapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.junit.Test;
public class MapperTest {
@Test
public void ensureCreateMapperForTypeThroughStaticFactoryMethod() {
Mapper<String> mapper = Mapper.forType(String.class);
assertNotNull("Not an instance", mapper);
assertTrue("Wrong type", Mapper.class.isAssignableFrom(mapper.getClass()));
}
@Test
public void ensureMapsMapPropertyToAnnotatedFields() throws Exception {
Map<Object, Object> properties = new HashMap<>();
properties.put("active", true);
Map<Object, Object> raul = new HashMap<>();
raul.put("name", "Raul Esteban Marques");
raul.put("age", 71);
raul.put("properties", properties);
Map<Object, Object> map = new HashMap<>();
map.put("foo", raul);
Foo foo = Mapper.forType(Foo.class).transform(map);
assertEquals("Wrong name", "Raul Esteban Marques", foo.name);
assertEquals("Wrong age", 71, foo.age);
assertEquals("Not active", true, foo.active);
}
@Test
public void ensureMapsMapPropertiesToAnnotatedFieldsAsNullableOptionals() throws Exception {
Map<Object, Object> vcard = new HashMap<>();
vcard.put("nickName", "roggy");
Map<Object, Object> user = new HashMap<>();
user.put("name", "rmoore");
user.put("vcard", vcard);
Map<Object, Object> map = new HashMap<>();
map.put("user", user);
Bar bar = Mapper.forType(Bar.class).transform(map);
assertEquals("Wrong username", "rmoore", bar.username);
assertEquals("Must not be present", false, bar.niceName.isPresent());
assertEquals("Wrong nickname", "roggy", bar.nickName.get());
}
@Test
public void ensureMapsArrayPropertiesToAnnotatedFields() throws Exception {
Map<Object, Object> vcard = new HashMap<>();
vcard.put("numbers", new String[] { "132", "321", "123" });
Map<Object, Object> user = new HashMap<>();
user.put("vcard", vcard);
Map<Object, Object> map = new HashMap<>();
map.put("user", user);
Baz baz = Mapper.forType(Baz.class).transform(map);
assertEquals("Wrong home number", "132", baz.home);
assertEquals("Wrong office number", "321", baz.office);
assertEquals("Wrong mobile number", "123", baz.mobile);
}
public static class Foo {
@Path("foo.name")
private String name;
@Path("foo.age")
private int age;
@Path("foo.properties.active")
private Boolean active;
}
public static class Bar {
@Path("user.name")
private String username;
@Path("user.vcard.fullName")
private Optional<String> niceName;
@Path("user.vcard.nickName")
private Optional<String> nickName;
}
public static class Baz {
@Path("user.vcard.numbers[0]")
private String home;
@Path("user.vcard.numbers[1]")
private String office;
@Path("user.vcard.numbers[2]")
private String mobile;
}
}