Package: PolymorphicSelectQueryRunner
PolymorphicSelectQueryRunner
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PolymorphicSelectQueryRunner(Logger, DataAccessor) |
|
|
|
|
|
||||||||||||||||||||
lambda$0(EntityManager, OWLClassT) |
|
|
|
|
|
||||||||||||||||||||
selectByTypeLoadsAllIndividualsAsMostConcreteSubclassInstances() |
|
|
|
|
|
||||||||||||||||||||
selectLoadsInstanceAsGivenTypeWhenItIsConcreteAndFoundInTypesOfIndividual() |
|
|
|
|
|
||||||||||||||||||||
selectLoadsInstanceOfMostConcreteSubclassOfAbstractEntity() |
|
|
|
|
|
||||||||||||||||||||
selectLoadsInstanceOfMostConcreteSubclassOfConcreteEntity() |
|
|
|
|
|
||||||||||||||||||||
tearDown() |
|
|
|
|
|
||||||||||||||||||||
verifyEntityTAttributes(OWLClassT, OWLClassT) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2022 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.query.runner;
16:
17: import cz.cvut.kbss.jopa.model.EntityManager;
18: import cz.cvut.kbss.jopa.test.OWLClassS;
19: import cz.cvut.kbss.jopa.test.OWLClassSParent;
20: import cz.cvut.kbss.jopa.test.OWLClassT;
21: import cz.cvut.kbss.jopa.test.Vocabulary;
22: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
23: import cz.cvut.kbss.jopa.test.environment.Generators;
24: import cz.cvut.kbss.jopa.test.query.QueryTestEnvironment;
25: import cz.cvut.kbss.jopa.vocabulary.RDFS;
26: import org.junit.jupiter.api.AfterEach;
27: import org.junit.jupiter.api.Test;
28: import org.slf4j.Logger;
29:
30: import java.net.URI;
31: import java.util.Arrays;
32: import java.util.Collections;
33: import java.util.HashSet;
34: import java.util.List;
35:
36: import static org.junit.jupiter.api.Assertions.*;
37:
38:
39: public abstract class PolymorphicSelectQueryRunner extends BaseQueryRunner {
40:
41: protected PolymorphicSelectQueryRunner(Logger logger, DataAccessor dataAccessor) {
42: super(logger, dataAccessor);
43: }
44:
45: @AfterEach
46: public void tearDown() {
47: getEntityManager().clear();
48: getEntityManager().getEntityManagerFactory().getCache().evictAll();
49: }
50:
51: @Test
52: public void selectLoadsInstanceOfMostConcreteSubclassOfAbstractEntity() {
53: final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
54: final EntityManager em = getEntityManager();
55: final OWLClassSParent result =
56: em.createNativeQuery("SELECT ?x WHERE { ?x ?hasName ?name . }", OWLClassSParent.class)
57: .setParameter("hasName", URI.create(RDFS.LABEL))
58: .setParameter("name", t.getName(), "en").getSingleResult();
59: assertNotNull(result);
60: assertTrue(result instanceof OWLClassT);
61: verifyEntityTAttributes(t, (OWLClassT) result);
62: }
63:
64: private static void verifyEntityTAttributes(OWLClassT expected, OWLClassT actual) {
65: assertEquals(expected.getUri(), actual.getUri());
66: assertEquals(expected.getName(), actual.getName());
67: assertEquals(expected.getDescription(), actual.getDescription());
68: assertEquals(expected.getIntAttribute(), actual.getIntAttribute());
69: assertEquals(expected.getOwlClassA().getUri(), actual.getOwlClassA().getUri());
70: }
71:
72: @Test
73: public void selectLoadsInstanceOfMostConcreteSubclassOfConcreteEntity() {
74: final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
75: final EntityManager em = getEntityManager();
76: final OWLClassS result =
77: em.createNativeQuery("SELECT ?x WHERE { ?x ?hasName ?name . }", OWLClassS.class)
78: .setParameter("hasName", URI.create(RDFS.LABEL))
79: .setParameter("name", t.getName(), "en").getSingleResult();
80: assertNotNull(result);
81: assertTrue(result instanceof OWLClassT);
82: verifyEntityTAttributes(t, (OWLClassT) result);
83: }
84:
85: @Test
86: void selectByTypeLoadsAllIndividualsAsMostConcreteSubclassInstances() {
87: final List<OWLClassT> data = QueryTestEnvironment.getData(OWLClassT.class);
88: final EntityManager em = getEntityManager();
89: // This will cause the type resolver to have to do some work
90: em.getTransaction().begin();
91: data.forEach(t -> {
92: t.setTypes(new HashSet<>(Arrays.asList(Vocabulary.C_OWL_CLASS_S_PARENT, Vocabulary.C_OWL_CLASS_S)));
93: em.merge(t);
94: });
95: em.getTransaction().commit();
96: final List<OWLClassSParent> result =
97: em.createNativeQuery("SELECT ?x WHERE { ?x a ?type . }", OWLClassSParent.class)
98: .setParameter("type", URI.create(
99: Vocabulary.C_OWL_CLASS_S_PARENT)).getResultList();
100: assertEquals(data.size(), result.size());
101:
102: boolean found;
103:• for (OWLClassT t : data) {
104: found = false;
105:• for (OWLClassSParent tt : result) {
106:• if (t.getUri().equals(tt.getUri())) {
107: found = true;
108: assertTrue(tt instanceof OWLClassT);
109: verifyEntityTAttributes(t, (OWLClassT) tt);
110: break;
111: }
112: }
113: assertTrue(found);
114: }
115: }
116:
117: @Test
118: void selectLoadsInstanceAsGivenTypeWhenItIsConcreteAndFoundInTypesOfIndividual() {
119: final OWLClassT t = Generators.getRandomItem(QueryTestEnvironment.getData(OWLClassT.class));
120: final EntityManager em = getEntityManager();
121: em.getTransaction().begin();
122: t.setTypes(Collections.singleton(Vocabulary.C_OWL_CLASS_S));
123: em.merge(t);
124: em.getTransaction().commit();
125:
126: final OWLClassS result = em.createNativeQuery("SELECT ?x WHERE { ?x ?hasInt ?int. }", OWLClassS.class)
127: .setParameter("hasInt", URI.create(Vocabulary.P_T_INTEGER_ATTRIBUTE))
128: .setParameter("int", t.getIntAttribute()).getSingleResult();
129: assertNotNull(result);
130: assertFalse(result instanceof OWLClassT);
131: assertEquals(t.getName(), result.getName());
132: assertEquals(t.getDescription(), result.getDescription());
133: assertTrue(result.getTypes().contains(Vocabulary.C_OWL_CLASS_T));
134: }
135: }