INT-1682 added tests for error-channel for both ChatMessageListeningEndpoint and PresenceListeningEndpoint

This commit is contained in:
Oleg Zhurakousky
2010-12-13 14:29:27 -05:00
parent 228edd8b37
commit 76f0afef4f
2 changed files with 87 additions and 1 deletions

View File

@@ -19,22 +19,31 @@ import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashSet;
import java.util.Set;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.integration.MessagingException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.xmpp.core.XmppContextUtils;
import org.springframework.integration.xmpp.inbound.ChatMessageListeningEndpoint;
/**
* @author Oleg Zhurakousky
@@ -97,4 +106,40 @@ public class ChatMessageListeningEndpointTests {
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint();
endpoint.afterPropertiesSet();
}
@Test
public void testWithErrorChannel(){
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XMPPConnection connection = mock(XMPPConnection.class);
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
ChatManager cm = mock(ChatManager.class);
when(connection.getChatManager()).thenReturn(cm);
Chat chat = mock(Chat.class);
when(cm.getThreadChat(Mockito.anyString())).thenReturn(chat);
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint();
DirectChannel outChannel = new DirectChannel();
outChannel.subscribe(new MessageHandler() {
public void handleMessage(org.springframework.integration.Message<?> message)
throws MessagingException {
throw new RuntimeException("ooops");
}
});
PollableChannel errorChannel = new QueueChannel();
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(outChannel);
endpoint.setErrorChannel(errorChannel);
endpoint.afterPropertiesSet();
PacketListener listener = (PacketListener) TestUtils.getPropertyValue(endpoint, "packetListener");
Message smackMessage = new Message("kermit@frog.com");
smackMessage.setBody("hello");
smackMessage.setThread("1234");
listener.processPacket(smackMessage);
ErrorMessage msg =
(ErrorMessage) errorChannel.receive();
assertEquals("hello", ((MessagingException)msg.getPayload()).getFailedMessage().getPayload());
}
}

View File

@@ -25,6 +25,8 @@ import static org.mockito.Mockito.when;
import java.util.HashSet;
import java.util.Set;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
@@ -38,7 +40,12 @@ import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.integration.Message;
import org.springframework.integration.MessagingException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.xmpp.core.XmppContextUtils;
@@ -117,4 +124,38 @@ public class PresenceListeningEndpointTests {
handler.afterPropertiesSet();
}
@Test
public void testWithErrorChannel(){
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XMPPConnection connection = mock(XMPPConnection.class);
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
ChatManager cm = mock(ChatManager.class);
when(connection.getChatManager()).thenReturn(cm);
Chat chat = mock(Chat.class);
when(cm.getThreadChat(Mockito.anyString())).thenReturn(chat);
PresenceListeningEndpoint endpoint = new PresenceListeningEndpoint();
DirectChannel outChannel = new DirectChannel();
outChannel.subscribe(new MessageHandler() {
public void handleMessage(org.springframework.integration.Message<?> message)
throws MessagingException {
throw new RuntimeException("ooops");
}
});
PollableChannel errorChannel = new QueueChannel();
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(outChannel);
endpoint.setErrorChannel(errorChannel);
endpoint.afterPropertiesSet();
RosterListener listener = (RosterListener) TestUtils.getPropertyValue(endpoint, "rosterListener");
Presence presence = new Presence(Type.available);
listener.presenceChanged(presence);
ErrorMessage msg =
(ErrorMessage) errorChannel.receive();
assertEquals(Type.available.toString(), ((MessagingException)msg.getPayload()).getFailedMessage().getPayload().toString());
}
}