Skip to content

Method: persistSupportsOrdinalEnumMapping()

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.annotations.Individual;
21: import cz.cvut.kbss.jopa.test.OWLClassM;
22: import cz.cvut.kbss.jopa.test.OWLClassR;
23: import cz.cvut.kbss.jopa.test.ObjectOneOfEnum;
24: import cz.cvut.kbss.jopa.test.Vocabulary;
25: import cz.cvut.kbss.jopa.test.environment.DataAccessor;
26: import cz.cvut.kbss.jopa.test.environment.Generators;
27: import cz.cvut.kbss.jopa.test.environment.PersistenceFactory;
28: import cz.cvut.kbss.jopa.test.environment.Quad;
29: import org.junit.jupiter.api.Test;
30: import org.slf4j.Logger;
31:
32: import java.net.URI;
33: import java.util.Arrays;
34: import java.util.Collections;
35:
36: import static org.junit.jupiter.api.Assertions.assertEquals;
37:
38: public abstract class EnumMappingTestRunner extends BaseRunner {
39:
40: public EnumMappingTestRunner(Logger logger,
41: PersistenceFactory persistenceFactory,
42: DataAccessor dataAccessor) {
43: super(logger, persistenceFactory, dataAccessor);
44: }
45:
46: @Test
47: public void persistSupportsMappingEnumValuesToObjectOneOfIndividuals() throws Exception {
48: this.em = getEntityManager("persistSupportsMappingEnumValuesToObjectOneOfIndividuals", false);
49: final OWLClassR instance = new OWLClassR(Generators.generateUri());
50: instance.setObjectOneOf(Generators.getRandomItem(Arrays.asList(ObjectOneOfEnum.values())));
51: persist(instance);
52: final String constantIri =
53: ObjectOneOfEnum.class.getDeclaredField(instance.getObjectOneOf().name()).getAnnotation(
54: Individual.class).iri();
55: verifyStatementsPresent(Collections.singletonList(
56: new Quad(instance.getUri(), URI.create(Vocabulary.P_HAS_OBJECT_ONE_OF), URI.create(constantIri))), em);
57: }
58:
59: @Test
60: public void entityLifecycleSupportsMappingEnumValuesToObjectOneOfIndividuals() {
61: this.em = getEntityManager("entityLifecycleSupportsMappingEnumValuesToObjectOneOfIndividuals", true);
62: final OWLClassR instance = new OWLClassR(Generators.generateUri());
63: instance.setObjectOneOf(ObjectOneOfEnum.ANNOTATION_PROPERTY);
64: persist(instance);
65:
66: transactional(() -> {
67: final OWLClassR toUpdate = findRequired(OWLClassR.class, instance.getUri());
68: assertEquals(ObjectOneOfEnum.ANNOTATION_PROPERTY, toUpdate.getObjectOneOf());
69: toUpdate.setObjectOneOf(ObjectOneOfEnum.DATATYPE_PROPERTY);
70: });
71:
72: final OWLClassR result = findRequired(OWLClassR.class, instance.getUri());
73: assertEquals(ObjectOneOfEnum.DATATYPE_PROPERTY, result.getObjectOneOf());
74: }
75:
76: @Test
77: public void persistSupportsOrdinalEnumMapping() throws Exception {
78: this.em = getEntityManager("persistSupportsOrdinalEnumMapping", false);
79: final OWLClassM instance = new OWLClassM();
80: instance.initializeTestValues(true);
81: persist(instance);
82:
83: verifyStatementsPresent(Collections.singletonList(
84: new Quad(URI.create(instance.getKey()), URI.create(Vocabulary.p_m_ordinalEnumAttribute),
85: instance.getOrdinalEnumAttribute().ordinal())), em);
86: }
87:
88: @Test
89: public void entityLifecycleSupportsOrdinalEnumMapping() {
90: this.em = getEntityManager("entityLifecycleSupportsOrdinalEnumMapping", true);
91: final OWLClassM instance = new OWLClassM();
92: instance.initializeTestValues(true);
93: persist(instance);
94:
95: transactional(() -> {
96: final OWLClassM toUpdate = findRequired(OWLClassM.class, instance.getKey());
97: assertEquals(instance.getOrdinalEnumAttribute(), toUpdate.getOrdinalEnumAttribute());
98: toUpdate.setOrdinalEnumAttribute(OWLClassM.Severity.HIGH);
99: });
100:
101: final OWLClassM result = findRequired(OWLClassM.class, instance.getKey());
102: assertEquals(OWLClassM.Severity.HIGH, result.getOrdinalEnumAttribute());
103: }
104: }