Skip to content

Package: RetrieveOperationsMultiContextRunner

RetrieveOperationsMultiContextRunner

nameinstructionbranchcomplexitylinemethod
RetrieveOperationsMultiContextRunner(Logger, PersistenceFactory, DataAccessor)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
lambda$retrieveFromContextWithAttributeInDefaultWorksCorrectly$3()
M: 6 C: 37
86%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 6
75%
M: 0 C: 1
100%
lambda$retrieveSupportsRetrievalOfReferenceWherePropertyAssertionIsInSubjectContext$2()
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$retrieveSupportsSpecifyingMultipleContextsForEntity$7(URI)
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
lambda$retrieveSupportsSpecifyingMultipleContextsForObjectPropertyValues$4(OWLClassA, URI)
M: 0 C: 97
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
lambda$retrieveSupportsSpecifyingMultipleContextsForObjectPropertyValues$5(OWLClassA)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$retrieveSupportsSpecifyingMultipleContextsForObjectPropertyValues$6(OWLClassA, OWLClassA)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$testRetrieveSimilarFromTwoContexts$0()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$testRetrieveSimilarFromTwoContexts$1(OWLClassA)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
retrieveFromContextWithAttributeInDefaultWorksCorrectly()
M: 0 C: 42
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
retrieveSupportsRetrievalOfReferenceWherePropertyAssertionIsInSubjectContext()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
retrieveSupportsSpecifyingMultipleContextsForEntity()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
retrieveSupportsSpecifyingMultipleContextsForObjectPropertyValues()
M: 0 C: 87
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
testRetrieveLazyReferenceFromContext()
M: 0 C: 87
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
testRetrievePropertiesFromContext()
M: 0 C: 73
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testRetrieveReferencedListFromContext()
M: 0 C: 120
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 22
100%
M: 0 C: 1
100%
testRetrieveSimilarFromTwoContexts()
M: 0 C: 58
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testRetrieveSimpleListFromContext()
M: 0 C: 120
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 22
100%
M: 0 C: 1
100%

Coverage

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.Descriptor;
21: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
22: import cz.cvut.kbss.jopa.model.descriptors.ObjectPropertyCollectionDescriptor;
23: import cz.cvut.kbss.jopa.model.metamodel.ListAttribute;
24: import cz.cvut.kbss.jopa.proxy.lazy.LazyLoadingProxy;
25: import cz.cvut.kbss.jopa.test.OWLClassA;
26: import cz.cvut.kbss.jopa.test.OWLClassB;
27: import cz.cvut.kbss.jopa.test.OWLClassC;
28: import cz.cvut.kbss.jopa.test.OWLClassD;
29: import cz.cvut.kbss.jopa.test.OWLClassF;
30: import cz.cvut.kbss.jopa.test.OWLClassI;
31: import cz.cvut.kbss.jopa.test.OWLClassM;
32: import cz.cvut.kbss.jopa.test.Vocabulary;
33: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
34: import cz.cvut.kbss.jopa.test.environment.Generators;
35: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
36: import cz.cvut.kbss.jopa.test.environment.Quad;
37: import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
38: import cz.cvut.kbss.jopa.vocabulary.RDF;
39: import org.junit.jupiter.api.Test;
40: import org.slf4j.Logger;
41:
42: import java.net.URI;
43: import java.util.Arrays;
44: import java.util.Optional;
45:
46: import static org.junit.jupiter.api.Assertions.assertEquals;
47: import static org.junit.jupiter.api.Assertions.assertInstanceOf;
48: import static org.junit.jupiter.api.Assertions.assertNotNull;
49: import static org.junit.jupiter.api.Assertions.assertTrue;
50:
51:
52: public abstract class RetrieveOperationsMultiContextRunner extends BaseRunner {
53:
54: private final EntityDescriptor cOneDescriptor = new EntityDescriptor(CONTEXT_ONE);
55: private final EntityDescriptor cTwoDescriptor = new EntityDescriptor(CONTEXT_TWO);
56:
57: public RetrieveOperationsMultiContextRunner(Logger logger, PersistenceFactory persistenceFactory,
58: DataAccessor dataAccessor) {
59: super(logger, persistenceFactory, dataAccessor);
60: }
61:
62: @Test
63: void testRetrieveSimilarFromTwoContexts() {
64: this.em = getEntityManager("MultiRetrieveSimilarFromTwoContexts", false);
65: final OWLClassA entityATwo = new OWLClassA();
66: entityATwo.setUri(entityA.getUri());
67: entityATwo.setStringAttribute("SomeCompletelyDifferentStringAttribute");
68: transactional(() -> em.persist(entityA, cOneDescriptor));
69: transactional(() -> em.persist(entityATwo, cTwoDescriptor));
70:
71: final OWLClassA resOne = findRequired(OWLClassA.class, entityA.getUri(), cOneDescriptor);
72: assertEquals(entityA.getStringAttribute(), resOne.getStringAttribute());
73: final OWLClassA resTwo = findRequired(OWLClassA.class, entityATwo.getUri(), cTwoDescriptor);
74: assertEquals(entityATwo.getStringAttribute(), resTwo.getStringAttribute());
75: }
76:
77: @Test
78: void testRetrieveSimpleListFromContext() {
79: this.em = getEntityManager("MultiRetrieveSimpleListFromContext", false);
80: entityC.setSimpleList(Generators.createSimpleList(10));
81: final Descriptor cDescriptor = new EntityDescriptor();
82: final ListAttribute<OWLClassC, ?> simpleListAtt =
83: em.getMetamodel().entity(OWLClassC.class).getDeclaredList("simpleList");
84: final ObjectPropertyCollectionDescriptor listDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
85: simpleListAtt, false);
86: cDescriptor.addAttributeDescriptor(simpleListAtt, listDescriptor);
87: em.getTransaction().begin();
88: em.persist(entityC, cDescriptor);
89:• for (OWLClassA a : entityC.getSimpleList()) {
90: em.persist(a, listDescriptor.getElementDescriptor());
91: }
92: em.getTransaction().commit();
93:
94: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
95: assertNotNull(resC.getSimpleList());
96: assertEquals(entityC.getSimpleList().size(), resC.getSimpleList().size());
97:• for (OWLClassA a : entityC.getSimpleList()) {
98: final OWLClassA resA = findRequired(OWLClassA.class, a.getUri(), listDescriptor);
99: assertEquals(a.getUri(), resA.getUri());
100: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
101: }
102: }
103:
104: @Test
105: void testRetrieveReferencedListFromContext() {
106: this.em = getEntityManager("MultiRetrieveReferencedListFromContext", false);
107: entityC.setReferencedList(Generators.createReferencedList(15));
108: final Descriptor cDescriptor = new EntityDescriptor();
109: final ListAttribute<OWLClassC, ?> referencedListAtt =
110: em.getMetamodel().entity(OWLClassC.class).getDeclaredList("referencedList");
111: final ObjectPropertyCollectionDescriptor listDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_ONE,
112: referencedListAtt, false);
113: cDescriptor.addAttributeDescriptor(referencedListAtt, listDescriptor);
114: em.getTransaction().begin();
115: em.persist(entityC, cDescriptor);
116:• for (OWLClassA a : entityC.getReferencedList()) {
117: em.persist(a, listDescriptor.getElementDescriptor());
118: }
119: em.getTransaction().commit();
120:
121: final OWLClassC resC = findRequired(OWLClassC.class, entityC.getUri(), cDescriptor);
122: assertNotNull(resC.getReferencedList());
123: assertEquals(entityC.getReferencedList().size(), resC.getReferencedList().size());
124:• for (OWLClassA a : entityC.getReferencedList()) {
125: final OWLClassA resA = findRequired(OWLClassA.class, a.getUri(), listDescriptor);
126: assertEquals(a.getUri(), resA.getUri());
127: assertEquals(a.getStringAttribute(), resA.getStringAttribute());
128: }
129: }
130:
131: @Test
132: void testRetrieveLazyReferenceFromContext() {
133: this.em = getEntityManager("MultiRetrieveLazyReferenceFromContext", false);
134: final Descriptor iDescriptor = new EntityDescriptor(CONTEXT_ONE, false);
135: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
136: aDescriptor
137: .addAttributeContext(em.getMetamodel().entity(OWLClassA.class).getDeclaredAttribute("stringAttribute"),
138: CONTEXT_ONE);
139: iDescriptor.addAttributeDescriptor(em.getMetamodel().entity(OWLClassI.class).getDeclaredAttribute("owlClassA"),
140: aDescriptor);
141: em.getTransaction().begin();
142: // The relationship is CascadeType.PERSIST
143: em.persist(entityI, iDescriptor);
144: em.getTransaction().commit();
145:
146: final OWLClassI resI = findRequired(OWLClassI.class, entityI.getUri(), iDescriptor);
147: assertInstanceOf(LazyLoadingProxy.class, resI.getOwlClassA());
148: // Trigger lazy loading
149: assertNotNull(resI.getOwlClassA().getUri());
150: final OWLClassA resA = findRequired(OWLClassA.class, entityA.getUri(), aDescriptor);
151: // If we were using cache, ref.getOwlClassA() and resA would be the same
152: assertEquals(resI.getOwlClassA().getStringAttribute(), resA.getStringAttribute());
153: }
154:
155: @Test
156: void testRetrievePropertiesFromContext() {
157: this.em = getEntityManager("MultiRetrievePropertiesFromContext", false);
158: entityB.setProperties(Generators.createProperties(50));
159: cOneDescriptor.addAttributeContext(em.getMetamodel().entity(OWLClassB.class).getProperties(), CONTEXT_TWO);
160: cOneDescriptor
161: .addAttributeContext(em.getMetamodel().entity(OWLClassB.class).getDeclaredAttribute("stringAttribute"),
162: null);
163: em.getTransaction().begin();
164: em.persist(entityB, cOneDescriptor);
165: em.getTransaction().commit();
166:
167: final OWLClassB res = findRequired(OWLClassB.class, entityB.getUri(), cOneDescriptor);
168: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
169: assertTrue(TestEnvironmentUtils.arePropertiesEqual(entityB.getProperties(), res.getProperties()));
170: }
171:
172: @Test
173: void retrieveSupportsRetrievalOfReferenceWherePropertyAssertionIsInSubjectContext() {
174: this.em = getEntityManager("retrieveSupportsRetrievalOfReferenceWherePropertyAssertionIsInSubjectContext",
175: false);
176: cOneDescriptor
177: .addAttributeDescriptor(em.getMetamodel().entity(OWLClassD.class).getDeclaredAttribute("owlClassA"),
178: cTwoDescriptor);
179: transactional(() -> {
180: em.persist(entityA, cTwoDescriptor);
181: em.persist(entityD, cOneDescriptor);
182: });
183:
184: final OWLClassD res = findRequired(OWLClassD.class, entityD.getUri(), cOneDescriptor);
185: assertNotNull(res.getOwlClassA());
186: assertEquals(entityA.getStringAttribute(), res.getOwlClassA().getStringAttribute());
187: }
188:
189: /**
190: * Bug #58
191: */
192: @Test
193: void retrieveFromContextWithAttributeInDefaultWorksCorrectly() {
194: this.em = getEntityManager("retrieveFromContextWithAttributeInDefaultWorksCorrectly", false);
195: cOneDescriptor
196: .addAttributeContext(em.getMetamodel().entity(OWLClassA.class).getDeclaredAttribute("stringAttribute"),
197: null);
198: transactional(() -> {
199: try {
200: persistTestData(Arrays.asList(
201: new Quad(entityA.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_A),
202: CONTEXT_ONE),
203: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE),
204: entityA.getStringAttribute())), em);
205: } catch (Exception e) {
206: throw new RuntimeException(e);
207: }
208: });
209:
210: final OWLClassA result = findRequired(OWLClassA.class, entityA.getUri(), cOneDescriptor);
211: assertNotNull(result.getStringAttribute());
212: assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
213: }
214:
215: @Test
216: void retrieveSupportsSpecifyingMultipleContextsForObjectPropertyValues() {
217: this.em = getEntityManager("retrieveSupportsSpecifyingMultipleContextsForObjectPropertyValues", false);
218: final EntityDescriptor descriptor = new EntityDescriptor();
219: descriptor.addAttributeContext(fieldSpecification(OWLClassF.class, "simpleSet"), CONTEXT_ONE)
220: .addAttributeContext(fieldSpecification(OWLClassF.class, "simpleSet"), CONTEXT_TWO);
221: final OWLClassA a2 = new OWLClassA(Generators.generateUri(), "string two");
222: final URI ownerUri = Generators.generateUri();
223: transactionalThrowing(() -> persistTestData(Arrays.asList(
224: new Quad(entityA.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_A),
225: CONTEXT_ONE),
226: new Quad(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE),
227: entityA.getStringAttribute(), CONTEXT_ONE),
228: new Quad(a2.getUri(), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_A), CONTEXT_TWO),
229: new Quad(a2.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), a2.getStringAttribute(),
230: CONTEXT_TWO),
231: new Quad(ownerUri, URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_F)),
232: new Quad(ownerUri, URI.create(Vocabulary.P_F_HAS_SIMPLE_SET), entityA.getUri()),
233: new Quad(ownerUri, URI.create(Vocabulary.P_F_HAS_SIMPLE_SET), a2.getUri())), em));
234:
235: final OWLClassF result = findRequired(OWLClassF.class, ownerUri);
236: assertEquals(2, result.getSimpleSet().size());
237: final Optional<OWLClassA> aOneResult =
238: result.getSimpleSet().stream().filter(a -> a.getUri().equals(entityA.getUri())).findAny();
239: assertTrue(aOneResult.isPresent());
240: assertEquals(entityA.getStringAttribute(), aOneResult.get().getStringAttribute());
241: final Optional<OWLClassA> aTwoResult =
242: result.getSimpleSet().stream().filter(a -> a.getUri().equals(a2.getUri())).findAny();
243: assertTrue(aTwoResult.isPresent());
244: assertEquals(a2.getStringAttribute(), aTwoResult.get().getStringAttribute());
245: }
246:
247: @Test
248: void retrieveSupportsSpecifyingMultipleContextsForEntity() {
249: this.em = getEntityManager("retrieveSupportsSpecifyingMultipleContextsForEntity", false);
250: final Descriptor descriptor = new EntityDescriptor(CONTEXT_ONE);
251: descriptor.addContext(CONTEXT_TWO);
252: final URI uri = URI.create(entityM.getKey());
253: transactionalThrowing(() -> persistTestData(Arrays.asList(
254: new Quad(uri, URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M), CONTEXT_ONE),
255: new Quad(uri, URI.create(Vocabulary.p_m_booleanAttribute), entityM.getBooleanAttribute(), CONTEXT_ONE),
256: new Quad(uri, URI.create(Vocabulary.p_m_dateAttribute), entityM.getDateAttribute(), CONTEXT_TWO)
257: ), em));
258:
259: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
260: assertEquals(entityM.getBooleanAttribute(), result.getBooleanAttribute());
261: assertEquals(entityM.getDateAttribute(), result.getDateAttribute());
262: }
263: }