EntityConstructor { <T> void setFieldValue(T entity, Field field, Collection<Axiom<?>> axioms, EntityType<T> et, Descriptor entityDescriptor) throws IllegalAccessException { final FieldSpecification<? super T, ?> fieldSpec = MappingUtils.getFieldSpecification(field, et); if (axioms.isEmpty()) { validateIntegrityConstraints(entity, fieldSpec, et); return; } final FieldStrategy<? extends FieldSpecification<? super T, ?>, T> fs = FieldStrategy .createFieldStrategy(et, fieldSpec, entityDescriptor, mapper); axioms.forEach(fs::addValueFromAxiom); fs.buildInstanceFieldValue(entity); validateIntegrityConstraints(entity, fieldSpec, et); } EntityConstructor(ObjectOntologyMapperImpl mapper);   }
@Test void testSetFieldValue_DataProperty() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); axioms.add(getStringAttAssertionAxiom(OWLClassA.getStrAttField())); final OWLClassA entityA = new OWLClassA(); entityA.setUri(PK); assertNull(entityA.getStringAttribute()); constructor.setFieldValue(entityA, OWLClassA.getStrAttField(), axioms, mocks.forOwlClassA().entityType(), descriptor); assertNotNull(entityA.getStringAttribute()); assertEquals(STRING_ATT, entityA.getStringAttribute()); }
@Test void testSetFieldValue_ObjectProperty() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(); final Descriptor fieldDesc = mock(Descriptor.class); descriptor.addAttributeDescriptor(OWLClassD.getOwlClassAField(), fieldDesc); axioms.add(createObjectPropertyAxiomForD()); final OWLClassD entityD = new OWLClassD(); entityD.setUri(PK); assertNull(entityD.getOwlClassA()); final OWLClassA entityA = new OWLClassA(); entityA.setUri(PK_TWO); entityA.setStringAttribute(STRING_ATT); when(mapperMock.getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDesc)) .thenReturn(entityA); constructor.setFieldValue(entityD, OWLClassD.getOwlClassAField(), axioms, mocks.forOwlClassD().entityType(), descriptor); assertNotNull(entityD.getOwlClassA()); assertEquals(PK_TWO, entityD.getOwlClassA().getUri()); verify(mapperMock).getEntityFromCacheOrOntology(OWLClassA.class, PK_TWO, fieldDesc); }
@Test void testSetFieldValue_Types() throws Exception { final Set<Axiom<?>> axioms = new HashSet<>(getTypesAxiomsForOwlClassA()); final Descriptor fieldDesc = mock(Descriptor.class); descriptor.addAttributeDescriptor(OWLClassA.getTypesField(), fieldDesc); final OWLClassA entityA = new OWLClassA(); entityA.setUri(PK); assertNull(entityA.getTypes()); constructor.setFieldValue(entityA, OWLClassA.getTypesField(), axioms, mocks.forOwlClassA().entityType(), descriptor); assertNotNull(entityA.getTypes()); assertEquals(TYPES.size(), entityA.getTypes().size()); assertTrue(entityA.getTypes().containsAll(TYPES)); }
@Test void testSetFieldValue_Properties() throws Exception { final Map<String, Set<String>> props = Generators.generateStringProperties(); final Set<Axiom<?>> axioms = initAxiomsForProperties(props); final OWLClassB entityB = new OWLClassB(); entityB.setUri(PK); assertNull(entityB.getProperties()); constructor.setFieldValue(entityB, OWLClassB.getPropertiesField(), axioms, mocks.forOwlClassB().entityType(), descriptor); assertNotNull(entityB.getProperties()); assertEquals(props, entityB.getProperties()); }
@Test void testSetFieldValue_ObjectPropertySet() throws Exception { final Descriptor desc = mock(Descriptor.class); final Set<OWLClassA> set = initEntities(desc); descriptor.addAttributeDescriptor(OWLClassJ.getOwlClassAField(), desc); final Collection<Axiom<?>> axioms = initAxiomsForReferencedSet(set); final OWLClassJ entityJ = new OWLClassJ(); entityJ.setUri(PK); assertNull(entityJ.getOwlClassA()); constructor.setFieldValue(entityJ, OWLClassJ.getOwlClassAField(), axioms, mocks.forOwlClassJ().entityType(), descriptor); assertNotNull(entityJ.getOwlClassA()); assertEquals(set.size(), entityJ.getOwlClassA().size()); assertTrue(areEqual(set, entityJ.getOwlClassA())); }
@Test void icsAreValidatedOnLazilyLoadedFieldFetch() throws Exception { final Set<Axiom<?>> fieldAxiom = initInvalidFieldValuesForICValidation(); final OWLClassL instance = new OWLClassL(); assertThrows(IntegrityConstraintViolatedException.class, () -> constructor .setFieldValue(instance, OWLClassL.getReferencedListField(), fieldAxiom, mocks.forOwlClassL().entityType(), descriptor)); }
@Test void icsAreNotValidatedOnLazilyLoadedFetchWhenLoadingICValidationIsDisabled() throws Exception { final Set<Axiom<?>> fieldAxiom = initInvalidFieldValuesForICValidation(); 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 instance = new OWLClassL(); constructor.setFieldValue(instance, OWLClassL.getReferencedListField(), fieldAxiom, mocks.forOwlClassL().entityType(), descriptor); }
@Test void setFieldValueSetsNothingWhenAxiomsAreEmpty() throws Exception { final OWLClassA entity = new OWLClassA(Generators.createIndividualIdentifier()); assertNull(entity.getTypes()); constructor.setFieldValue(entity, OWLClassA.getTypesField(), Collections.emptyList(), mocks.forOwlClassA().entityType(), descriptor); assertNull(entity.getTypes()); }
@Test void setFieldValueValidatesIntegrityConstraintsAlsoForEmptyAxiomCollection() { final OWLClassL instance = new OWLClassL(Generators.createIndividualIdentifier()); assertThrows(IntegrityConstraintViolatedException.class, () -> constructor .setFieldValue(instance, OWLClassL.getSingleAField(), Collections.emptyList(), mocks.forOwlClassL().entityType(), descriptor)); }