### Question:
RandomRelationshipSelector implements RelationshipSelector { @Override public Relationship selectRelationship(Node node) { ReservoirSampler<Relationship> randomSampler = new ReservoirSampler<>(1); for (Relationship relationship : node.getRelationships()) { if (this.relationshipInclusionPolicy.include(relationship, node)) { randomSampler.sample(relationship); } } if (randomSampler.isEmpty()) { return null; } return randomSampler.getSamples().iterator().next(); } RandomRelationshipSelector();  RandomRelationshipSelector(RelationshipInclusionPolicy relationshipInclusionPolicy); @Override Relationship selectRelationship(Node node);  }### Answer:
@Test public void shouldReturnNullOnNodeWithNoRelationships() { try (Transaction tx = database.beginTx()) { database.createNode(); tx.success(); } try (Transaction tx = database.beginTx()) { assertNull(new RandomRelationshipSelector().selectRelationship(database.getNodeById(0))); tx.success(); } }
@Test public void shouldReturnNullOnNodeWithNoMatchingRelationships() { try (Transaction tx = database.beginTx()) { Node node1 = database.createNode(); Node node2 = database.createNode(); node1.createRelationshipTo(node2, withName("TEST")); tx.success(); } try (Transaction tx = database.beginTx()) { assertNull(new RandomRelationshipSelector(IncludeRelationships.all().with(withName("NOT_EXIST"))).selectRelationship(database.getNodeById(0))); assertNull(new RandomRelationshipSelector(IncludeRelationships.all().with(INCOMING)).selectRelationship(database.getNodeById(0))); assertNull(new RandomRelationshipSelector(new SpelRelationshipInclusionPolicy("isOutgoing() && otherNode.hasLabel('Test')")).selectRelationship(database.getNodeById(0))); tx.success(); } }
@Test public void shouldReturnSingleMatchingRelationship() { try (Transaction tx = database.beginTx()) { Node node1 = database.createNode(); Node node2 = database.createNode(label("Test")); node1.createRelationshipTo(node2, withName("TEST")); tx.success(); } try (Transaction tx = database.beginTx()) { assertEquals(0, new RandomRelationshipSelector(IncludeRelationships.all().with(OUTGOING)).selectRelationship(database.getNodeById(0)).getId()); assertEquals(0, new RandomRelationshipSelector(new SpelRelationshipInclusionPolicy("isOutgoing() && otherNode.hasLabel('Test')")).selectRelationship(database.getNodeById(0)).getId()); tx.success(); } }