-
Notifications
You must be signed in to change notification settings - Fork 14
/
DuplicateIdentifiersTest.java
350 lines (304 loc) · 15.9 KB
/
DuplicateIdentifiersTest.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package cz.cvut.kbss.jopa.test.integration;
import cz.cvut.kbss.jopa.Persistence;
import cz.cvut.kbss.jopa.exceptions.OWLEntityExistsException;
import cz.cvut.kbss.jopa.model.*;
import cz.cvut.kbss.jopa.test.OWLClassA;
import cz.cvut.kbss.jopa.test.OWLClassB;
import cz.cvut.kbss.jopa.test.Vocabulary;
import cz.cvut.kbss.jopa.test.environment.Generators;
import cz.cvut.kbss.jopa.test.integration.environment.TestDataSource;
import cz.cvut.kbss.ontodriver.Connection;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.model.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
public class DuplicateIdentifiersTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
private EntityManagerFactory emf;
private EntityManager em;
@Mock
private Connection connectionMock;
private OWLClassA entityA;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final Map<String, String> properties = new HashMap<>();
properties.put(JOPAPersistenceProperties.SCAN_PACKAGE, "cz.cvut.kbss.jopa.test");
properties.put(JOPAPersistenceProperties.DATA_SOURCE_CLASS, TestDataSource.class.getCanonicalName());
properties.put(JOPAPersistenceProperties.JPA_PERSISTENCE_PROVIDER, JOPAPersistenceProvider.class.getName());
properties.put(JOPAPersistenceProperties.ONTOLOGY_PHYSICAL_URI_KEY, "TestOntology");
this.emf = Persistence.createEntityManagerFactory("testPU", properties);
this.em = emf.createEntityManager();
final TestDataSource testDs = ((EntityManagerFactoryImpl) emf).getServerSession().unwrap(TestDataSource.class);
testDs.setConnection(connectionMock);
initInstances();
}
private void initInstances() {
this.entityA = new OWLClassA(Generators.generateUri());
entityA.setStringAttribute("aStringAttribute");
}
@After
public void tearDown() {
em.close();
emf.close();
}
@Test
public void persistObjectTwiceInPersistenceContextIsLegal() throws Exception {
em.getTransaction().begin();
em.persist(entityA);
entityA.setStringAttribute("UpdatedString");
em.persist(entityA);
em.getTransaction().commit();
verify(connectionMock).persist(any(AxiomValueDescriptor.class));
final ArgumentCaptor<AxiomValueDescriptor> captor = ArgumentCaptor.forClass(AxiomValueDescriptor.class);
verify(connectionMock).update(captor.capture());
final AxiomValueDescriptor descriptor = captor.getValue();
assertEquals(1, descriptor.getAssertions().size());
final List<Value<?>> values = descriptor.getAssertionValues(Assertion.createDataPropertyAssertion(URI.create(
Vocabulary.pAStringAttribute), false));
assertEquals(1, values.size());
assertEquals(entityA.getStringAttribute(), values.get(0).getValue());
}
@Test
public void persistTwoInstancesOfDifferentClassesWithSameIdentifierInOnePersistenceContextIsIllegal()
throws Exception {
thrown.expect(OWLEntityExistsException.class);
thrown.expectMessage(
"An entity with URI " + entityA.getUri() + " is already present in the current persistence context.");
final OWLClassB entityB = new OWLClassB();
entityB.setUri(entityA.getUri());
em.getTransaction().begin();
em.persist(entityA);
em.persist(entityB);
em.getTransaction().commit();
}
@Test
public void persistTwoInstancesOfDifferentClassesWithSameIdentifierInDifferentPersistenceContextsIsLegal()
throws Exception {
final OWLClassB entityB = new OWLClassB();
entityB.setUri(entityA.getUri());
entityB.setStringAttribute("bStringAttribute");
em.getTransaction().begin();
em.persist(entityA);
em.getTransaction().commit();
when(connectionMock.contains(
new AxiomImpl<>(NamedResource.create(entityA.getUri()), Assertion.createClassAssertion(false),
new Value<>(NamedResource.create(OWLClassA.getClassIri()))), null)).thenReturn(true);
em.getTransaction().begin();
em.persist(entityB);
em.getTransaction().commit();
verify(connectionMock, times(2)).persist(any(AxiomValueDescriptor.class));
}
@Test
public void mergeInstanceTwiceInTwoPersistenceContextsIsLegal() throws Exception {
final NamedResource subject = NamedResource.create(entityA.getUri());
final Assertion stringAss = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pAStringAttribute), false);
em.getTransaction().begin();
em.persist(entityA);
em.getTransaction().commit();
initAxiomsForOWLClassA(subject, stringAss);
when(connectionMock.contains(
new AxiomImpl<>(subject, Assertion.createClassAssertion(false),
new Value<>(NamedResource.create(OWLClassA.getClassIri()))), null)).thenReturn(true);
final String newStringOne = "newStringAttributeOne";
entityA.setStringAttribute(newStringOne);
em.getTransaction().begin();
em.merge(entityA);
em.getTransaction().commit();
final ArgumentCaptor<AxiomValueDescriptor> captor = ArgumentCaptor.forClass(AxiomValueDescriptor.class);
final String newStringTwo = "newStringAttributeTwo";
entityA.setStringAttribute(newStringTwo);
em.getTransaction().begin();
em.merge(entityA);
em.getTransaction().commit();
verify(connectionMock, times(2)).update(captor.capture());
final AxiomValueDescriptor firstUpdate = captor.getAllValues().get(0);
assertEquals(1, firstUpdate.getAssertionValues(stringAss).size());
assertEquals(newStringOne, firstUpdate.getAssertionValues(stringAss).get(0).getValue());
final AxiomValueDescriptor secondUpdate = captor.getAllValues().get(1);
assertEquals(1, secondUpdate.getAssertionValues(stringAss).size());
assertEquals(newStringTwo, secondUpdate.getAssertionValues(stringAss).get(0).getValue());
}
private void initAxiomsForOWLClassA(NamedResource subject, Assertion stringAss) throws OntoDriverException {
final List<Axiom<?>> axioms = new ArrayList<>();
final Axiom<?> classAssertion = new AxiomImpl<>(subject, Assertion.createClassAssertion(false),
new Value<>(NamedResource.create(OWLClassA.getClassIri())));
axioms.add(classAssertion);
axioms.add(new AxiomImpl<>(subject, stringAss, new Value<>(entityA.getStringAttribute())));
final AxiomDescriptor desc = new AxiomDescriptor(subject);
desc.addAssertion(Assertion.createClassAssertion(false));
desc.addAssertion(stringAss);
when(connectionMock.find(desc)).thenReturn(axioms);
when(connectionMock.contains(classAssertion, null)).thenReturn(true);
}
@Test
public void mergeTwoInstancesWithTheSameIdentifierInTwoPersistenceContextsIsLegal() throws Exception {
final NamedResource subject = NamedResource.create(entityA.getUri());
final Assertion stringAssA = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pAStringAttribute), false);
final Assertion stringAssB = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pBStringAttribute), false);
em.getTransaction().begin();
em.persist(entityA);
em.getTransaction().commit();
initAxiomsForOWLClassA(subject, stringAssA);
final OWLClassB entityB = new OWLClassB();
entityB.setUri(entityA.getUri());
entityB.setStringAttribute("bStringAttribute");
em.getTransaction().begin();
em.persist(entityB);
em.getTransaction().commit();
initAxiomsForOWLClassB(entityB, subject, stringAssB);
final String newStringA = "newStringAttributeA";
entityA.setStringAttribute(newStringA);
em.getTransaction().begin();
em.merge(entityA);
em.getTransaction().commit();
final String newStringB = "newStringAttributeB";
entityB.setStringAttribute(newStringB);
em.getTransaction().begin();
em.merge(entityB);
em.getTransaction().commit();
final ArgumentCaptor<AxiomValueDescriptor> captor = ArgumentCaptor.forClass(AxiomValueDescriptor.class);
verify(connectionMock, times(2)).update(captor.capture());
final AxiomValueDescriptor firstUpdate = captor.getAllValues().get(0);
assertEquals(1, firstUpdate.getAssertionValues(stringAssA).size());
assertEquals(newStringA, firstUpdate.getAssertionValues(stringAssA).get(0).getValue());
final AxiomValueDescriptor secondUpdate = captor.getAllValues().get(1);
assertEquals(1, secondUpdate.getAssertionValues(stringAssB).size());
assertEquals(newStringB, secondUpdate.getAssertionValues(stringAssB).get(0).getValue());
}
private void initAxiomsForOWLClassB(OWLClassB entityB, NamedResource subject, Assertion stringAss)
throws OntoDriverException {
final List<Axiom<?>> axioms = new ArrayList<>();
final Axiom<?> classAssertion = new AxiomImpl<>(subject, Assertion.createClassAssertion(false),
new Value<>(NamedResource.create(Vocabulary.cOwlClassB)));
axioms.add(classAssertion);
axioms.add(new AxiomImpl<>(subject, stringAss, new Value<>(entityB.getStringAttribute())));
final AxiomDescriptor desc = new AxiomDescriptor(subject);
desc.addAssertion(Assertion.createClassAssertion(false));
desc.addAssertion(stringAss);
when(connectionMock.find(desc)).thenReturn(axioms);
when(connectionMock.contains(classAssertion, null)).thenReturn(true);
}
@Test
public void mergeSameInstanceMultipleTimesInOnePersistenceContextIsLegal() throws Exception {
final NamedResource subject = NamedResource.create(entityA.getUri());
final Assertion stringAssA = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pAStringAttribute), false);
initAxiomsForOWLClassA(subject, stringAssA);
final String updateOne = "updatedString";
entityA.setStringAttribute(updateOne);
em.getTransaction().begin();
em.merge(entityA);
final String updateTwo = "updatedStringAgain";
entityA.setStringAttribute(updateTwo);
em.merge(entityA);
em.getTransaction().commit();
final ArgumentCaptor<AxiomValueDescriptor> captor = ArgumentCaptor.forClass(AxiomValueDescriptor.class);
verify(connectionMock, times(2)).update(captor.capture());
final List<Value<?>> valuesOne = captor.getAllValues().get(0).getAssertionValues(stringAssA);
assertEquals(1, valuesOne.size());
assertEquals(updateOne, valuesOne.get(0).getValue());
final List<Value<?>> valuesTwo = captor.getAllValues().get(1).getAssertionValues(stringAssA);
assertEquals(1, valuesTwo.size());
assertEquals(updateTwo, valuesTwo.get(0).getValue());
}
@Test
public void mergeTwoInstancesOfDifferentClassesWithTheSameIdentifierIntoOnePersistenceContextIsIllegal()
throws Exception {
thrown.expect(OWLEntityExistsException.class);
thrown.expectMessage(
"An entity with URI " + entityA.getUri() + " is already present in the current persistence context.");
final NamedResource subject = NamedResource.create(entityA.getUri());
final Assertion stringAssA = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pAStringAttribute), false);
final Assertion stringAssB = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pBStringAttribute), false);
initAxiomsForOWLClassA(subject, stringAssA);
final OWLClassB entityB = new OWLClassB();
entityB.setUri(entityA.getUri());
entityB.setStringAttribute("bStringAttribute");
initAxiomsForOWLClassB(entityB, subject, stringAssB);
entityA.setStringAttribute("updatedStringAttribute");
em.getTransaction().begin();
em.merge(entityA);
em.merge(entityB);
em.getTransaction().commit();
verify(connectionMock).update(any(AxiomValueDescriptor.class));// Just for the entityA instance
}
@Test
public void mergeTwoInstancesOfTheSameClassWithTheSameIdentifierIntoTheSamePersistenceContextIsLegal()
throws Exception {
final NamedResource subject = NamedResource.create(entityA.getUri());
final Assertion stringAssA = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pAStringAttribute), false);
initAxiomsForOWLClassA(subject, stringAssA);
final String updateOne = "update";
entityA.setStringAttribute(updateOne);
em.getTransaction().begin();
em.merge(entityA);
final OWLClassA secondA = new OWLClassA(entityA.getUri());
final String updateTwo = "updatedAgain";
secondA.setStringAttribute(updateTwo);
em.merge(secondA);
em.getTransaction().commit();
final ArgumentCaptor<AxiomValueDescriptor> captor = ArgumentCaptor.forClass(AxiomValueDescriptor.class);
verify(connectionMock, times(2)).update(captor.capture());
final List<Value<?>> valuesOne = captor.getAllValues().get(0).getAssertionValues(stringAssA);
assertEquals(1, valuesOne.size());
assertEquals(updateOne, valuesOne.get(0).getValue());
final List<Value<?>> valuesTwo = captor.getAllValues().get(1).getAssertionValues(stringAssA);
assertEquals(1, valuesTwo.size());
assertEquals(updateTwo, valuesTwo.get(0).getValue());
}
@Test
public void findTwiceInOnePersistenceContextWithTheSameIdentifierAndTypeReturnsTheSameInstance() throws Exception {
final NamedResource subject = NamedResource.create(entityA.getUri());
final Assertion stringAssA = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pAStringAttribute), false);
initAxiomsForOWLClassA(subject, stringAssA);
final OWLClassA aOne = em.find(OWLClassA.class, entityA.getUri());
assertNotNull(aOne);
final OWLClassA aTwo = em.find(OWLClassA.class, entityA.getUri());
assertNotNull(aTwo);
assertSame(aOne, aTwo);
}
@Test
public void findIndividualAsDifferentTypeThanIsAlreadyLoadedInPersistenceContextIsIllegal() throws Exception {
thrown.expect(OWLEntityExistsException.class);
thrown.expectMessage(
"An entity with URI " + entityA.getUri() + " is already present in the current persistence context.");
final NamedResource subject = NamedResource.create(entityA.getUri());
final Assertion stringAssA = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pAStringAttribute), false);
final Assertion stringAssB = Assertion
.createDataPropertyAssertion(URI.create(Vocabulary.pBStringAttribute), false);
initAxiomsForOWLClassA(subject, stringAssA);
final OWLClassB entityB = new OWLClassB();
entityB.setUri(entityA.getUri());
entityB.setStringAttribute("bStringAttribute");
initAxiomsForOWLClassB(entityB, subject, stringAssB);
final OWLClassA aOne = em.find(OWLClassA.class, entityA.getUri());
assertNotNull(aOne);
em.find(OWLClassB.class, entityB.getUri());
}
}