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;
|
|
|
|
|
2018-05-09 20:16:05 +02:00
|
|
|
import static org.mockito.ArgumentMatchers.anyInt;
|
|
|
|
import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
|
import static org.mockito.ArgumentMatchers.isA;
|
2014-08-15 23:16:18 +02:00
|
|
|
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
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.StanzaCollector;
|
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;
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.filter.StanzaFilter;
|
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;
|
2019-10-29 10:40:41 +01:00
|
|
|
import org.jivesoftware.smack.packet.StanzaFactory;
|
|
|
|
import org.jivesoftware.smack.packet.id.StandardStanzaIdSource;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.DomainBareJid;
|
2015-05-27 19:29:51 +02:00
|
|
|
import org.jxmpp.jid.EntityFullJid;
|
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.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
|
|
|
public class ConnectionUtils {
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Creates a mocked XMPP connection that stores every stanza that is send over this
|
2014-02-17 23:58:40 +01:00
|
|
|
* 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
|
2017-01-03 11:12:34 +01:00
|
|
|
* StanzaCollector.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* <pre>
|
|
|
|
* <code>
|
2017-01-03 11:12:34 +01:00
|
|
|
* StanzaCollector collector = connection.createStanzaCollector(new PacketFilter());
|
2015-03-04 21:44:43 +01:00
|
|
|
* connection.sendStanza(packet);
|
2018-04-23 21:06:35 +02:00
|
|
|
* Stanza reply = collector.nextResult();
|
2014-02-17 23:58:40 +01:00
|
|
|
* </code>
|
|
|
|
* </pre>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @param protocol protocol helper containing answer packets
|
|
|
|
* @param initiatorJID the user associated to the XMPP connection
|
|
|
|
* @return a mocked XMPP connection
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws SmackException if Smack detected an exceptional situation.
|
|
|
|
* @throws XMPPErrorException if there was an XMPP error returned.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-03-10 09:45:50 +01:00
|
|
|
public static XMPPConnection createMockedConnection(final Protocol protocol,
|
2018-04-18 14:10:14 +02:00
|
|
|
EntityFullJid initiatorJID) throws SmackException, XMPPErrorException, InterruptedException {
|
|
|
|
|
|
|
|
DomainBareJid xmppServer = initiatorJID.asDomainBareJid();
|
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);
|
2015-05-18 16:48:23 +02:00
|
|
|
when(connection.getXMPPServiceDomain()).thenReturn(xmppServer);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2019-10-29 10:40:41 +01:00
|
|
|
final StanzaFactory stanzaFactory = new StanzaFactory(new StandardStanzaIdSource());
|
|
|
|
when(connection.getStanzaFactory()).thenReturn(stanzaFactory);
|
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
// mock packet collector
|
2017-01-03 11:12:34 +01:00
|
|
|
final StanzaCollector collector = mock(StanzaCollector.class);
|
|
|
|
when(connection.createStanzaCollector(isA(StanzaFilter.class))).thenReturn(
|
2014-02-17 23:58:40 +01:00
|
|
|
collector);
|
2017-01-03 11:12:34 +01:00
|
|
|
Answer<StanzaCollector> collectorAndSend = new Answer<StanzaCollector>() {
|
2014-02-18 15:05:19 +01:00
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public StanzaCollector 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2017-01-03 11:12:34 +01:00
|
|
|
when(connection.createStanzaCollectorAndSend(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>() {
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
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-03-04 21:44:43 +01:00
|
|
|
doAnswer(addIncoming).when(connection).sendStanza(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>() {
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2015-02-05 11:17:27 +01:00
|
|
|
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;
|
2016-11-29 16:40:08 +01:00
|
|
|
XMPPErrorException.ifHasErrorThenThrow(packet);
|
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
|
|
|
|
2021-05-11 22:03:24 +02:00
|
|
|
Answer<IQ> responseIq = new Answer<IQ>() {
|
|
|
|
@Override
|
|
|
|
public IQ answer(InvocationOnMock invocation) throws Throwable {
|
|
|
|
collectorAndSend.answer(invocation);
|
|
|
|
|
|
|
|
IQ response = (IQ) answerOrThrow.answer(invocation);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
when(connection.sendIqRequestAndWaitForResponse(isA(IQ.class))).thenAnswer(responseIq);
|
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
// initialize service discovery manager for this connection
|
|
|
|
ServiceDiscoveryManager.getInstanceFor(connection);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|