ChainedRouter implements Router { @Override public Agent[] route(Request request) { Agent[] agents = {}; for (Router router : routers) { agents = router.route(request); if (agents.length != 0) { break; } } return agents; } ChainedRouter(Router... routers); @Override Agent[] route(Request request); @Override Agent[] getAgents();  }
@Test public void testRouteWithFirstEmptyResponse() { Router r1 = mock(Router.class); Router r2 = mock(Router.class); Agent a = mock(Agent.class); Agent[] resp = {a}; Request request = mock(Request.class); when(r1.route(request)).thenReturn(new Agent[0]); when(r2.route(request)).thenReturn(resp); Router router = new ChainedRouter(r1, r2); assertArrayEquals(resp, router.route(request)); }
@Test public void testRouteWithNoEmptyResponse() { Router r1 = mock(Router.class); Router r2 = mock(Router.class); Agent a1 = mock(Agent.class); Agent a2 = mock(Agent.class); Agent[] resp1 = {a1}; Agent[] resp2 = {a2}; Request request = mock(Request.class); when(r1.route(request)).thenReturn(resp1); when(r2.route(request)).thenReturn(resp2); Router router = new ChainedRouter(r1, r2); assertArrayEquals(resp1, router.route(request)); }