TypesFieldStrategy extends FieldStrategy<TypesSpecification<? super X, ?>, X> { @Override void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) { final Object val = extractFieldValueFromInstance(instance); final X original = mapper.getOriginalInstance(instance); if (val == null) { if (original == null) { return; } final Set<?> origTypes = (Set<?>) extractFieldValueFromInstance(original); if (origTypes == null || origTypes.isEmpty()) { return; } valueBuilder.removeTypes(prepareTypes(origTypes), getAttributeContext()); } else { assert val instanceof Set; final Set<?> types = (Set<?>) val; if (original == null) { valueBuilder.addTypes(prepareTypes(types), getAttributeContext()); } else { Set<?> origTypes = (Set<?>) extractFieldValueFromInstance(original); if (origTypes == null) { origTypes = Collections.emptySet(); } extractTypesToAdd(valueBuilder, types, origTypes); extractTypesToRemove(valueBuilder, types, origTypes); } } } TypesFieldStrategy(EntityType<X> et, TypesSpecification<? super X, ?> att, Descriptor descriptor,
                       EntityMappingHelper mapper);   }
@Test public void extractsTypesForPersist() throws Exception { final TypesFieldStrategy<OWLClassA> strategy = strategy(mocks.forOwlClassA().entityType(), mocks.forOwlClassA().typesSpec()); final int count = 5; entityA.setTypes(Generators.generateTypes(count)); when(mapperMock.getOriginalInstance(entityA)).thenReturn(null); strategy.buildAxiomValuesFromInstance(entityA, gatherer); final Set<URI> toAdd = OOMTestUtils.getTypesToAdd(gatherer); assertEquals(count, toAdd.size()); verifyCollectionsAreEqual(entityA.getTypes(), toAdd); assertNull(OOMTestUtils.getTypesToRemove(gatherer)); }
@Test public void extractsAddedTypes() throws Exception { final TypesFieldStrategy<OWLClassA> strategy = strategy(mocks.forOwlClassA().entityType(), mocks.forOwlClassA().typesSpec()); final int count = 5; entityA.setTypes(Generators.generateTypes(count)); final OWLClassA original = createOriginal(); when(mapperMock.getOriginalInstance(entityA)).thenReturn(original); final Set<String> addedTypes = new HashSet<>(); addedTypes.add("http: addedTypes.add("http: entityA.getTypes().addAll(addedTypes); strategy.buildAxiomValuesFromInstance(entityA, gatherer); final Set<URI> toAdd = OOMTestUtils.getTypesToAdd(gatherer); assertEquals(addedTypes.size(), toAdd.size()); verifyCollectionsAreEqual(addedTypes, toAdd); assertTrue(OOMTestUtils.getTypesToRemove(gatherer).isEmpty()); }
@Test public void extractsRemovedTypes() throws Exception { final TypesFieldStrategy<OWLClassA> strategy = strategy(mocks.forOwlClassA().entityType(), mocks.forOwlClassA().typesSpec()); final int count = 5; entityA.setTypes(Generators.generateTypes(count)); final OWLClassA original = createOriginal(); when(mapperMock.getOriginalInstance(entityA)).thenReturn(original); final Set<String> removedTypes = removeRandomTypes(); assertFalse(removedTypes.isEmpty()); assertFalse(entityA.getTypes().isEmpty()); strategy.buildAxiomValuesFromInstance(entityA, gatherer); final Set<URI> toRemove = OOMTestUtils.getTypesToRemove(gatherer); assertFalse(toRemove.isEmpty()); verifyCollectionsAreEqual(removedTypes, toRemove); assertTrue(OOMTestUtils.getTypesToAdd(gatherer).isEmpty()); }
@Test public void extractsRemovedTypesWhenValueIsNull() throws Exception { final TypesFieldStrategy<OWLClassA> strategy = strategy(mocks.forOwlClassA().entityType(), mocks.forOwlClassA().typesSpec()); final int count = 5; entityA.setTypes(Generators.generateTypes(count)); final OWLClassA original = createOriginal(); when(mapperMock.getOriginalInstance(entityA)).thenReturn(original); entityA.setTypes(null); strategy.buildAxiomValuesFromInstance(entityA, gatherer); final Set<URI> toRemove = OOMTestUtils.getTypesToRemove(gatherer); assertFalse(toRemove.isEmpty()); verifyCollectionsAreEqual(original.getTypes(), toRemove); assertNull(OOMTestUtils.getTypesToAdd(gatherer)); }
@Test public void extractsAddedAndRemovedTypes() throws Exception { final TypesFieldStrategy<OWLClassA> strategy = strategy(mocks.forOwlClassA().entityType(), mocks.forOwlClassA().typesSpec()); final int count = 5; entityA.setTypes(Generators.generateTypes(count)); final OWLClassA original = createOriginal(); when(mapperMock.getOriginalInstance(entityA)).thenReturn(original); final Set<String> removedTypes = removeRandomTypes(); final Set<String> addedTypes = new HashSet<>(); addedTypes.add("http: addedTypes.add("http: entityA.getTypes().addAll(addedTypes); strategy.buildAxiomValuesFromInstance(entityA, gatherer); final Set<URI> toRemove = OOMTestUtils.getTypesToRemove(gatherer); assertFalse(toRemove.isEmpty()); verifyCollectionsAreEqual(removedTypes, toRemove); final Set<URI> toAdd = OOMTestUtils.getTypesToAdd(gatherer); assertEquals(addedTypes.size(), toAdd.size()); verifyCollectionsAreEqual(addedTypes, toAdd); }
@Test public void extractsNothingWhenThereAreNoTypesAndNoOriginal() throws Exception { final TypesFieldStrategy<OWLClassA> strategy = strategy(mocks.forOwlClassA().entityType(), mocks.forOwlClassA().typesSpec()); when(mapperMock.getOriginalInstance(entityA)).thenReturn(null); strategy.buildAxiomValuesFromInstance(entityA, gatherer); assertNull(OOMTestUtils.getTypesToRemove(gatherer)); assertNull(OOMTestUtils.getTypesToAdd(gatherer)); }
@Test public void extractsNothingWhenOriginalTypesAndCurrentTypesAreNull() throws Exception { final TypesFieldStrategy<OWLClassA> strategy = strategy(mocks.forOwlClassA().entityType(), mocks.forOwlClassA().typesSpec()); final OWLClassA original = createOriginal(); when(mapperMock.getOriginalInstance(entityA)).thenReturn(original); strategy.buildAxiomValuesFromInstance(entityA, gatherer); assertNull(OOMTestUtils.getTypesToRemove(gatherer)); assertNull(OOMTestUtils.getTypesToAdd(gatherer)); }
@Test public void extractsTypesToAddWhenOriginalTypesAreNull() throws Exception { final TypesFieldStrategy<OWLClassA> strategy = strategy(mocks.forOwlClassA().entityType(), mocks.forOwlClassA().typesSpec()); final int count = 5; entityA.setTypes(Generators.generateTypes(count)); final OWLClassA original = createOriginal(); original.setTypes(null); when(mapperMock.getOriginalInstance(entityA)).thenReturn(original); strategy.buildAxiomValuesFromInstance(entityA, gatherer); final Set<URI> toAdd = OOMTestUtils.getTypesToAdd(gatherer); assertEquals(count, toAdd.size()); }
@Test public void extractsTypesForPersistFromUriTypes() throws Exception { final TypesFieldStrategy<OWLClassP> strategy = strategy(mocks.forOwlClassP().entityType(), mocks.forOwlClassP().types()); final OWLClassP p = new OWLClassP(); p.setTypes(Generators.generateUriTypes(Generators.DEFAULT_SIZE)); strategy.buildAxiomValuesFromInstance(p, gatherer); final Set<URI> toPersist = OOMTestUtils.getTypesToAdd(gatherer); assertEquals(p.getTypes(), toPersist); }