FlickrAgent extends AbstractAgent { @Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); String agentRequest = request.getText(); String searchPhrase = getSearchPhrase(agentRequest); logger.debug("Search phrase: " + searchPhrase); if (searchPhrase == null) { return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } String json = getFlickrJson(searchPhrase); logger.debug("Flickr JSON: " + json); if (json == null) { return new AgentResponse(ResponseStatus.Code.AGENT_INTERNAL_ERROR); } String url = getImageUrl(json); logger.debug("Flickr image URL: " + url); byte[] data = getImage(url); if (data == null) { return new AgentResponse(ResponseStatus.Code.AGENT_INTERNAL_ERROR); } String payload = encodeImage(data); return new AgentResponse(Response.Type.IMAGE_EMBED, "Here is your image", payload); } FlickrAgent(HttpClient client, String apikey); @Override AgentResponse process(AgentRequest request);  }
@Test public void testGoodResponse() { String query = "Show me an apple"; String jsonData = FileReader.getFileAsString("flickr/apple.json"); byte[] image = FileReader.getFileAsBytes("flickr/15429554069_08c6748145_z.jpg"); HttpResponse httpResponse1 = mock(HttpResponse.class); when(httpResponse1.isSuccess()).thenReturn(true); when(httpResponse1.asString()).thenReturn(jsonData); HttpResponse httpResponse2 = mock(HttpResponse.class); when(httpResponse2.isSuccess()).thenReturn(true); when(httpResponse2.asBytes()).thenReturn(image); HttpClient client = mock(HttpClient.class); when(client.execute(any(HttpRequest.class))).thenReturn(httpResponse1) .thenReturn(httpResponse2); Agent agent = new FlickrAgent(client, "key"); AgentResponse response = agent.process(new AgentRequest(query)); assertNotNull(response); assertTrue(response.isSuccess()); assertNotNull(response.getPayload()); assertTrue(response.getPayload().startsWith("/9j/4AAQSkZJRgABAQEASABIAAD }
@Test public void testBadRequest() { String query = "Go jump in a lake"; HttpClient client = mock(HttpClient.class); Agent agent = new FlickrAgent(client, "key"); AgentResponse response = agent.process(new AgentRequest(query)); assertNotNull(response); assertFalse(response.isSuccess()); assertEquals(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST, response.getStatus().getCode()); }