Skip to content

Method: lambda$3()

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