2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 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.jivesoftware.util;
|
|
|
|
|
2014-08-15 23:16:18 +02:00
|
|
|
import static org.mockito.Matchers.anyInt;
|
|
|
|
import static org.mockito.Matchers.anyLong;
|
|
|
|
import static org.mockito.Matchers.isA;
|
|
|
|
import static org.mockito.Mockito.doAnswer;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.when;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
import org.jivesoftware.smack.PacketCollector;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2014-03-10 09:45:50 +01:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
2014-02-23 21:08:35 +01:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2014-02-18 15:05:19 +01:00
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.DomainBareJid;
|
|
|
|
import org.jxmpp.jid.FullJid;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.mockito.invocation.InvocationOnMock;
|
|
|
|
import org.mockito.stubbing.Answer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A collection of utility methods to create mocked XMPP connections.
|
|
|
|
*
|
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
|
|
|
public class ConnectionUtils {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a mocked XMPP connection that stores every packet that is send over this
|
|
|
|
* connection in the given protocol instance and returns the predefined answer packets
|
|
|
|
* form the protocol instance.
|
|
|
|
* <p>
|
|
|
|
* This mocked connection can used to collect packets that require a reply using a
|
|
|
|
* PacketCollector.
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* <code>
|
|
|
|
* PacketCollector collector = connection.createPacketCollector(new PacketFilter());
|
|
|
|
* connection.sendPacket(packet);
|
|
|
|
* Packet reply = collector.nextResult();
|
|
|
|
* </code>
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @param protocol protocol helper containing answer packets
|
|
|
|
* @param initiatorJID the user associated to the XMPP connection
|
|
|
|
* @param xmppServer the XMPP server associated to the XMPP connection
|
|
|
|
* @return a mocked XMPP connection
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException
|
|
|
|
* @throws XMPPErrorException
|
2015-02-14 09:43:44 +01:00
|
|
|
* @throws InterruptedException
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-03-10 09:45:50 +01:00
|
|
|
public static XMPPConnection createMockedConnection(final Protocol protocol,
|
2015-02-14 17:15:02 +01:00
|
|
|
FullJid initiatorJID, DomainBareJid xmppServer) throws SmackException, XMPPErrorException, InterruptedException {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// mock XMPP connection
|
2014-03-10 09:45:50 +01:00
|
|
|
XMPPConnection connection = mock(XMPPConnection.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
when(connection.getUser()).thenReturn(initiatorJID);
|
|
|
|
when(connection.getServiceName()).thenReturn(xmppServer);
|
|
|
|
|
|
|
|
// mock packet collector
|
2014-02-18 15:05:19 +01:00
|
|
|
final PacketCollector collector = mock(PacketCollector.class);
|
2014-02-17 23:58:40 +01:00
|
|
|
when(connection.createPacketCollector(isA(PacketFilter.class))).thenReturn(
|
|
|
|
collector);
|
2014-02-18 15:05:19 +01:00
|
|
|
Answer<PacketCollector> collectorAndSend = new Answer<PacketCollector>() {
|
|
|
|
@Override
|
|
|
|
public PacketCollector answer(InvocationOnMock invocation) throws Throwable {
|
2015-02-05 11:17:27 +01:00
|
|
|
Stanza packet = (Stanza) invocation.getArguments()[0];
|
2014-02-18 15:05:19 +01:00
|
|
|
protocol.getRequests().add(packet);
|
|
|
|
return collector;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2014-02-23 21:08:35 +01:00
|
|
|
when(connection.createPacketCollectorAndSend(isA(IQ.class))).thenAnswer(collectorAndSend);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-02-18 15:05:19 +01:00
|
|
|
// mock send method
|
|
|
|
Answer<Object> addIncoming = new Answer<Object>() {
|
2014-02-17 23:58:40 +01:00
|
|
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
2015-02-05 11:17:27 +01:00
|
|
|
protocol.getRequests().add((Stanza) invocation.getArguments()[0]);
|
2014-02-17 23:58:40 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
2015-02-05 11:17:27 +01:00
|
|
|
doAnswer(addIncoming).when(connection).sendPacket(isA(Stanza.class));
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-02-18 15:05:19 +01:00
|
|
|
// mock receive methods
|
2015-02-05 11:17:27 +01:00
|
|
|
Answer<Stanza> answer = new Answer<Stanza>() {
|
|
|
|
public Stanza answer(InvocationOnMock invocation) throws Throwable {
|
2014-02-17 23:58:40 +01:00
|
|
|
return protocol.getResponses().poll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
when(collector.nextResult(anyInt())).thenAnswer(answer);
|
|
|
|
when(collector.nextResult()).thenAnswer(answer);
|
2015-02-05 11:17:27 +01:00
|
|
|
Answer<Stanza> answerOrThrow = new Answer<Stanza>() {
|
2014-02-18 15:05:19 +01:00
|
|
|
@Override
|
2015-02-05 11:17:27 +01:00
|
|
|
public Stanza answer(InvocationOnMock invocation) throws Throwable {
|
|
|
|
Stanza packet = protocol.getResponses().poll();
|
2014-02-18 15:05:19 +01:00
|
|
|
if (packet == null) return packet;
|
|
|
|
XMPPError xmppError = packet.getError();
|
2014-03-12 11:50:05 +01:00
|
|
|
if (xmppError != null) throw new XMPPErrorException(xmppError);
|
2014-02-18 15:05:19 +01:00
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
when(collector.nextResultOrThrow()).thenAnswer(answerOrThrow);
|
|
|
|
when(collector.nextResultOrThrow(anyLong())).thenAnswer(answerOrThrow);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
// initialize service discovery manager for this connection
|
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|