Skip to contentMethod: lambda$maxParticipationConstraintsInInterfaceThrowExceptionIfNotMet$1()
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.exceptions.RollbackException;
21: import cz.cvut.kbss.jopa.test.*;
22: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
23: import cz.cvut.kbss.jopa.test.environment.Generators;
24: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
25: import cz.cvut.kbss.jopa.test.environment.Quad;
26: import org.junit.jupiter.api.Test;
27: import org.slf4j.Logger;
28:
29: import java.net.URI;
30: import java.time.ZoneOffset;
31: import java.util.Collections;
32: import java.util.HashSet;
33: import java.util.Set;
34:
35: import static org.junit.jupiter.api.Assertions.*;
36:
37: public abstract class AnnotatedMethodsTestRunner extends BaseRunner {
38: protected OWLClassWithAnnotatedMethodsInInterfaceParent classWithAnnotatedMethodsInInterfaceParent;
39: protected OWLClassWithUnProperties classWithUnProperties;
40:
41: public AnnotatedMethodsTestRunner(Logger logger, PersistenceFactory persistenceFactory, DataAccessor dataAccessor) {
42: super(logger, persistenceFactory, dataAccessor);
43: classWithUnProperties = new OWLClassWithUnProperties(Generators.generateUri());
44:
45: classWithAnnotatedMethodsInInterfaceParent = new OWLClassWithAnnotatedMethodsInInterfaceParent(Generators.generateUri());
46: classWithAnnotatedMethodsInInterfaceParent.setData(new OWLClassWithUnProperties(Generators.generateUri()));
47: classWithAnnotatedMethodsInInterfaceParent.setOrdinalEnumAttribute(OWLInterfaceE.Color.BLACK);
48: }
49:
50: @Test
51: void nonEmptyParticipationConstraintsInInterfaceThrowExceptionIfNotMet() {
52: this.em = getEntityManager("nonEmptyParticipationConstraintsInInterfaceThrowExceptionIfNotMet", false);
53:
54: /// violate constraints
55: classWithAnnotatedMethodsInInterfaceParent.setData(null);
56: em.getTransaction().begin();
57:
58: em.persist(classWithAnnotatedMethodsInInterfaceParent);
59:
60: assertThrows(RollbackException.class, () -> em.getTransaction().commit());
61:
62: }
63:
64: @Test
65: void nonEmptyParticipationConstraintsInInterfaceDoesNotThrowExceptionIfMet() {
66: this.em = getEntityManager("nonEmptyParticipationConstraintsInInterfaceDoesNotThrowExceptionIfMet", false);
67:
68: em.getTransaction().begin();
69:
70: em.persist(classWithAnnotatedMethodsInInterfaceParent);
71:
72: em.getTransaction().commit();
73:
74: verifyExists(OWLClassWithAnnotatedMethodsInInterfaceParent.class, classWithAnnotatedMethodsInInterfaceParent.getUri());
75: }
76:
77:
78: @Test
79: void maxParticipationConstraintsInInterfaceThrowExceptionIfNotMet() {
80: this.em = getEntityManager("maxParticipationConstraintsInInterfaceThrowExceptionIfNotMet", false);
81:
82: Set<OWLClassWithUnProperties> dataList = new HashSet<>();
83: dataList.add(new OWLClassWithUnProperties(Generators.generateUri()));
84: dataList.add(new OWLClassWithUnProperties(Generators.generateUri()));
85: dataList.add(new OWLClassWithUnProperties(Generators.generateUri()));
86: classWithAnnotatedMethodsInInterfaceParent.setDataList(dataList);
87:
88: em.getTransaction().begin();
89:
90: em.persist(classWithAnnotatedMethodsInInterfaceParent);
91:
92: assertThrows(RollbackException.class, () -> em.getTransaction().commit());
93: }
94:
95: @Test
96: void maxParticipationConstraintsInInterfaceDoesNotThrowExceptionIfMet() {
97: this.em = getEntityManager("maxParticipationConstraintsInInterfaceDoesNotThrowExceptionIfMet", false);
98:
99: Set<OWLClassWithUnProperties> dataList = new HashSet<>();
100: dataList.add(new OWLClassWithUnProperties(Generators.generateUri()));
101: dataList.add(new OWLClassWithUnProperties(Generators.generateUri()));
102: classWithAnnotatedMethodsInInterfaceParent.setDataList(dataList);
103:
104: em.getTransaction().begin();
105:
106: em.persist(classWithAnnotatedMethodsInInterfaceParent);
107:
108: em.getTransaction().commit();
109:
110: verifyExists(OWLClassWithAnnotatedMethodsInInterfaceParent.class, classWithAnnotatedMethodsInInterfaceParent.getUri());
111: }
112:
113: @Test
114: void converterAnnotatedFieldsGetConverted() throws Exception {
115: this.em = getEntityManager("converterAnnotatedFieldsGetConverted", false);
116:
117: classWithAnnotatedMethodsInInterfaceParent.setData(classWithUnProperties);
118:
119: final ZoneOffset value = ZoneOffset.ofHours(2);
120: classWithAnnotatedMethodsInInterfaceParent.setWithConverter(value);
121:
122: persist(classWithAnnotatedMethodsInInterfaceParent);
123:
124: verifyStatementsPresent(Collections.singleton(
125: new Quad(classWithAnnotatedMethodsInInterfaceParent.getUri(), URI.create(Vocabulary.p_m_withConverter),
126: classWithAnnotatedMethodsInInterfaceParent.getWithConverter().getId(), (String) null)), em);
127: }
128:
129: @Test
130: void enumeratedAnnotatedFieldsPersistedCorrectly() throws Exception {
131: this.em = getEntityManager("enumeratedAnnotatedFieldsPersistedCorrectly", false);
132: persist(classWithAnnotatedMethodsInInterfaceParent);
133:
134: verifyStatementsPresent(Collections.singletonList(
135: new Quad(classWithAnnotatedMethodsInInterfaceParent.getUri(), URI.create(Vocabulary.p_e_enumeratedOrdinalColor),
136: classWithAnnotatedMethodsInInterfaceParent.getOrdinalEnumAttribute().ordinal())), em);
137: }
138:
139:
140: @Test
141: void sequenceAnnotationGetsUpdatedCorrectly(){
142: this.em = getEntityManager("sequenceAnnotationGetsUpdatedCorrectly", true);
143: classWithAnnotatedMethodsInInterfaceParent.setSimpleList(Generators.createListOfIdentifiers());
144:
145: persist(classWithAnnotatedMethodsInInterfaceParent);
146:
147: final OWLClassWithAnnotatedMethodsInInterfaceParent update = findRequired(OWLClassWithAnnotatedMethodsInInterfaceParent.class, classWithAnnotatedMethodsInInterfaceParent.getUri());
148:
149: em.getTransaction().begin();
150: for (int i = 0; i < Generators.randomPositiveInt(2, 20); i++) {
151: final URI u = URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#Added-" + i);
152: // Insert at random position
153: update.getSimpleList().add(Generators.randomInt(update.getSimpleList().size()), u);
154: }
155: em.getTransaction().commit();
156:
157: final OWLClassWithAnnotatedMethodsInInterfaceParent res = findRequired(OWLClassWithAnnotatedMethodsInInterfaceParent.class, classWithAnnotatedMethodsInInterfaceParent.getUri());
158: assertEquals(update.getSimpleList(), res.getSimpleList());
159: }
160:
161:
162: }