@Test public void testStatusUpdatingMessageHandler() { TimelineOperations timelineOperations = Mockito.mock(TimelineOperations.class); Mockito.when(this.twitter.timelineOperations()).thenReturn(timelineOperations); ArgumentCaptor<TweetData> argument = ArgumentCaptor.forClass(TweetData.class); this.in1.send(new GenericMessage<String>("foo")); Mockito.verify(timelineOperations).updateStatus(argument.capture()); assertEquals("foo", argument.getValue().toRequestParameters().getFirst("status")); Mockito.reset(timelineOperations); ClassPathResource media = new ClassPathResource("log4j.properties"); this.in2.send( MessageBuilder.withPayload(Collections.singletonMap("foo", "bar")) .setHeader("media", media) .build()); Mockito.verify(timelineOperations).updateStatus(argument.capture()); TweetData tweetData = argument.getValue(); MultiValueMap<String, Object> requestParameters = tweetData.toRequestParameters(); assertEquals("bar", requestParameters.getFirst("status")); assertNull(requestParameters.getFirst("media")); MultiValueMap<String, Object> uploadMediaParameters = tweetData.toUploadMediaParameters(); assertEquals(media, uploadMediaParameters.getFirst("media")); }
/** * updates the users twitter-status to the tweets content. 1. reads the tweet with the given * tweetID from the database 2. checks if the related tweetGroup is still enabled and tweet wasn't * tweeted already 3. reads the users oAuthToken and oAuthTokenSecret from the database 4. updates * the users twitter status to the tweets tweetContent * * @param userID userID * @param tweetID tweetID */ public void run(int userID, int tweetID) { // read tweet from DB Tweet toTweet = DBConnector.getTweetByID(tweetID, userID); if (toTweet == null) { return; } // check if tweet was not tweeted already if (toTweet.tweeted) { return; } // check if tweetGroup is still enabled if (!DBConnector.isEnabledGroup(toTweet.groupID, userID)) { return; } // read userConfig (oAuthToken, tokenSecret) from DB String[] userConfig = DBConnector.getUserConfig(userID); String token = userConfig[1]; String tokenSecret = userConfig[2]; // Tweeting Twitter twitter = new TwitterTemplate(appID, appSecret, token, tokenSecret); // Add TweetContent String tweet = toTweet.content; TweetData tweetData = new TweetData(tweet); // add image if (toTweet.imageUrl != null) { try { Resource img = new UrlResource(toTweet.imageUrl); tweetData = tweetData.withMedia(img); } catch (MalformedURLException e) { tweetData = new TweetData(tweet + " " + toTweet.imageUrl); } } // add Geo-Locations if (toTweet.longitude != 0 || toTweet.latitude != 0) { System.out.println("long: " + toTweet.longitude); System.out.println("lat: " + toTweet.latitude); tweetData = tweetData.atLocation(toTweet.longitude, toTweet.latitude).displayCoordinates(true); } // update Status twitter.timelineOperations().updateStatus(tweetData); // update Tweet-Status in DB DBConnector.flagAsTweeted(tweetID, userID); }