-
Notifications
You must be signed in to change notification settings - Fork 14
/
ReferencedListPropertyStrategyTest.java
229 lines (205 loc) · 10.9 KB
/
ReferencedListPropertyStrategyTest.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* 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.
*
* 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.jopa.oom;
import cz.cvut.kbss.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.OWLClassC;
import cz.cvut.kbss.jopa.environment.OWLClassP;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jopa.model.metamodel.ListAttribute;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
import cz.cvut.kbss.ontodriver.model.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockitoAnnotations;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class ReferencedListPropertyStrategyTest extends ListPropertyStrategyTestBase {
private static List<OWLClassA> list;
private ListAttribute<OWLClassC, OWLClassA> refListMock;
private ReferencedListPropertyStrategy<OWLClassC> strategy;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
list = generateList();
}
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
super.setUp();
this.refListMock = mocks.forOwlClassC().referencedListAtt();
this.strategy = new ReferencedListPropertyStrategy<>(mocks.forOwlClassC().entityType(), refListMock, descriptor,
mapperMock);
strategy.setCascadeResolver(cascadeResolverMock);
}
@Test
public void buildsInstanceFieldFromAxiomsIncludingNodes() throws Exception {
final OWLClassC c = new OWLClassC(PK);
final List<Axiom<NamedResource>> axioms = initRefListAxioms(true);
when(mapperMock.loadReferencedList(any(ReferencedListDescriptor.class))).thenReturn(axioms);
strategy.addValueFromAxiom(axioms.iterator().next());
assertNull(c.getReferencedList());
strategy.buildInstanceFieldValue(c);
assertNotNull(c.getReferencedList());
assertEquals(list, c.getReferencedList());
final ArgumentCaptor<ReferencedListDescriptor> captor = ArgumentCaptor
.forClass(ReferencedListDescriptor.class);
verify(mapperMock).loadReferencedList(captor.capture());
final ReferencedListDescriptor listDescriptor = captor.getValue();
assertEquals(PK, listDescriptor.getListOwner().getIdentifier());
assertEquals(refListMock.getIRI().toURI(), listDescriptor.getListProperty().getIdentifier());
assertEquals(refListMock.getOWLObjectPropertyHasNextIRI().toURI(), listDescriptor.getNextNode()
.getIdentifier());
assertEquals(refListMock.getOWLPropertyHasContentsIRI().toURI(), listDescriptor
.getNodeContent().getIdentifier());
}
private List<Axiom<NamedResource>> initRefListAxioms(boolean includeNodes) throws Exception {
final List<Axiom<NamedResource>> axioms = new ArrayList<>();
NamedResource previous = NamedResource.create(PK);
int i = 0;
for (OWLClassA a : list) {
final NamedResource nodeUri =
NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa/refListNode_" + i);
if (includeNodes) {
final Axiom<NamedResource> node;
if (i == 0) {
node = new AxiomImpl<>(previous, Assertion.createObjectPropertyAssertion(
URI.create(OWLClassC.getRefListField()
.getAnnotation(OWLObjectProperty.class).iri()),
refListMock.isInferred()), new Value<>(nodeUri));
} else {
node = new AxiomImpl<>(
previous,
Assertion.createObjectPropertyAssertion(refListMock
.getOWLObjectPropertyHasNextIRI().toURI(), refListMock.isInferred()),
new Value<>(nodeUri));
}
axioms.add(node);
}
final Axiom<NamedResource> content = new AxiomImpl<>(nodeUri,
Assertion.createObjectPropertyAssertion(refListMock.getOWLPropertyHasContentsIRI()
.toURI(), refListMock.isInferred()),
new Value<>(NamedResource.create(a.getUri())));
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, a.getUri(), descriptor)).thenReturn(a);
axioms.add(content);
previous = nodeUri;
i++;
}
return axioms;
}
/**
* No node axioms, only content axioms.
*/
@Test
public void buildsInstanceFieldFromAxiomsWithoutNodes() throws Exception {
final OWLClassC c = new OWLClassC(PK);
final List<Axiom<NamedResource>> axioms = initRefListAxioms(false);
when(mapperMock.loadReferencedList(any(ReferencedListDescriptor.class))).thenReturn(axioms);
strategy.addValueFromAxiom(axioms.iterator().next());
assertNull(c.getReferencedList());
strategy.buildInstanceFieldValue(c);
assertNotNull(c.getReferencedList());
assertEquals(list, c.getReferencedList());
}
@Test
public void buildsInstanceFieldWithPlainIdentifiers() throws Exception {
final ListAttribute<OWLClassP, URI> listAtt = mocks.forOwlClassP().pReferencedListAttribute();
final ReferencedListPropertyStrategy<OWLClassP> strategy =
new ReferencedListPropertyStrategy<>(mocks.forOwlClassP().entityType(), listAtt, descriptor,
mapperMock);
final List<Axiom<NamedResource>> axioms = initRefListAxioms(true);
when(mapperMock.loadReferencedList(any(ReferencedListDescriptor.class))).thenReturn(axioms);
strategy.addValueFromAxiom(axioms.iterator().next());
final OWLClassP p = new OWLClassP();
p.setUri(PK);
strategy.buildInstanceFieldValue(p);
assertNotNull(p.getReferencedList());
assertEquals(list.size(), p.getReferencedList().size());
for (int i = 0; i < list.size(); i++) {
assertEquals(list.get(i).getUri(), p.getReferencedList().get(i));
}
}
@Test
public void extractsValuesIntoAxiomsForSave() throws Exception {
final OWLClassC c = new OWLClassC(PK);
c.setReferencedList(list);
final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), descriptor.getContext());
strategy.buildAxiomValuesFromInstance(c, builder);
final List<ReferencedListValueDescriptor> descriptors = OOMTestUtils.getReferencedListValueDescriptors(builder);
assertEquals(1, descriptors.size());
final ReferencedListValueDescriptor res = descriptors.get(0);
assertEquals(res.getListOwner(), NamedResource.create(PK));
assertEquals(
res.getListProperty(),
Assertion.createObjectPropertyAssertion(
URI.create(OWLClassC.getRefListField()
.getAnnotation(OWLObjectProperty.class).iri()),
refListMock.isInferred()));
assertEquals(res.getNextNode(), Assertion.createObjectPropertyAssertion(refListMock
.getOWLObjectPropertyHasNextIRI().toURI(), refListMock.isInferred()));
assertEquals(res.getNodeContent(), Assertion.createObjectPropertyAssertion(refListMock
.getOWLPropertyHasContentsIRI().toURI(), refListMock.isInferred()));
assertEquals(list.size(), res.getValues().size());
for (OWLClassA a : list) {
assertTrue(res.getValues().contains(NamedResource.create(a.getUri())));
}
}
@Test
public void extractsValuesIntoAxiomsForSaveFromEmptyList() throws Exception {
final OWLClassC c = new OWLClassC(PK);
c.setReferencedList(Collections.emptyList());
final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK),
descriptor.getContext());
strategy.buildAxiomValuesFromInstance(c, builder);
final List<ReferencedListValueDescriptor> descriptors = OOMTestUtils.getReferencedListValueDescriptors(builder);
assertEquals(1, descriptors.size());
final ReferencedListValueDescriptor res = descriptors.get(0);
assertTrue(res.getValues().isEmpty());
}
@Test
public void extractsValuesIntoAxiomsForSaveFromNullList() throws Exception {
final OWLClassC c = new OWLClassC(PK);
c.setReferencedList(null);
final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), descriptor.getContext());
strategy.buildAxiomValuesFromInstance(c, builder);
final List<ReferencedListValueDescriptor> descriptors = OOMTestUtils.getReferencedListValueDescriptors(builder);
assertEquals(1, descriptors.size());
final ReferencedListValueDescriptor res = descriptors.get(0);
assertTrue(res.getValues().isEmpty());
}
@Test
public void extractsValuesIntoAxiomsFromListOfPlainIdentifiers() throws Exception {
final ListAttribute<OWLClassP, URI> listAtt = mocks.forOwlClassP().pReferencedListAttribute();
final ReferencedListPropertyStrategy<OWLClassP> strategy =
new ReferencedListPropertyStrategy<>(mocks.forOwlClassP().entityType(), listAtt, descriptor,
mapperMock);
final OWLClassP p = new OWLClassP();
p.setUri(PK);
p.setReferencedList(list.stream().map(OWLClassA::getUri).collect(Collectors.toList()));
final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK),
descriptor.getContext());
strategy.buildAxiomValuesFromInstance(p, builder);
final ReferencedListValueDescriptor res = OOMTestUtils.getReferencedListValueDescriptors(builder).get(0);
p.getReferencedList().forEach(uri -> assertTrue(res.getValues().contains(NamedResource.create(uri))));
}
}