StockAgent extends AbstractAgent { @Override public AgentResponse process(AgentRequest request) { logger.debug("Handling the request: " + request.getText()); String regex = "\\bstocks\\b"; Pattern pat = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); String requestCompanyName = pat.matcher(request.getText()).replaceAll("").trim(); CompanyInfo stockCompanyInfo = findStockSymbol(requestCompanyName); if (stockCompanyInfo == null) { return new AgentResponse(new ResponseStatus(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER, "I am sorry, I don't understand which company you are asking about.")); } String keywordCompanyName = stockCompanyInfo.getName(); String keywordCompanySymbol = stockCompanyInfo.getSymbol(); String json = null; json = requestData(keywordCompanySymbol); if (json == null) { return new AgentResponse(new ResponseStatus(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER, ("I am sorry, I can't find the proper stock data for the company " + keywordCompanyName + ".").replace("..", "."))); } JsonObject returnObj = processData(json, keywordCompanyName); if (returnObj != null) { return new AgentResponse(Response.Type.JSON, ("Here is the longitudinal stock market data from the last " + DAYS_IN_A_MONTH + " days for " + keywordCompanyName + ".").replace("..", "."), returnObj.toString()); } return new AgentResponse(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER); } StockAgent(HttpClient client, String apiKey); @Override AgentResponse process(AgentRequest request);  }
@Test public void testStockAgentExactMatch() { String keyword = "facebook, Inc."; String jsonData = getJsonData("stockagent/MockJson_Facebook.json"); HttpClient client = new FakeHttpClient(new FakeHttpResponse(jsonData)); Agent stocks = new StockAgent(client, apikey); AgentRequest request = new AgentRequest("Stocks " + keyword); AgentResponse response = stocks.process(request); assertEquals("Here is the longitudinal stock market data from the last 30 days for Facebook, Inc.", response.getText()); }
@Test public void testStockAgentCloseMatch() { String keyword = "facebook"; String jsonData = getJsonData("stockagent/MockJson_Facebook.json"); HttpClient client = new FakeHttpClient(new FakeHttpResponse(jsonData)); Agent stocks = new StockAgent(client, apikey); AgentRequest request = new AgentRequest(keyword); AgentResponse response = stocks.process(request); assertEquals("Here is the longitudinal stock market data from the last 30 days for Facebook, Inc.", response.getText()); }
@Test public void testStockAgentExactMatch2() { String keyword = "Apple inc."; String jsonData = getJsonData("stockagent/MockJson_Apple.json"); HttpClient client = new FakeHttpClient(new FakeHttpResponse(jsonData)); Agent stocks = new StockAgent(client, apikey); AgentRequest request = new AgentRequest(keyword); AgentResponse response = stocks.process(request); assertEquals("Here is the longitudinal stock market data from the last 30 days for Apple Inc.", response.getText()); }
@Test public void testStockAgentCloseMatch2() { String keyword = "apple"; String jsonData = getJsonData("stockagent/MockJson_Apple.json"); HttpClient client = new FakeHttpClient(new FakeHttpResponse(jsonData)); Agent stocks = new StockAgent(client, apikey); AgentRequest request = new AgentRequest(keyword); AgentResponse response = stocks.process(request); assertEquals("Here is the longitudinal stock market data from the last 30 days for Apple Inc.", response.getText()); }
@Test public void testStockAgentNoQuandlData() { String keyword = "First Bank"; String jsonData = getJsonData("stockagent/MockJson_404.json"); 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 stocks = new StockAgent(client, apikey); AgentRequest request = new AgentRequest(keyword); AgentResponse response = stocks.process(request); assertEquals("I am sorry, I can't find the proper stock data for the company " + keyword + ".", response.getStatus().getMessage()); }
@Test public void testStockAgentFailure() { String keyword = "company that does not exist"; HttpClient client = mock(HttpClient.class); Agent stocks = new StockAgent(client, apikey); AgentRequest request = new AgentRequest(keyword); AgentResponse response = stocks.process(request); assertEquals("I am sorry, I don't understand which company you are asking about.", response.getStatus().getMessage()); }