From 3d2ca03c4c95694fbe14b54944533336274d0024 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Fri, 17 Jun 2016 09:55:11 +0200 Subject: [PATCH] Testing handling of entities with mapped superclass. --- TODO.txt | 2 + .../cz/cvut/kbss/jopa/oom/FieldStrategy.java | 41 +++--- .../cvut/kbss/jopa/environment/OWLClassQ.java | 8 ++ .../environment/utils/MetamodelFactory.java | 27 ++-- .../environment/utils/MetamodelMocks.java | 4 + .../jopa/oom/AxiomDescriptorFactoryTest.java | 79 +++++++---- .../kbss/jopa/oom/AxiomValueGathererTest.java | 37 +++-- ...EntityConstructorPluralAttributesTest.java | 22 ++- .../kbss/jopa/oom/EntityConstructorTest.java | 49 +++++-- .../jopa/oom/EntityDeconstructorTest.java | 133 +++++++++++------- 10 files changed, 242 insertions(+), 160 deletions(-) diff --git a/TODO.txt b/TODO.txt index 073296e1e..e1874cbd5 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,4 +1,6 @@ - Consider using FieldSpecification instead of attribute name in ChangeRecord +- Do not allow working with entities which are not part of the metamodel + - e.g. when a entity class was not found by EntityLoader, trying to persist it should result in exception, not in adding it to metamodel - When persisting a collection of instances without generated ids, how to handle references between them? - It can happen that JOPA will try to insert a referenced individual which has not yet set id, which results in assigning it a generated URI. E.g. foreach(inst -> {inst.generateUri(); em.persist(inst); }) diff --git a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/FieldStrategy.java b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/FieldStrategy.java index c1995eeff..e96a940a8 100644 --- a/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/FieldStrategy.java +++ b/jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/FieldStrategy.java @@ -1,16 +1,14 @@ /** * Copyright (C) 2016 Czech Technical University in Prague - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + *

+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + *

+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jopa.oom; @@ -31,8 +29,7 @@ abstract class FieldStrategy, X> { final EntityMappingHelper mapper; CascadeResolver cascadeResolver; - FieldStrategy(EntityType et, T att, Descriptor attributeDescriptor, - EntityMappingHelper mapper) { + FieldStrategy(EntityType et, T att, Descriptor attributeDescriptor, EntityMappingHelper mapper) { this.et = et; this.attribute = att; this.attributeDescriptor = attributeDescriptor; @@ -43,11 +40,9 @@ abstract class FieldStrategy, X> { EntityType et, FieldSpecification att, Descriptor fieldDescriptor, EntityMappingHelper mapper) { if (att instanceof TypesSpecification) { - return new TypesFieldStrategy<>(et, - (TypesSpecification) att, fieldDescriptor, mapper); + return new TypesFieldStrategy<>(et, (TypesSpecification) att, fieldDescriptor, mapper); } else if (att instanceof PropertiesSpecification) { - return new PropertiesFieldStrategy<>(et, - (PropertiesSpecification) att, fieldDescriptor, + return new PropertiesFieldStrategy<>(et, (PropertiesSpecification) att, fieldDescriptor, mapper); } final Attribute attribute = (Attribute) att; @@ -57,8 +52,7 @@ abstract class FieldStrategy, X> { case DATA: throw new NotYetImplementedException(); case OBJECT: - return createPluralObjectPropertyStrategy(et, - (PluralAttribute) attribute, + return createPluralObjectPropertyStrategy(et, (PluralAttribute) attribute, fieldDescriptor, mapper); default: break; @@ -84,16 +78,13 @@ abstract class FieldStrategy, X> { Descriptor descriptor, EntityMappingHelper mapper) { switch (attribute.getCollectionType()) { case LIST: - return createOwlListPropertyStrategy(et, (ListAttribute) attribute, descriptor, - mapper); + return createOwlListPropertyStrategy(et, (ListAttribute) attribute, descriptor, mapper); case COLLECTION: case SET: - return new SimpleSetPropertyStrategy<>(et, attribute, descriptor, - mapper); + return new SimpleSetPropertyStrategy<>(et, attribute, descriptor, mapper); default: throw new NotYetImplementedException( - "Unsupported plural attribute collection type " - + attribute.getCollectionType()); + "Unsupported plural attribute collection type " + attribute.getCollectionType()); } } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/OWLClassQ.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/OWLClassQ.java index 85fe72bf1..aa0a78ea2 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/OWLClassQ.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/OWLClassQ.java @@ -4,6 +4,9 @@ import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; @OWLClass(iri = "http://krizik.felk.cvut.cz/ontologies/jopa/entities#OWLClassQ") public class OWLClassQ extends QMappedSuperclass { @@ -42,4 +45,9 @@ public static Field getParentStringField() throws Exception { public static Field getOwlClassAField() throws Exception { return QMappedSuperclass.class.getDeclaredField("owlClassA"); } + + public static Set getPersistentFields() throws Exception { + return new HashSet<>( + Arrays.asList(getStringAttributeField(), getParentStringField(), getLabelField(), getOwlClassAField())); + } } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java index fb85fe971..2f963e843 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelFactory.java @@ -1,16 +1,14 @@ /** * Copyright (C) 2016 Czech Technical University in Prague *

- * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. *

- * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jopa.environment.utils; @@ -42,7 +40,7 @@ private MetamodelFactory() { */ public static void initOWLClassAMocks(EntityType etMock, Attribute strAttMock, TypesSpecification typesMock, Identifier idMock) throws NoSuchFieldException, - SecurityException { + SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassA.class); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassA.getClassIri())); when(etMock.getAttribute(OWLClassA.getStrAttField().getName())).thenReturn(strAttMock); @@ -77,8 +75,8 @@ public static void initOWLClassAMocks(EntityType etMock, Attribute st */ public static void initOWLClassBMocks(EntityType etMock, Attribute strAttMock, PropertiesSpecification propsMock, Identifier idMock) throws - NoSuchFieldException, - SecurityException { + NoSuchFieldException, + SecurityException { when(etMock.getJavaType()).thenReturn(OWLClassB.class); when(etMock.getIRI()).thenReturn(IRI.create(OWLClassB.getClassIri())); when(etMock.getAttribute(OWLClassB.getStrAttField().getName())).thenReturn(strAttMock); @@ -572,7 +570,7 @@ public static void initOWLClassPMock(EntityType et, TypesSpecificatio PropertiesSpecification props, SingularAttribute uriAtt, PluralAttribute urlsAtt, ListAttribute simpleListAtt, ListAttribute refListAtt, Identifier idP) throws - Exception { + Exception { when(et.getIdentifier()).thenReturn(idP); when(idP.getJavaField()).thenReturn(OWLClassP.getUriField()); when(et.getIRI()).thenReturn(IRI.create(OWLClassP.getClassIri())); @@ -658,6 +656,7 @@ public static void initOwlClassQMock(EntityType et, SingularAttribute SingularAttribute qLabelAtt, SingularAttribute qOwlClassAAtt, Identifier idQ) throws Exception { when(et.getIdentifier()).thenReturn(idQ); + when(et.getJavaType()).thenReturn(OWLClassQ.class); when(idQ.getJavaField()).thenReturn(OWLClassQ.getUriField()); when(et.getIRI()).thenReturn(IRI.create(OWLClassQ.getClassIri())); when(et.getFieldSpecifications()) @@ -699,7 +698,7 @@ public static void initOwlClassQMock(EntityType et, SingularAttribute when(qLabelAtt.getJavaType()).thenReturn(OWLClassQ.getLabelField().getType()); when(qLabelAtt.getName()).thenReturn(OWLClassQ.getLabelField().getName()); when(et.getAttribute(OWLClassQ.getLabelField().getName())).thenReturn(qLabelAtt); - when(qLabelAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA); + when(qLabelAtt.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.ANNOTATION); when(qLabelAtt.isCollection()).thenReturn(false); when(qLabelAtt.getBindableJavaType()).thenReturn(String.class); when(qLabelAtt.getIRI()).thenReturn( diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java index 819c49a9e..2ccff1828 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/environment/utils/MetamodelMocks.java @@ -282,6 +282,10 @@ public OWLClassPMetamodel forOwlClassP() { return new OWLClassPMetamodel(); } + public OWLClassQMetamodel forOwlClassQ() { + return new OWLClassQMetamodel(); + } + public class OWLClassAMetamodel { public EntityType entityType() { return MetamodelMocks.this.etA; diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/AxiomDescriptorFactoryTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/AxiomDescriptorFactoryTest.java index d39188a2a..39e5150c7 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/AxiomDescriptorFactoryTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/AxiomDescriptorFactoryTest.java @@ -1,46 +1,43 @@ /** * Copyright (C) 2016 Czech Technical University in Prague - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + *

+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + *

+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jopa.oom; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - -import java.net.URI; - +import cz.cvut.kbss.jopa.environment.OWLClassA; +import cz.cvut.kbss.jopa.environment.OWLClassB; +import cz.cvut.kbss.jopa.environment.OWLClassD; +import cz.cvut.kbss.jopa.environment.OWLClassQ; import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks; -import cz.cvut.kbss.jopa.sessions.LoadingParameters; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - import cz.cvut.kbss.jopa.model.annotations.FetchType; +import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty; import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty; import cz.cvut.kbss.jopa.model.descriptors.Descriptor; import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; import cz.cvut.kbss.jopa.model.metamodel.Attribute.PersistentAttributeType; -import cz.cvut.kbss.jopa.environment.OWLClassA; -import cz.cvut.kbss.jopa.environment.OWLClassB; -import cz.cvut.kbss.jopa.environment.OWLClassD; +import cz.cvut.kbss.jopa.sessions.LoadingParameters; import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor; import cz.cvut.kbss.ontodriver.model.Assertion; import cz.cvut.kbss.ontodriver.model.NamedResource; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.net.URI; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.when; public class AxiomDescriptorFactoryTest { @@ -217,7 +214,31 @@ public void testCreateForFieldLoadingProperties() throws Exception { assertEquals(1, res.getAssertions().size()); final Assertion as = res.getAssertions().iterator().next(); assertEquals(Assertion - .createUnspecifiedPropertyAssertion(metamodelMocks.forOwlClassB().propertiesSpec().isInferred()), + .createUnspecifiedPropertyAssertion(metamodelMocks.forOwlClassB().propertiesSpec().isInferred()), as); } + + @Test + public void createForEntityLoadingIncludesMappedSuperclassAttributes() throws Exception { + final Descriptor desc = new EntityDescriptor(); + final AxiomDescriptor res = factory.createForEntityLoading(new LoadingParameters<>(OWLClassQ.class, PK, desc), + metamodelMocks.forOwlClassQ().entityType()); + final Set propertyIris = OWLClassQ.getPersistentFields().stream().map(f -> { + if (f.getAnnotation(OWLDataProperty.class) != null) { + return f.getAnnotation(OWLDataProperty.class).iri(); + } else if (f.getAnnotation(OWLObjectProperty.class) != null) { + return f.getAnnotation(OWLObjectProperty.class).iri(); + } else { + return f.getAnnotation(OWLAnnotationProperty.class).iri(); + } + }).collect(Collectors.toSet()); + final Set assertions = res.getAssertions(); + // + class assertion + assertEquals(OWLClassQ.getPersistentFields().size() + 1, assertions.size()); + for (Assertion a : assertions) { + if (a.getType() != Assertion.AssertionType.CLASS) { + assertTrue(propertyIris.contains(a.getIdentifier().toString())); + } + } + } } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/AxiomValueGathererTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/AxiomValueGathererTest.java index 3c58e6ddb..ff188a50a 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/AxiomValueGathererTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/AxiomValueGathererTest.java @@ -1,16 +1,14 @@ /** * Copyright (C) 2016 Czech Technical University in Prague - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + *

+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + *

+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jopa.oom; @@ -41,7 +39,8 @@ public class AxiomValueGathererTest { - private static final NamedResource SUBJECT = NamedResource.create("http://krizik.felk.cvut.cz/ontologies/jopa#Subject"); + private static final NamedResource SUBJECT = NamedResource + .create("http://krizik.felk.cvut.cz/ontologies/jopa#Subject"); private static final Assertion DATA_ASSERTION = Assertion.createDataPropertyAssertion( URI.create("http://krizik.felk.cvut.cz/ontologies/jopa#dataProperty"), false); private static final Assertion OBJECT_ASSERTION = Assertion.createObjectPropertyAssertion( @@ -95,8 +94,8 @@ public void testAddPropertiesTwice() throws Exception { propsOne.put(DATA_ASSERTION, new HashSet<>(Arrays.asList(new Value<>("one"), new Value<>("two")))); gatherer.addProperties(propsOne, null); final Map>> propsTwo = new HashMap<>(); - propsTwo.put(OBJECT_ASSERTION, Collections.>singleton(new Value<>("http://krizik.felk.cvut.cz/pp"))); - propsTwo.put(DATA_ASSERTION, Collections.>singleton(new Value<>("117"))); + propsTwo.put(OBJECT_ASSERTION, Collections.singleton(new Value<>("http://krizik.felk.cvut.cz/pp"))); + propsTwo.put(DATA_ASSERTION, Collections.singleton(new Value<>("117"))); gatherer.addProperties(propsTwo, null); final Map>> res = OOMTestUtils.getPropertiesToAdd(gatherer); @@ -141,9 +140,9 @@ private Set typesToAdd() { private Map>> propertiesToAdd() { final Map>> props = new HashMap<>(); - props.put(DATA_ASSERTION, Collections.>singleton(new Value<>("valueOne"))); - props.put(OBJECT_ASSERTION, Collections.>singleton( - new Value<>(URI.create("http://krizik.felk.cvut.cz/valueTwo")))); + props.put(DATA_ASSERTION, Collections.singleton(new Value<>("valueOne"))); + props.put(OBJECT_ASSERTION, + Collections.singleton(new Value<>(URI.create("http://krizik.felk.cvut.cz/valueTwo")))); return props; } @@ -183,8 +182,8 @@ private Set typesToRemove() { private Map>> propertiesToRemove() { final Map>> props = new HashMap<>(); - props.put(DATA_ASSERTION, Collections.>singleton(new Value<>("valueThree"))); - props.put(DATA_ASSERTION, Collections.>singleton(new Value<>("valueFour"))); + props.put(DATA_ASSERTION, Collections.singleton(new Value<>("valueThree"))); + props.put(DATA_ASSERTION, Collections.singleton(new Value<>("valueFour"))); return props; } } \ No newline at end of file diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorPluralAttributesTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorPluralAttributesTest.java index 3b0c73085..be74cfd29 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorPluralAttributesTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorPluralAttributesTest.java @@ -1,16 +1,14 @@ /** * Copyright (C) 2016 Czech Technical University in Prague - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + *

+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + *

+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jopa.oom; @@ -145,7 +143,7 @@ private Collection> initSimpleListAxioms() throws Exception @Test public void setsSimpleListLazilyLoadedFieldValue() throws Exception { - final Collection> axioms = Collections.>singleton(new AxiomImpl<>( + final Collection> axioms = Collections.singleton(new AxiomImpl<>( SUBJECT, hasSimpleListAssertion, new Value<>(firstListElem))); prepareMapperMockForSimpleListLoad(); diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorTest.java index ecb3cb4ea..fbe584f06 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityConstructorTest.java @@ -1,16 +1,14 @@ /** * Copyright (C) 2016 Czech Technical University in Prague - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + *

+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + *

+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jopa.oom; @@ -21,6 +19,7 @@ import cz.cvut.kbss.jopa.exceptions.IntegrityConstraintViolatedException; import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties; import cz.cvut.kbss.jopa.model.SequencesVocabulary; +import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty; import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty; import cz.cvut.kbss.jopa.model.annotations.ParticipationConstraints; @@ -443,7 +442,7 @@ public void reconstructsEntityWithDataPropertiesOfBasicTypesAndStringIdentifier( private Collection> createAxiomsForValues(Boolean b, Integer i, Long lng, Double d, Date date, OWLClassM.Severity severity) throws - Exception { + Exception { final Collection> axioms = new ArrayList<>(); if (b != null) { final String boolAttIri = OWLClassM.getBooleanAttributeField().getAnnotation(OWLDataProperty.class).iri(); @@ -579,4 +578,30 @@ public void instanceReconstructionSkipsAxiomsForWhichNoAttributeCanBeFound() thr assertNotNull(res.getStringAttribute()); assertNull(res.getTypes()); } + + @Test + public void reconstructsInstanceWithMappedSuperclass() throws Exception { + final Set> axioms = new HashSet<>(); + axioms.add(getClassAssertionAxiomForType(OWLClassQ.getClassIri())); + axioms.add(getStringAttAssertionAxiom(OWLClassQ.getStringAttributeField())); + axioms.add(getStringAttAssertionAxiom(OWLClassQ.getParentStringField())); + final URI labelUri = URI.create(OWLClassQ.getLabelField().getAnnotation(OWLAnnotationProperty.class).iri()); + axioms.add(new AxiomImpl<>(PK_RESOURCE, Assertion.createAnnotationPropertyAssertion(labelUri, false), + new Value<>(STRING_ATT))); + final URI owlClassAUri = URI.create(OWLClassQ.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri()); + axioms.add(new AxiomImpl<>(PK_RESOURCE, Assertion.createObjectPropertyAssertion(owlClassAUri, false), + new Value<>(NamedResource.create(PK_TWO)))); + final OWLClassA a = new OWLClassA(); + a.setUri(PK_TWO); + when(mapperMock.getEntityFromCacheOrOntology(eq(OWLClassA.class), eq(PK_TWO), any(Descriptor.class))) + .thenReturn(a); + + final OWLClassQ res = constructor.reconstructEntity(PK, mocks.forOwlClassQ().entityType(), descriptor, axioms); + assertNotNull(res); + assertEquals(PK, res.getUri()); + assertEquals(STRING_ATT, res.getStringAttribute()); + assertEquals(STRING_ATT, res.getParentString()); + assertEquals(STRING_ATT, res.getLabel()); + assertSame(a, res.getOwlClassA()); + } } diff --git a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java index 351afdc2a..c01d98b59 100644 --- a/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java +++ b/jopa-impl/src/test/java/cz/cvut/kbss/jopa/oom/EntityDeconstructorTest.java @@ -1,16 +1,14 @@ /** * Copyright (C) 2016 Czech Technical University in Prague - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any - * later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. You should have received a copy of the GNU General Public License - * along with this program. If not, see . + *

+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. + *

+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. You should have received a copy of the GNU General Public License along with this program. If not, see + * . */ package cz.cvut.kbss.jopa.oom; @@ -18,6 +16,7 @@ import cz.cvut.kbss.jopa.environment.utils.Generators; import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks; import cz.cvut.kbss.jopa.model.annotations.Inferred; +import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty; import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty; import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty; import cz.cvut.kbss.jopa.model.descriptors.Descriptor; @@ -113,9 +112,8 @@ public void testMapEntityWithDataProperty() throws Exception { assertEquals(entityA.getUri(), res.getSubject().getIdentifier()); // Class assertion and the data property assertion assertEquals(2, res.getAssertions().size()); - assertTrue(res.getAssertions().contains(Assertion.createClassAssertion(false))); - assertTrue(res.getAssertions().contains( - Assertion.createDataPropertyAssertion(strAttAIdentifier, false))); + assertTrue(containsInstanceClassAssertion(res, OWLClassA.getClassIri())); + assertTrue(containsDPAssertion(res, OWLClassA.getStrAttField(), entityA.getStringAttribute(), true)); } @Test @@ -129,7 +127,7 @@ public void testMapEntityWithDataPropertyNullValue() throws Exception { assertNotNull(res); assertEquals(entity.getUri(), res.getSubject().getIdentifier()); assertEquals(2, res.getAssertions().size()); - assertTrue(res.getAssertions().contains(Assertion.createClassAssertion(false))); + assertTrue(containsInstanceClassAssertion(res, OWLClassA.getClassIri())); final List> v = res.getAssertionValues(Assertion.createDataPropertyAssertion( strAttAIdentifier, false)); assertEquals(1, v.size()); @@ -199,17 +197,17 @@ public void testMapEntityWithObjectProperty() throws Exception { assertEquals(entityD.getUri(), res.getSubject().getIdentifier()); // Class assertion and the object property assertion assertEquals(2, res.getAssertions().size()); - for (Assertion a : res.getAssertions()) { - final List> vals = res.getAssertionValues(a); - if (a.getType() == AssertionType.CLASS) { - assertEquals(1, vals.size()); - assertEquals(URI.create(OWLClassD.getClassIri()), vals.get(0).getValue()); - } else { - assertTrue(AssertionType.OBJECT_PROPERTY == a.getType()); - assertEquals(1, vals.size()); - assertEquals(NamedResource.create(entityA.getUri()), vals.get(0).getValue()); - } - } + assertTrue(containsInstanceClassAssertion(res, OWLClassD.getClassIri())); + assertTrue( + containsOPAssertion(res, OWLClassD.getOwlClassAField(), NamedResource.create(entityA.getUri()), true)); + } + + private boolean containsOPAssertion(AxiomValueDescriptor descriptor, Field attributeField, Object value, + boolean checkSingle) { + final URI propertyUri = URI.create(attributeField.getAnnotation(OWLObjectProperty.class).iri()); + final Assertion assertion = Assertion + .createObjectPropertyAssertion(propertyUri, attributeField.getAnnotation(Inferred.class) != null); + return containsAssertionValue(descriptor, assertion, value, checkSingle); } @Test @@ -332,12 +330,7 @@ public void mapsEntityDataPropertyFieldToAxiomDescriptor() throws Exception { OWLClassA.getStrAttField(), mocks.forOwlClassA().entityType(), aDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(1, res.getAssertions().size()); - assertTrue(res.getAssertions().contains( - Assertion.createDataPropertyAssertion(strAttAIdentifier, - mocks.forOwlClassA().stringAttribute().isInferred()))); - final List> val = res.getAssertionValues(res.getAssertions().iterator().next()); - assertEquals(1, val.size()); - assertEquals(entityA.getStringAttribute(), val.get(0).getValue()); + assertTrue(containsDPAssertion(res, OWLClassA.getStrAttField(), entityA.getStringAttribute(), true)); } @Test @@ -365,14 +358,11 @@ public void mapsEntityObjectPropertyValueInContextToAxiomDescriptor() throws Exc OWLClassD.getOwlClassAField(), mocks.forOwlClassD().entityType(), dDescriptor); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); assertEquals(1, res.getAssertions().size()); - assertTrue(res.getAssertions().contains( - Assertion.createObjectPropertyAssertion(owlClassAAttIdentifier, - mocks.forOwlClassD().owlClassAAtt().isInferred()))); - final Assertion ass = res.getAssertions().iterator().next(); - final List> val = res.getAssertionValues(ass); - assertEquals(1, val.size()); + final Assertion ass = Assertion.createObjectPropertyAssertion(owlClassAAttIdentifier, + mocks.forOwlClassD().owlClassAAtt().isInferred()); assertEquals(CONTEXT, res.getAssertionContext(ass)); - assertEquals(NamedResource.create(entityA.getUri()), val.get(0).getValue()); + assertTrue( + containsOPAssertion(res, OWLClassD.getOwlClassAField(), NamedResource.create(entityA.getUri()), true)); } @Test @@ -383,26 +373,36 @@ public void mapsEntityWithStringKeyAndBasicDataAttributes() throws Exception { .mapEntityToAxioms(URI.create(entityM.getKey()), entityM, mocks.forOwlClassM().entityType(), desc); final AxiomValueDescriptor res = getAxiomValueDescriptor(builder); - assertTrue(containsInstanceAssertion(res)); - assertTrue(containsDPAssertion(res, OWLClassM.getBooleanAttributeField(), entityM.getBooleanAttribute())); - assertTrue(containsDPAssertion(res, OWLClassM.getIntAttributeField(), entityM.getIntAttribute())); - assertTrue(containsDPAssertion(res, OWLClassM.getDoubleAttributeField(), entityM.getDoubleAttribute())); - assertTrue(containsDPAssertion(res, OWLClassM.getLongAttributeField(), entityM.getLongAttribute())); - assertTrue(containsDPAssertion(res, OWLClassM.getDateAttributeField(), entityM.getDateAttribute())); + assertTrue(containsInstanceClassAssertion(res, OWLClassM.getClassIri())); + assertTrue(containsDPAssertion(res, OWLClassM.getBooleanAttributeField(), entityM.getBooleanAttribute(), true)); + assertTrue(containsDPAssertion(res, OWLClassM.getIntAttributeField(), entityM.getIntAttribute(), true)); + assertTrue(containsDPAssertion(res, OWLClassM.getDoubleAttributeField(), entityM.getDoubleAttribute(), true)); + assertTrue(containsDPAssertion(res, OWLClassM.getLongAttributeField(), entityM.getLongAttribute(), true)); + assertTrue(containsDPAssertion(res, OWLClassM.getDateAttributeField(), entityM.getDateAttribute(), true)); } - private boolean containsInstanceAssertion(AxiomValueDescriptor descriptor) throws Exception { + private boolean containsInstanceClassAssertion(AxiomValueDescriptor descriptor, String classIri) throws Exception { final List> values = descriptor.getAssertionValues(Assertion.createClassAssertion(false)); assertEquals(1, values.size()); - return values.get(0).getValue().toString().equals(OWLClassM.getClassIri()); + return values.get(0).getValue().toString().equals(classIri); } - private boolean containsDPAssertion(AxiomValueDescriptor descriptor, Field attributeField, Object value) { - OWLDataProperty annotation = attributeField.getAnnotation(OWLDataProperty.class); + private boolean containsDPAssertion(AxiomValueDescriptor descriptor, Field attributeField, Object value, + boolean checkSingle) { + final OWLDataProperty annotation = attributeField.getAnnotation(OWLDataProperty.class); final URI propertyUri = URI.create(annotation.iri()); final Assertion assertion = Assertion.createDataPropertyAssertion(propertyUri, attributeField.getAnnotation( Inferred.class) != null); + return containsAssertionValue(descriptor, assertion, value, checkSingle); + } + + private boolean containsAssertionValue(AxiomValueDescriptor descriptor, Assertion assertion, Object value, + boolean checkSingle) { final List> values = descriptor.getAssertionValues(assertion); + assertFalse(values.isEmpty()); + if (checkSingle) { + assertEquals(1, values.size()); + } for (Value val : values) { if (val.getValue().equals(value)) { return true; @@ -411,6 +411,41 @@ private boolean containsDPAssertion(AxiomValueDescriptor descriptor, Field attri return false; } + @Test + public void mapEntityToAxiomsIncludesAttributesInheritedFromMappedSuperclass() throws Exception { + final Descriptor desc = new EntityDescriptor(); + final OWLClassQ q = initQ(); + + final AxiomValueGatherer builder = entityBreaker + .mapEntityToAxioms(q.getUri(), q, mocks.forOwlClassQ().entityType(), desc); + final AxiomValueDescriptor valueDescriptor = getAxiomValueDescriptor(builder); + assertTrue(containsInstanceClassAssertion(valueDescriptor, OWLClassQ.getClassIri())); + assertTrue(containsDPAssertion(valueDescriptor, OWLClassQ.getStringAttributeField(), q.getStringAttribute(), + true)); + assertTrue(containsDPAssertion(valueDescriptor, OWLClassQ.getParentStringField(), q.getParentString(), true)); + assertTrue(containsAPAssertion(valueDescriptor, OWLClassQ.getLabelField(), q.getLabel(), true)); + assertTrue(containsOPAssertion(valueDescriptor, OWLClassQ.getOwlClassAField(), + NamedResource.create(entityA.getUri()), true)); + } + + private OWLClassQ initQ() { + final OWLClassQ q = new OWLClassQ(); + q.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/test#entityQ")); + q.setStringAttribute("stringAttribute"); + q.setParentString("parentStringAttribute"); + q.setLabel("label"); + q.setOwlClassA(entityA); + return q; + } + + private boolean containsAPAssertion(AxiomValueDescriptor descriptor, Field attributeField, Object value, + boolean checkSingle) { + final URI propertyUri = URI.create(attributeField.getAnnotation(OWLAnnotationProperty.class).iri()); + final Assertion assertion = Assertion + .createAnnotationPropertyAssertion(propertyUri, attributeField.getAnnotation(Inferred.class) != null); + return containsAssertionValue(descriptor, assertion, value, checkSingle); + } + private static AxiomValueDescriptor getAxiomValueDescriptor(AxiomValueGatherer builder) throws Exception { return OOMTestUtils.getAxiomValueDescriptor(builder);