Skip to contentMethod: persistYWithPluralMultilingualAttributeValues()
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.MultilingualString;
21: import cz.cvut.kbss.jopa.test.OWLClassM;
22: import cz.cvut.kbss.jopa.test.OWLClassY;
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.Generators;
26: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
27: import cz.cvut.kbss.jopa.test.environment.Quad;
28: import cz.cvut.kbss.jopa.vocabulary.RDF;
29: import cz.cvut.kbss.ontodriver.model.LangString;
30: import org.junit.jupiter.api.Test;
31: import org.slf4j.Logger;
32:
33: import java.net.URI;
34: import java.util.*;
35: import java.util.stream.Collectors;
36:
37: import static org.hamcrest.CoreMatchers.anyOf;
38: import static org.hamcrest.CoreMatchers.nullValue;
39: import static org.hamcrest.MatcherAssert.assertThat;
40: import static org.hamcrest.Matchers.empty;
41: import static org.junit.jupiter.api.Assertions.*;
42:
43: public abstract class MultilingualAttributesTestRunner extends BaseRunner {
44:
45: protected MultilingualAttributesTestRunner(Logger logger, PersistenceFactory persistenceFactory,
46: DataAccessor dataAccessor) {
47: super(logger, persistenceFactory, dataAccessor);
48: }
49:
50: @Test
51: void loadEntitySupportsSingularMultilingualAttribute() throws Exception {
52: this.em = getEntityManager("loadEntitySupportsSingularMultilingualAttribute", false);
53: final URI uri = Generators.generateUri();
54: final Map<String, String> translations = generateMultilingualData(uri);
55:
56: final OWLClassY result = findRequired(OWLClassY.class, uri);
57: assertNotNull(result.getSingularString());
58: translations.forEach((key, value) -> {
59: assertTrue(result.getSingularString().contains(key));
60: assertEquals(value, result.getSingularString().get(key));
61: });
62: }
63:
64: private Map<String, String> generateMultilingualData(URI uri) throws Exception {
65: final URI singularProperty = URI.create(Vocabulary.P_Y_SINGULAR_MULTILINGUAL_ATTRIBUTE);
66: final Map<String, String> translations = new HashMap<>();
67: translations.put("en", "building");
68: translations.put("cs", "stavba");
69: translations.put("de", "der Bau");
70: final Collection<Quad> quads = new ArrayList<>();
71: quads.add(new Quad(uri, URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_Y)));
72: translations.entrySet().stream().map(e -> new Quad(uri, singularProperty, e.getValue(), e.getKey())).forEach(
73: quads::add);
74: persistTestData(quads, em);
75: return translations;
76: }
77:
78: @Test
79: void persistSupportsMultilingualAttributes() throws Exception {
80: final Map<String, String> translations = new HashMap<>();
81: translations.put("en", "building");
82: translations.put("cs", "stavba");
83: translations.put("de", "der Bau");
84: this.em = getEntityManager("persistSupportsMultilingualAttributes", false);
85:
86: final OWLClassY toPersist = new OWLClassY();
87: toPersist.setSingularString(new MultilingualString(translations));
88: persist(toPersist);
89:
90: final OWLClassY result = findRequired(OWLClassY.class, toPersist.getUri());
91: assertNotNull(result.getSingularString());
92: assertEquals(translations, result.getSingularString().getValue());
93: verifyStatementsPresent(translations.entrySet().stream().map(e -> new Quad(toPersist.getUri(),
94: URI.create(Vocabulary.P_Y_SINGULAR_MULTILINGUAL_ATTRIBUTE), e.getValue(), e.getKey())).collect(
95: Collectors.toSet()), em);
96: }
97:
98: @Test
99: void removeDeletesAllValuesOfMultilingualAttribute() throws Exception {
100: this.em = getEntityManager("removeDeletesAllValuesOfMultilingualAttribute", false);
101: final URI uri = Generators.generateUri();
102: generateMultilingualData(uri);
103:
104: em.getTransaction().begin();
105: final OWLClassY toRemove = findRequired(OWLClassY.class, uri);
106: assertNotNull(toRemove.getSingularString());
107: assertFalse(toRemove.getSingularString().isEmpty());
108: em.remove(toRemove);
109: em.getTransaction().commit();
110:
111: assertFalse(em.createNativeQuery("ASK { ?x ?singularString ?y . }", Boolean.class)
112: .setParameter("x", toRemove.getUri())
113: .setParameter("singularString", URI.create(Vocabulary.P_Y_SINGULAR_MULTILINGUAL_ATTRIBUTE))
114: .getSingleResult());
115: }
116:
117: @Test
118: void settingMultilingualStringAttributeToNullRemovesAllValues() {
119: this.em = getEntityManager("settingMultilingualStringAttributeToNullRemovesAllValues", false);
120: final OWLClassY entityY = new OWLClassY();
121: entityY.setSingularString(new MultilingualString());
122: entityY.getSingularString().set("en", "building");
123: entityY.getSingularString().set("cs", "stavba");
124: entityY.getSingularString().set("de", "der Bau");
125: persist(entityY);
126:
127: em.getTransaction().begin();
128: final OWLClassY toRemove = findRequired(OWLClassY.class, entityY.getUri());
129: assertNotNull(toRemove.getSingularString());
130: toRemove.setSingularString(null);
131: em.getTransaction().commit();
132:
133: final OWLClassY result = findRequired(OWLClassY.class, entityY.getUri());
134: assertNull(result.getSingularString());
135: }
136:
137: @Test
138: void updateSupportsAddingTranslationsToMultilingualAttribute() {
139: this.em = getEntityManager("updateSupportsAddingTranslationsToMultilingualAttribute", true);
140: final OWLClassY y = new OWLClassY();
141: y.setSingularString(new MultilingualString());
142: y.getSingularString().set("en", "building");
143: persist(y);
144:
145: y.getSingularString().set("cs", "stavba");
146: y.getSingularString().set("de", "der Bau");
147: transactional(() -> em.merge(y));
148:
149: final OWLClassY result = findRequired(OWLClassY.class, y.getUri());
150: assertEquals(3, result.getSingularString().getLanguages().size());
151: assertEquals(y.getSingularString(), result.getSingularString());
152: }
153:
154: @Test
155: void updateSupportsReplacingTranslationsInMultilingualAttribute() {
156: this.em = getEntityManager("updateSupportsReplacingTranslationsInMultilingualAttribute", true);
157: final OWLClassY y = new OWLClassY();
158: y.setSingularString(new MultilingualString());
159: y.getSingularString().set("en", "construction");
160: persist(y);
161:
162: final String replacement = "building";
163: transactional(() -> {
164: final OWLClassY toUpdate = findRequired(OWLClassY.class, y.getUri());
165: toUpdate.getSingularString().set("en", replacement);
166: toUpdate.getSingularString().set("cs", "stavba");
167: });
168:
169: final OWLClassY result = findRequired(OWLClassY.class, y.getUri());
170: assertEquals(2, result.getSingularString().getLanguages().size());
171: assertEquals(replacement, result.getSingularString().get("en"));
172: assertEquals("stavba", result.getSingularString().get("cs"));
173: }
174:
175: @Test
176: void updateSupportsCompletelyReplacingMultilingualAttributeValueInTransaction() {
177: this.em = getEntityManager("updateSupportsCompletelyReplacingMultilingualAttributeValueInTransaction", true);
178: final OWLClassY y = new OWLClassY();
179: y.setSingularString(new MultilingualString());
180: y.getSingularString().set("en", "construction");
181: persist(y);
182:
183: final String replacement = "building";
184: transactional(() -> {
185: final OWLClassY toUpdate = findRequired(OWLClassY.class, y.getUri());
186: toUpdate.setSingularString(new MultilingualString());
187: toUpdate.getSingularString().set("en", replacement);
188: toUpdate.getSingularString().set("cs", "stavba");
189: });
190:
191: final OWLClassY result = findRequired(OWLClassY.class, y.getUri());
192: assertEquals(2, result.getSingularString().getLanguages().size());
193: assertEquals(replacement, result.getSingularString().get("en"));
194: assertEquals("stavba", result.getSingularString().get("cs"));
195: }
196:
197: @Test
198: void persistSupportsPluralMultilingualAttributes() {
199: this.em = getEntityManager("persistSupportsPluralMultilingualAttributes", false);
200: final OWLClassY y = persistYWithPluralMultilingualAttributeValues();
201:
202: final URI property = URI.create(Vocabulary.P_Y_PLURAL_MULTILINGUAL_ATTRIBUTE);
203: transactionalThrowing(
204: () -> verifyStatementsPresent(Arrays.asList(new Quad(y.getUri(), property, "construction", "en"),
205: new Quad(y.getUri(), property, "stavba", "cs"),
206: new Quad(y.getUri(), property, "building", "en"),
207: new Quad(y.getUri(), property, "budova", "cs")), em));
208: }
209:
210: @Test
211: void loadEntitySupportsPluralMultilingualAttributes() throws Exception {
212: this.em = getEntityManager("loadEntitySupportsPluralMultilingualAttributes", true);
213: final URI individual = Generators.generateUri();
214: final URI singularProperty = URI.create(Vocabulary.P_Y_PLURAL_MULTILINGUAL_ATTRIBUTE);
215: final Map<String, Set<String>> translations = new HashMap<>();
216: translations.put("en", new HashSet<>(Arrays.asList("building", "construction")));
217: translations.put("cs", new HashSet<>(Arrays.asList("stavba", "budova")));
218: translations.put("de", Collections.singleton("der Bau"));
219: final Collection<Quad> quads = new ArrayList<>();
220: quads.add(new Quad(individual, URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_Y)));
221: translations.entrySet().stream()
222: .flatMap(e -> e.getValue().stream().map(s -> new Quad(individual, singularProperty, s, e.getKey())))
223: .forEach(quads::add);
224: persistTestData(quads, em);
225:
226: final OWLClassY result = findRequired(OWLClassY.class, individual);
227: assertNotNull(result.getPluralString());
228: for (Map.Entry<String, Set<String>> e : translations.entrySet()) {
229: for (String s : e.getValue()) {
230: verifyPluralContainsValue(result, e.getKey(), s);
231: }
232: }
233: }
234:
235: private static void verifyPluralContainsValue(OWLClassY instance, String lang, String value) {
236: assertTrue(instance.getPluralString().stream()
237: .anyMatch(ms -> ms.contains(lang) && ms.get(lang).equals(value)));
238: }
239:
240: @Test
241: void updateSupportsReplacingValuesInPluralMultilingualAttributes() {
242: this.em = getEntityManager("updateSupportsReplacingValuesInPluralMultilingualAttributes", true);
243: final OWLClassY y = persistYWithPluralMultilingualAttributeValues();
244:
245: transactional(() -> {
246: final OWLClassY toUpdate = findRequired(OWLClassY.class, y.getUri());
247: toUpdate.getPluralString().iterator().next().set("de", "der Bau");
248: });
249: em.getEntityManagerFactory().getCache().evictAll();
250:
251: final OWLClassY result = findRequired(OWLClassY.class, y.getUri());
252: verifyPluralContainsValue(result, "de", "der Bau");
253: verifyPluralContainsValue(result, "en", "construction");
254: verifyPluralContainsValue(result, "cs", "stavba");
255: verifyPluralContainsValue(result, "en", "building");
256: verifyPluralContainsValue(result, "cs", "budova");
257: }
258:
259: private OWLClassY persistYWithPluralMultilingualAttributeValues() {
260: final OWLClassY y = new OWLClassY();
261: final MultilingualString sOne = new MultilingualString();
262: sOne.set("en", "construction");
263: sOne.set("cs", "stavba");
264: final MultilingualString sTwo = new MultilingualString();
265: sTwo.set("en", "building");
266: sTwo.set("cs", "budova");
267: y.setPluralString(new HashSet<>(Arrays.asList(sOne, sTwo)));
268: persist(y);
269: return y;
270: }
271:
272: @Test
273: void updateSupportsRemovingValuesFromPluralMultilingualAttributes() {
274: this.em = getEntityManager("updateSupportsRemovingValuesFromPluralMultilingualAttributes", true);
275: final OWLClassY y = persistYWithPluralMultilingualAttributeValues();
276:
277: transactional(() -> {
278: final Iterator<MultilingualString> it = y.getPluralString().iterator();
279: it.next();
280: it.remove();
281: em.merge(y);
282: });
283:
284: final OWLClassY result = findRequired(OWLClassY.class, y.getUri());
285: assertEquals(1, result.getPluralString().size());
286: }
287:
288: @Test
289: void clearingPluralMultilingualAttributeValueRemovesAllItsValues() {
290: this.em = getEntityManager("settingPluralMultilingualAttributeValueToNullRemovesAllItsValues", true);
291: final OWLClassY y = persistYWithPluralMultilingualAttributeValues();
292:
293: transactional(() -> {
294: final OWLClassY toUpdate = findRequired(OWLClassY.class, y.getUri());
295: toUpdate.getPluralString().clear();
296: });
297:
298: final OWLClassY result = findRequired(OWLClassY.class, y.getUri());
299: assertThat(result.getPluralString(), anyOf(nullValue(), empty()));
300: }
301:
302: @Test
303: void persistSupportsOntoDriverLangStringAttribute() throws Exception {
304: this.em = getEntityManager("persistSupportsOntoDriverLangStringAttribute", true);
305: entityM.setLangString(new LangString("testovaci hodnota", "cs"));
306: persist(entityM);
307:
308: verifyStatementsPresent(Collections.singleton(new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_langString), entityM.getLangString()
309: .getValue(), entityM.getLangString().getLanguage()
310: .get())), em);
311: }
312:
313: @Test
314: void entityLoadingSupportsOntoDriverLangStringAttribute() throws Exception {
315: this.em = getEntityManager("entityLoadingSupportsOntoDriverLangStringAttribute", true);
316: final String langStringValue = "test value";
317: final String language = "en";
318: persistTestData(Arrays.asList(
319: new Quad(URI.create(entityM.getKey()), URI.create(RDF.TYPE), URI.create(Vocabulary.C_OWL_CLASS_M)),
320: new Quad(URI.create(entityM.getKey()), URI.create(Vocabulary.p_m_langString), langStringValue, language)
321: ), em);
322:
323: final OWLClassM result = findRequired(OWLClassM.class, entityM.getKey());
324: assertEquals(new LangString(langStringValue, language), result.getLangString());
325: }
326: }