TwitterAgent extends AbstractAgent { public AgentResponse process(AgentRequest agentRequest) { try { verfiyAccountCredentials(); } catch (TwitterException e) { return new AgentResponse(new ResponseStatus(ResponseStatus.Code.AGENT_INTERNAL_ERROR, "Failed to authenticate with Twitter")); } String tweetText = agentRequest.getText().trim(); if (tweetText.toLowerCase().startsWith("tweet")) { tweetText = tweetText.replaceFirst("tweet", "").trim(); } return postTweet(tweetText); } TwitterAgent(Config config);  TwitterAgent(OAuth10aService service, OAuth1AccessToken oauth); AgentResponse process(AgentRequest agentRequest);  }
@Test public void testPostGood() { OAuth10aService service = getServiceMock(200, "OK"); Agent agent = new TwitterAgent(service, null); String tweet = "This is a valid tweet"; AgentRequest request = new AgentRequest("tweet" + tweet); AgentResponse response = agent.process(request); assertEquals("Tweeted " + tweet, response.getText()); }
@Test public void testInvalidCredentials() { OAuth10aService service = getServiceMock(401, "Authorization Required"); Agent agent = new TwitterAgent(service, null); String tweet = "This is a valid tweet"; AgentRequest request = new AgentRequest("tweet" + tweet); AgentResponse response = agent.process(request); assertEquals(ResponseStatus.Code.AGENT_INTERNAL_ERROR, response.getStatus().getCode()); assertTrue(response.getStatus().getMessage().indexOf("Failed to authenticate") != -1); }
@Test public void testPostLengthBad() { OAuth10aService service = getServiceMock(200, "OK"); Agent agent = new TwitterAgent(service, null); String tweet = "This is an invalid tweet. This tweet is going to be too long, which means that this text needs to be longer than one hundred and forty characters..."; AgentRequest request = new AgentRequest("tweet" + tweet); AgentResponse response = agent.process(request); assertEquals(ResponseStatus.Code.AGENT_CANNOT_COMPLETE_ACTION, response.getStatus().getCode()); assertTrue(response.getStatus().getMessage().indexOf("too many characters") != -1); }