-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
SnapshotFactoryTest.groovy
219 lines (182 loc) · 9.31 KB
/
SnapshotFactoryTest.groovy
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package org.javers.core.snapshot
import org.javers.common.exception.JaversException
import org.javers.common.exception.JaversExceptionCode
import org.javers.core.JaversTestBuilder
import org.javers.core.commit.CommitId
import org.javers.core.commit.CommitMetadata
import org.javers.core.metamodel.object.CdoSnapshot
import org.javers.core.model.DummyAddress
import org.javers.core.model.PrimitiveEntity
import org.javers.core.model.SnapshotEntity
import org.joda.time.LocalDate
import org.joda.time.LocalDateTime
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
import static JaversExceptionCode.VALUE_OBJECT_IS_NOT_SUPPORTED_AS_MAP_KEY
import static org.javers.core.metamodel.object.InstanceIdDTO.instanceId
import static org.javers.core.metamodel.object.ValueObjectIdDTO.valueObjectId
/**
* @author bartosz walacik
*/
class SnapshotFactoryTest extends Specification{
@Shared JaversTestBuilder javers
def setupSpec(){
javers = JaversTestBuilder.javersTestAssembly()
}
def "should create snapshot with given GlobalId"() {
given:
def snapshotFactory = javers.snapshotFactory
def cdo = new SnapshotEntity(id:1)
def id = javers.instanceId(cdo)
when:
CdoSnapshot snapshot = snapshotFactory.create(cdo, id, new CommitMetadata("kazik", LocalDateTime.now(), new CommitId(1, 0)))
then:
snapshot.globalId == id
}
def "should skip primitives with default value"() {
given:
def snapshotFactory = javers.snapshotFactory
def cdo = new PrimitiveEntity()
def id = javers.instanceId(cdo)
when:
CdoSnapshot snapshot = snapshotFactory.create(cdo, id, new CommitMetadata("kazik", LocalDateTime.now(), new CommitId(1, 0)))
then:
snapshot.size() == 0
}
@Unroll
def "should record #propertyType property value"() {
given:
def snapshotFactory = javers.snapshotFactory
when:
CdoSnapshot snapshot = snapshotFactory.create(cdo, javers.instanceId(cdo),
new CommitMetadata("kazik", LocalDateTime.now(), new CommitId(1, 0)))
then:
snapshot.getPropertyValue(propertyName) == cdo.getAt(propertyName)
where:
propertyType << ["Primitive", "Value"]
propertyName << ["intProperty","dob"]
cdo << [new SnapshotEntity(intProperty: 5),
new SnapshotEntity(dob: new LocalDate(2000,1,1))]
}
@Unroll
def "should record #propertyType reference"() {
given:
def snapshotFactory = javers.snapshotFactory
when:
CdoSnapshot snapshot = snapshotFactory.create(cdo, javers.instanceId(cdo),
new CommitMetadata("kazik", LocalDateTime.now(), new CommitId(1, 0)))
then:
snapshot.getPropertyValue(propertyName) == expectedVal
where:
propertyType << ["Entity","ValueObject"]
propertyName << ["entityRef", "valueObjectRef"]
cdo << [new SnapshotEntity(id:1, entityRef:new SnapshotEntity(id:5)),
new SnapshotEntity(id:1, valueObjectRef: new DummyAddress("street"))]
expectedVal << [instanceId(5, SnapshotEntity),
valueObjectId(1, SnapshotEntity, "valueObjectRef")]
}
@Unroll
def "should record #containerType of #propertyType"() {
given:
def snapshotFactory = javers.snapshotFactory
when:
CdoSnapshot snapshot = snapshotFactory.create(cdo, javers.instanceId(cdo),
new CommitMetadata("kazik", LocalDateTime.now(), new CommitId(1, 0)))
then:
snapshot.getPropertyValue(propertyName) == expectedVal
//we need shallow copy
System.identityHashCode(snapshot.getPropertyValue(propertyName)) !=
System.identityHashCode( cdo.getAt(propertyName) )
where:
containerType << ["Array"]*4 +["List"]*4
propertyType << ["Primitive", "Value", "Entity", "ValueObject"]*2
propertyName << ["arrayOfIntegers", "arrayOfDates", "arrayOfEntities", "arrayOfValueObjects",
"listOfIntegers", "listOfDates", "listOfEntities", "listOfValueObjects"]
cdo << [
new SnapshotEntity(arrayOfIntegers: [1, 2]),
new SnapshotEntity(arrayOfDates: [new LocalDate(2000, 1, 1), new LocalDate(2002, 1, 1)]),
new SnapshotEntity(arrayOfEntities: [new SnapshotEntity(id:2), new SnapshotEntity(id:3)]),
new SnapshotEntity(arrayOfValueObjects: [new DummyAddress("London"), new DummyAddress("London City")]),
new SnapshotEntity(listOfIntegers: [1, 2]),
new SnapshotEntity(listOfDates: [new LocalDate(2000, 1, 1), new LocalDate(2002, 1, 1)]),
new SnapshotEntity(listOfEntities: [new SnapshotEntity(id:2), new SnapshotEntity(id:3)]),
new SnapshotEntity(listOfValueObjects: [new DummyAddress("London"), new DummyAddress("London City")])
]
expectedVal << [
[1, 2],
[new LocalDate(2000, 1, 1), new LocalDate(2002, 1, 1)],
[instanceId(2, SnapshotEntity), instanceId(3, SnapshotEntity)],
[valueObjectId(1, SnapshotEntity,"arrayOfValueObjects/0"),
valueObjectId(1, SnapshotEntity,"arrayOfValueObjects/1")] ,
[1, 2],
[new LocalDate(2000, 1, 1), new LocalDate(2002, 1, 1)],
[instanceId(2, SnapshotEntity), instanceId(3, SnapshotEntity)],
[valueObjectId(1, SnapshotEntity,"listOfValueObjects/0"),
valueObjectId(1, SnapshotEntity,"listOfValueObjects/1")]
]
}
@Unroll
def "should record Set of #propertyType"() {
given:
def snapshotFactory = javers.snapshotFactory
when:
CdoSnapshot snapshot = snapshotFactory.create(cdo, javers.instanceId(cdo),
new CommitMetadata("kazik", LocalDateTime.now(), new CommitId(1, 0)))
then:
snapshot.getPropertyValue(propertyName) == expectedVal
//we need shallow copy
System.identityHashCode(snapshot.getPropertyValue(propertyName)) != System.identityHashCode(cdo.getAt(propertyName))
where:
propertyType << ["Primitive", "Value", "Entity", "ValueObject"]
propertyName << ["setOfIntegers", "setOfDates", "setOfEntities", "setOfValueObjects"]
cdo << [new SnapshotEntity(setOfIntegers: [1, 2]),
new SnapshotEntity(setOfDates: [new LocalDate(2000, 1, 1), new LocalDate(2002, 1, 1)]),
new SnapshotEntity(setOfEntities: [new SnapshotEntity(id:2), new SnapshotEntity(id:3)]),
new SnapshotEntity(setOfValueObjects: [new DummyAddress("London"), new DummyAddress("London City")])
]
expectedVal << [[1, 2] as Set,
[new LocalDate(2000, 1, 1), new LocalDate(2002, 1, 1)] as Set,
[instanceId(2, SnapshotEntity), instanceId(3, SnapshotEntity)] as Set,
[valueObjectId(1, SnapshotEntity, "setOfValueObjects/random_0"),
valueObjectId(1, SnapshotEntity, "setOfValueObjects/random_1")] as Set
]
}
def "should throw exception when property Type is not fully parametrized"() {
given:
def snapshotFactory = javers.snapshotFactory
when:
def cdo = new SnapshotEntity(nonParametrizedMap: ["a":1])
snapshotFactory.create(cdo, javers.instanceId(cdo), new CommitMetadata("kazik", LocalDateTime.now(), new CommitId(1, 0)))
then:
def e = thrown(JaversException)
e.code == JaversExceptionCode.GENERIC_TYPE_NOT_PARAMETRIZED;
}
@Unroll
def "should record Map of #enrtyType"() {
given:
def snapshotFactory = javers.snapshotFactory
when:
CdoSnapshot snapshot = snapshotFactory.create(cdo, javers.instanceId(cdo),
new CommitMetadata("kazik", LocalDateTime.now(), new CommitId(1, 0)))
then:
snapshot.getPropertyValue(propertyName) == expectedVal
//we need shallow copy
System.identityHashCode(snapshot.getPropertyValue(propertyName)) != System.identityHashCode(cdo.getAt(propertyName))
where:
enrtyType << ["<Primitive,Primitive>", "<Value,Value>", "<Primitive,ValueObject>", "<Entity,Entity>"]
propertyName << ["mapOfPrimitives", "mapOfValues", "mapPrimitiveToVO", "mapOfEntities"]
cdo << [new SnapshotEntity(mapOfPrimitives: ["this":1,"that":2]),
new SnapshotEntity(mapOfValues: [(new LocalDate(2000, 1, 1)):1.5]),
new SnapshotEntity(mapPrimitiveToVO: ["key1":new DummyAddress("London")]),
new SnapshotEntity(mapOfEntities: [(new SnapshotEntity(id:2)):new SnapshotEntity(id:3)])
]
expectedVal << [
["this":1,"that":2],
[(new LocalDate(2000, 1, 1)):1.5],
["key1":valueObjectId(1, SnapshotEntity,"mapPrimitiveToVO/key1")],
[(javers.idBuilder().instanceId(2,SnapshotEntity)):
javers.idBuilder().instanceId(3,SnapshotEntity)]
]
}
}