EntityOwnerChangeListener extends AbstractEntityOwnerChangeListener { @Override public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) { for (DataTreeCandidate change: changes) { DataTreeCandidateNode changeRoot = change.getRootNode(); LeafNode<?> ownerLeaf = (LeafNode<?>) changeRoot.getDataAfter().get(); LOG.debug("{}: Entity node changed: {}, {}", logId(), changeRoot.getModificationType(), change.getRootPath()); String newOwner = extractOwner(ownerLeaf); String origOwner = null; Optional<NormalizedNode<?, ?>> dataBefore = changeRoot.getDataBefore(); if (dataBefore.isPresent()) { origOwner = extractOwner((LeafNode<?>) changeRoot.getDataBefore().get()); } LOG.debug("{}: New owner: {}, Original owner: {}", logId(), newOwner, origOwner); if (!Objects.equals(origOwner, newOwner)) { boolean isOwner = localMemberName.equals(newOwner); boolean wasOwner = localMemberName.equals(origOwner); boolean hasOwner = !Strings.isNullOrEmpty(newOwner); DOMEntity entity = createEntity(change.getRootPath()); LOG.debug( "{}: Calling notifyEntityOwnershipListeners: entity: {}, wasOwner: {}, isOwner: {}, hasOwner: {}", logId(), entity, wasOwner, isOwner, hasOwner); publisher.notifyEntityOwnershipListeners(entity, wasOwner, isOwner, hasOwner); } } } EntityOwnerChangeListener(final MemberName localMemberName, final EntityOwnershipChangePublisher publisher); @Override void onDataTreeChanged(final Collection<DataTreeCandidate> changes);  }
@Test public void testOnDataTreeChanged() throws Exception { writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, LOCAL_MEMBER_NAME)); writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID2, LOCAL_MEMBER_NAME)); verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(DOMEntity.class), anyBoolean(), anyBoolean(), anyBoolean()); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME)); verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true, true); reset(mockListenerSupport); writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1)); verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(DOMEntity.class), anyBoolean(), anyBoolean(), anyBoolean()); reset(mockListenerSupport); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1)); verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, true, false, true); reset(mockListenerSupport); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2)); verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, false, true); reset(mockListenerSupport); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, "")); verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, false, false); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME)); verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY1, false, true, true); reset(mockListenerSupport); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1)); verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, false, false, true); reset(mockListenerSupport); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME)); verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, false, true, true); reset(mockListenerSupport); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME)); verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(DOMEntity.class), anyBoolean(), anyBoolean(), anyBoolean()); reset(mockListenerSupport); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null)); verify(mockListenerSupport).notifyEntityOwnershipListeners(ENTITY2, true, false, false); reset(mockListenerSupport); writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, null)); verify(mockListenerSupport, never()).notifyEntityOwnershipListeners(any(DOMEntity.class), anyBoolean(), anyBoolean(), anyBoolean()); }