-
Notifications
You must be signed in to change notification settings - Fork 14
/
PropertiesHandlerTest.java
328 lines (286 loc) · 17.2 KB
/
PropertiesHandlerTest.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p/>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* <p/>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.owlapi;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.model.Value;
import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.impl.OWLNamedIndividualNodeSet;
import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
import java.net.URI;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
public class PropertiesHandlerTest {
private static final URI PK = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Entity");
private static final NamedResource INDIVIDUAL = NamedResource.create(PK);
private static final URI DP_ONE = URI.create("http://krizik.felk.cvut.cz/dpOne");
private static final URI DP_TWO = URI.create("http://krizik.felk.cvut.cz/dpTwo");
private static final URI OP_ONE = URI.create("http://krizik.felk.cvut.cz/opOne");
@Mock
private OwlapiAdapter adapterMock;
@Mock
private OWLOntology ontologyMock;
@Mock
private OWLOntologyManager managerMock;
@Mock
private OWLReasoner reasonerMock;
private OWLDataFactory dataFactory;
private PropertiesHandler propertiesHandler;
private Set<Object> dataValues = new HashSet<>();
private Set<Object> objectValues = new HashSet<>();
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
this.dataFactory = new OWLDataFactoryImpl();
this.propertiesHandler = new PropertiesHandler(adapterMock,
new OntologySnapshot(ontologyMock, managerMock, dataFactory, reasonerMock));
}
@Test
public void getPropertiesLoadsExplicitPropertiesFromOntology() throws Exception {
initSampleProperties();
final Collection<Axiom<?>> axioms = propertiesHandler.getProperties(INDIVIDUAL, false);
assertFalse(axioms.isEmpty());
final OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(PK));
verify(ontologyMock).getDataPropertyAssertionAxioms(individual);
verify(ontologyMock).getObjectPropertyAssertionAxioms(individual);
verify(ontologyMock).getAnnotationAssertionAxioms(individual.getIRI());
verifyAxioms(axioms, Assertion.AssertionType.DATA_PROPERTY, dataValues);
verifyAxioms(axioms, Assertion.AssertionType.OBJECT_PROPERTY, objectValues);
}
private void initSampleProperties() {
final OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(PK));
when(ontologyMock.getDataPropertyAssertionAxioms(individual)).thenReturn(initSampleDataProperties());
when(ontologyMock.getObjectPropertyAssertionAxioms(individual)).thenReturn(initSampleObjectProperties());
}
private Set<OWLDataPropertyAssertionAxiom> initSampleDataProperties() {
final OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(PK));
final OWLDataProperty dpOne = dataFactory.getOWLDataProperty(IRI.create(DP_ONE));
final Set<OWLDataPropertyAssertionAxiom> dpValues = new HashSet<>();
dpValues.add(dataFactory.getOWLDataPropertyAssertionAxiom(dpOne, individual, 100));
dataValues.add(100);
dpValues.add(dataFactory.getOWLDataPropertyAssertionAxiom(dpOne, individual, 1000));
dataValues.add(1000);
final OWLDataProperty dpTwo = dataFactory.getOWLDataProperty(IRI.create(DP_TWO));
dpValues.add(dataFactory.getOWLDataPropertyAssertionAxiom(dpTwo, individual, "Test"));
dataValues.add("Test");
dpValues.add(dataFactory.getOWLDataPropertyAssertionAxiom(dpTwo, individual, "TestTwo"));
dataValues.add("TestTwo");
return dpValues;
}
private Set<OWLObjectPropertyAssertionAxiom> initSampleObjectProperties() {
final OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(PK));
final OWLObjectProperty op = dataFactory.getOWLObjectProperty(IRI.create(OP_ONE));
objectValues.add(INDIVIDUAL);
final Set<OWLObjectPropertyAssertionAxiom> opValues = new HashSet<>();
opValues.add(dataFactory.getOWLObjectPropertyAssertionAxiom(op, individual, individual));
return opValues;
}
private void verifyAxioms(Collection<Axiom<?>> axioms, Assertion.AssertionType assertionType,
Set<?> expectedValues) {
boolean found;
for (Object val : expectedValues) {
found = false;
for (Axiom<?> ax : axioms) {
if (ax.getValue().getValue().equals(val)) {
found = true;
assertEquals(assertionType, ax.getAssertion().getType());
break;
}
}
assertTrue(found);
}
}
@Test
public void getPropertiesReturnsEmptyCollectionsForNonExistingIndividual() throws Exception {
final OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(PK));
initEmptyOntology(individual);
final Collection<Axiom<?>> axioms = propertiesHandler.getProperties(INDIVIDUAL, false);
assertNotNull(axioms);
assertTrue(axioms.isEmpty());
}
private void initEmptyOntology(OWLNamedIndividual individual) {
when(ontologyMock.getDataPropertyAssertionAxioms(individual)).thenReturn(Collections.emptySet());
when(ontologyMock.getObjectPropertyAssertionAxioms(individual)).thenReturn(Collections.emptySet());
when(ontologyMock.getAnnotationAssertionAxioms(individual.getIRI())).thenReturn(Collections.emptySet());
}
@Test
public void getPropertiesWithInferenceGetsInferredProperties() throws Exception {
final OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(PK));
initEmptyOntology(individual);
when(ontologyMock.getDataPropertiesInSignature()).thenReturn(new HashSet<>(
Arrays.asList(dataFactory.getOWLDataProperty(IRI.create(DP_ONE)),
dataFactory.getOWLDataProperty(IRI.create(DP_TWO)))));
when(ontologyMock.getObjectPropertiesInSignature())
.thenReturn(Collections.singleton(dataFactory.getOWLObjectProperty(IRI.create(OP_ONE))));
when(reasonerMock.getDataPropertyValues(eq(individual), any(OWLDataProperty.class)))
.thenReturn(Collections.emptySet());
when(reasonerMock.getObjectPropertyValues(eq(individual),
any(OWLObjectProperty.class))).thenReturn(new OWLNamedIndividualNodeSet());
final Collection<Axiom<?>> axioms = propertiesHandler.getProperties(INDIVIDUAL, true);
assertNotNull(axioms);
verify(reasonerMock, atLeastOnce()).getDataPropertyValues(eq(individual), any(OWLDataProperty.class));
verify(reasonerMock, atLeastOnce()).getObjectPropertyValues(eq(individual), any(OWLObjectProperty.class));
}
@Test
public void addPropertiesForExistingDataPropertyAddsAssertionsToOntology() throws Exception {
final Assertion assertion = Assertion.createDataPropertyAssertion(DP_ONE, false);
final Set<Value<?>> values = new HashSet<>(
Arrays.asList(new Value<>("One"), new Value<>("Two"), new Value<>("Three")));
when(ontologyMock.containsDataPropertyInSignature(IRI.create(DP_ONE))).thenReturn(true);
propertiesHandler.addProperties(INDIVIDUAL, Collections.singletonMap(assertion, values));
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(managerMock).applyChanges(captor.capture());
for (Object ob : captor.getValue()) {
assertTrue(ob instanceof AddAxiom);
final AddAxiom ax = (AddAxiom) ob;
assertTrue(ax.getAxiom() instanceof OWLDataPropertyAssertionAxiom);
final OWLDataPropertyAssertionAxiom dp = (OWLDataPropertyAssertionAxiom) ax.getAxiom();
assertTrue(values.contains(new Value<>(OwlapiUtils.owlLiteralToValue(dp.getObject()))));
}
}
@Test
public void addPropertiesForExistingDataPropertyAddsValuesOfCorrespondingTypes() throws Exception {
final Assertion assertion = Assertion.createDataPropertyAssertion(DP_ONE, false);
final Set<Object> baseValues = new HashSet<>(Arrays.asList(false, 1, new Date()));
final Set<Value<?>> values = baseValues.stream().map(Value::new).collect(Collectors.toSet());
when(ontologyMock.containsDataPropertyInSignature(IRI.create(DP_ONE))).thenReturn(true);
propertiesHandler.addProperties(INDIVIDUAL, Collections.singletonMap(assertion, values));
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(managerMock).applyChanges(captor.capture());
verifyAddedDataPropertyAxioms(baseValues, captor);
}
private void verifyAddedDataPropertyAxioms(Collection<Object> expectedValues, ArgumentCaptor<List> captor) {
for (Object ob : captor.getValue()) {
assertTrue(ob instanceof AddAxiom);
final AddAxiom ax = (AddAxiom) ob;
assertTrue(ax.getAxiom() instanceof OWLDataPropertyAssertionAxiom);
final OWLDataPropertyAssertionAxiom dp = (OWLDataPropertyAssertionAxiom) ax.getAxiom();
assertTrue(expectedValues.contains(OwlapiUtils.owlLiteralToValue(dp.getObject())));
}
}
@Test
public void addPropertiesForExistingObjectPropertyAddsAssertionsToOntology() throws Exception {
final Assertion assertion = Assertion.createObjectPropertyAssertion(OP_ONE, false);
final Set<Value<?>> values = new HashSet<>(Arrays.asList(
new Value<>(NamedResource.create("http://individualOne")),
new Value<>(NamedResource.create("http://individualTwo"))));
when(ontologyMock.containsObjectPropertyInSignature(IRI.create(OP_ONE))).thenReturn(true);
propertiesHandler.addProperties(INDIVIDUAL, Collections.singletonMap(assertion, values));
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(managerMock).applyChanges(captor.capture());
verifyAddedAxioms(values.stream().map(v -> URI.create(v.stringValue())).collect(Collectors.toSet()), captor);
}
@Test
public void addPropertiesForExistingObjectPropertyHandlesIndividualsAsStringAndUri() throws Exception {
final Assertion assertion = Assertion.createObjectPropertyAssertion(OP_ONE, false);
final Set<Value<?>> values = new HashSet<>(Arrays.asList(
new Value<>("http://individualOne"),
new Value<>(URI.create("http://individualTwo"))));
when(ontologyMock.containsObjectPropertyInSignature(IRI.create(OP_ONE))).thenReturn(true);
propertiesHandler.addProperties(INDIVIDUAL, Collections.singletonMap(assertion, values));
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(managerMock).applyChanges(captor.capture());
verifyAddedAxioms(values.stream().map(v -> URI.create(v.stringValue())).collect(Collectors.toSet()), captor);
}
private void verifyAddedAxioms(Set<URI> values, ArgumentCaptor<List> captor) {
for (Object ob : captor.getValue()) {
assertTrue(ob instanceof AddAxiom);
final AddAxiom ax = (AddAxiom) ob;
assertTrue(ax.getAxiom() instanceof OWLObjectPropertyAssertionAxiom);
final OWLObjectPropertyAssertionAxiom dp = (OWLObjectPropertyAssertionAxiom) ax.getAxiom();
assertTrue(values.contains(dp.getObject().asOWLNamedIndividual().getIRI().toURI()));
}
}
@Test
public void addPropertiesInfersDataPropertyAndLiteralsFromValuesForUnknownProperty() throws Exception {
final Assertion assertion = Assertion.createPropertyAssertion(DP_ONE, false);
final Set<Object> typedValues = new HashSet<>(Arrays.asList(true, 117L, new Date()));
final Set<Value<?>> values = typedValues.stream().map(Value::new).collect(Collectors.toSet());
when(ontologyMock.containsObjectPropertyInSignature(IRI.create(DP_ONE))).thenReturn(false);
when(ontologyMock.containsDataPropertyInSignature(IRI.create(DP_ONE))).thenReturn(false);
when(ontologyMock.containsAnnotationPropertyInSignature(IRI.create(DP_ONE))).thenReturn(false);
propertiesHandler.addProperties(INDIVIDUAL, Collections.singletonMap(assertion, values));
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(managerMock).applyChanges(captor.capture());
verifyAddedDataPropertyAxioms(typedValues, captor);
}
@Test
public void addPropertiesInfersObjectPropertyAndIrisFromValuesForUnknownProperty() throws Exception {
final Assertion assertion = Assertion.createPropertyAssertion(OP_ONE, false);
final Set<Object> typedValues = new HashSet<>(
Arrays.asList("http://individualOne", URI.create("http://individualTwo"),
new URL("http://individualThree")));
final Set<Value<?>> values = typedValues.stream().map(Value::new).collect(Collectors.toSet());
when(ontologyMock.containsObjectPropertyInSignature(IRI.create(OP_ONE))).thenReturn(false);
when(ontologyMock.containsDataPropertyInSignature(IRI.create(OP_ONE))).thenReturn(false);
when(ontologyMock.containsAnnotationPropertyInSignature(IRI.create(OP_ONE))).thenReturn(false);
propertiesHandler.addProperties(INDIVIDUAL, Collections.singletonMap(assertion, values));
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(managerMock).applyChanges(captor.capture());
verifyAddedAxioms(typedValues.stream().map(v -> URI.create(v.toString())).collect(Collectors.toSet()), captor);
}
@Test
public void addPropertiesAddsValuesOfKnownAnnotationProperty() throws Exception {
final Assertion assertion = Assertion.createPropertyAssertion(OP_ONE, false);
final Set<Object> individualValues = new HashSet<>(Collections.singletonList("http://individualOne"));
final Set<Object> literalValues = new HashSet<>(Arrays.asList("label", 1));
final Set<Value<?>> values = literalValues.stream().map(Value::new).collect(Collectors.toSet());
values.addAll(individualValues.stream().map(Value::new).collect(Collectors.toSet()));
when(ontologyMock.containsAnnotationPropertyInSignature(IRI.create(OP_ONE))).thenReturn(true);
propertiesHandler.addProperties(INDIVIDUAL, Collections.singletonMap(assertion, values));
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(managerMock).applyChanges(captor.capture());
assertEquals(values.size(), captor.getValue().size());
for (Object ob : captor.getValue()) {
assertTrue(ob instanceof AddAxiom);
final AddAxiom ax = (AddAxiom) ob;
assertTrue(ax.getAxiom() instanceof OWLAnnotationAssertionAxiom);
final OWLAnnotationAssertionAxiom ap = (OWLAnnotationAssertionAxiom) ax.getAxiom();
final OWLAnnotationValue val = ap.getValue();
if (val instanceof IRI) {
assertTrue(individualValues.contains(val.asIRI().get().toString()));
} else {
assertTrue(literalValues.contains(OwlapiUtils.owlLiteralToValue((OWLLiteral) val)));
}
}
}
@Test
public void removePropertiesRemovesSpecifiedPropertyValues() throws Exception {
final String prop = "http://krizik.felk.cvut.cz/jopa#property";
when(ontologyMock.containsDataPropertyInSignature(IRI.create(prop))).thenReturn(true);
final Assertion assertion = Assertion.createPropertyAssertion(URI.create(prop), false);
final Map<Assertion, Set<Value<?>>> toRemove = Collections
.singletonMap(assertion, Collections.singleton(new Value<>("Test")));
propertiesHandler.removeProperties(INDIVIDUAL, toRemove);
final ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(managerMock).applyChanges(captor.capture());
final List changes = captor.getValue();
assertEquals(1, changes.size());
}
}