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,40 @@
/*
* 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 org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser;
import org.w3c.dom.Element;
/**
* Parser for 'xmpp:message-outbound-channel-adapter' element
*
* @author Oleg Zhurakousky
* @since 2.0
*/
public class XmppMessageOutboundEndpointParser extends AbstractOutboundChannelAdapterParser {
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.integration.xmpp.messages.XmppMessageSendingMessageHandler");
String connectionName = element.getAttribute("xmpp-connection");
builder.addConstructorArgReference(connectionName);
return builder.getBeanDefinition();
}
}

View File

@@ -17,12 +17,10 @@
package org.springframework.integration.xmpp.config;
import org.jivesoftware.smack.packet.Presence;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser;
import org.springframework.integration.config.xml.HeaderEnricherParserSupport;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.xmpp.XmppHeaders;
@@ -54,20 +52,6 @@ public class XmppNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("header-enricher", new XmppHeaderEnricherParser());
}
// messages
private static class XmppMessageOutboundEndpointParser extends AbstractOutboundChannelAdapterParser {
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
PACKAGE_NAME + ".messages.XmppMessageSendingMessageHandler");
String connectionName = element.getAttribute("xmpp-connection");
Assert.hasText(connectionName, "'xmpp-connection' must be defined");
builder.addPropertyReference("xmppConnection", connectionName);
return builder.getBeanDefinition();
}
}
private static class XmppMessageInboundEndpointParser extends AbstractSingleBeanDefinitionParser {

View File

@@ -16,12 +16,10 @@
package org.springframework.integration.xmpp.messages;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.springframework.context.Lifecycle;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHandlingException;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.xmpp.XmppHeaders;
@@ -34,19 +32,18 @@ import org.springframework.util.StringUtils;
* @author Oleg Zhurakousky
* @since 2.0
*/
public class XmppMessageSendingMessageHandler extends AbstractMessageHandler implements Lifecycle {
public class XmppMessageSendingMessageHandler extends AbstractMessageHandler {
private static final Log logger = LogFactory.getLog(XmppMessageSendingMessageHandler.class);
//private static final Log logger = LogFactory.getLog(XmppMessageSendingMessageHandler.class);
private volatile boolean running;
private volatile XMPPConnection xmppConnection;
public void setXmppConnection(final XMPPConnection xmppConnection) {
private final XMPPConnection xmppConnection;
public XmppMessageSendingMessageHandler(XMPPConnection xmppConnection){
Assert.notNull(xmppConnection, "'xmppConnection' must no be null");
this.xmppConnection = xmppConnection;
}
protected void handleMessageInternal(final org.springframework.integration.Message<?> message) {
// pre-reqs: user to send, string to send as msg body
protected void handleMessageInternal(Message<?> message) {
String messageBody = null;
String destinationUser = null;
Object payload = message.getPayload();
@@ -65,29 +62,12 @@ public class XmppMessageSendingMessageHandler extends AbstractMessageHandler imp
}
}
public boolean isRunning() {
return this.running;
}
public void start() {
this.running = true;
}
public void stop() {
this.running = false;
if (xmppConnection.isConnected()) {
if (logger.isInfoEnabled()) {
logger.info("shutting down XMPP connection");
}
xmppConnection.disconnect();
}
}
private Chat getOrCreateChatWithParticipant(String userId, String thread) {
Chat chat = null;
if (!StringUtils.hasText(thread)) {
chat = xmppConnection.getChatManager().createChat(userId, null);
} else {
}
else {
chat = xmppConnection.getChatManager().getThreadChat(thread);
if (chat == null) {
chat = xmppConnection.getChatManager().createChat(userId, thread, null);

View File

@@ -43,8 +43,6 @@
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
@@ -158,6 +156,9 @@
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="integration:poller" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="channel" use="required" type="xsd:string">
<xsd:annotation>

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));
}
}