SingularAnnotationPropertyStrategy extends SingularDataPropertyStrategy<X> { @Override void addValueFromAxiom(Axiom<?> ax) { final Object val = ax.getValue().getValue(); if (!isValidRange(val)) { return; } verifyCardinalityConstraint(ax.getSubject()); if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType())) { this.value = IdentifierTransformer .transformToIdentifier(ToLexicalFormConverter.INSTANCE.convertToAttribute(val), attribute.getJavaType()); } else { this.value = toAttributeValue(val); } } SingularAnnotationPropertyStrategy(EntityType<X> et, AbstractAttribute<? super X, ?> att, Descriptor descriptor,
                                       EntityMappingHelper mapper);   }
@Test void addAxiomValueAddsStringAnnotationValue() { final String str = "stringValue"; final SingularAnnotationPropertyStrategy<OWLClassN> strategy = forN(mocks.forOwlClassN().annotationAttribute()); final Axiom<String> ax = new AxiomImpl<>(NamedResource.create(PK), annotationForN(), new Value<>(str)); strategy.addValueFromAxiom(ax); final OWLClassN n = new OWLClassN(); strategy.buildInstanceFieldValue(n); assertEquals(str, n.getAnnotationProperty()); }
@Test void addAxiomValueSkipsAxiomWhoseValueDoesNotMatchTargetFieldType() { final SingularAnnotationPropertyStrategy<OWLClassN> strategy = forN(mocks.forOwlClassN().annotationAttribute()); final Axiom<Integer> ax = new AxiomImpl<>(NamedResource.create(PK), annotationForN(), new Value<>(117)); strategy.addValueFromAxiom(ax); final OWLClassN n = new OWLClassN(); strategy.buildInstanceFieldValue(n); assertNull(n.getAnnotationProperty()); }
@Test void addAxiomValueAddsPlainIdentifierValueOfAnnotationProperty() { final URI value = URI.create("http: final SingularAnnotationPropertyStrategy<OWLClassN> strategy = forN( mocks.forOwlClassN().annotationUriAttribute()); final Axiom<NamedResource> ax = new AxiomImpl<>(NamedResource.create(PK), annotationWithUriForN(), new Value<>(NamedResource.create(value))); strategy.addValueFromAxiom(ax); final OWLClassN n = new OWLClassN(); strategy.buildInstanceFieldValue(n); assertEquals(value, n.getAnnotationUri()); }
@Test void addAxiomValueTransformsValueToLexicalForm() { final SingularAnnotationPropertyStrategy<OWLClassM> sut = new SingularAnnotationPropertyStrategy<>( mocks.forOwlClassM().entityType(), mocks.forOwlClassM().lexicalFormAttribute(), descriptor, mapperMock); final Integer value = 117; final Axiom<Integer> axiom = new AxiomImpl<>(NamedResource.create(PK), lexicalFormAssertion(), new Value<>(value)); sut.addValueFromAxiom(axiom); final OWLClassM result = new OWLClassM(); result.setKey(PK.toString()); sut.buildInstanceFieldValue(result); assertEquals(value.toString(), result.getLexicalForm()); }
@Test void addAxiomValueAcceptsNamedResourceForLexicalFormAttribute() { final SingularAnnotationPropertyStrategy<OWLClassM> sut = new SingularAnnotationPropertyStrategy<>( mocks.forOwlClassM().entityType(), mocks.forOwlClassM().lexicalFormAttribute(), descriptor, mapperMock); final URI identifier = Generators.createIndividualIdentifier(); final Axiom<NamedResource> axiom = new AxiomImpl<>(NamedResource.create(PK), lexicalFormAssertion(), new Value<>(NamedResource.create(identifier))); sut.addValueFromAxiom(axiom); final OWLClassM result = new OWLClassM(); result.setKey(PK.toString()); sut.buildInstanceFieldValue(result); assertEquals(identifier.toString(), result.getLexicalForm()); }
@Test void addAxiomValueConvertsNamedResourceToUriForAttributeOfTypeObject() throws Exception { final EntityType<ClassWithObjectAnnotation> et = mock(EntityType.class); final SingularAttributeImpl<ClassWithObjectAnnotation, Object> att = objectAnnotationAttribute(et); final SingularAnnotationPropertyStrategy<ClassWithObjectAnnotation> sut = new SingularAnnotationPropertyStrategy<>( et, att, descriptor, mapperMock); final URI identifier = Generators.createIndividualIdentifier(); final Axiom<NamedResource> axiom = new AxiomImpl<>(NamedResource.create(PK), annotationWithUriForN(), new Value<>(NamedResource.create(identifier))); sut.addValueFromAxiom(axiom); final ClassWithObjectAnnotation instance = new ClassWithObjectAnnotation(); sut.buildInstanceFieldValue(instance); assertEquals(identifier, instance.singularAnnotation); }
@Test void addAxiomValueConvertsLangStringToMultilingualStringWhenTargetIsObjectAndMultilingualStringsArePreferred() throws Exception { final EntityType<ClassWithObjectAnnotation> et = mock(EntityType.class); final SingularAttributeImpl<ClassWithObjectAnnotation, Object> att = objectAnnotationAttribute(et); when(att.getConverter()).thenReturn(new ObjectConverter(true)); final SingularAnnotationPropertyStrategy<ClassWithObjectAnnotation> sut = new SingularAnnotationPropertyStrategy<>( et, att, descriptor, mapperMock); final LangString langString = new LangString("test", "en"); final Axiom<LangString> axiom = new AxiomImpl<>(NamedResource.create(PK), annotationWithUriForN(), new Value<>(langString)); sut.addValueFromAxiom(axiom); final ClassWithObjectAnnotation instance = new ClassWithObjectAnnotation(); sut.buildInstanceFieldValue(instance); assertEquals(MultilingualString.create(langString.getValue(), langString.getLanguage().get()), instance.singularAnnotation); }