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;
}
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);
}
}
Aggregations