DetachedInstanceMerger { Object mergeChangesFromDetachedToManagedInstance(ObjectChangeSet changeSet, Descriptor descriptor) { assert changeSet != null; assert changeSet.getCloneObject() != null; final Object original = changeSet.getChangedObject(); assert original != null; for (ChangeRecord change : changeSet.getChanges()) { final FieldSpecification<?, ?> fs = change.getAttribute(); final Object origValue = EntityPropertiesUtils.getAttributeValue(fs, original); valueMerger .mergeValue(fs, original, origValue, change.getNewValue(), descriptor.getAttributeDescriptor(fs)); } return original; } DetachedInstanceMerger(UnitOfWorkImpl uow);   }
@Test public void mergeFromDetachedAssignsValuesOfNonManagedTypes() throws Exception { final OWLClassA original = Generators.generateOwlClassAInstance(); final OWLClassA detached = new OWLClassA(original.getUri()); detached.setTypes(original.getTypes()); final String updatedString = "updatedString"; detached.setStringAttribute(updatedString); final ObjectChangeSet changeSet = createChangeSet(original, detached); changeSet.addChangeRecord(new ChangeRecordImpl(metamodelMocks.forOwlClassA().stringAttribute(), updatedString)); final OWLClassA result = (OWLClassA) merger.mergeChangesFromDetachedToManagedInstance(changeSet, descriptor); assertEquals(updatedString, result.getStringAttribute()); }
@Test public void mergeFromDetachedSetsValueToNullWhenChangeValueIsNull() throws Exception { final OWLClassA original = Generators.generateOwlClassAInstance(); final OWLClassA detached = new OWLClassA(original.getUri()); detached.setTypes(original.getTypes()); final ObjectChangeSet changeSet = createChangeSet(original, detached); changeSet.addChangeRecord(new ChangeRecordImpl(metamodelMocks.forOwlClassA().stringAttribute(), null)); final OWLClassA result = (OWLClassA) merger.mergeChangesFromDetachedToManagedInstance(changeSet, descriptor); assertNull(result.getStringAttribute()); }
@Test public void mergeFromDetachedLoadsExistingInstanceCorrespondingToNewValue() throws Exception { final OWLClassD orig = new OWLClassD(Generators.createIndividualIdentifier()); final OWLClassA origRef = Generators.generateOwlClassAInstance(); orig.setOwlClassA(origRef); final OWLClassD clone = new OWLClassD(orig.getUri()); final OWLClassA newRef = Generators.generateOwlClassAInstance(); final OWLClassA newRefOrig = new OWLClassA(newRef.getUri()); newRefOrig.setStringAttribute(newRef.getStringAttribute()); newRefOrig.setTypes(new HashSet<>(newRef.getTypes())); final ObjectChangeSet chSet = createChangeSet(orig, clone); chSet.addChangeRecord(new ChangeRecordImpl(metamodelMocks.forOwlClassD().owlClassAAtt(), newRef)); when(uow.readObject(OWLClassA.class, newRef.getUri(), descriptor)).thenReturn(newRefOrig); when(uow.isEntityType(OWLClassA.class)).thenReturn(true); final OWLClassD result = (OWLClassD) merger.mergeChangesFromDetachedToManagedInstance(chSet, descriptor); verify(uow).readObject(OWLClassA.class, newRef.getUri(), descriptor); assertNotNull(result); assertSame(newRefOrig, result.getOwlClassA()); }