RottenTomatoesAgent extends AbstractAgent { @Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); String agentRequest = request.getText(); String movieTitle = findMovieTitle(agentRequest); if (movieTitle.equals("")) { return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } String keyword = findKeyWord(agentRequest); if (keyword == null) { return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST); } AgentResponse response = null; String json = requestData(movieTitle); if (json == null) { response = new AgentResponse(new ResponseStatus( ResponseStatus.Code.AGENT_INTERNAL_ERROR, "Something went wrong with the Rotten Tomatoes API request. Please check your API key.")); } else { String responseText = formatResponse(keyword, json); if (responseText != null) { response = new AgentResponse(responseText); } else { response = new AgentResponse(ResponseStatus.Code.AGENT_INTERNAL_ERROR); } } return response; } RottenTomatoesAgent(HttpClient client, String apikey); @Override AgentResponse process(AgentRequest request);  }
@Test public void testRuntime() { String jsonData = FileReader.getFileAsString("rotten_tomatoes/test.json"); String testQuestion = "What is the runtime for the film finding nemo?"; String correctAnswer = "The runtime for the film, Finding Nemo, is 100 minutes long."; HttpClient client = new FakeHttpClient(new FakeHttpResponse(jsonData)); Agent movie = new RottenTomatoesAgent(client, apikey); AgentRequest request = new AgentRequest(testQuestion); AgentResponse response = movie.process(request); assertEquals(correctAnswer, response.getText()); }
@Test public void testYear() { String jsonData = FileReader.getFileAsString("rotten_tomatoes/test.json"); String testQuestion = "What is the creation year for the film finding nemo?"; String correctAnswer = "The year the film, Finding Nemo, was created is 2003."; HttpClient client = new FakeHttpClient(new FakeHttpResponse(jsonData)); Agent movie = new RottenTomatoesAgent(client, apikey); AgentRequest request = new AgentRequest(testQuestion); AgentResponse response = movie.process(request); assertEquals(correctAnswer, response.getText()); }
@Test public void testActors() { String jsonData = FileReader.getFileAsString("rotten_tomatoes/test.json"); String testQuestion = "Who are the main actors in the movie finding nemo?"; String correctAnswer = "The film, Finding Nemo, stars actors Albert Brooks, " + "Ellen DeGeneres, Alexander Gould, Willem Dafoe, Brad Garrett."; HttpClient client = new FakeHttpClient(new FakeHttpResponse(jsonData)); Agent movie = new RottenTomatoesAgent(client, apikey); AgentRequest request = new AgentRequest(testQuestion); AgentResponse response = movie.process(request); assertEquals(correctAnswer, response.getText()); }
@Test public void testNoKeyword() { String jsonData = FileReader.getFileAsString("rotten_tomatoes/test.json"); String testQuestion = "What is the movie finding nemo?"; HttpClient client = new FakeHttpClient(new FakeHttpResponse(jsonData)); Agent movie = new RottenTomatoesAgent(client, apikey); AgentRequest request = new AgentRequest(testQuestion); AgentResponse response = movie.process(request); assertFalse(response.isSuccess()); assertEquals(ResponseStatus.Code.AGENT_DID_NOT_UNDERSTAND_REQUEST, response.getStatus().getCode()); }
@Test public void testBadApiKey() { String jsonData = FileReader.getFileAsString("rotten_tomatoes/bad_key.json"); String testQuestion = "What is the runtime for the movie finding nemo?"; HttpResponse httpResponse = mock(HttpResponse.class); when(httpResponse.isSuccess()).thenReturn(false); when(httpResponse.asString()).thenReturn(jsonData); HttpClient client = mock(HttpClient.class); when(client.execute(any(HttpRequest.class))).thenReturn(httpResponse); Agent movie = new RottenTomatoesAgent(client, apikey); AgentRequest request = new AgentRequest(testQuestion); AgentResponse response = movie.process(request); assertFalse(response.isSuccess()); assertEquals(ResponseStatus.Code.AGENT_INTERNAL_ERROR, response.getStatus().getCode()); }