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