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