TreeRouter implements Router { @Override public Agent[] getAgents() { Set<Agent> agents = new HashSet<Agent>(); Deque<Node> stack = new ArrayDeque<Node>(); stack.addLast(root); while (!stack.isEmpty()) { Node node = stack.pollLast(); if (node.isLeaf()) { agents.addAll(Arrays.asList(node.getValue())); } else { stack.addAll(Arrays.asList(node.getChildren())); } } return agents.toArray(new Agent[agents.size()]); } TreeRouter(Node root); @Override Agent[] route(Request request); @Override Agent[] getAgents();  }
@Test public void testGettingAgentsFromRouter() { Router router = createBinaryTreeRouter(); Agent[] agents = router.getAgents(); assertEquals(4, agents.length); List<String> names = new ArrayList<String>(); for (Agent agent : agents) { names.add(agent.getName()); } assertTrue(names.contains("historical")); assertTrue(names.contains("current")); assertTrue(names.contains("baseball")); assertTrue(names.contains("football")); }