### Question:
ReachabilityIndex { public boolean canReach(Node startNode, Node endNode) { if (!indexExists()) { throw new IllegalStateException("Reachability index must be created first."); } try (Transaction tx = graphDb.beginTx()) { long[] outList = (long[]) startNode.getProperty(OUT_LIST_PROPERTY); long[] inList = (long[]) endNode.getProperty(IN_LIST_PROPERTY); int i = 0, j = 0; while (i < outList.length && j < inList.length) { if (outList[i] < inList[j]) { i++; } else if (inList[j] < outList[i]) { j++; } else { return true; } } return false; } } @Inject  ReachabilityIndex(GraphDatabaseService graphDb); boolean indexExists(); void createIndex(); void setTransactionBatchSize(int size); void createIndex(Predicate<Node> nodePredicate); void dropIndex(); boolean canReach(Node startNode, Node endNode); Set<Pair<Node, Node>> getConnectedPairs(Set<Node> sources, Set<Node> targets); Set<Pair<Node, Node>> getDisconnectedPairs(Set<Node> sources, Set<Node> targets); boolean allReachable(Set<Node> sources, Set<Node> targets);  }### Answer:
@Test(expected = IllegalStateException.class) public void testUncreatedIndex() { GraphDatabaseService testDb = new GraphDatabaseFactory().newEmbeddedDatabase(new File(path)); ReachabilityIndex index = new ReachabilityIndex(testDb); index.canReach(a, b); testDb.shutdown(); }
@Test public void testSelfReachability() { assertThat(index.canReach(a, a), is(true)); }
@Test public void testDirectionalReachability() { assertThat(index.canReach(a, b), is(true)); assertThat(index.canReach(b, a), is(false)); }
@Test public void testBidirectionalReachability() { assertThat(index.canReach(a, c), is(true)); assertThat(index.canReach(b, c), is(false)); }
@Test public void testDisconnectedNode() { for (Node n : newArrayList(a, b, c)) { assertThat(index.canReach(n, d), is(false)); assertThat(index.canReach(d, n), is(false)); } }
@Test public void testForbiddenNodes() { assertThat(index.canReach(a, f), is(false)); assertThat(index.canReach(e, f), is(false)); assertThat(index.canReach(a, e), is(false)); }