INT-1553 polishing, added more tests for status update, code coverage is at 76%

This commit is contained in:
Oleg Zhurakousky
2010-11-08 12:31:39 -05:00
parent c5b072b2a8
commit 8d280e4a99
8 changed files with 99 additions and 29 deletions

View File

@@ -23,7 +23,6 @@ import twitter4j.http.AccessToken;
*/
public class Twitter4jTemplate implements TwitterOperations{
private final Twitter twitter;
/**
* Used to construct this template to perform Twitter API calls that do not require authorization.
* (e.g., search)

View File

@@ -19,8 +19,6 @@ import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.util.Assert;
import twitter4j.Twitter;
/**
* Base adapter class for all outbound Twitter adapters

View File

@@ -39,26 +39,21 @@ public class DirectMessageSendingMessageHandler extends AbstractOutboundTwitterE
if (this.twitter == null) {
this.afterPropertiesSet();
}
// try {
Assert.isInstanceOf(String.class, message.getPayload(), "Only payload of type String is supported. If your payload " +
"is not of type String consider adding a transformer to the message flow in front of this adapter.");
Assert.isTrue(message.getHeaders().containsKey(TwitterHeaders.DM_TARGET_USER_ID),
"the '" + TwitterHeaders.DM_TARGET_USER_ID + "' header is required");
Object toUser = message.getHeaders().get(TwitterHeaders.DM_TARGET_USER_ID);
Assert.isTrue(toUser instanceof String || toUser instanceof Integer,
"the header '" + TwitterHeaders.DM_TARGET_USER_ID +
"' must be either a String (a screenname) or an int (a user ID)");
String payload = (String) message.getPayload();
if (toUser instanceof Integer) {
this.twitter.sendDirectMessage((Integer) toUser, payload);
}
else if (toUser instanceof String) {
this.twitter.sendDirectMessage((String) toUser, payload);
}
// }
// catch (TwitterException e) {
// throw new MessageHandlingException(message, e);
// }
Assert.isInstanceOf(String.class, message.getPayload(), "Only payload of type String is supported. If your payload " +
"is not of type String consider adding a transformer to the message flow in front of this adapter.");
Assert.isTrue(message.getHeaders().containsKey(TwitterHeaders.DM_TARGET_USER_ID),
"the '" + TwitterHeaders.DM_TARGET_USER_ID + "' header is required");
Object toUser = message.getHeaders().get(TwitterHeaders.DM_TARGET_USER_ID);
Assert.isTrue(toUser instanceof String || toUser instanceof Integer,
"the header '" + TwitterHeaders.DM_TARGET_USER_ID +
"' must be either a String (a screenname) or an int (a user ID)");
String payload = (String) message.getPayload();
if (toUser instanceof Integer) {
this.twitter.sendDirectMessage((Integer) toUser, payload);
}
else if (toUser instanceof String) {
this.twitter.sendDirectMessage((String) toUser, payload);
}
}
}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.outbound;
import org.springframework.integration.Message;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010 the original author or authors
* Copyright 2002-2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@ import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.util.Assert;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
/**

View File

@@ -110,8 +110,8 @@ public class Twitter4jTemplateTests {
@Test
public void testUpdateStatus() throws Exception{
StatusUpdate statusUpdate = new StatusUpdate("writing twitter test");
template.updateStatus(statusUpdate);
verify(twitter, times(1)).updateStatus(statusUpdate);
StatusUpdate status = new StatusUpdate("writing twitter test");
template.updateStatus(status);
verify(twitter, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
}
}

View File

@@ -78,6 +78,7 @@ public class DirectMessageReceivingMessageSourceTests {
assertTrue(source.isRunning());
}
@SuppressWarnings("rawtypes")
@Test
public void testSuccessfullInitializationWithMessages() throws Exception{
this.setUpMockScenarioForMessagePolling();
@@ -116,6 +117,7 @@ public class DirectMessageReceivingMessageSourceTests {
when(twitter.getDirectMessages(Mockito.any(Paging.class))).thenReturn(testMessages);
}
@SuppressWarnings({ "rawtypes", "serial" })
public static class SampleResoponceList extends ArrayList implements ResponseList {
@Override

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.outbound;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.lang.reflect.Field;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.integration.Message;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.Twitter4jTemplate;
import org.springframework.integration.twitter.core.TwitterHeaders;
import org.springframework.integration.twitter.core.TwitterOperations;
import twitter4j.GeoLocation;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
*
*/
public class TimelineUpdateSendingMessageHandlerTests {
TwitterOperations twitterOperations;
Twitter twitter;
@Before
public void prepare() throws Exception{
twitterOperations = spy(new Twitter4jTemplate());
Field twitterField = Twitter4jTemplate.class.getDeclaredField("twitter");
twitterField.setAccessible(true);
twitter = mock(Twitter.class);
twitterField.set(twitterOperations, twitter);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testSendingStatusUpdate() throws Exception{
TimelineUpdateSendingMessageHandler handler = new TimelineUpdateSendingMessageHandler(twitterOperations);
handler.handleMessage(new GenericMessage("writing twitter tests"));
verify(twitterOperations, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
verify(twitter, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
}
@Test
public void testSendingStatusUpdateWithHeaders() throws Exception{
TimelineUpdateSendingMessageHandler handler = new TimelineUpdateSendingMessageHandler(twitterOperations);
Message<?> message = MessageBuilder.withPayload("writing twitter tests")
.setHeader(TwitterHeaders.IN_REPLY_TO_STATUS_ID, new Long(123))
.setHeader(TwitterHeaders.PLACE_ID, "123")
.setHeader(TwitterHeaders.GEOLOCATION, mock(GeoLocation.class))
.setHeader(TwitterHeaders.DISPLAY_COORDINATES, true)
.build();
handler.handleMessage(message);
verify(twitterOperations, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
verify(twitter, times(1)).updateStatus(Mockito.any(StatusUpdate.class));
}
}