Search in sources :

Example 26 with BareJid

use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.

the class RosterIntegrationTest method subscribeRequestListenerTest.

@SmackIntegrationTest
public void subscribeRequestListenerTest() throws TimeoutException, Exception {
    ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
    final SubscribeListener subscribeListener = new SubscribeListener() {

        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(conOne.getUser().asBareJid())) {
                return SubscribeAnswer.Approve;
            }
            return SubscribeAnswer.Deny;
        }
    };
    rosterTwo.addSubscribeListener(subscribeListener);
    final String conTwosRosterName = "ConTwo " + testRunId;
    final SimpleResultSyncPoint addedAndSubscribed = new SimpleResultSyncPoint();
    rosterOne.addRosterListener(new AbstractRosterListener() {

        @Override
        public void entriesAdded(Collection<Jid> addresses) {
            checkIfAddedAndSubscribed(addresses);
        }

        @Override
        public void entriesUpdated(Collection<Jid> addresses) {
            checkIfAddedAndSubscribed(addresses);
        }

        private void checkIfAddedAndSubscribed(Collection<Jid> addresses) {
            for (Jid jid : addresses) {
                if (!jid.equals(conTwo.getUser().asBareJid())) {
                    continue;
                }
                BareJid bareJid = conTwo.getUser().asBareJid();
                RosterEntry rosterEntry = rosterOne.getEntry(bareJid);
                if (rosterEntry == null) {
                    addedAndSubscribed.signalFailure("No roster entry for " + bareJid);
                    return;
                }
                String name = rosterEntry.getName();
                if (StringUtils.isNullOrEmpty(name)) {
                    addedAndSubscribed.signalFailure("Roster entry without name");
                    return;
                }
                if (!rosterEntry.getName().equals(conTwosRosterName)) {
                    addedAndSubscribed.signalFailure("Roster name does not match");
                    return;
                }
                if (!rosterEntry.getType().equals(ItemType.to)) {
                    return;
                }
                addedAndSubscribed.signal();
            }
        }
    });
    try {
        rosterOne.createEntry(conTwo.getUser().asBareJid(), conTwosRosterName, null);
        assertTrue(addedAndSubscribed.waitForResult(2 * connection.getReplyTimeout()));
    } finally {
        rosterTwo.removeSubscribeListener(subscribeListener);
    }
}
Also used : Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) BareJid(org.jxmpp.jid.BareJid) Presence(org.jivesoftware.smack.packet.Presence) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Aggregations

BareJid (org.jxmpp.jid.BareJid)26 RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)9 Test (org.junit.Test)9 Item (org.jivesoftware.smack.roster.packet.RosterPacket.Item)8 Jid (org.jxmpp.jid.Jid)7 EntityBareJid (org.jxmpp.jid.EntityBareJid)6 Presence (org.jivesoftware.smack.packet.Presence)5 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)3 ArrayList (java.util.ArrayList)2 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)2 SmackException (org.jivesoftware.smack.SmackException)2 IQ (org.jivesoftware.smack.packet.IQ)2 IoTIsFriendResponse (org.jivesoftware.smackx.iot.provisioning.element.IoTIsFriendResponse)2 XmlPullParser (org.xmlpull.v1.XmlPullParser)2 File (java.io.File)1 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 ServiceDiscoveryManager (org.jivesoftware.smackx.disco.ServiceDiscoveryManager)1 IoTRemove (org.jivesoftware.smackx.iot.discovery.element.IoTRemove)1