Skip to contentMethod: findLoadsSubclassOfAbstractParent()
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.test.runner;
19:
20: import cz.cvut.kbss.jopa.model.EntityManager;
21: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
22: import cz.cvut.kbss.jopa.oom.exception.AmbiguousEntityTypeException;
23: import cz.cvut.kbss.jopa.test.*;
24: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
25: import cz.cvut.kbss.jopa.test.environment.Generators;
26: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
27: import cz.cvut.kbss.jopa.test.environment.Quad;
28: import cz.cvut.kbss.jopa.vocabulary.DC;
29: import cz.cvut.kbss.jopa.vocabulary.RDF;
30: import cz.cvut.kbss.jopa.vocabulary.RDFS;
31: import org.junit.jupiter.api.Test;
32: import org.slf4j.Logger;
33:
34: import java.net.URI;
35: import java.util.ArrayList;
36: import java.util.Collection;
37:
38: import static org.junit.jupiter.api.Assertions.*;
39:
40: public abstract class RetrieveOperationsWithInheritanceRunner extends BaseInheritanceRunner {
41:
42: public RetrieveOperationsWithInheritanceRunner(Logger logger, PersistenceFactory persistenceFactory,
43: DataAccessor dataAccessor) {
44: super(logger, persistenceFactory, dataAccessor);
45: }
46:
47: @Test
48: void findReadsAttributesOfMappedSuperclass() throws Exception {
49: final Collection<Quad> data = new ArrayList<>();
50: entityQ.setUri(Generators.generateUri());
51: data.add(
52: new Quad(entityQ.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_Q)));
53: data.add(new Quad(entityQ.getUri(), URI.create(RDFS.LABEL), entityQ.getLabel()));
54: data.add(
55: new Quad(entityQ.getUri(), URI.create(Vocabulary.P_Q_PARENT_STRING_ATTRIBUTE),
56: entityQ.getParentString()));
57: data.add(new Quad(entityQ.getUri(), URI.create(Vocabulary.P_Q_STRING_ATTRIBUTE),
58: entityQ.getStringAttribute()));
59: data.addAll(triplesForA());
60: data.add(new Quad(entityQ.getUri(), URI.create(Vocabulary.P_HAS_OWL_CLASS_A), entityA.getUri()));
61: final EntityManager em = getEntityManager("findReadsAttributesOfMappedSuperclass", false);
62: persistTestData(data, em);
63:
64: final OWLClassQ result = em.find(OWLClassQ.class, entityQ.getUri());
65: assertNotNull(result);
66: assertEquals(entityQ.getStringAttribute(), result.getStringAttribute());
67: assertEquals(entityQ.getLabel(), result.getLabel());
68: assertEquals(entityQ.getParentString(), result.getParentString());
69: assertNotNull(result.getOwlClassA());
70: assertEquals(entityQ.getOwlClassA().getUri(), result.getOwlClassA().getUri());
71: }
72:
73: private Collection<Quad> triplesForA() {
74: final Collection<Quad> data = new ArrayList<>();
75: entityA.setUri(Generators.generateUri());
76: data.add(
77: new Quad(entityA.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_A)));
78: data.add(new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE),
79: entityA.getStringAttribute()));
80: return data;
81: }
82:
83: @Test
84: void findReadsAttributesOfEntitySuperclass() throws Exception {
85: final Collection<Quad> data = triplesForEntityT();
86: final EntityManager em = getEntityManager("findReadsAttributesOfEntitySuperclass", false);
87: persistTestData(data, em);
88:
89: final OWLClassT result = em.find(OWLClassT.class, entityT.getUri());
90: assertNotNull(result);
91: assertEquals(entityT.getName(), result.getName());
92: assertEquals(entityT.getDescription(), result.getDescription());
93: assertEquals(entityT.getIntAttribute(), result.getIntAttribute());
94: assertNotNull(result.getOwlClassA());
95: assertEquals(entityA.getUri(), result.getOwlClassA().getUri());
96: }
97:
98: private Collection<Quad> triplesForEntityT() {
99: final Collection<Quad> data = new ArrayList<>();
100: entityT.setUri(Generators.generateUri());
101: data.add(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_T)));
102: data.add(new Quad(entityT.getUri(), URI.create(RDFS.LABEL), entityT.getName()));
103: data.add(new Quad(entityT.getUri(), URI.create(DC.Terms.DESCRIPTION), entityT.getDescription()));
104: data.add(new Quad(entityT.getUri(), URI.create(Vocabulary.P_T_INTEGER_ATTRIBUTE), entityT.getIntAttribute()));
105: data.addAll(triplesForA());
106: data.add(new Quad(entityT.getUri(), URI.create(Vocabulary.P_HAS_OWL_CLASS_A), entityA.getUri()));
107: return data;
108: }
109:
110: @Test
111: void findLoadsSuperclassInstanceWhenRequestedAndClassAssertionIsPresent() throws Exception {
112: final EntityManager em = getEntityManager("findLoadsSuperclassInstanceWhenRequestedAndClassAssertionIsPresent",
113: false);
114: final Collection<Quad> data = triplesForEntityT();
115: data.add(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
116: persistTestData(data, em);
117:
118: final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
119: assertNotNull(result);
120: assertEquals(entityT.getName(), result.getName());
121: assertEquals(entityT.getDescription(), result.getDescription());
122: assertTrue(result.getTypes().contains(Vocabulary.C_OWL_CLASS_T));
123: }
124:
125: @Test
126: void findLoadsSubclassWhenSuperclassIsPassedInAndTypeCorrespondsToSubclass() throws Exception {
127: final Collection<Quad> data = triplesForEntityT();
128:
129: final EntityManager em = getEntityManager(
130: "findLoadsSubclassWhenSuperclassIsPassedInAndTypeCorrespondsToSubclass",
131: false);
132: persistTestData(data, em);
133:
134: final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
135: assertNotNull(result);
136: assertInstanceOf(OWLClassT.class, result);
137: verifyEntityTAttributes((OWLClassT) result);
138: }
139:
140: private void verifyEntityTAttributes(OWLClassT result) {
141: assertEquals(entityT.getName(), result.getName());
142: assertEquals(entityT.getDescription(), result.getDescription());
143: assertEquals(entityT.getIntAttribute(), result.getIntAttribute());
144: assertEquals(entityT.getOwlClassA().getUri(), result.getOwlClassA().getUri());
145: }
146:
147: @Test
148: void findLoadsSubclassOfAbstractParent() throws Exception {
149: final Collection<Quad> data = new ArrayList<>();
150: entityT.setUri(Generators.generateUri());
151: data.add(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
152: data.add(new Quad(entityT.getUri(), URI.create(RDFS.LABEL), entityT.getName()));
153: data.add(new Quad(entityT.getUri(), URI.create(DC.Terms.DESCRIPTION), entityT.getDescription()));
154:
155: final EntityManager em = getEntityManager("findLoadsSubclassOfAbstractParent", false);
156: persistTestData(data, em);
157:
158: final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
159: assertNotNull(result);
160: assertInstanceOf(OWLClassS.class, result);
161: final OWLClassS sResult = (OWLClassS) result;
162: assertEquals(entityT.getName(), sResult.getName());
163: assertEquals(entityT.getDescription(), sResult.getDescription());
164: }
165:
166: @Test
167: void findLoadsMostConcreteSubclassOfAbstractAncestor() throws Exception {
168: final Collection<Quad> data = triplesForEntityT();
169: data.add(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
170:
171: final EntityManager em = getEntityManager("findLoadsMostConcreteSubclassOfAbstractAncestor", false);
172: persistTestData(data, em);
173:
174: final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
175: assertNotNull(result);
176: assertInstanceOf(OWLClassT.class, result);
177: verifyEntityTAttributes((OWLClassT) result);
178: }
179:
180: @Test
181: void findReturnsNullWhenMatchingClassIsAbstract() throws Exception {
182: final Collection<Quad> data = triplesForEntityT();
183: data.remove(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_T)));
184: data.add(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S_PARENT)));
185:
186: final EntityManager em = getEntityManager("findReturnsNullWhenMatchingClassIsAbstract", false);
187: persistTestData(data, em);
188:
189: assertNull(em.find(OWLClassSParent.class, entityT.getUri()));
190: }
191:
192: @Test
193: void findReturnsMostSpecificSubtypeWhenReturnTypeIsAbstractAncestor() throws Exception {
194: final Collection<Quad> data = triplesForEntityT();
195: data.add(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S)));
196: data.add(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_S_PARENT)));
197:
198: final EntityManager em = getEntityManager("findReturnsMostSpecificSubtypeWhenReturnTypeIsAbstractAncestor",
199: false);
200: persistTestData(data, em);
201:
202: final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
203: assertNotNull(result);
204: assertInstanceOf(OWLClassT.class, result);
205: verifyEntityTAttributes((OWLClassT) result);
206: }
207:
208: @Test
209: void findLoadsMostSpecificSubclassFromCache() {
210: this.em = getEntityManager("findLoadsMostSpecificSubclassFromCache", true);
211: persist(entityT, entityA);
212:
213: final OWLClassSParent result = em.find(OWLClassSParent.class, entityT.getUri());
214: assertNotNull(result);
215: assertInstanceOf(OWLClassT.class, result);
216: verifyEntityTAttributes((OWLClassT) result);
217: }
218:
219: @Test
220: void findLoadsInstanceOfSuperclassFromCacheWhenTypeMatchesAndIsSpecifiedAsReturnType() {
221: this.em = getEntityManager("findLoadsInstanceOfSuperclassWhenTypeMatchesAndIsSpecifiedAsReturnType", true);
222: persist(entityT, entityA);
223:
224: final OWLClassS result = em.find(OWLClassS.class, entityT.getUri());
225: assertNotNull(result);
226: assertEquals(entityT.getName(), result.getName());
227: assertEquals(entityT.getDescription(), result.getDescription());
228: }
229:
230: @Test
231: void findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypes() throws Exception {
232: final Collection<Quad> data = triplesForEntityT();
233: data.add(new Quad(entityT.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_U)));
234: this.em = getEntityManager("findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypes", false);
235: persistTestData(data, em);
236:
237: assertThrows(AmbiguousEntityTypeException.class, () -> em.find(OWLClassS.class, entityT.getUri()));
238: }
239:
240: @Test
241: void findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypesInCache() {
242: this.em = getEntityManager("findThrowsAmbiguousTypeExceptionWhenIndividualHasMultipleMostSpecificTypesInCache",
243: true);
244: persist(entityT, entityA);
245: em.clear();
246: final OWLClassU sameIndividual = new OWLClassU();
247: sameIndividual.setUri(entityT.getUri());
248: sameIndividual.setName(entityT.getName());
249: persist(sameIndividual);
250: assertTrue(em.getEntityManagerFactory().getCache()
251: .contains(OWLClassT.class, entityT.getUri(), new EntityDescriptor()));
252: assertTrue(em.getEntityManagerFactory().getCache()
253: .contains(OWLClassU.class, sameIndividual.getUri(), new EntityDescriptor()));
254:
255: assertThrows(AmbiguousEntityTypeException.class, () -> em.find(OWLClassS.class, entityT.getUri()));
256: }
257:
258: @Test
259: void loadingEntityLoadsExactMatchOfPolymorphicAttribute() {
260: this.em = getEntityManager("loadingEntityLoadsExactMatchOfPolymorphicAttribute", false);
261: final OWLClassS s = new OWLClassS();
262: s.setName("s");
263: s.setDescription("S - description");
264: entityU.setOwlClassS(s);
265: persist(entityU, s);
266:
267: final OWLClassU result = em.find(OWLClassU.class, entityU.getUri());
268: assertNotNull(result);
269: assertNotNull(result.getOwlClassS());
270: assertEquals(s.getUri(), result.getOwlClassS().getUri());
271: assertEquals(s.getName(), result.getOwlClassS().getName());
272: assertEquals(s.getDescription(), result.getOwlClassS().getDescription());
273: }
274:
275: @Test
276: void loadingEntityLoadsCorrectSubtypeInPolymorphicAttribute() {
277: this.em = getEntityManager("loadingEntityLoadsCorrectSubtypeInPolymorphicAttribute", false);
278: persist(entityU, entityT, entityA);
279:
280: final OWLClassU result = em.find(OWLClassU.class, entityU.getUri());
281: assertEquals(entityT.getUri(), result.getOwlClassS().getUri());
282: assertInstanceOf(OWLClassT.class, result.getOwlClassS());
283: final OWLClassT tResult = (OWLClassT) result.getOwlClassS();
284: verifyEntityTAttributes(tResult);
285: assertEquals(entityA.getUri(), tResult.getOwlClassA().getUri());
286: }
287: }