RemoteAgent extends AbstractAgent { @Override public AgentResponse process(AgentRequest request) { AgentResponse response; String json = gson.toJson(request); HttpRequest httpRequest = new HttpRequest(HttpMethod.POST, requestUrl).setBody(json, ContentType.APPLICATION_JSON); HttpResponse httpResponse = client.execute(httpRequest); if (httpResponse != null) { String text = httpResponse.asString(); try { response = gson.fromJson(text, AgentResponse.class); if (!response.isValid()) { response = new AgentResponse(ResponseStatus.Code.INVALID_RESPONSE_FROM_AGENT); logger.warn("Invalid json for request: " + text); } } catch (JsonSyntaxException e) { response = new AgentResponse(ResponseStatus.Code.INVALID_RESPONSE_FROM_AGENT); logger.warn("Invalid json for request: " + text); } } else { response = new AgentResponse(ResponseStatus.Code.NO_RESPONSE_FROM_AGENT); } return response; } RemoteAgent(String urlBase, HttpClient client); @Override AgentResponse process(AgentRequest request); boolean ping(); String getUrlBase();  }
@Test public void testProcess() { String json = "{\"type\":\"text\",\"text\":\"2\", \"status\":{\"code\":0, \"message\":\"Success\"}}"; HttpClient client = getMockClient(json); RemoteAgent agent = new RemoteAgent("http: AgentRequest request = new AgentRequest("what is 1 + 1?"); AgentResponse response = agent.process(request); assertEquals("2", response.getText()); assertEquals(ResponseStatus.createSuccess(), response.getStatus()); }
@Test public void testProcessWithInvalidJson() { HttpClient client = getMockClient("{\"ans\":, \"statusCode\":0}"); RemoteAgent agent = new RemoteAgent("http: AgentRequest request = new AgentRequest("what is 1 + 1?"); AgentResponse response = agent.process(request); assertNull(response.getText()); assertEquals(ResponseStatus.Code.INVALID_RESPONSE_FROM_AGENT, response.getStatus().getCode()); }
@Test public void testProcessWithInvalidResponse() { HttpClient client = getMockClient("{\"ans\":\"2\", \"statusCode\":0}"); RemoteAgent agent = new RemoteAgent("http: AgentRequest request = new AgentRequest("what is 1 + 1?"); AgentResponse response = agent.process(request); assertNull(response.getText()); assertEquals(ResponseStatus.Code.INVALID_RESPONSE_FROM_AGENT, response.getStatus().getCode()); }