INT-1554 refactored XmppMessageOutboundEndpointParser and XmppMessageSendingMessageHandler, added tests

This commit is contained in:
Oleg Zhurakousky
2010-11-04 13:29:15 -04:00
parent 8d1e5e3f1d
commit 601d67bcf8
7 changed files with 164 additions and 52 deletions

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp-2.0.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
<bean id="testConnection" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.jivesoftware.smack.XMPPConnection"/>
</bean>
<int:channel id="outboundEventChannel"/>
<int-xmpp:message-outbound-channel-adapter id="outboundEventAdapter"
channel="outboundEventChannel"
xmpp-connection="testConnection"/>
<int:channel id="outboundPollingChannel">
<int:queue/>
</int:channel>
<int-xmpp:message-outbound-channel-adapter id="outboundPollingAdapter"
channel="outboundPollingChannel"
xmpp-connection="testConnection">
<int:poller fixed-rate="1000" max-messages-per-poll="1"/>
</int-xmpp:message-outbound-channel-adapter>
</beans>

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.xmpp.config;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.endpoint.PollingConsumer;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.xmpp.XmppHeaders;
/**
* @author Oleg Zhurakousky
*
*/
public class XmppMessageOutboundEndpointParserTests {
@Test
public void testPollingConsumer(){
ApplicationContext context =
new ClassPathXmlApplicationContext("XmppMessageOutboundEndpointParserTests-context.xml", XmppMessageOutboundEndpointParserTests.class);
Object pollingConsumer = context.getBean("outboundPollingAdapter");
assertTrue(pollingConsumer instanceof PollingConsumer);
}
@Test
public void testEventConsumer(){
ApplicationContext context =
new ClassPathXmlApplicationContext("XmppMessageOutboundEndpointParserTests-context.xml", XmppMessageOutboundEndpointParserTests.class);
Object pollingConsumer = context.getBean("outboundEventAdapter");
assertTrue(pollingConsumer instanceof EventDrivenConsumer);
}
@Test
public void testPollingConsumerUsage() throws Exception{
ApplicationContext context =
new ClassPathXmlApplicationContext("XmppMessageOutboundEndpointParserTests-context.xml", XmppMessageOutboundEndpointParserTests.class);
Object pollingConsumer = context.getBean("outboundPollingAdapter");
assertTrue(pollingConsumer instanceof PollingConsumer);
MessageChannel channel = context.getBean("outboundEventChannel", MessageChannel.class);
Message<?> message = MessageBuilder.withPayload("hello").setHeader(XmppHeaders.CHAT_TO_USER, "oleg").build();
XMPPConnection connection = context.getBean("testConnection", XMPPConnection.class);
ChatManager chatManager = mock(ChatManager.class);
when(connection.getChatManager()).thenReturn(chatManager);
Chat chat = mock(Chat.class);
when(chatManager.createChat(Mockito.anyString(), Mockito.any(MessageListener.class))).thenReturn(chat);
channel.send(message);
verify(chat, times(1)).sendMessage("hello");
}
}

View File

@@ -36,8 +36,7 @@ public class XmppMessageSendingMessageHandlerTests {
Chat chat = mock(Chat.class);
when(chantManager.createChat(Mockito.any(String.class), Mockito.any(MessageListener.class))).thenReturn(chat);
XmppMessageSendingMessageHandler handler = new XmppMessageSendingMessageHandler();
handler.setXmppConnection(connection);
XmppMessageSendingMessageHandler handler = new XmppMessageSendingMessageHandler(connection);
Message<?> message = MessageBuilder.withPayload("Test Message").
setHeader(XmppHeaders.CHAT_TO_USER, "kermit@frog.com").
@@ -65,13 +64,13 @@ public class XmppMessageSendingMessageHandlerTests {
@Test(expected=MessageHandlingException.class)
public void validateFailureNoChatToUser() throws Exception{
XmppMessageSendingMessageHandler handler = new XmppMessageSendingMessageHandler();
XmppMessageSendingMessageHandler handler = new XmppMessageSendingMessageHandler(mock(XMPPConnection.class));
handler.handleMessage(new GenericMessage<String>("hello"));
}
@Test(expected=MessageHandlingException.class)
public void validateMessageWithUnsupportedPayload() throws Exception{
XmppMessageSendingMessageHandler handler = new XmppMessageSendingMessageHandler();
XmppMessageSendingMessageHandler handler = new XmppMessageSendingMessageHandler(mock(XMPPConnection.class));
handler.handleMessage(new GenericMessage<Integer>(123));
}
}