EntityConstructor { <T> T reconstructEntity(URI identifier, EntityType<T> et, Descriptor descriptor, Collection<Axiom<?>> axioms) throws InstantiationException, IllegalAccessException { assert !axioms.isEmpty(); if (!axiomsContainEntityClassAssertion(axioms, et)) { return null; } final T instance = createEntityInstance(identifier, et); mapper.registerInstance(identifier, instance, descriptor.getContext()); populateAttributes(instance, et, descriptor, axioms); validateIntegrityConstraints(instance, et); return instance; } EntityConstructor(ObjectOntologyMapperImpl mapper);   }
@Test void testReconstructEntityWithTypesAndDataProperty() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri())); axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField())); axioms.addAll(getTypesAxiomsForOwlClassA()); final OWLClassA res = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms); assertNotNull(res); assertEquals(PK, res.getUri()); assertEquals(STRING_ATT, res.getStringAttribute()); assertEquals(TYPES, res.getTypes()); verify(mapperMock).registerInstance(PK, res, descriptor.getContext()); }
@Test void testReconstructEntityWithDataPropertyEmptyTypes() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri())); axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField())); final OWLClassA res = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms); assertNotNull(res); assertEquals(PK, res.getUri()); assertEquals(STRING_ATT, res.getStringAttribute()); assertNull(res.getTypes()); verify(mapperMock).registerInstance(PK, res, descriptor.getContext()); }
@Test void testReconstructEntityWithDataPropertyAndProperties() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassB.getClassIri())); axioms.add(getStringAttAssertionAxiom(OWLClassB.getStrAttField())); final Collection<Axiom<?>> properties = getProperties(); axioms.addAll(properties); final OWLClassB res = constructor.reconstructEntity(PK, mocks.forOwlClassB().entityType(), descriptor, axioms); assertNotNull(res); assertEquals(PK, res.getUri()); assertEquals(STRING_ATT, res.getStringAttribute()); assertNotNull(res.getProperties()); assertEquals(properties.size(), res.getProperties().size()); for (Axiom<?> a : properties) { final String key = a.getAssertion().getIdentifier().toString(); assertTrue(res.getProperties().containsKey(key)); assertEquals(1, res.getProperties().get(key).size()); assertEquals(a.getValue().stringValue(), res.getProperties().get(key).iterator().next()); } verify(mapperMock).registerInstance(PK, res, descriptor.getContext()); }
@Test void testReconstructEntityWithDataPropertiesAndEmptyProperties() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassB.getClassIri())); axioms.add(getStringAttAssertionAxiom(OWLClassB.getStrAttField())); final OWLClassB res = constructor.reconstructEntity(PK, mocks.forOwlClassB().entityType(), descriptor, axioms); assertNotNull(res); assertEquals(PK, res.getUri()); assertEquals(STRING_ATT, res.getStringAttribute()); assertNull(res.getProperties()); verify(mapperMock).registerInstance(PK, res, descriptor.getContext()); }
@Test void testReconstructEntityWithObjectProperty() throws Exception { final Set<Axiom<?>> axiomsD = getAxiomsForD(); final Descriptor fieldDesc = new EntityDescriptor(); descriptor.addAttributeDescriptor(OWLClassD.getOwlClassAField(), fieldDesc); final OWLClassA entityA = new OWLClassA(); entityA.setUri(PK_TWO); entityA.setStringAttribute(STRING_ATT); when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDesc)) .thenReturn(entityA); final OWLClassD res = constructor.reconstructEntity(PK, mocks.forOwlClassD().entityType(), descriptor, axiomsD); assertNotNull(res); assertEquals(PK, res.getUri()); verify(mapperMock).getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDesc); assertNotNull(res.getOwlClassA()); assertEquals(PK_TWO, res.getOwlClassA().getUri()); assertEquals(STRING_ATT, res.getOwlClassA().getStringAttribute()); verify(mapperMock).registerInstance(PK, res, descriptor.getContext()); }
@Test void reconstructsEntityWithDataPropertyWhereRangeDoesNotMatch() throws Exception { final List<Axiom<?>> axioms = new ArrayList<>(); axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri())); axioms.add(createDataPropertyAxiomWithWrongRange(OWLClassA.getStrAttField())); axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField())); OWLClassA entityA = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms); assertNotNull(entityA); assertEquals(STRING_ATT, entityA.getStringAttribute()); axioms.clear(); axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri())); axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField())); axioms.add(createDataPropertyAxiomWithWrongRange(OWLClassA.getStrAttField())); entityA = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms); assertNotNull(entityA); assertEquals(STRING_ATT, entityA.getStringAttribute()); }
@Test void throwsExceptionWhenDataPropertyCardinalityRestrictionIsNotMet() throws Exception { final List<Axiom<?>> axioms = new ArrayList<>(); axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri())); axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField())); axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField())); assertThrows(CardinalityConstraintViolatedException.class, () -> constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms)); }
@Test void reconstructsEntityWithObjectPropertyWhereRangeDoesNotMatch() throws Exception { final Set<Axiom<?>> axioms = getAxiomsForD(); final Descriptor fieldDescriptor = new EntityDescriptor(); descriptor.addAttributeDescriptor(OWLClassD.getOwlClassAField(), fieldDescriptor); when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDescriptor)).thenReturn(null); final OWLClassD res = constructor.reconstructEntity(PK, mocks.forOwlClassD().entityType(), descriptor, axioms); assertNotNull(res); assertNull(res.getOwlClassA()); }
@Test void throwsExceptionWhenObjectPropertyCardinalityRestrictionIsNotMet() throws Exception { final Set<Axiom<?>> axioms = getAxiomsForD(); final OWLClassA entityA = new OWLClassA(); entityA.setUri(PK_TWO); final URI pkThree = URI.create("http: axioms.add(new AxiomImpl<>(NamedResource.create(PK), getClassDObjectPropertyAssertion(), new Value<>(NamedResource.create(pkThree)))); final OWLClassA entityThree = new OWLClassA(); entityThree.setUri(pkThree); final Descriptor fieldDescriptor = new EntityDescriptor(); descriptor.addAttributeDescriptor(OWLClassD.getOwlClassAField(), fieldDescriptor); when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDescriptor)).thenReturn(entityA); when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, pkThree, fieldDescriptor)) .thenReturn(entityThree); assertThrows(CardinalityConstraintViolatedException.class, () -> constructor.reconstructEntity(PK, mocks.forOwlClassD().entityType(), descriptor, axioms)); }
@Test void reconstructsEntityWithDataPropertiesOfBasicTypesAndStringIdentifier() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassM.getClassIri())); final Integer i = 117; final Long lng = 365L; final Double d = 3.14; final Date date = new Date(); axioms.addAll(createAxiomsForValues(true, i, lng, d, date, null)); final OWLClassM res = constructor.reconstructEntity(PK, mocks.forOwlClassM().entityType(), descriptor, axioms); assertEquals(true, res.getBooleanAttribute()); assertEquals(i, res.getIntAttribute()); assertEquals(lng, res.getLongAttribute()); assertEquals(d, res.getDoubleAttribute()); assertEquals(date, res.getDateAttribute()); }
@Test void reconstructsEntityWithEnumDataProperty() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassM.getClassIri())); final OWLClassM.Severity enumValue = OWLClassM.Severity.HIGH; axioms.addAll(createAxiomsForValues(null, null, null, null, null, enumValue)); final OWLClassM result = constructor .reconstructEntity(PK, mocks.forOwlClassM().entityType(), descriptor, axioms); assertNotNull(result); assertNotNull(result.getEnumAttribute()); assertEquals(enumValue, result.getEnumAttribute()); }
@Test void icsAreValidatedForAllFieldsOnEntityLoad() { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassL.getClassIri())); assertThrows(IntegrityConstraintViolatedException.class, () -> constructor.reconstructEntity(PK, mocks.forOwlClassL().entityType(), descriptor, axioms)); }
@Test void icValidationIsSkippedOnLoadBasedOnProperties() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassL.getClassIri())); final Map<String, String> props = Collections .singletonMap(JOPAPersistenceProperties.DISABLE_IC_VALIDATION_ON_LOAD, Boolean.TRUE.toString()); final Configuration conf = new Configuration(props); when(mapperMock.getConfiguration()).thenReturn(conf); final OWLClassL result = constructor .reconstructEntity(PK, mocks.forOwlClassL().entityType(), descriptor, axioms); assertNotNull(result); assertNull(result.getSingleA()); }
@Test void instanceReconstructionSkipsAxiomsForWhichNoAttributeCanBeFound() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassA.getClassIri())); axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField())); final Assertion unknown = Assertion.createAnnotationPropertyAssertion(Generators.createPropertyIdentifier(), false); axioms.add(new AxiomImpl<>(PK_RESOURCE, unknown, new Value<>("UnknownPropertyValue"))); final OWLClassA res = constructor.reconstructEntity(PK, mocks.forOwlClassA().entityType(), descriptor, axioms); assertNotNull(res); assertEquals(PK, res.getUri()); assertNotNull(res.getStringAttribute()); assertNull(res.getTypes()); }
@Test void reconstructsInstanceWithMappedSuperclass() throws Exception { final Set<Axiom<?>> 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()); }
@Test void reconstructsInstanceWithPluralAnnotationProperty() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getClassAssertionAxiomForType(OWLClassN.getClassIri())); final Assertion assertion = Assertion.createAnnotationPropertyAssertion(URI.create(Vocabulary.DC_SOURCE), false); axioms.add(new AxiomImpl<>(PK_RESOURCE, assertion, new Value<>(STRING_ATT))); final OWLClassN result = constructor.reconstructEntity(PK, mocks.forOwlClassN().entityType(), descriptor, axioms); assertNotNull(result); assertEquals(PK.toString(), result.getId()); assertEquals(Collections.singleton(STRING_ATT), result.getPluralAnnotation()); }