-
Notifications
You must be signed in to change notification settings - Fork 14
/
ChangeTrackingStorageConnectorTest.java
361 lines (323 loc) · 16.4 KB
/
ChangeTrackingStorageConnectorTest.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
351
352
353
354
355
356
357
358
359
360
361
package cz.cvut.kbss.ontodriver.jena.connector;
import cz.cvut.kbss.ontodriver.config.Configuration;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import cz.cvut.kbss.ontodriver.jena.exception.JenaDriverException;
import cz.cvut.kbss.ontodriver.util.Vocabulary;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static cz.cvut.kbss.ontodriver.jena.connector.StorageTestUtil.*;
import static org.apache.jena.rdf.model.ResourceFactory.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
public class ChangeTrackingStorageConnectorTest {
private SharedStorageConnector centralConnector;
private ChangeTrackingStorageConnector connector;
@Before
public void setUp() {
final Configuration configuration = StorageTestUtil.createConfiguration("test:uri");
this.centralConnector = spy(new SharedStorageConnector(configuration));
this.connector = new ChangeTrackingStorageConnector(centralConnector);
}
@After
public void tearDown() {
centralConnector.close();
}
@Test
public void beginCreatesNewLocalModel() throws Exception {
assertNull(getLocalModel());
connector.begin();
assertNotNull(getLocalModel());
assertTrue(connector.transaction.isActive());
}
private LocalModel getLocalModel() throws Exception {
final Field field = ChangeTrackingStorageConnector.class.getDeclaredField("localModel");
field.setAccessible(true);
return (LocalModel) field.get(connector);
}
@Test
public void findEnhancesResultFromCentralConnectorWithTransactionalChangesFromLocalModel() throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added));
final Collection<Statement> result = connector.find(createResource(SUBJECT), null, null);
assertEquals(2, result.size());
assertTrue(result.contains(existing));
assertTrue(result.contains(added));
}
@Test
public void findEnhancesResultFromCentralConnectorWithTransactionalChangesFromLocalModel_Context()
throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added), NAMED_GRAPH);
final Collection<Statement> result = connector.find(createResource(SUBJECT), null, null, NAMED_GRAPH);
assertEquals(1, result.size());
assertTrue(result.contains(added));
}
@Test
public void findPreventsDuplicateStatementsFromCentralAndLocalModel() throws Exception {
centralConnector.begin();
final Statement statement = ResourceFactory
.createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(statement));
centralConnector.commit();
connector.begin();
getLocalModel().addStatements(Collections.singletonList(statement));
final Collection<Statement> result = connector.find(createResource(SUBJECT), null, null);
assertEquals(1, result.size());
assertTrue(result.contains(statement));
}
@Test
public void containsReturnsTrueForStatementsPresentInLocalChanges() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added));
assertTrue(connector.contains(null, null, added.getObject()));
}
@Test
public void containsChecksCentralConnectorWhenLocalChangesDoNotContainValue() throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
assertTrue(connector.contains(null, null, createResource(TYPE_ONE)));
}
@Test
public void containsReturnsFalseForStatementLocallyRemoved() throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
final Statement removed = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
getLocalModel().removeStatements(Collections.singletonList(removed));
assertFalse(connector.contains(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE), null));
}
@Test
public void containsWorksInContextAsWell() throws Exception {
centralConnector.begin();
final Statement existing = ResourceFactory
.createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.add(Collections.singletonList(existing), NAMED_GRAPH);
centralConnector.commit();
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added));
assertFalse(connector.contains(null, null, added.getObject(), NAMED_GRAPH));
}
@Test
public void addAddsStatementsToLocalModel() throws Exception {
final Statement statement = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
connector.begin();
assertTrue(getLocalModel().getAdded().isEmpty());
connector.add(Collections.singletonList(statement));
assertTrue(getLocalModel().getAdded().getDefaultModel().contains(statement));
}
@Test
public void addAddsStatementsToCorrectContextInLocalModel() throws Exception {
final Statement statement = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
connector.begin();
assertTrue(getLocalModel().getAdded().isEmpty());
connector.add(Collections.singletonList(statement), NAMED_GRAPH);
assertTrue(getLocalModel().getAdded().getNamedModel(NAMED_GRAPH).contains(statement));
}
@Test
public void removeAddsStatementsToLocalModelForRemoval() throws Exception {
final Statement statement = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
connector.begin();
assertTrue(getLocalModel().getRemoved().isEmpty());
connector.remove(Collections.singletonList(statement));
assertTrue(getLocalModel().getRemoved().getDefaultModel().contains(statement));
}
@Test
public void removeAddsStatementsForRemovalToCorrectContextInLocalModel() throws Exception {
final Statement statement = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
connector.begin();
assertTrue(getLocalModel().getRemoved().isEmpty());
connector.remove(Collections.singletonList(statement), NAMED_GRAPH);
assertTrue(getLocalModel().getRemoved().getNamedModel(NAMED_GRAPH).contains(statement));
}
@Test
public void removeAddsStatementsMatchingRemovalCriteriaToLocalModelForRemoval() throws Exception {
final Statement existing = createStatement(RESOURCE, createProperty(Vocabulary.RDF_TYPE), createResource(TYPE_ONE));
centralConnector.begin();
centralConnector.add(Collections.singletonList(existing));
centralConnector.commit();
connector.begin();
assertTrue(getLocalModel().getRemoved().isEmpty());
connector.remove(RESOURCE, createProperty(Vocabulary.RDF_TYPE), null);
assertTrue(getLocalModel().getRemoved().getDefaultModel().contains(existing));
}
@Test
public void removeRemovesLocallyAddedStatements() throws Exception {
final Statement added = createStatement(RESOURCE, createProperty(Vocabulary.RDF_TYPE), createResource(TYPE_ONE));
connector.begin();
assertTrue(getLocalModel().getRemoved().isEmpty());
getLocalModel().addStatements(Collections.singletonList(added));
connector.remove(RESOURCE, createProperty(Vocabulary.RDF_TYPE), null);
assertTrue(getLocalModel().getRemoved().getDefaultModel().contains(added));
assertTrue(getLocalModel().getAdded().isEmpty());
}
@Test
public void removeAddsStatementsMatchingRemovalCriteriaToLocalModelContext() throws Exception {
final Statement existing = createStatement(RESOURCE, createProperty(Vocabulary.RDF_TYPE), createResource(TYPE_ONE));
centralConnector.begin();
centralConnector.add(Collections.singletonList(existing), NAMED_GRAPH);
centralConnector.commit();
connector.begin();
assertTrue(getLocalModel().getRemoved().isEmpty());
connector.remove(RESOURCE, createProperty(Vocabulary.RDF_TYPE), null, NAMED_GRAPH);
assertTrue(getLocalModel().getRemoved().getNamedModel(NAMED_GRAPH).contains(existing));
}
@Test
public void rollbackDiscardsLocalModel() throws Exception {
final Statement statement = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
connector.begin();
connector.add(Collections.singletonList(statement));
connector.rollback();
assertNull(getLocalModel());
assertFalse(connector.transaction.isActive());
}
@Test
public void commitAppliesChangesFromLocalModelToCentralConnector() throws Exception {
final Statement removed = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE));
centralConnector.begin();
centralConnector.add(Collections.singletonList(removed));
centralConnector.commit();
connector.begin();
getLocalModel().removeStatements(Collections.singletonList(removed));
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added));
connector.commit();
assertTrue(centralConnector.contains(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO)));
assertFalse(centralConnector.contains(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_ONE)));
}
@Test
public void commitAppliesChangesInContexts() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added), NAMED_GRAPH);
connector.commit();
assertTrue(centralConnector.contains(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO), NAMED_GRAPH));
}
@Test
public void commitDiscardsLocalModelOnSuccessfulFinish() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added), NAMED_GRAPH);
connector.commit();
assertNull(getLocalModel());
assertFalse(connector.transaction.isActive());
}
@Test(expected = JenaDriverException.class)
public void commitRollsBackChangesOnException() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
getLocalModel().addStatements(Collections.singletonList(added), NAMED_GRAPH);
centralConnector.storage = spy(centralConnector.storage);
doThrow(new JenaDriverException("Write failed.")).when(centralConnector.storage).writeChanges();
try {
connector.commit();
} finally {
assertFalse(centralConnector.contains(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO), NAMED_GRAPH));
}
}
@Test
public void closeDiscardsRunningTransaction() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
connector.add(Collections.singletonList(added));
connector.close();
assertNull(getLocalModel());
}
@Test
public void getContextsGetsContextFromBothLocalModelAndSharedConnector() throws Exception {
connector.begin();
final Statement added = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
centralConnector.begin();
centralConnector.add(Collections.singletonList(added), NAMED_GRAPH);
centralConnector.commit();
final String contextTwo = Generator.generateUri().toString();
final Statement addedTwo = createStatement(createResource(SUBJECT), createProperty(Vocabulary.RDF_TYPE),
createResource(TYPE_TWO));
connector.add(Collections.singletonList(addedTwo), contextTwo);
final List<String> contexts = connector.getContexts();
assertTrue(contexts.contains(NAMED_GRAPH));
assertTrue(contexts.contains(contextTwo));
}
@Test
public void unwrapPassesCallToUnderlyingSharedConnector() {
final Dataset dataset = connector.unwrap(Dataset.class);
assertSame(centralConnector.storage.dataset, dataset);
}
@Test
public void executeSelectQueryPassesQueryToSharedConnector() throws Exception {
final Query query = QueryFactory.create("SELECT * WHERE { ?x ?y ?z . }");
connector.executeSelectQuery(query);
verify(centralConnector).executeSelectQuery(query);
}
@Test
public void executeAskQueryPassesQueryToSharedConnector() throws Exception {
final Query query = QueryFactory.create("ASK WHERE { ?x a <" + Generator.generateUri() + ">. }");
connector.executeAskQuery(query);
verify(centralConnector).executeAskQuery(query);
}
@Test
public void executeUpdateQueryPassesQueryToSharedConnector() throws Exception {
final String query = "INSERT DATA { _:a a <" + Generator.generateUri() + "> . }";
connector.executeUpdate(query);
verify(centralConnector).executeUpdate(query);
}
}