Search in sources :

Example 1 with StanzaFactory

use of org.jivesoftware.smack.packet.StanzaFactory 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)

Example 2 with StanzaFactory

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

the class MultipleRecipientManager method sendToIndividualRecipients.

private static void sendToIndividualRecipients(XMPPConnection connection, StanzaView stanza, Collection<? extends Jid> to, Collection<? extends Jid> cc, Collection<? extends Jid> bcc) throws NotConnectedException, InterruptedException {
    final StanzaFactory stanzaFactory = connection.getStanzaFactory();
    final StanzaBuilder<?> stanzaBuilder;
    if (stanza instanceof Message) {
        Message message = (Message) stanza;
        stanzaBuilder = stanzaFactory.buildMessageStanzaFrom(message);
    } else if (stanza instanceof Presence) {
        Presence presence = (Presence) stanza;
        stanzaBuilder = stanzaFactory.buildPresenceStanzaFrom(presence);
    } else if (stanza instanceof IQ) {
        throw new IllegalArgumentException("IQ stanzas have no supported fallback in case no XEP-0033 service is available");
    } else {
        throw new AssertionError();
    }
    if (to == null)
        to = Collections.emptyList();
    if (cc == null)
        cc = Collections.emptyList();
    if (bcc == null)
        bcc = Collections.emptyList();
    final int numRecipients = to.size() + cc.size() + bcc.size();
    final List<Jid> recipients = new ArrayList<>(numRecipients);
    recipients.addAll(to);
    recipients.addAll(cc);
    recipients.addAll(bcc);
    final List<Stanza> stanzasToSend = new ArrayList<>(numRecipients);
    for (Jid recipient : recipients) {
        Stanza stanzaToSend = stanzaBuilder.to(recipient).build();
        stanzasToSend.add(stanzaToSend);
    }
    // TODO: Use XMPPConnection.sendStanzas(Collection<? extends Stanza>) once this method exists.
    for (Stanza stanzaToSend : stanzasToSend) {
        connection.sendStanza(stanzaToSend);
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) ArrayList(java.util.ArrayList) StanzaFactory(org.jivesoftware.smack.packet.StanzaFactory) Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

IQ (org.jivesoftware.smack.packet.IQ)2 Stanza (org.jivesoftware.smack.packet.Stanza)2 StanzaFactory (org.jivesoftware.smack.packet.StanzaFactory)2 DomainBareJid (org.jxmpp.jid.DomainBareJid)2 ArrayList (java.util.ArrayList)1 StanzaCollector (org.jivesoftware.smack.StanzaCollector)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)1 Message (org.jivesoftware.smack.packet.Message)1 Presence (org.jivesoftware.smack.packet.Presence)1 StandardStanzaIdSource (org.jivesoftware.smack.packet.id.StandardStanzaIdSource)1 EntityBareJid (org.jxmpp.jid.EntityBareJid)1 EntityFullJid (org.jxmpp.jid.EntityFullJid)1 Jid (org.jxmpp.jid.Jid)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1