PluralAnnotationPropertyStrategy extends PluralDataPropertyStrategy<X> { @Override void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) { final Object value = extractFieldValueFromInstance(instance); assert value instanceof Collection || value == null; final Collection<?> valueCollection = (Collection<?>) value; if (valueCollection == null || valueCollection.isEmpty()) { valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeContext()); } else { final Function<Object, Collection<Value<?>>> mapper; if (IdentifierTransformer.isValidIdentifierType(elementType) && !elementType .isAssignableFrom(String.class)) { mapper = v -> Collections .singleton(new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(v)))); } else { mapper = v -> v instanceof MultilingualString ? SingularMultilingualStringFieldStrategy.translationsToLangStrings( (MultilingualString) v) : Collections.singleton(new Value<>(toAxiomValue(v))); } final Set<Value<?>> assertionValues = valueCollection.stream().filter(Objects::nonNull).map(mapper).flatMap(Collection::stream) .collect(Collectors.toSet()); valueBuilder.addValues(createAssertion(), assertionValues, getAttributeContext()); } } PluralAnnotationPropertyStrategy(EntityType<X> et, AbstractPluralAttribute<? super X, ?, ?> att,
                                     Descriptor attributeDescriptor, EntityMappingHelper mapper);   }
@Test void buildAxiomValuesFromInstanceAddNullValueForNullCollection() throws Exception { final PluralAnnotationPropertyStrategy<OWLClassN> sut = strategyForN(); final OWLClassN instance = new OWLClassN(); sut.buildAxiomValuesFromInstance(instance, gatherer); verifyValuesForNPluralAnnotation(Collections.singleton(Value.nullValue())); }
@Test void buildAxiomValuesFromInstanceAddsNullValueForEmptyCollection() throws Exception { final PluralAnnotationPropertyStrategy<OWLClassN> sut = strategyForN(); final OWLClassN instance = new OWLClassN(); instance.setPluralAnnotation(Collections.emptySet()); sut.buildAxiomValuesFromInstance(instance, gatherer); verifyValuesForNPluralAnnotation(Collections.singleton(Value.nullValue())); }
@Test void buildAxiomValuesFromInstanceAddsStringValues() throws Exception { final PluralAnnotationPropertyStrategy<OWLClassN> sut = strategyForN(); final OWLClassN instance = new OWLClassN(); final Set<String> values = IntStream.range(0, 5).mapToObj(i -> "Value" + i).collect(Collectors.toSet()); instance.setPluralAnnotation(values); sut.buildAxiomValuesFromInstance(instance, gatherer); verifyValuesForNPluralAnnotation(values.stream().map(Value::new).collect(Collectors.toSet())); }
@Test void buildAxiomValuesFromInstanceAddsUrisAsNamedResources() throws Exception { final PluralAnnotationPropertyStrategy<WithPluralUriAnnotations> sut = createStrategyWithPluralAnnotations( WithPluralUriAnnotations.class, URI.class, null); final Set<URI> values = IntStream.range(0, 5).mapToObj(i -> Generators.createIndividualIdentifier()).collect( Collectors.toSet()); final WithPluralUriAnnotations instance = new WithPluralUriAnnotations(); instance.sources = values; sut.buildAxiomValuesFromInstance(instance, gatherer); verifyValuesForNPluralAnnotation( values.stream().map(v -> new Value<>(NamedResource.create(v))).collect(Collectors.toSet())); }
@Test void buildAxiomValuesFromInstanceConvertsUriToNamedResourceForAttributeOfTypeObject() throws Exception { final EntityType<ClassWithObjectAnnotation> et = mock(EntityType.class); final SetAttributeImpl<ClassWithObjectAnnotation, Object> att = objectAnnotationAttribute(et); final PluralAnnotationPropertyStrategy<ClassWithObjectAnnotation> sut = new PluralAnnotationPropertyStrategy<>( et, att, descriptor, mapperMock); final ClassWithObjectAnnotation instance = new ClassWithObjectAnnotation(); instance.pluralAnnotation = Collections.singleton(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.pluralAnnotation.iterator().next()), values.get(0).getValue()); }
@Test void buildAxiomValuesFromInstanceConvertsMultilingualStringTranslationsToLangStringValues() throws Exception { final EntityType<ClassWithObjectAnnotation> et = mock(EntityType.class); final SetAttributeImpl<ClassWithObjectAnnotation, Object> att = objectAnnotationAttribute(et); final PluralAnnotationPropertyStrategy<ClassWithObjectAnnotation> sut = new PluralAnnotationPropertyStrategy<>( et, att, descriptor, mapperMock); final ClassWithObjectAnnotation instance = new ClassWithObjectAnnotation(); final MultilingualString mls = MultilingualString.create("test", "en"); mls.set("cs", "test"); final MultilingualString mlsTwo = MultilingualString.create("building", "en"); instance.pluralAnnotation = new HashSet<>(Arrays.asList(mls, mlsTwo)); 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))))); mlsTwo.getValue() .forEach((lang, value) -> assertThat(values, hasItem(new Value<>(new LangString(value, lang))))); }