+ @Test
+ public void testGetOwnershipState() throws Exception {
+ final TestShardPropsCreator shardPropsCreator = new TestShardPropsCreator();
+ DistributedEntityOwnershipService service = new DistributedEntityOwnershipService(dataStore) {
+ @Override
+ protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
+ return shardPropsCreator;
+ }
+ };
+
+ service.start();
+
+ ShardDataTree shardDataTree = new ShardDataTree(SchemaContextHelper.entityOwners());
+ shardPropsCreator.setDataTree(shardDataTree.getDataTree());
+
+ Entity entity1 = new Entity(ENTITY_TYPE, "one");
+ writeNode(ENTITY_OWNERS_PATH, entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entity1.getType(),
+ entityEntryWithOwner(entity1.getId(), "member-1"))), shardDataTree);
+ verifyGetOwnershipState(service, entity1, true, true);
+
+ writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), "member-2"),
+ shardDataTree);
+ verifyGetOwnershipState(service, entity1, false, true);
+
+ writeNode(entityPath(entity1.getType(), entity1.getId()), entityEntryWithOwner(entity1.getId(), ""),
+ shardDataTree);
+ verifyGetOwnershipState(service, entity1, false, false);
+
+ Entity entity2 = new Entity(ENTITY_TYPE, "two");
+ Optional<EntityOwnershipState> state = service.getOwnershipState(entity2);
+ assertEquals("getOwnershipState present", false, state.isPresent());
+
+ writeNode(entityPath(entity2.getType(), entity2.getId()), ImmutableNodes.mapEntry(ENTITY_QNAME,
+ ENTITY_ID_QNAME, entity2.getId()), shardDataTree);
+ verifyGetOwnershipState(service, entity2, false, false);
+
+ service.close();
+ }
+
+ private void verifyGetOwnershipState(DistributedEntityOwnershipService service, Entity entity,
+ boolean isOwner, boolean hasOwner) {
+ Optional<EntityOwnershipState> state = service.getOwnershipState(entity);
+ assertEquals("getOwnershipState present", true, state.isPresent());
+ assertEquals("isOwner", isOwner, state.get().isOwner());
+ assertEquals("hasOwner", hasOwner, state.get().hasOwner());
+ }
+