### Question:
RandomNodeSelector implements NodeSelector { @Override public Node selectNode(GraphDatabaseService database) { Node candidate = randomNodeO1(database); if (candidate != null) { return candidate; } return randomNodeON(database); } RandomNodeSelector();  RandomNodeSelector(NodeInclusionPolicy inclusionPolicy); @Override Node selectNode(GraphDatabaseService database);  }### Answer:
@Test public void shouldReturnNullOnEmptyDatabase() { try (Transaction tx = database.beginTx()) { assertNull(new RandomNodeSelector().selectNode(database)); tx.success(); } }
@Test public void shouldReturnNullOnDatabaseWithAllNodesDeleted() { new NoInputBatchTransactionExecutor(database, 1000, 1000, (db, input, batchNumber, stepNumber) -> db.createNode()).execute(); new IterableInputBatchTransactionExecutor<>(database, 1000, new AllNodes(database, 1000), (db, node, batchNumber, stepNumber) -> node.delete()).execute(); try (Transaction tx = database.beginTx()) { assertNull(new RandomNodeSelector().selectNode(database)); tx.success(); } }
@Test public void shouldReturnNullWhenNoNodeMatchesTheSpec() { new NoInputBatchTransactionExecutor(database, 1000, 1000, (db, input, batchNumber, stepNumber) -> db.createNode()).execute(); try (Transaction tx = database.beginTx()) { assertNull(new RandomNodeSelector(IncludeNoNodes.getInstance()).selectNode(database)); tx.success(); } }
@Test public void shouldReturnCorrectNode() { new NoInputBatchTransactionExecutor(database, 1000, 1000, (db, input, batchNumber, stepNumber) -> db.createNode(Label.label("Label" + (stepNumber % 10)))).execute(); try (Transaction tx = database.beginTx()) { assertNotNull(new RandomNodeSelector().selectNode(database)); Node node = new RandomNodeSelector(new BaseNodeInclusionPolicy() { @Override public boolean include(Node object) { return object.hasLabel(Label.label("Label4")); } }).selectNode(database); assertNotNull(node); assertTrue(node.hasLabel(Label.label("Label4"))); tx.success(); } }
@Test public void shouldReturnTheOnlyNode() { try (Transaction tx = database.beginTx()) { database.createNode(); tx.success(); } try (Transaction tx = database.beginTx()) { Node node = new RandomNodeSelector().selectNode(database); assertEquals(0, node.getId()); tx.success(); } }
@Test public void shouldReturnTheOnlyRemainingNodeAfterTheRestHasBeenDeleted() { new NoInputBatchTransactionExecutor(database, 1000, 1000, (db, input, batchNumber, stepNumber) -> db.createNode()).execute(); new IterableInputBatchTransactionExecutor<>(database, 1000, new AllNodes(database, 1000), (db, node, batchNumber, stepNumber) -> { if (stepNumber != 1000) { node.delete(); } }).execute(); try (Transaction tx = database.beginTx()) { Node node = new RandomNodeSelector().selectNode(database); assertEquals(999, node.getId()); tx.success(); } }