SingularObjectPropertyStrategy extends FieldStrategy<Attribute<? super X, ?>, X> { @Override void addValueFromAxiom(Axiom<?> ax) { assert ax.getValue().getValue() instanceof NamedResource; final NamedResource valueIdentifier = (NamedResource) ax.getValue().getValue(); final Object newValue; if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType())) { newValue = IdentifierTransformer .transformToIdentifier(valueIdentifier.getIdentifier(), attribute.getJavaType()); } else { newValue = mapper.getEntityFromCacheOrOntology(attribute.getJavaType(), valueIdentifier.getIdentifier(), entityDescriptor.getAttributeDescriptor(attribute)); if (newValue == null) { LOG.trace("Value of axiom {} could not be loaded as entity filling attribute {}.", ax, attribute); return; } } verifyCardinality(ax.getSubject()); this.value = newValue; } SingularObjectPropertyStrategy(EntityType<X> et, Attribute<? super X, ?> att,
                                   Descriptor descriptor, EntityMappingHelper mapper);   }
@Test void addValueThrowsCardinalityViolationWhenValueIsAlreadyPresent() { final FieldStrategy<? extends FieldSpecification<? super OWLClassD, ?>, OWLClassD> sut = strategy(metamodelMocks.forOwlClassD().entityType(), metamodelMocks.forOwlClassD().owlClassAAtt()); final OWLClassA existing = Generators.generateOwlClassAInstance(); when(mapperMock.getEntityFromCacheOrOntology(eq(OWLClassA.class), eq(VALUE), any())).thenReturn(existing); final Assertion assertion = Assertion.createObjectPropertyAssertion(URI.create(Vocabulary.P_HAS_A), false); sut.addValueFromAxiom( new AxiomImpl<>(NamedResource.create(IDENTIFIER), assertion, new Value<>(NamedResource.create(VALUE)))); final OWLClassA another = Generators.generateOwlClassAInstance(); when(mapperMock.getEntityFromCacheOrOntology(eq(OWLClassA.class), eq(another.getUri()), any())) .thenReturn(another); final Axiom<NamedResource> violationAxiom = new AxiomImpl<>(NamedResource.create(IDENTIFIER), assertion, new Value<>(NamedResource.create(another.getUri()))); assertThrows(CardinalityConstraintViolatedException.class, () -> sut.addValueFromAxiom(violationAxiom)); }
@Test void addValueGetsAttributeDescriptorFromEntityDescriptorForLoading() throws Exception { final EntityDescriptor aDescriptor = new EntityDescriptor(Generators.createIndividualIdentifier()); descriptor.addAttributeDescriptor(OWLClassD.getOwlClassAField(), aDescriptor); final FieldStrategy<? extends FieldSpecification<? super OWLClassD, ?>, OWLClassD> sut = strategy(metamodelMocks.forOwlClassD().entityType(), metamodelMocks.forOwlClassD().owlClassAAtt()); final OWLClassA existing = Generators.generateOwlClassAInstance(); when(mapperMock.getEntityFromCacheOrOntology(eq(OWLClassA.class), eq(VALUE), any())).thenReturn(existing); final Assertion assertion = Assertion.createObjectPropertyAssertion(URI.create(Vocabulary.P_HAS_A), false); sut.addValueFromAxiom( new AxiomImpl<>(NamedResource.create(IDENTIFIER), assertion, new Value<>(NamedResource.create(VALUE)))); verify(mapperMock).getEntityFromCacheOrOntology(OWLClassA.class, VALUE, aDescriptor); }