-
Notifications
You must be signed in to change notification settings - Fork 14
/
SimpleListPropertyStrategyTest.java
249 lines (222 loc) · 12.2 KB
/
SimpleListPropertyStrategyTest.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
/**
* Copyright (C) 2019 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.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.annotations.Sequence;
import cz.cvut.kbss.jopa.model.metamodel.ListAttribute;
import cz.cvut.kbss.jopa.model.metamodel.ListAttributeImpl;
import cz.cvut.kbss.ontodriver.descriptor.SimpleListDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
import cz.cvut.kbss.ontodriver.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockitoAnnotations;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
public class SimpleListPropertyStrategyTest extends ListPropertyStrategyTestBase {
private ListAttributeImpl<OWLClassC, OWLClassA> simpleList;
private SimpleListPropertyStrategy<OWLClassC> strategy;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
super.setUp();
this.simpleList = mocks.forOwlClassC().simpleListAtt();
this.strategy =
new SimpleListPropertyStrategy<>(mocks.forOwlClassC().entityType(), simpleList, descriptor, mapperMock);
strategy.setReferenceSavingResolver(new ReferenceSavingResolver(mapperMock));
}
@Test
void buildsInstanceFieldFromAxioms() {
final Axiom<URI> ax = new AxiomImpl<>(NamedResource.create(PK),
Assertion.createObjectPropertyAssertion(simpleList.getIRI().toURI(), false), new Value<>(
URI.create("http://someSequence.org")));
final List<OWLClassA> entitiesA = generateList();
final Collection<Axiom<NamedResource>> axioms = buildAxiomsForList(simpleList, entitiesA);
when(mapperMock.loadSimpleList(any(SimpleListDescriptor.class))).thenReturn(axioms);
strategy.addValueFromAxiom(ax);
final OWLClassC instance = new OWLClassC();
instance.setUri(PK);
strategy.buildInstanceFieldValue(instance);
assertEquals(entitiesA.size(), instance.getSimpleList().size());
for (OWLClassA a : entitiesA) {
assertTrue(instance.getSimpleList().contains(a));
}
}
private Collection<Axiom<NamedResource>> buildAxiomsForList(ListAttribute<?, ?> la, List<OWLClassA> lst) {
final Collection<Axiom<NamedResource>> axioms = new ArrayList<>();
URI previous = PK;
for (OWLClassA item : lst) {
final Axiom<NamedResource> a = new AxiomImpl<>(
NamedResource.create(previous),
Assertion.createObjectPropertyAssertion(la.getOWLObjectPropertyHasNextIRI().toURI(), false),
new Value<>(NamedResource.create(item.getUri())));
axioms.add(a);
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, item.getUri(),
descriptor.getAttributeDescriptor(simpleList))).thenReturn(item);
previous = item.getUri();
}
return axioms;
}
@Test
void buildsInstanceFieldOfPlainIdentifiersFromAxioms() {
final ListAttributeImpl<OWLClassP, URI> simpleList = mocks.forOwlClassP().pSimpleListAttribute();
final SimpleListPropertyStrategy<OWLClassP> strategy =
new SimpleListPropertyStrategy<>(mocks.forOwlClassP().entityType(), simpleList, descriptor, mapperMock);
final Axiom<URI> ax = new AxiomImpl<>(NamedResource.create(PK),
Assertion.createObjectPropertyAssertion(simpleList.getIRI().toURI(), false), new Value<>(
URI.create("http://someSequence.org")));
final List<OWLClassA> as = generateList();
final Collection<Axiom<NamedResource>> axioms = buildAxiomsForList(simpleList, as);
when(mapperMock.loadSimpleList(any(SimpleListDescriptor.class))).thenReturn(axioms);
strategy.addValueFromAxiom(ax);
final OWLClassP instance = new OWLClassP();
instance.setUri(PK);
strategy.buildInstanceFieldValue(instance);
assertEquals(as.size(), instance.getSimpleList().size());
for (int i = 0; i < as.size(); i++) {
assertEquals(as.get(i).getUri(), instance.getSimpleList().get(i));
}
}
@Test
void addsValueFromAxiomAndVerifiesCorrectDescriptorWasCreated() {
final Axiom<NamedResource> ax = new AxiomImpl<>(NamedResource.create(PK),
Assertion.createObjectPropertyAssertion(simpleList.getIRI()
.toURI(), false), new Value<>(
NamedResource.create("http://someSequence.org")));
final Collection<Axiom<NamedResource>> axioms = Collections.emptyList();
when(mapperMock.loadSimpleList(any(SimpleListDescriptor.class)))
.thenReturn(axioms);
strategy.addValueFromAxiom(ax);
final ArgumentCaptor<SimpleListDescriptor> captor = ArgumentCaptor
.forClass(SimpleListDescriptor.class);
verify(mapperMock).loadSimpleList(captor.capture());
final SimpleListDescriptor res = captor.getValue();
assertEquals(PK, res.getListOwner().getIdentifier());
assertEquals(simpleList.getIRI().toURI(), res.getListProperty()
.getIdentifier());
assertEquals(simpleList.getOWLObjectPropertyHasNextIRI().toURI(), res
.getNextNode().getIdentifier());
assertNull(res.getContext());
}
@Test
void extractsListValuesForSave() throws Exception {
final OWLClassC c = new OWLClassC(PK);
c.setSimpleList(generateList());
strategy.buildAxiomValuesFromInstance(c, builder);
final SimpleListValueDescriptor res = listValueDescriptor();
assertEquals(PK, res.getListOwner().getIdentifier());
final Field simpleListField = OWLClassC.getSimpleListField();
assertEquals(simpleListField.getAnnotation(OWLObjectProperty.class)
.iri(), res.getListProperty().getIdentifier().toString());
assertEquals(simpleListField.getAnnotation(Sequence.class)
.ObjectPropertyHasNextIRI(), res.getNextNode().getIdentifier()
.toString());
assertEquals(c.getSimpleList().size(), res.getValues().size());
for (int i = 0; i < c.getSimpleList().size(); i++) {
assertEquals(c.getSimpleList().get(i).getUri(), res.getValues()
.get(i).getIdentifier());
}
}
private SimpleListValueDescriptor listValueDescriptor() throws Exception {
final List<SimpleListValueDescriptor> descriptors = OOMTestUtils.getSimpleListValueDescriptors(builder);
assertEquals(1, descriptors.size());
return descriptors.get(0);
}
@Test
void extractsListValuesForSaveListIsEmpty() throws Exception {
final OWLClassC c = new OWLClassC(PK);
c.setSimpleList(new ArrayList<>());
strategy.buildAxiomValuesFromInstance(c, builder);
final SimpleListValueDescriptor res = listValueDescriptor();
assertEquals(PK, res.getListOwner().getIdentifier());
final Field simpleListField = OWLClassC.getSimpleListField();
assertEquals(simpleListField.getAnnotation(OWLObjectProperty.class)
.iri(), res.getListProperty().getIdentifier().toString());
assertEquals(simpleListField.getAnnotation(Sequence.class)
.ObjectPropertyHasNextIRI(), res.getNextNode().getIdentifier()
.toString());
assertTrue(res.getValues().isEmpty());
}
@Test
void extractsListValuesForSaveListIsNull() throws Exception {
final OWLClassC c = new OWLClassC(PK);
c.setSimpleList(null);
strategy.buildAxiomValuesFromInstance(c, builder);
final SimpleListValueDescriptor res = listValueDescriptor();
assertEquals(PK, res.getListOwner().getIdentifier());
final Field simpleListField = OWLClassC.getSimpleListField();
assertEquals(simpleListField.getAnnotation(OWLObjectProperty.class)
.iri(), res.getListProperty().getIdentifier().toString());
assertEquals(simpleListField.getAnnotation(Sequence.class)
.ObjectPropertyHasNextIRI(), res.getNextNode().getIdentifier()
.toString());
assertTrue(res.getValues().isEmpty());
}
@Test
void extractListValuesSkipsNullItems() throws Exception {
final OWLClassC c = new OWLClassC(PK);
c.setSimpleList(generateList());
setRandomListItemsToNull(c.getSimpleList());
strategy.buildAxiomValuesFromInstance(c, builder);
final SimpleListValueDescriptor res = listValueDescriptor();
final List<URI> expected = c.getSimpleList().stream().filter(Objects::nonNull).map(OWLClassA::getUri)
.collect(
Collectors.toList());
verifyListItems(expected, res);
}
@Test
void extractValuesFromListOfPlainIdentifiersForPersist() throws Exception {
final OWLClassP p = new OWLClassP();
p.setUri(PK);
p.setSimpleList(generateListOfIdentifiers());
final ListAttributeImpl<OWLClassP, URI> simpleList = mocks.forOwlClassP().pSimpleListAttribute();
final SimpleListPropertyStrategy<OWLClassP> strategy =
new SimpleListPropertyStrategy<>(mocks.forOwlClassP().entityType(), simpleList, descriptor, mapperMock);
strategy.buildAxiomValuesFromInstance(p, builder);
final SimpleListValueDescriptor valueDescriptor = listValueDescriptor();
verifyListItems(p.getSimpleList(), valueDescriptor);
}
@Test
void extractValuesFromListSkipsNullItemsInListOfPlainIdentifiers() throws Exception {
final OWLClassP p = new OWLClassP();
p.setUri(PK);
p.setSimpleList(generateListOfIdentifiers());
setRandomListItemsToNull(p.getSimpleList());
final List<URI> nonNulls = p.getSimpleList().stream().filter(Objects::nonNull).collect(Collectors.toList());
final ListAttributeImpl<OWLClassP, URI> simpleList = mocks.forOwlClassP().pSimpleListAttribute();
final SimpleListPropertyStrategy<OWLClassP> strategy =
new SimpleListPropertyStrategy<>(mocks.forOwlClassP().entityType(), simpleList, descriptor, mapperMock);
strategy.buildAxiomValuesFromInstance(p, builder);
final SimpleListValueDescriptor valueDescriptor = listValueDescriptor();
verifyListItems(nonNulls, valueDescriptor);
}
@Test
void extractValuesRegistersPendingListItemsWhenListContainsUnpersistedItems() {
final OWLClassC c = new OWLClassC(PK);
c.setSimpleList(generateList());
c.getSimpleList()
.forEach(a -> when(mapperMock.containsEntity(OWLClassA.class, a.getUri(), descriptor)).thenReturn(false));
strategy.buildAxiomValuesFromInstance(c, builder);
c.getSimpleList()
.forEach(item -> verify(mapperMock).registerPendingListReference(eq(item), any(), eq(c.getSimpleList())));
verify(builder, never()).addSimpleListValues(any());
}
}