SingularAnnotationPropertyStrategy extends SingularDataPropertyStrategy<X> { @Override void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) { final Object value = extractFieldValueFromInstance(instance); if (value == null) { valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeContext()); return; } if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType()) && !attribute.getJavaType().isAssignableFrom(String.class)) { valueBuilder.addValue(createAssertion(), new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(value))), getAttributeContext()); } else if (value instanceof MultilingualString) { valueBuilder.addValues(createAssertion(), SingularMultilingualStringFieldStrategy.translationsToLangStrings((MultilingualString) value), getAttributeContext()); } else { valueBuilder.addValue(createAssertion(), new Value<>(toAxiomValue(value)), getAttributeContext()); } } SingularAnnotationPropertyStrategy(EntityType<X> et, AbstractAttribute<? super X, ?> att, Descriptor descriptor,
                                       EntityMappingHelper mapper);   }
@Test void buildAxiomsExtractsStringValueOfAnnotationPropertyField() throws Exception { final String str = "stringValue"; final SingularAnnotationPropertyStrategy<OWLClassN> strategy = forN(mocks.forOwlClassN().annotationAttribute()); final OWLClassN n = new OWLClassN(); n.setId(PK.toString()); n.setAnnotationProperty(str); final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), null); strategy.buildAxiomValuesFromInstance(n, builder); final Value<?> val = getValueForAssertion(builder, annotationForN()); assertEquals(str, val.getValue()); }
@Test void buildAxiomsExtractsPlainIdentifiersAttributeValuesAsNamedResources() throws Exception { final URI uri = URI.create("http: final SingularAnnotationPropertyStrategy<OWLClassN> strategy = forN( mocks.forOwlClassN().annotationUriAttribute()); final OWLClassN n = new OWLClassN(); n.setId(PK.toString()); n.setAnnotationUri(uri); final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), null); strategy.buildAxiomValuesFromInstance(n, builder); final Value<?> val = getValueForAssertion(builder, annotationWithUriForN()); assertTrue(val.getValue() instanceof NamedResource); assertEquals(uri, ((NamedResource) val.getValue()).getIdentifier()); }
@Test void buildAxiomsUsesNullValueWhenExtractedFieldValueIsNull() throws Exception { final SingularAnnotationPropertyStrategy<OWLClassN> strategy = forN( mocks.forOwlClassN().annotationUriAttribute()); final OWLClassN n = new OWLClassN(); n.setId(PK.toString()); final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), null); strategy.buildAxiomValuesFromInstance(n, builder); final Value<?> val = getValueForAssertion(builder, annotationWithUriForN()); assertSame(Value.nullValue(), val); }
@Test void buildAxiomValuesFromInstanceConvertsUriToNamedResourceForAttributeOfTypeObject() 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 ClassWithObjectAnnotation instance = new ClassWithObjectAnnotation(); instance.singularAnnotation = Generators.createIndividualIdentifier(); final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), null); sut.buildAxiomValuesFromInstance(instance, builder); final AxiomValueDescriptor valueDescriptor = OOMTestUtils.getAxiomValueDescriptor(builder); assertEquals(1, valueDescriptor.getAssertions().size()); final List<Value<?>> values = valueDescriptor .getAssertionValues(valueDescriptor.getAssertions().iterator().next()); assertEquals(1, values.size()); assertEquals(NamedResource.create((URI) instance.singularAnnotation), values.get(0).getValue()); }
@Test void buildAxiomValuesFromInstanceConvertsMultilingualStringTranslationsToLangStringValues() 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 ClassWithObjectAnnotation instance = new ClassWithObjectAnnotation(); final MultilingualString mls = MultilingualString.create("test", "en"); mls.set("cs", "test"); instance.singularAnnotation = mls; final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), null); sut.buildAxiomValuesFromInstance(instance, builder); final AxiomValueDescriptor valueDescriptor = OOMTestUtils.getAxiomValueDescriptor(builder); assertEquals(1, valueDescriptor.getAssertions().size()); final List<Value<?>> values = valueDescriptor .getAssertionValues(valueDescriptor.getAssertions().iterator().next()); mls.getValue().forEach((lang, value) -> assertThat(values, hasItem(new Value<>(new LangString(value, lang))))); }