@Test
public void testGetOwnershipState() throws Exception {

    DistributedEntityOwnershipService service = spy(DistributedEntityOwnershipService.start(
        dataStore.getActorUtils(), EntityOwnerSelectionStrategyConfig.newBuilder().build()));
    final Shard mockShard = Mockito.mock(Shard.class);
    ShardDataTree shardDataTree = new ShardDataTree(mockShard, EOSTestUtils.SCHEMA_CONTEXT, TreeType.OPERATIONAL);
    when(service.getLocalEntityOwnershipShardDataTree()).thenReturn(shardDataTree.getDataTree());
    DOMEntity entity1 = new DOMEntity(ENTITY_TYPE, "one");
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity1.getIdentifier(), "member-1"),
            shardDataTree);
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
            entityEntryWithOwner(entity1.getIdentifier(), "member-1"))), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.IS_OWNER);
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE,
            entity1.getIdentifier(), "member-2"), shardDataTree);
    writeNode(entityPath(entity1.getType(), entity1.getIdentifier()),
            entityEntryWithOwner(entity1.getIdentifier(), "member-2"), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.OWNED_BY_OTHER);
    writeNode(entityPath(entity1.getType(), entity1.getIdentifier()), entityEntryWithOwner(entity1.getIdentifier(),
            ""), shardDataTree);
    verifyGetOwnershipState(service, entity1, EntityOwnershipState.NO_OWNER);
    DOMEntity entity2 = new DOMEntity(ENTITY_TYPE, "two");
    Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
    assertFalse("getOwnershipState present", state.isPresent());
    writeNode(ENTITY_OWNERS_PATH, entityOwnersWithCandidate(ENTITY_TYPE, entity2.getIdentifier(), "member-1"),
            shardDataTree);
    writeNode(entityPath(entity2.getType(), entity2.getIdentifier()), ImmutableNodes.mapEntry(ENTITY_QNAME,
            ENTITY_ID_QNAME, entity2.getIdentifier()), shardDataTree);
    verifyGetOwnershipState(service, entity2, EntityOwnershipState.NO_OWNER);
    deleteNode(candidatePath(entityPath(entity2.getType(), entity2.getIdentifier()), "member-1"), shardDataTree);
    Optional<EntityOwnershipState> state2 = service.getOwnershipState(entity2);
    assertFalse("getOwnershipState present", state2.isPresent());
    service.close();
}