Skip to content

Commit

Permalink
Testing handling of entities with mapped superclass.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Jun 17, 2016
1 parent 2bf0dd0 commit 3d2ca03
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 160 deletions.
2 changes: 2 additions & 0 deletions 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); })
Expand Down
41 changes: 16 additions & 25 deletions 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 <http://www.gnu.org/licenses/>.
* <p>
* 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.
* <p>
* 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
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.oom;

Expand All @@ -31,8 +29,7 @@ abstract class FieldStrategy<T extends FieldSpecification<? super X, ?>, X> {
final EntityMappingHelper mapper;
CascadeResolver cascadeResolver;

FieldStrategy(EntityType<X> et, T att, Descriptor attributeDescriptor,
EntityMappingHelper mapper) {
FieldStrategy(EntityType<X> et, T att, Descriptor attributeDescriptor, EntityMappingHelper mapper) {
this.et = et;
this.attribute = att;
this.attributeDescriptor = attributeDescriptor;
Expand All @@ -43,11 +40,9 @@ abstract class FieldStrategy<T extends FieldSpecification<? super X, ?>, X> {
EntityType<X> et, FieldSpecification<? super X, ?> att,
Descriptor fieldDescriptor, EntityMappingHelper mapper) {
if (att instanceof TypesSpecification) {
return new TypesFieldStrategy<>(et,
(TypesSpecification<? super X, ?>) att, fieldDescriptor, mapper);
return new TypesFieldStrategy<>(et, (TypesSpecification<? super X, ?>) att, fieldDescriptor, mapper);
} else if (att instanceof PropertiesSpecification) {
return new PropertiesFieldStrategy<>(et,
(PropertiesSpecification<? super X, ?, ?, ?>) att, fieldDescriptor,
return new PropertiesFieldStrategy<>(et, (PropertiesSpecification<? super X, ?, ?, ?>) att, fieldDescriptor,
mapper);
}
final Attribute<? super X, ?> attribute = (Attribute<? super X, ?>) att;
Expand All @@ -57,8 +52,7 @@ abstract class FieldStrategy<T extends FieldSpecification<? super X, ?>, X> {
case DATA:
throw new NotYetImplementedException();
case OBJECT:
return createPluralObjectPropertyStrategy(et,
(PluralAttribute<? super X, ?, ?>) attribute,
return createPluralObjectPropertyStrategy(et, (PluralAttribute<? super X, ?, ?>) attribute,
fieldDescriptor, mapper);
default:
break;
Expand All @@ -84,16 +78,13 @@ abstract class FieldStrategy<T extends FieldSpecification<? super X, ?>, X> {
Descriptor descriptor, EntityMappingHelper mapper) {
switch (attribute.getCollectionType()) {
case LIST:
return createOwlListPropertyStrategy(et, (ListAttribute<? super Y, ?>) attribute, descriptor,
mapper);
return createOwlListPropertyStrategy(et, (ListAttribute<? super Y, ?>) 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());
}
}

Expand Down
Expand Up @@ -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 {
Expand Down Expand Up @@ -42,4 +45,9 @@ public static Field getParentStringField() throws Exception {
public static Field getOwlClassAField() throws Exception {
return QMappedSuperclass.class.getDeclaredField("owlClassA");
}

public static Set<Field> getPersistentFields() throws Exception {
return new HashSet<>(
Arrays.asList(getStringAttributeField(), getParentStringField(), getLabelField(), getOwlClassAField()));
}
}
@@ -1,16 +1,14 @@
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* 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.
* <p>
* 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 <http://www.gnu.org/licenses/>.
* 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
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.environment.utils;

Expand Down Expand Up @@ -42,7 +40,7 @@ private MetamodelFactory() {
*/
public static void initOWLClassAMocks(EntityType<OWLClassA> 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);
Expand Down Expand Up @@ -77,8 +75,8 @@ public static void initOWLClassAMocks(EntityType<OWLClassA> etMock, Attribute st
*/
public static void initOWLClassBMocks(EntityType<OWLClassB> 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);
Expand Down Expand Up @@ -572,7 +570,7 @@ public static void initOWLClassPMock(EntityType<OWLClassP> 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()));
Expand Down Expand Up @@ -658,6 +656,7 @@ public static void initOwlClassQMock(EntityType<OWLClassQ> 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())
Expand Down Expand Up @@ -699,7 +698,7 @@ public static void initOwlClassQMock(EntityType<OWLClassQ> 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(
Expand Down
Expand Up @@ -282,6 +282,10 @@ public OWLClassPMetamodel forOwlClassP() {
return new OWLClassPMetamodel();
}

public OWLClassQMetamodel forOwlClassQ() {
return new OWLClassQMetamodel();
}

public class OWLClassAMetamodel {
public EntityType<OWLClassA> entityType() {
return MetamodelMocks.this.etA;
Expand Down
@@ -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 <http://www.gnu.org/licenses/>.
* <p>
* 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.
* <p>
* 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
* <http://www.gnu.org/licenses/>.
*/
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 {

Expand Down Expand Up @@ -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<String> 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<Assertion> 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()));
}
}
}
}

0 comments on commit 3d2ca03

Please sign in to comment.