-
Notifications
You must be signed in to change notification settings - Fork 14
/
InferredAxiomLoaderTest.java
175 lines (160 loc) · 9.74 KB
/
InferredAxiomLoaderTest.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
package cz.cvut.kbss.ontodriver.jena;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import cz.cvut.kbss.ontodriver.model.*;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.*;
import static org.apache.jena.rdf.model.ResourceFactory.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class InferredAxiomLoaderTest extends AxiomLoaderTestBase {
@Mock
private InferredStorageConnector connectorMock;
private AxiomDescriptor descriptor;
private InferredAxiomLoader axiomLoader;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.descriptor = new AxiomDescriptor(SUBJECT);
this.axiomLoader = new InferredAxiomLoader(connectorMock, null);
}
@Test
public void containsChecksForInferredAxiomExistenceInDefaultGraph() {
final URI clsUri = Generator.generateUri();
final Axiom<?> axiom = new AxiomImpl<>(SUBJECT, Assertion.createClassAssertion(true),
new Value<>(NamedResource.create(clsUri)));
when(connectorMock.containsWithInference(SUBJECT_RES, RDF.type, createResource(clsUri.toString()), null))
.thenReturn(true);
assertTrue(axiomLoader.contains(axiom, null));
verify(connectorMock).containsWithInference(SUBJECT_RES, RDF.type, createResource(clsUri.toString()), null);
}
@Test
public void containsChecksForInferredAxiomExistenceInNamedGraph() {
final URI clsUri = Generator.generateUri();
final Axiom<?> axiom = new AxiomImpl<>(SUBJECT, Assertion.createClassAssertion(true),
new Value<>(NamedResource.create(clsUri)));
when(connectorMock
.containsWithInference(SUBJECT_RES, RDF.type, createResource(clsUri.toString()), CONTEXT.toString()))
.thenReturn(true);
assertTrue(axiomLoader.contains(axiom, CONTEXT));
verify(connectorMock)
.containsWithInference(SUBJECT_RES, RDF.type, createResource(clsUri.toString()), CONTEXT.toString());
}
@Test
public void findLoadsValuesOfSpecifiedAssertions() {
final Assertion aOne = Assertion.createClassAssertion(true);
final Assertion aTwo = Assertion.createObjectPropertyAssertion(Generator.generateUri(), true);
final Resource cls = createResource(Generator.generateUri().toString());
final Property property = createProperty(aTwo.getIdentifier().toString());
final Resource object = createResource(Generator.generateUri().toString());
when(connectorMock.findWithInference(SUBJECT_RES, RDF.type, null, null))
.thenReturn(Collections.singletonList(createStatement(SUBJECT_RES, RDF.type, cls)));
when(connectorMock.findWithInference(SUBJECT_RES, property, null, null))
.thenReturn(Collections.singletonList(createStatement(SUBJECT_RES, property, object)));
descriptor.addAssertion(aOne);
descriptor.addAssertion(aTwo);
final List<Axiom<?>> result = axiomLoader.find(descriptor, mapAssertions(descriptor));
assertEquals(2, result.size());
final Optional<Axiom<?>> clsAxiom = result.stream().filter(a -> a.getAssertion().equals(aOne)).findAny();
assertTrue(clsAxiom.isPresent());
assertEquals(NamedResource.create(cls.getURI()), clsAxiom.get().getValue().getValue());
final Optional<Axiom<?>> refAxiom = result.stream().filter(a -> a.getAssertion().equals(aTwo)).findAny();
assertTrue(refAxiom.isPresent());
assertEquals(NamedResource.create(object.getURI()), refAxiom.get().getValue().getValue());
verify(connectorMock).findWithInference(SUBJECT_RES, RDF.type, null, null);
verify(connectorMock).findWithInference(SUBJECT_RES, property, null, null);
}
@Test
public void findLoadsValuesOfAssertionsInContext() {
final Assertion aOne = Assertion.createClassAssertion(true);
final Assertion aTwo = Assertion.createObjectPropertyAssertion(Generator.generateUri(), true);
final Resource cls = createResource(Generator.generateUri().toString());
final Property property = createProperty(aTwo.getIdentifier().toString());
final Resource object = createResource(Generator.generateUri().toString());
when(connectorMock.findWithInference(SUBJECT_RES, RDF.type, null, CONTEXT.toString()))
.thenReturn(Collections.singletonList(createStatement(SUBJECT_RES, RDF.type, cls)));
when(connectorMock.findWithInference(SUBJECT_RES, property, null, CONTEXT.toString()))
.thenReturn(Collections.singletonList(createStatement(SUBJECT_RES, property, object)));
descriptor.addAssertion(aOne);
descriptor.setAssertionContext(aOne, CONTEXT);
descriptor.addAssertion(aTwo);
descriptor.setSubjectContext(CONTEXT);
final List<Axiom<?>> result = axiomLoader.find(descriptor, mapAssertions(descriptor));
assertEquals(2, result.size());
final Optional<Axiom<?>> clsAxiom = result.stream().filter(a -> a.getAssertion().equals(aOne)).findAny();
assertTrue(clsAxiom.isPresent());
assertEquals(NamedResource.create(cls.getURI()), clsAxiom.get().getValue().getValue());
final Optional<Axiom<?>> refAxiom = result.stream().filter(a -> a.getAssertion().equals(aTwo)).findAny();
assertTrue(refAxiom.isPresent());
assertEquals(NamedResource.create(object.getURI()), refAxiom.get().getValue().getValue());
verify(connectorMock).findWithInference(SUBJECT_RES, RDF.type, null, CONTEXT.toString());
verify(connectorMock).findWithInference(SUBJECT_RES, property, null, CONTEXT.toString());
}
@Test
public void findBySubjectLoadsAllPropertyAxiomsRelatedToSpecifiedSubject() {
final Assertion aOne = Assertion.createDataPropertyAssertion(Generator.generateUri(), true);
final Assertion aTwo = Assertion.createObjectPropertyAssertion(Generator.generateUri(), true);
final Literal literal = createTypedLiteral(117);
final Property propertyOne = createProperty(aOne.getIdentifier().toString());
final Property propertyTwo = createProperty(aTwo.getIdentifier().toString());
final Resource object = createResource(Generator.generateUri().toString());
when(connectorMock.findWithInference(SUBJECT_RES, null, null, null)).thenReturn(Arrays.asList(
createStatement(SUBJECT_RES, propertyOne, literal),
createStatement(SUBJECT_RES, propertyTwo, object)
));
final Collection<Axiom<?>> result = axiomLoader.find(SUBJECT, null);
assertEquals(2, result.size());
final Optional<Axiom<?>> dpAxiom = result.stream().filter(a -> a.getAssertion().equals(aOne)).findAny();
assertTrue(dpAxiom.isPresent());
assertEquals(117, dpAxiom.get().getValue().getValue());
final Optional<Axiom<?>> refAxiom = result.stream().filter(a -> a.getAssertion().equals(aTwo)).findAny();
assertTrue(refAxiom.isPresent());
assertEquals(NamedResource.create(object.getURI()), refAxiom.get().getValue().getValue());
verify(connectorMock).findWithInference(SUBJECT_RES, null, null, null);
}
@Test
public void findBySubjectSkipsClassAssertions() {
final Literal literal = createTypedLiteral(117);
final Property propertyOne = createProperty(Generator.generateUri().toString());
final Resource object = createResource(Generator.generateUri().toString());
when(connectorMock.findWithInference(SUBJECT_RES, null, null, null)).thenReturn(Arrays.asList(
createStatement(SUBJECT_RES, propertyOne, literal),
createStatement(SUBJECT_RES, RDF.type, object)
));
final Collection<Axiom<?>> result = axiomLoader.find(SUBJECT, null);
assertEquals(1, result.size());
assertFalse(result.stream().anyMatch(a -> a.getAssertion().isClassAssertion()));
}
@Test
public void findBySubjectWorksInContext() {
final Assertion aOne = Assertion.createDataPropertyAssertion(Generator.generateUri(), true);
final Assertion aTwo = Assertion.createObjectPropertyAssertion(Generator.generateUri(), true);
final Literal literal = createTypedLiteral(117);
final Property propertyOne = createProperty(aOne.getIdentifier().toString());
final Property propertyTwo = createProperty(aTwo.getIdentifier().toString());
final Resource object = createResource(Generator.generateUri().toString());
when(connectorMock.findWithInference(SUBJECT_RES, null, null, CONTEXT.toString())).thenReturn(Arrays.asList(
createStatement(SUBJECT_RES, propertyOne, literal),
createStatement(SUBJECT_RES, propertyTwo, object)
));
final Collection<Axiom<?>> result = axiomLoader.find(SUBJECT, CONTEXT);
assertEquals(2, result.size());
final Optional<Axiom<?>> dpAxiom = result.stream().filter(a -> a.getAssertion().equals(aOne)).findAny();
assertTrue(dpAxiom.isPresent());
assertEquals(117, dpAxiom.get().getValue().getValue());
final Optional<Axiom<?>> refAxiom = result.stream().filter(a -> a.getAssertion().equals(aTwo)).findAny();
assertTrue(refAxiom.isPresent());
assertEquals(NamedResource.create(object.getURI()), refAxiom.get().getValue().getValue());
verify(connectorMock).findWithInference(SUBJECT_RES, null, null, CONTEXT.toString());
}
}