Skip to content

Method: getReferenceResultListAttributeCanBeUpdated()

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.test.*;
18: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
19: import cz.cvut.kbss.jopa.test.environment.Generators;
20: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
21: import org.junit.jupiter.api.Test;
22: import org.slf4j.Logger;
23:
24: import java.util.Collections;
25: import java.util.Set;
26: import java.util.stream.Collectors;
27: import java.util.stream.IntStream;
28:
29: import static org.junit.jupiter.api.Assertions.*;
30:
31: public abstract class UpdateOperationsOnGetReferenceRunner extends BaseRunner {
32:
33: protected UpdateOperationsOnGetReferenceRunner(Logger logger, PersistenceFactory persistenceFactory,
34: DataAccessor dataAccessor) {
35: super(logger, persistenceFactory, dataAccessor);
36: }
37:
38: @Test
39: void getReferenceResultCanBeUsedAsAttributeValueInUpdate() {
40: this.em = getEntityManager("getReferenceResultCanBeUsedAsAttributeValueInUpdate", true);
41: entityD.setOwlClassA(null);
42: persist(entityD, entityA);
43:
44: em.getTransaction().begin();
45: final OWLClassD d = findRequired(OWLClassD.class, entityD.getUri());
46: d.setOwlClassA(em.getReference(OWLClassA.class, entityA.getUri()));
47: em.getTransaction().commit();
48:
49: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
50: assertNotNull(result.getOwlClassA());
51: assertEquals(entityA.getUri(), result.getOwlClassA().getUri());
52: }
53:
54: @Test
55: void getReferenceResultDataAttributesCanBeAssignedNewValuesInUpdate() {
56: this.em = getEntityManager("getReferenceResultDataAttributesCanBeAssignedNewValuesInUpdate", true);
57: persist(entityM);
58:
59: em.getTransaction().begin();
60: final OWLClassM m = em.getReference(OWLClassM.class, entityM.getKey());
61: final int iVal = Generators.randomInt();
62: m.setIntAttribute(iVal);
63: assertEquals(iVal, m.getIntAttribute().intValue());
64: final long lVal = System.currentTimeMillis();
65: m.setLongAttribute(lVal);
66: final Set<Integer> intSet = IntStream.generate(Generators::randomInt).limit(5).boxed()
67: .collect(Collectors.toSet());
68: m.setIntegerSet(intSet);
69: em.getTransaction().commit();
70:
71: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
72: assertEquals(iVal, result.getIntAttribute().intValue());
73: assertEquals(lVal, result.getLongAttribute().longValue());
74: assertEquals(intSet, result.getIntegerSet());
75: // Ensure other values were not changed
76: assertEquals(entityM.getBooleanAttribute(), result.getBooleanAttribute());
77: assertEquals(entityM.getEnumAttribute(), result.getEnumAttribute());
78: assertEquals(entityM.getDateAttribute(), result.getDateAttribute());
79: assertEquals(entityM.getDoubleAttribute(), result.getDoubleAttribute());
80: }
81:
82: @Test
83: void getReferenceResultObjectAttributesCanBeAssignedNewValuesInUpdate() {
84: this.em = getEntityManager("getReferenceResultObjectAttributesCanBeAssignedNewValuesInUpdate", true);
85: entityD.setOwlClassA(null);
86: persist(entityD, entityA);
87:
88: em.getTransaction().begin();
89: final OWLClassD d = em.getReference(OWLClassD.class, entityD.getUri());
90: final OWLClassA a = em.getReference(OWLClassA.class, entityA.getUri());
91: d.setOwlClassA(a);
92: em.getTransaction().commit();
93:
94: final OWLClassD result = em.find(OWLClassD.class, entityD.getUri());
95: assertEquals(entityA.getUri(), result.getOwlClassA().getUri());
96: }
97:
98: @Test
99: void getReferenceResultPluralObjectAttributeCanBeUpdated() {
100: this.em = getEntityManager("getReferenceResultPluralObjectAttributeCanBeUpdated", true);
101: final OWLClassJ entityJ = new OWLClassJ(Generators.generateUri());
102: entityJ.setOwlClassA(Collections.singleton(entityA));
103: persist(entityJ);
104:
105: em.getTransaction().begin();
106: final OWLClassJ j = em.getReference(OWLClassJ.class, entityJ.getUri());
107: final OWLClassA newA = new OWLClassA(Generators.generateUri());
108: newA.setStringAttribute("newA");
109: j.getOwlClassA().add(newA);
110: em.persist(newA);
111: em.getTransaction().commit();
112:
113: final OWLClassJ result = findRequired(OWLClassJ.class, entityJ.getUri());
114: assertEquals(j.getOwlClassA().size(), result.getOwlClassA().size());
115: assertTrue(j.getOwlClassA().stream().anyMatch(a -> a.getUri().equals(entityA.getUri())));
116: assertTrue(j.getOwlClassA().stream().anyMatch(a -> a.getUri().equals(newA.getUri())));
117: }
118:
119: @Test
120: void getReferenceResultListAttributeCanBeUpdated() {
121: this.em = getEntityManager("getReferenceResultListAttributeCanBeUpdated", true);
122: final OWLClassK entityK = new OWLClassK();
123: entityK.setReferencedList(Collections.singletonList(entityE));
124: entityK.setSimpleList(Collections.singletonList(entityE));
125: persist(entityK, entityE);
126:
127: em.getTransaction().begin();
128: final OWLClassK upd = em.getReference(OWLClassK.class, entityK.getUri());
129: final OWLClassE newE = new OWLClassE();
130: upd.getReferencedList().add(newE);
131: upd.getSimpleList().add(newE);
132: em.persist(newE);
133: em.getTransaction().commit();
134:
135: final OWLClassK result = findRequired(OWLClassK.class, entityK.getUri());
136: assertEquals(upd.getReferencedList().size(), result.getReferencedList().size());
137: assertEquals(upd.getSimpleList().size(), result.getSimpleList().size());
138: assertTrue(result.getReferencedList().stream().anyMatch(a -> a.getUri().equals(newE.getUri())));
139: assertTrue(result.getSimpleList().stream().anyMatch(a -> a.getUri().equals(newE.getUri())));
140: }
141:
142: @Test
143: void assigningGetReferenceResultToInstanceRemovesItFromCacheToPreventSubsequentIncompleteDataRetrieval() {
144: this.em = getEntityManager(
145: "assigningGetReferenceResultToInstanceRemovesItFromCacheToPreventSubsequentIncompleteDataRetrieval",
146: true);
147: entityD.setOwlClassA(null);
148: persist(entityD, entityA);
149:
150: em.getEntityManagerFactory().getCache().evictAll();
151: em.getTransaction().begin();
152: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri());
153: final OWLClassA a = em.getReference(OWLClassA.class, entityA.getUri());
154: d.setOwlClassA(a);
155: em.getTransaction().commit();
156:
157: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
158: em.detach(result);
159: assertNotNull(result.getOwlClassA());
160: assertNotNull(result.getOwlClassA().getStringAttribute());
161: }
162:
163: @Test
164: void mergeWithFieldValueBeingResultOfGetReferenceRemovesItFromCacheToPreventSubsequentIncompleteDataRetrieval() {
165: this.em = getEntityManager(
166: "mergeWithFieldValueBeingResultOfGetReferenceRemovesItFromCacheToPreventSubsequentIncompleteDataRetrieval",
167: true);
168: entityD.setOwlClassA(null);
169: persist(entityD, entityA);
170:
171: em.getEntityManagerFactory().getCache().evictAll();
172: em.getTransaction().begin();
173: final OWLClassA a = em.getReference(OWLClassA.class, entityA.getUri());
174: entityD.setOwlClassA(a);
175: em.merge(entityD);
176: em.getTransaction().commit();
177:
178: final OWLClassD result = findRequired(OWLClassD.class, entityD.getUri());
179: em.detach(result);
180: assertNotNull(result.getOwlClassA());
181: assertNotNull(result.getOwlClassA().getStringAttribute());
182: }
183: }