use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class ItemValidationTest method parseBasicItem.
@Test
public void parseBasicItem() throws Exception {
XmlPullParser parser = PacketParserUtils.getParserFor("<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" + "<event xmlns='http://jabber.org/protocol/pubsub#event'>" + "<items node='testNode'>" + "<item id='testid1' />" + "</items>" + "</event>" + "</message>");
Stanza message = PacketParserUtils.parseMessage(parser);
XmlElement eventExt = message.getExtension(PubSubNamespace.event.getXmlns());
assertTrue(eventExt instanceof EventElement);
EventElement event = (EventElement) eventExt;
assertEquals(EventElementType.items, event.getEventType());
assertEquals(1, event.getExtensions().size());
assertTrue(event.getExtensions().get(0) instanceof ItemsExtension);
assertEquals(1, ((ItemsExtension) event.getExtensions().get(0)).items.size());
NamedElement itemExt = ((ItemsExtension) event.getExtensions().get(0)).items.get(0);
assertTrue(itemExt instanceof Item);
assertEquals("testid1", ((Item) itemExt).getId());
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class PingTest method checkProvider.
@Test
public void checkProvider() throws Exception {
// @formatter:off
String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>" + "<ping xmlns='urn:xmpp:ping'/>" + "</iq>";
// @formatter:on
DummyConnection con = new DummyConnection();
con.connect();
// Enable ping for this connection
PingManager.getInstanceFor(con);
IQ pingRequest = PacketParserUtils.parseStanza(control);
assertTrue(pingRequest instanceof Ping);
con.processStanza(pingRequest);
Stanza pongPacket = con.getSentPacket();
assertTrue(pongPacket instanceof IQ);
IQ pong = (IQ) pongPacket;
assertThat("capulet.lit", equalsCharSequence(pong.getTo()));
assertEquals("s2c1", pong.getStanzaId());
assertEquals(IQ.Type.result, pong.getType());
}
use of org.jivesoftware.smack.packet.Stanza 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.Stanza in project Smack by igniterealtime.
the class AbstractSmackIntegrationTest method performActionAndWaitForPresence.
/**
* Perform action and wait until conA observes a presence form conB.
* <p>
* This method is usually used so that 'action' performs an operation that changes one entities
* features/nodes/capabilities, and we want to check that another connection is able to observe this change, and use
* that new "thing" that was added to the connection.
* </p>
* <p>
* Note that this method is a workaround at best and not reliable. Because it is not guaranteed that any XEP-0030
* related manager, e.g. EntityCapsManager, already processed the presence when this method returns.
* </p>
* TODO: Come up with a better solution.
*
* @param conA the connection to observe the presence on.
* @param conB the connection sending the presence
* @param action the action to perform.
* @throws Exception in case of an exception.
*/
@SuppressWarnings("ThreadPriorityCheck")
protected void performActionAndWaitForPresence(XMPPConnection conA, XMPPConnection conB, ThrowingRunnable action) throws Exception {
final SimpleResultSyncPoint presenceReceivedSyncPoint = new SimpleResultSyncPoint();
final StanzaListener presenceListener = new StanzaListener() {
@Override
public void processStanza(Stanza packet) {
presenceReceivedSyncPoint.signal();
}
};
// Add a stanzaListener to listen for incoming presence
conA.addAsyncStanzaListener(presenceListener, new AndFilter(PresenceTypeFilter.AVAILABLE, FromMatchesFilter.create(conB.getUser())));
action.runOrThrow();
try {
// wait for the dummy feature to get sent via presence
presenceReceivedSyncPoint.waitForResult(timeout);
} finally {
conA.removeAsyncStanzaListener(presenceListener);
}
// TODO: Ugly hack to make tests using this method more reliable. Ideally no test would use this method.
Thread.yield();
}
use of org.jivesoftware.smack.packet.Stanza in project Smack by igniterealtime.
the class ChatConnectionTest method chatFoundWithSameThreadEntityFullJid.
/**
* Confirm that an existing chat created with a base jid is matched to an incoming chat message that has the same id
* and the user is a full jid.
*/
@Test
public void chatFoundWithSameThreadEntityFullJid() {
Chat outgoing = cm.createChat(JidTestUtil.DUMMY_AT_EXAMPLE_ORG, null);
Stanza incomingChat = createChatMessage(outgoing.getThreadID(), true);
processServerMessage(incomingChat);
Chat newChat = listener.getNewChat();
assertNotNull(newChat);
assertTrue(newChat == outgoing);
}
Aggregations