-
Notifications
You must be signed in to change notification settings - Fork 14
/
EntityConstructorTest.java
654 lines (588 loc) · 32.2 KB
/
EntityConstructorTest.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/**
* Copyright (C) 2020 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.*;
import cz.cvut.kbss.jopa.environment.utils.Generators;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.exceptions.CardinalityConstraintViolatedException;
import cz.cvut.kbss.jopa.exceptions.IntegrityConstraintViolatedException;
import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.model.SequencesVocabulary;
import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
import cz.cvut.kbss.ontodriver.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class EntityConstructorTest {
private static final URI PK = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityX");
private static final NamedResource PK_RESOURCE = NamedResource.create(PK);
private static final URI PK_TWO = URI
.create("http://krizik.felk.cvut.cz/ontologies/jopa/entityXX");
private static final String STRING_ATT = "StringAttributeValue";
private static final Set<String> TYPES = initTypes();
@Mock
private ObjectOntologyMapperImpl mapperMock;
private MetamodelMocks mocks;
private Descriptor descriptor;
private EntityConstructor constructor;
@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(mapperMock.getConfiguration()).thenReturn(new Configuration(Collections.emptyMap()));
this.mocks = new MetamodelMocks();
this.descriptor = new EntityDescriptor();
this.constructor = new EntityConstructor(mapperMock);
}
private static Set<String> initTypes() {
final Set<String> set = new HashSet<>(8);
set.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassU");
set.add("http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassV");
return set;
}
@Test
void testReconstructEntityWithTypesAndDataProperty() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri()));
axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField()));
axioms.addAll(getTypesAxiomsForOwlClassA());
final OWLClassA res = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms);
assertNotNull(res);
assertEquals(PK, res.getUri());
assertEquals(STRING_ATT, res.getStringAttribute());
assertEquals(TYPES, res.getTypes());
verify(mapperMock).registerInstance(PK, res);
}
private Axiom<URI> getClassAssertionAxiomForType(String type) {
return new AxiomImpl<>(NamedResource.create(PK), Assertion.createClassAssertion(false),
new Value<>(URI.create(type)));
}
private Axiom<String> getStringAttAssertionAxiom(Field attField) {
final String assertionIri = attField.getAnnotation(OWLDataProperty.class).iri();
return new AxiomImpl<>(NamedResource.create(PK),
Assertion.createDataPropertyAssertion(URI.create(assertionIri), false),
new Value<>(STRING_ATT));
}
private Set<Axiom<?>> getTypesAxiomsForOwlClassA() {
final Set<Axiom<?>> axs = new HashSet<>();
for (String type : TYPES) {
final Axiom<URI> ax = new AxiomImpl<>(NamedResource.create(PK), Assertion.createClassAssertion(false),
new Value<>(URI.create(type)));
axs.add(ax);
}
return axs;
}
@Test
void testReconstructEntityWithDataPropertyEmptyTypes() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri()));
axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField()));
final OWLClassA res = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms);
assertNotNull(res);
assertEquals(PK, res.getUri());
assertEquals(STRING_ATT, res.getStringAttribute());
assertNull(res.getTypes());
verify(mapperMock).registerInstance(PK, res);
}
@Test
void testReconstructEntityWithDataPropertyAndProperties() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassB.getClassIri()));
axioms.add(getStringAttAssertionAxiom(OWLClassB.getStrAttField()));
final Collection<Axiom<?>> properties = getProperties();
axioms.addAll(properties);
final OWLClassB res = constructor.reconstructEntity(PK, mocks.forOwlClassB().entityType(), descriptor, axioms);
assertNotNull(res);
assertEquals(PK, res.getUri());
assertEquals(STRING_ATT, res.getStringAttribute());
assertNotNull(res.getProperties());
assertEquals(properties.size(), res.getProperties().size());
for (Axiom<?> a : properties) {
final String key = a.getAssertion().getIdentifier().toString();
assertTrue(res.getProperties().containsKey(key));
assertEquals(1, res.getProperties().get(key).size());
assertEquals(a.getValue().stringValue(), res.getProperties().get(key).iterator().next());
}
verify(mapperMock).registerInstance(PK, res);
}
private Collection<Axiom<?>> getProperties() {
final Set<Axiom<?>> props = new HashSet<>();
final Axiom<String> axOne = new AxiomImpl<>(PK_RESOURCE, Assertion
.createDataPropertyAssertion(URI.create("http://someDataPropertyOne"),
false), new Value<>("SomePropertyValue"));
props.add(axOne);
final Axiom<String> axTwo = new AxiomImpl<>(PK_RESOURCE, Assertion.createAnnotationPropertyAssertion(
URI.create("http://someAnnotationPropertyOne"), false), new Value<>("annotationValue"));
props.add(axTwo);
final Axiom<URI> axThree = new AxiomImpl<>(PK_RESOURCE, Assertion
.createObjectPropertyAssertion(URI.create("http://someObjectPropertyOne"),
false), new Value<>(URI.create("http://someObjectPropertyOne")));
props.add(axThree);
return props;
}
@Test
void testReconstructEntityWithDataPropertiesAndEmptyProperties() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassB.getClassIri()));
axioms.add(getStringAttAssertionAxiom(OWLClassB.getStrAttField()));
final OWLClassB res = constructor.reconstructEntity(PK, mocks.forOwlClassB().entityType(), descriptor, axioms);
assertNotNull(res);
assertEquals(PK, res.getUri());
assertEquals(STRING_ATT, res.getStringAttribute());
assertNull(res.getProperties());
verify(mapperMock).registerInstance(PK, res);
}
@Test
void testReconstructEntityWithObjectProperty() throws Exception {
final Set<Axiom<?>> axiomsD = getAxiomsForD();
final Descriptor fieldDesc = new EntityDescriptor();
descriptor.addAttributeDescriptor(mocks.forOwlClassD().owlClassAAtt(), fieldDesc);
final OWLClassA entityA = new OWLClassA();
entityA.setUri(PK_TWO);
entityA.setStringAttribute(STRING_ATT);
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDesc))
.thenReturn(entityA);
final OWLClassD res = constructor.reconstructEntity(PK, mocks.forOwlClassD().entityType(), descriptor, axiomsD);
assertNotNull(res);
assertEquals(PK, res.getUri());
verify(mapperMock).getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDesc);
assertNotNull(res.getOwlClassA());
assertEquals(PK_TWO, res.getOwlClassA().getUri());
assertEquals(STRING_ATT, res.getOwlClassA().getStringAttribute());
verify(mapperMock).registerInstance(PK, res);
}
private Set<Axiom<?>> getAxiomsForD() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassD.getClassIri()));
final Axiom<NamedResource> opAssertion = createObjectPropertyAxiomForD();
axioms.add(opAssertion);
return axioms;
}
private Axiom<NamedResource> createObjectPropertyAxiomForD() throws NoSuchFieldException {
return new AxiomImpl<>(NamedResource.create(PK), getClassDObjectPropertyAssertion(),
new Value<>(NamedResource.create(PK_TWO)));
}
private Assertion getClassDObjectPropertyAssertion() throws NoSuchFieldException {
final URI assertionUri = URI.create(OWLClassD.getOwlClassAField()
.getAnnotation(OWLObjectProperty.class).iri());
return Assertion.createObjectPropertyAssertion(assertionUri, false);
}
@Test
void reconstructsEntityWithDataPropertyWhereRangeDoesNotMatch() throws Exception {
final List<Axiom<?>> axioms = new ArrayList<>();
axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri()));
// Using a list and adding the incorrect range value before the right value will cause it to be processed first
axioms.add(createDataPropertyAxiomWithWrongRange(OWLClassA.getStrAttField()));
axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField()));
OWLClassA entityA = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms);
assertNotNull(entityA);
assertEquals(STRING_ATT, entityA.getStringAttribute());
axioms.clear();
axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri()));
// Now reverse it
axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField()));
axioms.add(createDataPropertyAxiomWithWrongRange(OWLClassA.getStrAttField()));
entityA = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms);
assertNotNull(entityA);
assertEquals(STRING_ATT, entityA.getStringAttribute());
}
@Test
void throwsExceptionWhenDataPropertyCardinalityRestrictionIsNotMet() throws Exception {
final List<Axiom<?>> axioms = new ArrayList<>();
axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri()));
axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField()));
axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField()));
assertThrows(CardinalityConstraintViolatedException.class,
() -> constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms));
}
private Axiom<?> createDataPropertyAxiomWithWrongRange(Field valueField) {
final OWLDataProperty prop = valueField.getAnnotation(OWLDataProperty.class);
assert prop != null;
final Assertion assertion = Assertion.createDataPropertyAssertion(URI.create(prop.iri()), false);
return new AxiomImpl<>(NamedResource.create(PK), assertion, new Value<>(System.currentTimeMillis()));
}
@Test
void reconstructsEntityWithObjectPropertyWhereRangeDoesNotMatch() throws Exception {
final Set<Axiom<?>> axioms = getAxiomsForD();
final Descriptor fieldDescriptor = new EntityDescriptor();
descriptor.addAttributeDescriptor(mocks.forOwlClassD().owlClassAAtt(), fieldDescriptor);
// The property value hasn't the corresponding type, so it cannot be returned by mapper
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDescriptor)).thenReturn(null);
final OWLClassD res = constructor.reconstructEntity(PK, mocks.forOwlClassD().entityType(), descriptor, axioms);
assertNotNull(res);
assertNull(res.getOwlClassA());
}
@Test
void throwsExceptionWhenObjectPropertyCardinalityRestrictionIsNotMet() throws Exception {
final Set<Axiom<?>> axioms = getAxiomsForD();
final OWLClassA entityA = new OWLClassA();
entityA.setUri(PK_TWO);
final URI pkThree = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/entity3");
axioms.add(new AxiomImpl<>(NamedResource.create(PK), getClassDObjectPropertyAssertion(),
new Value<>(NamedResource.create(pkThree))));
final OWLClassA entityThree = new OWLClassA();
entityThree.setUri(pkThree);
final Descriptor fieldDescriptor = new EntityDescriptor();
descriptor.addAttributeDescriptor(mocks.forOwlClassD().owlClassAAtt(), fieldDescriptor);
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDescriptor)).thenReturn(entityA);
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, pkThree, fieldDescriptor))
.thenReturn(entityThree);
assertThrows(CardinalityConstraintViolatedException.class,
() -> constructor.reconstructEntity(PK, mocks.forOwlClassD().entityType(), descriptor, axioms));
}
@Test
void testSetFieldValue_DataProperty() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField()));
final OWLClassA entityA = new OWLClassA();
entityA.setUri(PK);
assertNull(entityA.getStringAttribute());
constructor.setFieldValue(entityA, OWLClassA.getStrAttField(), axioms, mocks.forOwlClassA().entityType(),
descriptor);
assertNotNull(entityA.getStringAttribute());
assertEquals(STRING_ATT, entityA.getStringAttribute());
}
@Test
void testSetFieldValue_ObjectProperty() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
final Descriptor fieldDesc = mock(Descriptor.class);
descriptor.addAttributeDescriptor(mocks.forOwlClassD().owlClassAAtt(), fieldDesc);
axioms.add(createObjectPropertyAxiomForD());
final OWLClassD entityD = new OWLClassD();
entityD.setUri(PK);
assertNull(entityD.getOwlClassA());
final OWLClassA entityA = new OWLClassA();
entityA.setUri(PK_TWO);
entityA.setStringAttribute(STRING_ATT);
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDesc))
.thenReturn(entityA);
constructor.setFieldValue(entityD, OWLClassD.getOwlClassAField(), axioms, mocks.forOwlClassD().entityType(),
descriptor);
assertNotNull(entityD.getOwlClassA());
assertEquals(PK_TWO, entityD.getOwlClassA().getUri());
verify(mapperMock).getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDesc);
}
@Test
void testSetFieldValue_Types() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>(getTypesAxiomsForOwlClassA());
final Descriptor fieldDesc = mock(Descriptor.class);
descriptor.addAttributeDescriptor(mocks.forOwlClassA().typesSpec(), fieldDesc);
final OWLClassA entityA = new OWLClassA();
entityA.setUri(PK);
assertNull(entityA.getTypes());
constructor.setFieldValue(entityA, OWLClassA.getTypesField(), axioms, mocks.forOwlClassA().entityType(),
descriptor);
assertNotNull(entityA.getTypes());
assertEquals(TYPES.size(), entityA.getTypes().size());
assertTrue(entityA.getTypes().containsAll(TYPES));
}
@Test
void testSetFieldValue_Properties() throws Exception {
final Map<String, Set<String>> props = Generators.generateStringProperties();
final Set<Axiom<?>> axioms = initAxiomsForProperties(props);
final OWLClassB entityB = new OWLClassB();
entityB.setUri(PK);
assertNull(entityB.getProperties());
constructor.setFieldValue(entityB, OWLClassB.getPropertiesField(), axioms, mocks.forOwlClassB().entityType(),
descriptor);
assertNotNull(entityB.getProperties());
assertEquals(props, entityB.getProperties());
}
private Set<Axiom<?>> initAxiomsForProperties(Map<String, Set<String>> props) {
final Set<Axiom<?>> axioms = new HashSet<>();
for (Entry<String, Set<String>> e : props.entrySet()) {
final URI property = URI.create(e.getKey());
axioms.addAll(e.getValue().stream().map(val -> new AxiomImpl<>(PK_RESOURCE, Assertion
.createPropertyAssertion(property, false), new Value<>(URI.create(val))))
.collect(Collectors.toList()));
}
return axioms;
}
@Test
void testSetFieldValue_ObjectPropertySet() throws Exception {
final Descriptor desc = mock(Descriptor.class);
final Set<OWLClassA> set = initEntities(desc);
descriptor.addAttributeDescriptor(mocks.forOwlClassJ().setAttribute(), desc);
final Collection<Axiom<?>> axioms = initAxiomsForReferencedSet(set);
final OWLClassJ entityJ = new OWLClassJ();
entityJ.setUri(PK);
assertNull(entityJ.getOwlClassA());
constructor.setFieldValue(entityJ, OWLClassJ.getOwlClassAField(), axioms, mocks.forOwlClassJ().entityType(),
descriptor);
assertNotNull(entityJ.getOwlClassA());
assertEquals(set.size(), entityJ.getOwlClassA().size());
assertTrue(areEqual(set, entityJ.getOwlClassA()));
}
private Set<OWLClassA> initEntities(Descriptor desc) {
final Set<OWLClassA> entities = new HashSet<>();
for (int i = 0; i < 5; i++) {
final OWLClassA a = new OWLClassA();
a.setUri(URI.create("http://krizik.felk.cvut.cz/entityA" + i));
a.setStringAttribute(STRING_ATT);
entities.add(a);
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, a.getUri(), desc))
.thenReturn(a);
}
return entities;
}
private Collection<Axiom<?>> initAxiomsForReferencedSet(Set<OWLClassA> as) throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
final URI property = URI.create(OWLClassJ.getOwlClassAField()
.getAnnotation(OWLObjectProperty.class).iri());
for (OWLClassA a : as) {
final Axiom<NamedResource> ax = new AxiomImpl<>(NamedResource.create(PK),
Assertion.createObjectPropertyAssertion(property, false),
new Value<>(NamedResource.create(
a.getUri())));
axioms.add(ax);
}
return axioms;
}
private static boolean areEqual(Set<OWLClassA> expected, Set<OWLClassA> actual) {
boolean found = false;
for (OWLClassA a : expected) {
found = false;
for (OWLClassA aa : actual) {
if (a.getUri().equals(aa.getUri())) {
found = true;
assertEquals(a.getStringAttribute(), aa.getStringAttribute());
break;
}
}
if (!found) {
return false;
}
}
return found;
}
@Test
void reconstructsEntityWithDataPropertiesOfBasicTypesAndStringIdentifier() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassM.getClassIri()));
final Integer i = 117;
final Long lng = 365L;
final Double d = 3.14;
final Date date = new Date();
axioms.addAll(createAxiomsForValues(true, i, lng, d, date, null));
final OWLClassM res = constructor.reconstructEntity(PK, mocks.forOwlClassM().entityType(), descriptor, axioms);
assertEquals(true, res.getBooleanAttribute());
assertEquals(i, res.getIntAttribute());
assertEquals(lng, res.getLongAttribute());
assertEquals(d, res.getDoubleAttribute());
assertEquals(date, res.getDateAttribute());
}
private Collection<Axiom<?>> createAxiomsForValues(Boolean b, Integer i, Long lng, Double d, Date date,
OWLClassM.Severity severity) throws
Exception {
final Collection<Axiom<?>> axioms = new ArrayList<>();
if (b != null) {
final String boolAttIri = OWLClassM.getBooleanAttributeField().getAnnotation(OWLDataProperty.class).iri();
axioms.add(
new AxiomImpl<>(PK_RESOURCE, Assertion.createDataPropertyAssertion(URI.create(boolAttIri), false),
new Value<>(b)));
}
if (i != null) {
final String intAttIri = OWLClassM.getIntAttributeField().getAnnotation(OWLDataProperty.class).iri();
axioms.add(new AxiomImpl<>(PK_RESOURCE, Assertion.createDataPropertyAssertion(URI.create(intAttIri), false),
new Value<>(i)));
}
if (lng != null) {
final String longAttIri = OWLClassM.getLongAttributeField().getAnnotation(OWLDataProperty.class).iri();
final Assertion laAssertion = Assertion.createDataPropertyAssertion(URI.create(longAttIri), false);
axioms.add(new AxiomImpl<>(PK_RESOURCE, laAssertion, new Value<>(lng)));
}
if (d != null) {
final String doubleAttIri = OWLClassM.getDoubleAttributeField().getAnnotation(OWLDataProperty.class).iri();
axioms.add(
new AxiomImpl<>(PK_RESOURCE, Assertion.createDataPropertyAssertion(URI.create(doubleAttIri), false),
new Value<>(d)));
}
if (date != null) {
final String dateAttIri = OWLClassM.getDateAttributeField().getAnnotation(OWLDataProperty.class).iri();
axioms.add(
new AxiomImpl<>(PK_RESOURCE, Assertion.createDataPropertyAssertion(URI.create(dateAttIri), false),
new Value<>(date)));
}
if (severity != null) {
final String enumAttIri = OWLClassM.getEnumAttributeField().getAnnotation(OWLDataProperty.class).iri();
axioms.add(
new AxiomImpl<>(PK_RESOURCE, Assertion.createDataPropertyAssertion(URI.create(enumAttIri), false),
new Value<>(severity.toString())));
}
return axioms;
}
@Test
void reconstructsEntityWithEnumDataProperty() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassM.getClassIri()));
final OWLClassM.Severity enumValue = OWLClassM.Severity.HIGH;
axioms.addAll(createAxiomsForValues(null, null, null, null, null, enumValue));
final OWLClassM result = constructor
.reconstructEntity(PK, mocks.forOwlClassM().entityType(), descriptor, axioms);
assertNotNull(result);
assertNotNull(result.getEnumAttribute());
assertEquals(enumValue, result.getEnumAttribute());
}
@Test
void icsAreValidatedForAllFieldsOnEntityLoad() {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassL.getClassIri()));
assertThrows(IntegrityConstraintViolatedException.class,
() -> constructor.reconstructEntity(PK, mocks.forOwlClassL().entityType(), descriptor, axioms));
}
@Test
void icValidationIsSkippedOnLoadBasedOnProperties() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassL.getClassIri()));
final Map<String, String> props = Collections
.singletonMap(JOPAPersistenceProperties.DISABLE_IC_VALIDATION_ON_LOAD, Boolean.TRUE.toString());
final Configuration conf = new Configuration(props);
when(mapperMock.getConfiguration()).thenReturn(conf);
final OWLClassL result = constructor
.reconstructEntity(PK, mocks.forOwlClassL().entityType(), descriptor, axioms);
assertNotNull(result);
assertNull(result.getSingleA());
}
@Test
void icsAreValidatedOnLazilyLoadedFieldFetch() throws Exception {
final Set<Axiom<?>> fieldAxiom = initInvalidFieldValuesForICValidation();
final OWLClassL instance = new OWLClassL();
assertThrows(IntegrityConstraintViolatedException.class, () -> constructor
.setFieldValue(instance, OWLClassL.getReferencedListField(), fieldAxiom,
mocks.forOwlClassL().entityType(), descriptor));
}
private Set<Axiom<?>> initInvalidFieldValuesForICValidation() throws NoSuchFieldException {
final Set<Axiom<NamedResource>> listAxioms = new HashSet<>();
when(mapperMock.loadReferencedList(any(ReferencedListDescriptor.class))).thenReturn(listAxioms);
final Descriptor attDescriptor = new EntityDescriptor();
descriptor.addAttributeDescriptor(mocks.forOwlClassL().referencedListAtt(), attDescriptor);
final Assertion nodeContent = Assertion
.createObjectPropertyAssertion(URI.create(SequencesVocabulary.s_p_hasContents), false);
final int max = OWLClassL.getReferencedListField().getAnnotation(ParticipationConstraints.class).value()[0]
.max();
for (int i = 0; i < max + 1; i++) {
final URI uri = URI.create("http://value" + i);
listAxioms.add(new AxiomImpl<>(PK_RESOURCE, nodeContent, new Value<>(NamedResource.create(uri))));
when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, uri, attDescriptor))
.thenReturn(new OWLClassA(uri));
}
final Assertion assertion = Assertion.createObjectPropertyAssertion(
URI.create(OWLClassL.getReferencedListField().getAnnotation(OWLObjectProperty.class).iri()), false);
return Collections.singleton(
new AxiomImpl<>(PK_RESOURCE, assertion, new Value<>(URI.create("http://referencedList"))));
}
@Test
void icsAreNotValidatedOnLazilyLoadedFetchWhenLoadingICValidationIsDisabled() throws Exception {
final Set<Axiom<?>> fieldAxiom = initInvalidFieldValuesForICValidation();
final Map<String, String> props = Collections
.singletonMap(JOPAPersistenceProperties.DISABLE_IC_VALIDATION_ON_LOAD, Boolean.TRUE.toString());
final Configuration conf = new Configuration(props);
when(mapperMock.getConfiguration()).thenReturn(conf);
final OWLClassL instance = new OWLClassL();
constructor.setFieldValue(instance, OWLClassL.getReferencedListField(), fieldAxiom,
mocks.forOwlClassL().entityType(), descriptor);
}
@Test
void instanceReconstructionSkipsAxiomsForWhichNoAttributeCanBeFound() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri()));
axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField()));
final Assertion unknown =
Assertion.createAnnotationPropertyAssertion(Generators.createPropertyIdentifier(), false);
axioms.add(new AxiomImpl<>(PK_RESOURCE, unknown, new Value<>("UnknownPropertyValue")));
final OWLClassA res = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms);
assertNotNull(res);
assertEquals(PK, res.getUri());
assertNotNull(res.getStringAttribute());
assertNull(res.getTypes());
}
@Test
void reconstructsInstanceWithMappedSuperclass() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassQ.getClassIri()));
axioms.add(getStringAttAssertionAxiom(OWLClassQ.getStringAttributeField()));
axioms.add(getStringAttAssertionAxiom(OWLClassQ.getParentStringField()));
final URI labelUri = URI.create(OWLClassQ.getLabelField().getAnnotation(OWLAnnotationProperty.class).iri());
axioms.add(new AxiomImpl<>(PK_RESOURCE, Assertion.createAnnotationPropertyAssertion(labelUri, false),
new Value<>(STRING_ATT)));
final URI owlClassAUri = URI.create(OWLClassQ.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri());
axioms.add(new AxiomImpl<>(PK_RESOURCE, Assertion.createObjectPropertyAssertion(owlClassAUri, false),
new Value<>(NamedResource.create(PK_TWO))));
final OWLClassA a = new OWLClassA();
a.setUri(PK_TWO);
when(mapperMock.getEntityFromCacheOrOntology(eq(OWLClassA.class), eq(PK_TWO), any(Descriptor.class)))
.thenReturn(a);
final OWLClassQ res = constructor.reconstructEntity(PK, mocks.forOwlClassQ().entityType(), descriptor, axioms);
assertNotNull(res);
assertEquals(PK, res.getUri());
assertEquals(STRING_ATT, res.getStringAttribute());
assertEquals(STRING_ATT, res.getParentString());
assertEquals(STRING_ATT, res.getLabel());
assertSame(a, res.getOwlClassA());
}
@Test
void reconstructsInstanceWithPluralAnnotationProperty() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassN.getClassIri()));
final Assertion assertion =
Assertion.createAnnotationPropertyAssertion(URI.create(Vocabulary.DC_SOURCE), false);
axioms.add(new AxiomImpl<>(PK_RESOURCE, assertion, new Value<>(STRING_ATT)));
final OWLClassN result =
constructor.reconstructEntity(PK, mocks.forOwlClassN().entityType(), descriptor, axioms);
assertNotNull(result);
assertEquals(PK.toString(), result.getId());
assertEquals(Collections.singleton(STRING_ATT), result.getPluralAnnotation());
}
@Test
void setFieldValueSetsNothingWhenAxiomsAreEmpty() throws Exception {
final OWLClassA entity = new OWLClassA(Generators.createIndividualIdentifier());
assertNull(entity.getTypes());
constructor.setFieldValue(entity, OWLClassA.getTypesField(), Collections.emptyList(),
mocks.forOwlClassA().entityType(), descriptor);
assertNull(entity.getTypes());
}
@Test
void setFieldValueValidatesIntegrityConstraintsAlsoForEmptyAxiomCollection() {
final OWLClassL instance = new OWLClassL(Generators.createIndividualIdentifier());
assertThrows(IntegrityConstraintViolatedException.class, () -> constructor
.setFieldValue(instance, OWLClassL.getSingleAField(), Collections.emptyList(),
mocks.forOwlClassL().entityType(), descriptor));
}
@Test
void testReconstructEntityWithQueryAttribute() throws Exception {
final Set<Axiom<?>> axioms = new HashSet<>();
axioms.add(getClassAssertionAxiomForType(OWLClassWithQueryAttr.getClassIri()));
axioms.add(getStringAttAssertionAxiom(OWLClassWithQueryAttr.getStrAttField()));
//TODO mock query factory, typed query
final OWLClassWithQueryAttr res = constructor.reconstructEntity(PK, mocks.forOwlClassWithQueryAttr().entityType(), descriptor, axioms);
assertNotNull(res);
assertEquals(PK, res.getUri());
assertEquals(STRING_ATT, res.getStringAttribute());
assertEquals(STRING_ATT, res.getStringQueryAttribute());
verify(mapperMock).registerInstance(PK, res);
}
}