Search in sources :

Example 36 with Stanza

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());
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) XmlElement(org.jivesoftware.smack.packet.XmlElement) NamedElement(org.jivesoftware.smack.packet.NamedElement) Test(org.junit.jupiter.api.Test)

Example 37 with Stanza

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());
}
Also used : DummyConnection(org.jivesoftware.smack.DummyConnection) ThreadedDummyConnection(org.jivesoftware.smack.ThreadedDummyConnection) Ping(org.jivesoftware.smackx.ping.packet.Ping) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.jupiter.api.Test)

Example 38 with Stanza

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;
}
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 39 with Stanza

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();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaListener(org.jivesoftware.smack.StanzaListener) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)

Example 40 with Stanza

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);
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Aggregations

Stanza (org.jivesoftware.smack.packet.Stanza)101 StanzaListener (org.jivesoftware.smack.StanzaListener)24 Test (org.junit.Test)22 IQ (org.jivesoftware.smack.packet.IQ)20 Test (org.junit.jupiter.api.Test)18 XMPPConnection (org.jivesoftware.smack.XMPPConnection)14 Message (org.jivesoftware.smack.packet.Message)14 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 Jid (org.jxmpp.jid.Jid)11 IOException (java.io.IOException)9 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)8 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)8 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)7 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)7 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)6 Protocol (org.jivesoftware.util.Protocol)6 EntityFullJid (org.jxmpp.jid.EntityFullJid)6 LinkedList (java.util.LinkedList)5