### Question:
Nodes { public static int countAll( NodeCursor nodeCursor, CursorFactory cursors ) { if ( nodeCursor.isDense() ) { try ( RelationshipGroupCursor group = cursors.allocateRelationshipGroupCursor() ) { nodeCursor.relationships( group ); int count = 0; while ( group.next() ) { count += group.totalCount(); } return count; } } else { try ( RelationshipTraversalCursor traversal = cursors.allocateRelationshipTraversalCursor() ) { int count = 0; nodeCursor.allRelationships( traversal ); while ( traversal.next() ) { count++; } return count; } } } private  Nodes(); static int countOutgoing( NodeCursor nodeCursor, CursorFactory cursors ); static int countIncoming( NodeCursor nodeCursor, CursorFactory cursors ); static int countAll( NodeCursor nodeCursor, CursorFactory cursors ); static int countOutgoing( NodeCursor nodeCursor, CursorFactory cursors, int type ); static int countIncoming( NodeCursor nodeCursor, CursorFactory cursors, int type ); static int countAll( NodeCursor nodeCursor, CursorFactory cursors, int type );  }### Answer:
@Test public void shouldCountAllDense() { StubGroupCursor groupCursor = new StubGroupCursor( group().withOutCount( 1 ).withInCount( 1 ).withLoopCount( 5 ), group().withOutCount( 1 ).withInCount( 1 ).withLoopCount( 3 ), group().withOutCount( 2 ).withInCount( 1 ).withLoopCount( 2 ), group().withOutCount( 3 ).withInCount( 1 ).withLoopCount( 1 ), group().withOutCount( 5 ).withInCount( 1 ).withLoopCount( 1 ) ); StubCursorFactory cursors = new StubCursorFactory().withGroupCursors( groupCursor ); int count = countAll( new StubNodeCursor( true ), cursors ); assertThat( count, equalTo( 29 ) ); }
@Test public void shouldCountAllSparse() { StubRelationshipCursor relationshipCursor = new StubRelationshipCursor( new TestRelationshipChain( 11 ) .outgoing( 55, 0, 1 ) .incoming( 56, 0, 1 ) .outgoing( 57, 0, 1 ) .loop( 58, 0 ) ); StubCursorFactory cursors = new StubCursorFactory().withRelationshipTraversalCursors( relationshipCursor ); StubNodeCursor nodeCursor = new StubNodeCursor( false ).withNode( 11 ); nodeCursor.next(); int count = countAll( nodeCursor, cursors ); assertThat( count, equalTo( 4 ) ); }
@Test public void shouldCountAllWithTypeDense() { StubGroupCursor groupCursor = new StubGroupCursor( group( 1 ).withOutCount( 1 ).withInCount( 1 ).withLoopCount( 5 ), group( 2 ).withOutCount( 1 ).withInCount( 1 ).withLoopCount( 3 ) ); StubCursorFactory cursors = new StubCursorFactory().withGroupCursors( groupCursor, groupCursor ); assertThat( countAll( new StubNodeCursor( true ), cursors, 1 ), equalTo( 7 ) ); assertThat( countAll( new StubNodeCursor( true ), cursors, 2 ), equalTo( 5 ) ); }
@Test public void shouldCountAllWithTypeSparse() { StubRelationshipCursor relationshipCursor = new StubRelationshipCursor( new TestRelationshipChain( 11 ) .outgoing( 55, 0, 1 ) .incoming( 56, 0, 1 ) .outgoing( 57, 0, 1 ) .loop( 58, 2 ) ); StubCursorFactory cursors = new StubCursorFactory( true ) .withRelationshipTraversalCursors( relationshipCursor ); StubNodeCursor nodeCursor = new StubNodeCursor( false ).withNode( 11 ); nodeCursor.next(); assertThat( countAll( nodeCursor, cursors, 1 ), equalTo( 3 ) ); assertThat( countAll( nodeCursor, cursors, 2 ), equalTo( 1) ); }