DistributedEntityOwnershipService implements DOMEntityOwnershipService, AutoCloseable { @Override public Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity) { requireNonNull(forEntity, "forEntity cannot be null"); DataTree dataTree = getLocalEntityOwnershipShardDataTree(); if (dataTree == null) { return Optional.empty(); } Optional<NormalizedNode<?, ?>> entityNode = dataTree.takeSnapshot().readNode( entityPath(forEntity.getType(), forEntity.getIdentifier())); if (!entityNode.isPresent()) { return Optional.empty(); } final MapEntryNode entity = (MapEntryNode) entityNode.get(); final Optional<DataContainerChild<? extends PathArgument, ?>> optionalCandidates = entity.getChild(CANDIDATE_NODE_ID); final boolean hasCandidates = optionalCandidates.isPresent() && ((MapNode) optionalCandidates.get()).getValue().size() > 0; if (!hasCandidates) { return Optional.empty(); } MemberName localMemberName = context.getCurrentMemberName(); Optional<DataContainerChild<? extends PathArgument, ?>> ownerLeaf = entity.getChild(ENTITY_OWNER_NODE_ID); String owner = ownerLeaf.isPresent() ? ownerLeaf.get().getValue().toString() : null; boolean hasOwner = !Strings.isNullOrEmpty(owner); boolean isOwner = hasOwner && localMemberName.getName().equals(owner); return Optional.of(EntityOwnershipState.from(isOwner, hasOwner)); } DistributedEntityOwnershipService(final ActorUtils context); static DistributedEntityOwnershipService start(final ActorUtils context,
            final EntityOwnerSelectionStrategyConfig strategyConfig); @Override DOMEntityOwnershipCandidateRegistration registerCandidate(final DOMEntity entity); @Override DOMEntityOwnershipListenerRegistration registerListener(final String entityType,
            final DOMEntityOwnershipListener listener); @Override Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity); @Override boolean isCandidateRegistered(final DOMEntity entity); @Override void close();  }
@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(); }