Application { public Response process(Request request) { logger.info("Processing request: " + request.getText()); for (RequestFilter rf : requestFilters) { request = rf.filter(request); } Response response = new Response(new ResponseStatus( Code.NO_AVAILABLE_AGENT)); Report report = new Report(request); long startTime = System.nanoTime(); Agent[] agents = router.route(request); report.setRouteTime(startTime, System.nanoTime()); if (agents.length > 0) { response = new Response(new ResponseStatus( Code.NO_RESPONSE_FROM_AGENT)); startTime = System.nanoTime(); for (Agent agent : agents) { report.setAgent(agent); AgentResponse agentResponse = null; if (this.user != null) { agentResponse = agent.process(new AgentRequest(new Request(request.getText(), this.user, request.getId()))); } else { agentResponse = agent.process(new AgentRequest(request)); } if (agentResponse != null) { response = new Response(agentResponse); break; } } report.setAgentTime(startTime, System.nanoTime()); } report.setResponse(response); reporter.store(report); return response; } Application(Router router);  Application(Router router, Reporter reporter); Agent[] getAgents(); void addRequestFilter(RequestFilter filter); void addResponseFilter(ResponseFilter filter); void addShutdownListener(ShutdownListener listener); void setUser(User user); Response process(Request request); void shutdown();  }
@Test public void testProcessWithNoAgents() { Request request = new Request("test"); Router router = mock(Router.class); when(router.route(request)).thenReturn(new Agent[0]); Application app = new Application(router); Response response = app.process(request); assertFalse(response.isSuccess()); assertEquals(ResponseStatus.Code.NO_AVAILABLE_AGENT, response.getStatus().getCode()); }
@Test public void testProcessWithAgentWithResponse() { Request request = new Request("test"); Agent agent = mock(Agent.class); when(agent.process((AgentRequest)anyObject())).thenReturn(new AgentResponse("the answer")); Agent rtn[] = new Agent[1]; rtn[0] = agent; Router router = mock(Router.class); when(router.route(request)).thenReturn(rtn); Application app = new Application(router); Response response = app.process(request); assertEquals("the answer", response.getText()); }
@Test public void testProcessWithAgentWithNoResponse() { Request request = new Request("test"); Agent agent = mock(Agent.class); when(agent.process((AgentRequest)anyObject())).thenReturn(null); Agent rtn[] = new Agent[1]; rtn[0] = agent; Router router = mock(Router.class); when(router.route(request)).thenReturn(rtn); Application app = new Application(router); Response response = app.process(request); assertFalse(response.isSuccess()); assertEquals(ResponseStatus.Code.NO_RESPONSE_FROM_AGENT, response.getStatus().getCode()); }