@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));
    // Write local member as owner for entity 1
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
    assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
    // Add remote member 1 as candidate for entity 1 - ownershipStatistics support should not get notified
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, ENTITY_ID1, REMOTE_MEMBER_NAME1));
    assertStatistics(ownershipStatistics.all(), LOCAL_MEMBER_NAME, 1L);
    // Change owner to remote member 1 for entity 1
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME1));
    Map<String, Map<String, Long>> statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
    // Change owner to remote member 2 for entity 1
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, REMOTE_MEMBER_NAME2));
    statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME2, 1L);
    // Clear the owner for entity 1
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, ""));
    statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
    // Change owner to the local member for entity 1
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID1), entityEntryWithOwner(ENTITY_ID1, LOCAL_MEMBER_NAME));
    statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
    // Change owner to remote member 1 for entity 2
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, REMOTE_MEMBER_NAME1));
    statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 1L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
    // Change owner to the local member for entity 2
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
    statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
    // Write local member owner for entity 2 again - expect no change
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, LOCAL_MEMBER_NAME));
    statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 2L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
    // Clear the owner for entity 2
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, ""));
    statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
    // Clear the owner for entity 2 again - expect no change
    writeNode(entityPath(ENTITY_TYPE, ENTITY_ID2), entityEntryWithOwner(ENTITY_ID2, ""));
    statistics = ownershipStatistics.all();
    assertStatistics(statistics, LOCAL_MEMBER_NAME, 1L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME1, 0L);
    assertStatistics(statistics, REMOTE_MEMBER_NAME2, 0L);
}