### Question:
FriendshipStrengthCounter extends TransactionEventHandler.Adapter<Void> { public static long getTotalFriendshipStrength(GraphDatabaseService database) { long result = 0; try (Transaction tx = database.beginTx()) { result = (long) getCounterNode(database).getProperty(TOTAL_FRIENDSHIP_STRENGTH, 0L); tx.success(); } return result; } FriendshipStrengthCounter(GraphDatabaseService database); @Override Void beforeCommit(TransactionData data); static long getTotalFriendshipStrength(GraphDatabaseService database); static final RelationshipType FRIEND_OF; static final String STRENGTH; static final String TOTAL_FRIENDSHIP_STRENGTH; static final Label COUNTER_NODE_LABEL; }### Answer:
@Test public void totalFriendshipStrengthShouldBeZeroInEmptyDatabase() { assertEquals(0L, getTotalFriendshipStrength(database)); }
@Test public void totalFriendshipStrengthShouldBeCounted() { long p1id, p2id; try (Transaction tx = database.beginTx()) { Node person1 = database.createNode(); Node person2 = database.createNode(); Node person3 = database.createNode(); p1id = person1.getId(); p2id = person2.getId(); person1.setProperty("name", "Person One"); person2.setProperty("name", "Person Two"); person3.setProperty("name", "Person Three"); person1.createRelationshipTo(person2, FRIEND_OF).setProperty(STRENGTH, 2L); person1.createRelationshipTo(person3, FRIEND_OF).setProperty(STRENGTH, 1L); person2.createRelationshipTo(person1, FRIEND_OF).setProperty(STRENGTH, 3L); person2.createRelationshipTo(person3, FRIEND_OF).setProperty(STRENGTH, 1L); person3.createRelationshipTo(person1, FRIEND_OF).setProperty(STRENGTH, 2L); tx.success(); } assertEquals(9L, getTotalFriendshipStrength(database)); try (Transaction tx = database.beginTx()) { for (Relationship relationship : database.getNodeById(p1id).getRelationships(FRIEND_OF, Direction.OUTGOING)) { if (relationship.getEndNode().getId() == p2id) { relationship.delete(); } else { relationship.setProperty(STRENGTH, 2L); } } tx.success(); } assertEquals(8L, getTotalFriendshipStrength(database)); }