Search in sources :

Example 1 with StandardStanzaIdSource

use of org.jivesoftware.smack.packet.id.StandardStanzaIdSource in project Smack by igniterealtime.

the class ConnectionUtils method createMockedConnection.

/**
 * Creates a mocked XMPP connection that stores every stanza 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
 * StanzaCollector.
 *
 * <pre>
 * <code>
 *   StanzaCollector collector = connection.createStanzaCollector(new PacketFilter());
 *   connection.sendStanza(packet);
 *   Stanza reply = collector.nextResult();
 * </code>
 * </pre>
 *
 * @param protocol protocol helper containing answer packets
 * @param initiatorJID the user associated to the XMPP connection
 * @return a mocked XMPP connection
 * @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.
 */
public static XMPPConnection createMockedConnection(final Protocol protocol, EntityFullJid initiatorJID) throws SmackException, XMPPErrorException, InterruptedException {
    DomainBareJid xmppServer = initiatorJID.asDomainBareJid();
    // mock XMPP connection
    XMPPConnection connection = mock(XMPPConnection.class);
    when(connection.getUser()).thenReturn(initiatorJID);
    when(connection.getXMPPServiceDomain()).thenReturn(xmppServer);
    final StanzaFactory stanzaFactory = new StanzaFactory(new StandardStanzaIdSource());
    when(connection.getStanzaFactory()).thenReturn(stanzaFactory);
    // mock packet collector
    final StanzaCollector collector = mock(StanzaCollector.class);
    when(connection.createStanzaCollector(isA(StanzaFilter.class))).thenReturn(collector);
    Answer<StanzaCollector> collectorAndSend = new Answer<StanzaCollector>() {

        @Override
        public StanzaCollector answer(InvocationOnMock invocation) throws Throwable {
            Stanza packet = (Stanza) invocation.getArguments()[0];
            protocol.getRequests().add(packet);
            return collector;
        }
    };
    when(connection.createStanzaCollectorAndSend(isA(IQ.class))).thenAnswer(collectorAndSend);
    // mock send method
    Answer<Object> addIncoming = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            protocol.getRequests().add((Stanza) invocation.getArguments()[0]);
            return null;
        }
    };
    doAnswer(addIncoming).when(connection).sendStanza(isA(Stanza.class));
    // mock receive methods
    Answer<Stanza> answer = new Answer<Stanza>() {

        @Override
        public Stanza answer(InvocationOnMock invocation) throws Throwable {
            return protocol.getResponses().poll();
        }
    };
    when(collector.nextResult(anyInt())).thenAnswer(answer);
    when(collector.nextResult()).thenAnswer(answer);
    Answer<Stanza> answerOrThrow = new Answer<Stanza>() {

        @Override
        public Stanza answer(InvocationOnMock invocation) throws Throwable {
            Stanza packet = protocol.getResponses().poll();
            if (packet == null)
                return packet;
            XMPPErrorException.ifHasErrorThenThrow(packet);
            return packet;
        }
    };
    when(collector.nextResultOrThrow()).thenAnswer(answerOrThrow);
    when(collector.nextResultOrThrow(anyLong())).thenAnswer(answerOrThrow);
    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);
    // initialize service discovery manager for this connection
    ServiceDiscoveryManager.getInstanceFor(connection);
    return connection;
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) StandardStanzaIdSource(org.jivesoftware.smack.packet.id.StandardStanzaIdSource) StanzaFactory(org.jivesoftware.smack.packet.StanzaFactory) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StanzaCollector(org.jivesoftware.smack.StanzaCollector) DomainBareJid(org.jxmpp.jid.DomainBareJid)

Aggregations

StanzaCollector (org.jivesoftware.smack.StanzaCollector)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)1 IQ (org.jivesoftware.smack.packet.IQ)1 Stanza (org.jivesoftware.smack.packet.Stanza)1 StanzaFactory (org.jivesoftware.smack.packet.StanzaFactory)1 StandardStanzaIdSource (org.jivesoftware.smack.packet.id.StandardStanzaIdSource)1 DomainBareJid (org.jxmpp.jid.DomainBareJid)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1