INT-1648 added support for sending Smack Message
This commit is contained in:
@@ -88,7 +88,7 @@ public class ChatMessageOutboundChannelAdapterParserTests {
|
||||
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");
|
||||
//verify(chat, times(1)).sendMessage("hello");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<?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/context http://www.springframework.org/schema/context/spring-context-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:context="http://www.springframework.org/schema/context"
|
||||
xmlns:int="http://www.springframework.org/schema/integration"
|
||||
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
|
||||
|
||||
<context:property-placeholder location="classpath:test.properties"/>
|
||||
|
||||
<int-xmpp:xmpp-connection
|
||||
id="testConnection"
|
||||
user="${user.1.login}"
|
||||
password="${user.1.password}"
|
||||
host="${user.1.host}"
|
||||
service-name="${user.1.service}"/>
|
||||
|
||||
<int:channel id="xmppInput"/>
|
||||
|
||||
<int-xmpp:outbound-channel-adapter channel="xmppInput" xmpp-connection="testConnection"/>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.ignore;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
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.message.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
*/
|
||||
public class SmackMessageSampleTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void validateSmackMessageSent(){
|
||||
ApplicationContext ac = new ClassPathXmlApplicationContext("SmackMessageSampleTest-context.xml", this.getClass());
|
||||
MessageChannel xmppInput = ac.getBean("xmppInput", MessageChannel.class);
|
||||
|
||||
|
||||
org.jivesoftware.smack.packet.Message smackMessage = new org.jivesoftware.smack.packet.Message("springintegration@gmail.com");
|
||||
smackMessage.setBody("Message sent as Smack Message");
|
||||
|
||||
Message<org.jivesoftware.smack.packet.Message> message =
|
||||
new GenericMessage<org.jivesoftware.smack.packet.Message>(smackMessage);
|
||||
|
||||
xmppInput.send(message);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,11 @@ import org.jivesoftware.smack.ChatManager;
|
||||
import org.jivesoftware.smack.MessageListener;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.internal.verification.api.VerificationData;
|
||||
import org.mockito.verification.VerificationMode;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHandlingException;
|
||||
@@ -47,37 +51,76 @@ public class ChatMessageSendingMessageHandlerTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void validateMessagePost() throws Exception{
|
||||
public void validateMessagePostAsString() throws Exception{
|
||||
XMPPConnection connection = mock(XMPPConnection.class);
|
||||
ChatManager chantManager = mock(ChatManager.class);
|
||||
when(connection.getChatManager()).thenReturn(chantManager);
|
||||
Chat chat = mock(Chat.class);
|
||||
when(chantManager.createChat(Mockito.any(String.class), Mockito.any(MessageListener.class))).thenReturn(chat);
|
||||
|
||||
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler(connection);
|
||||
handler.afterPropertiesSet();
|
||||
Message<?> message = MessageBuilder.withPayload("Test Message").
|
||||
setHeader(XmppHeaders.CHAT_TO, "kermit@frog.com").
|
||||
build();
|
||||
// first Message
|
||||
// first Message new
|
||||
handler.handleMessage(message);
|
||||
|
||||
verify(chantManager, times(1)).createChat(Mockito.any(String.class), Mockito.any(MessageListener.class));
|
||||
verify(chat, times(1)).sendMessage("Test Message");
|
||||
class EqualSmackMessage extends ArgumentMatcher<org.jivesoftware.smack.packet.Message> {
|
||||
public boolean matches(Object msg) {
|
||||
org.jivesoftware.smack.packet.Message smackMessage = (org.jivesoftware.smack.packet.Message) msg;
|
||||
boolean bodyMatches = smackMessage.getBody().equals("Test Message");
|
||||
boolean toMatches = smackMessage.getTo().equals("kermit@frog.com");
|
||||
return bodyMatches & toMatches;
|
||||
}
|
||||
}
|
||||
|
||||
verify(connection, times(1)).sendPacket(Mockito.argThat(new EqualSmackMessage()));
|
||||
|
||||
// assuming we know thread ID although currently we do not provide this capability
|
||||
message = MessageBuilder.withPayload("Hello Kitty").
|
||||
setHeader(XmppHeaders.CHAT_TO, "kermit@frog.com").
|
||||
setHeader(XmppHeaders.CHAT_THREAD_ID, "123").
|
||||
build();
|
||||
reset(chat, chantManager);
|
||||
when(chantManager.getThreadChat("123")).thenReturn(chat);
|
||||
|
||||
class EqualSmackMessageWithThreadId extends ArgumentMatcher<org.jivesoftware.smack.packet.Message> {
|
||||
public boolean matches(Object msg) {
|
||||
org.jivesoftware.smack.packet.Message smackMessage = (org.jivesoftware.smack.packet.Message) msg;
|
||||
boolean bodyMatches = smackMessage.getBody().equals("Hello Kitty");
|
||||
boolean toMatches = smackMessage.getTo().equals("kermit@frog.com");
|
||||
boolean threadIdMatches = smackMessage.getThread().equals("123");
|
||||
return bodyMatches & toMatches & threadIdMatches;
|
||||
}
|
||||
}
|
||||
reset(connection);
|
||||
handler.handleMessage(message);
|
||||
|
||||
// in threaded conversation we need to look for existing chat
|
||||
verify(chantManager, times(0)).createChat(Mockito.any(String.class), Mockito.any(MessageListener.class));
|
||||
verify(chantManager, times(1)).getThreadChat("123");
|
||||
verify(chat, times(1)).sendMessage("Hello Kitty");
|
||||
verify(connection, times(1)).sendPacket(Mockito.argThat(new EqualSmackMessageWithThreadId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateMessagePostAsSmackMessage() throws Exception{
|
||||
XMPPConnection connection = mock(XMPPConnection.class);
|
||||
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler(connection);
|
||||
handler.afterPropertiesSet();
|
||||
|
||||
org.jivesoftware.smack.packet.Message smackMessage = new org.jivesoftware.smack.packet.Message("kermit@frog.com");
|
||||
smackMessage.setBody("Test Message");
|
||||
|
||||
|
||||
Message<?> message = MessageBuilder.withPayload(smackMessage).build();
|
||||
// first Message new
|
||||
handler.handleMessage(message);
|
||||
|
||||
verify(connection, times(1)).sendPacket(smackMessage);
|
||||
|
||||
// assuming we know thread ID although currently we do not provide this capability
|
||||
smackMessage = new org.jivesoftware.smack.packet.Message("kermit@frog.com");
|
||||
smackMessage.setBody("Hello Kitty");
|
||||
smackMessage.setThread("123");
|
||||
message = MessageBuilder.withPayload(smackMessage).build();
|
||||
|
||||
reset(connection);
|
||||
handler.handleMessage(message);
|
||||
|
||||
// in threaded conversation we need to look for existing chat
|
||||
verify(connection, times(1)).sendPacket(smackMessage);
|
||||
}
|
||||
|
||||
@Test(expected=MessageHandlingException.class)
|
||||
|
||||
Reference in New Issue
Block a user