-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInheritanceMapperTest.java
107 lines (90 loc) · 3.31 KB
/
InheritanceMapperTest.java
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 features;
import io.github.gfrmoretti.annotations.Map;
import io.github.gfrmoretti.annotations.FunctionMap;
import io.github.gfrmoretti.functionmap.IntegerToStringFunctionMapper;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import static io.github.gfrmoretti.AnMap.mapOrElseThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class InheritanceMapperTest {
@Test
@DisplayName("Should map class with inheritance.")
void shouldMapClassWithInheritance() {
var source = new Child("grand", "father", "name", 12);
var result = mapOrElseThrow(source, ChildTarget.class);
assertEquals("grand", result.getGrandFatherName());
assertEquals("father", result.getFatherName());
assertEquals("name", result.getFullName());
assertEquals("12", result.getAgeStr());
assertEquals(source.getCreated(), result.getCreatedAt());
}
@Test
@DisplayName("Should map class with inheritance with target without inheritance.")
void shouldMapClassWithInheritanceTargetWithoutInheritance() {
var source = new Child("grand", "father", "name", 50);
var result = mapOrElseThrow(source, SingleTarget.class);
assertEquals("grand", result.getGrandFatherName());
assertEquals("father", result.getFatherName());
assertEquals("name", result.getFullName());
assertEquals("50", result.getAgeStr());
assertEquals(source.getCreated(), result.getCreatedAt());
}
@Data
private static abstract class GrandFather {
protected String grandFatherName;
@Map("createdAt")
protected Instant created = Instant.now();
}
@Data
@EqualsAndHashCode(callSuper = true)
private static class Father extends GrandFather {
protected String fatherName;
@Map("ageStr")
@FunctionMap(IntegerToStringFunctionMapper.class)
protected Integer age;
}
@Data
@EqualsAndHashCode(callSuper = true)
private static class Child extends Father {
@Map("fullName")
private final String name;
Child(String grand, String father, String name, Integer age) {
this.grandFatherName = grand;
this.fatherName = father;
this.name = name;
this.age = age;
}
}
@Data
private static abstract class GrandFatherTarget {
protected String grandFatherName;
protected Instant createdAt;
}
@Data
@EqualsAndHashCode(callSuper = true)
private static class FatherTarget extends GrandFatherTarget {
protected String fatherName;
protected String ageStr;
}
@Data
@EqualsAndHashCode(callSuper = true)
private static class ChildTarget extends FatherTarget {
private String fullName;
ChildTarget(String grandFatherName, String fatherName, String fullName) {
this.grandFatherName = grandFatherName;
this.fatherName = fatherName;
this.fullName = fullName;
}
}
@Data
private static class SingleTarget {
private String grandFatherName;
private String fatherName;
private String fullName;
private String ageStr;
private Instant createdAt;
}
}