Skip to content

Method: findReturnsOnlyAssertedDataWhenDescriptorDisablesInference()

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.descriptors.EntityDescriptor;
21: import cz.cvut.kbss.jopa.query.QueryHints;
22: import cz.cvut.kbss.jopa.test.OWLClassW;
23: import cz.cvut.kbss.jopa.test.Vocabulary;
24: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
25: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
26: import cz.cvut.kbss.jopa.test.environment.Quad;
27: import cz.cvut.kbss.jopa.vocabulary.RDFS;
28: import org.junit.jupiter.api.Test;
29: import org.slf4j.Logger;
30:
31: import java.net.URI;
32: import java.util.Collections;
33:
34: import static org.hamcrest.MatcherAssert.assertThat;
35: import static org.hamcrest.Matchers.hasItem;
36: import static org.hamcrest.Matchers.not;
37: import static org.junit.jupiter.api.Assertions.assertFalse;
38: import static org.junit.jupiter.api.Assertions.assertTrue;
39:
40: public abstract class RetrieveWithInferenceRunner extends BaseRunner {
41:
42: protected OWLClassW entityW = new OWLClassW();
43:
44: public RetrieveWithInferenceRunner(Logger logger, PersistenceFactory persistenceFactory,
45: DataAccessor dataAccessor) {
46: super(logger, persistenceFactory, dataAccessor);
47: }
48:
49: @Test
50: public void retrievedEntityWithInferredTypesContainsInferredData() throws Exception {
51: persistTestData(Collections.singleton(
52: new Quad(URI.create(Vocabulary.C_OWL_CLASS_W), URI.create(RDFS.SUB_CLASS_OF),
53: URI.create(Vocabulary.C_OWL_CLASS_A))), em);
54: persist(entityW);
55:
56: final OWLClassW result = findRequired(OWLClassW.class, entityW.getUri());
57: assertThat(result.getTypes(), hasItem(URI.create(Vocabulary.C_OWL_CLASS_A)));
58: }
59:
60: @Test
61: public void isInferredReturnsTrueForInferredPropertyValue() throws Exception {
62: persistTestData(Collections.singleton(
63: new Quad(URI.create(Vocabulary.C_OWL_CLASS_W), URI.create(RDFS.SUB_CLASS_OF),
64: URI.create(Vocabulary.C_OWL_CLASS_A))), em);
65: persist(entityW);
66:
67: final OWLClassW w = findRequired(OWLClassW.class, entityW.getUri());
68: assertTrue(em.isInferred(w, em.getMetamodel().entity(OWLClassW.class).getTypes(),
69: URI.create(Vocabulary.C_OWL_CLASS_A)));
70: assertFalse(em.isInferred(w, em.getMetamodel().entity(OWLClassW.class).getTypes(),
71: URI.create(Vocabulary.C_OWL_CLASS_W)));
72: }
73:
74: @Test
75: public void findReturnsOnlyAssertedDataWhenDescriptorDisablesInference() throws Exception {
76: persistTestData(Collections.singleton(
77: new Quad(URI.create(Vocabulary.C_OWL_CLASS_W), URI.create(RDFS.SUB_CLASS_OF),
78: URI.create(Vocabulary.C_OWL_CLASS_A))), em);
79: persist(entityW);
80:
81: final OWLClassW result =
82: findRequired(OWLClassW.class, entityW.getUri(), new EntityDescriptor().disableInference());
83: assertThat(result.getTypes(), not(hasItem(URI.create(Vocabulary.C_OWL_CLASS_A))));
84: }
85:
86: @Test
87: public void selectQueryWithDisabledInferenceAppliesThisSettingToLoadedResultsAsWell() throws Exception {
88: persistTestData(Collections.singleton(
89: new Quad(URI.create(Vocabulary.C_OWL_CLASS_W), URI.create(RDFS.SUB_CLASS_OF),
90: URI.create(Vocabulary.C_OWL_CLASS_A))), em);
91: persist(entityW);
92:
93: final OWLClassW result = em.createQuery("SELECT w FROM OWLClassW w WHERE w.uri = :uri", OWLClassW.class)
94: .setParameter("uri", entityW.getUri())
95: .setHint(QueryHints.DISABLE_INFERENCE, Boolean.TRUE.toString())
96: .getSingleResult();
97: assertThat(result.getTypes(), not(hasItem(URI.create(Vocabulary.C_OWL_CLASS_A))));
98: }
99: }