Search in sources :

Example 1 with SimpleResultSyncPoint

use of org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint in project Smack by igniterealtime.

the class LowLevelRosterIntegrationTest method testPresenceEventListenersOffline.

@SmackIntegrationTest
public void testPresenceEventListenersOffline(final XMPPTCPConnection conOne, final XMPPTCPConnection conTwo) throws TimeoutException, Exception {
    RosterIntegrationTest.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
    final Roster rosterOne = Roster.getInstanceFor(conOne);
    final Roster rosterTwo = Roster.getInstanceFor(conTwo);
    // TODO create Roster.createEntry() with boolean flag for subscribe or not.
    rosterOne.createEntry(conTwo.getUser().asBareJid(), "Con Two", null);
    rosterTwo.createEntry(conOne.getUser().asBareJid(), "Con One", null);
    // TODO Change timeout form '5000' to something configurable.
    final long timeout = 5000;
    RosterIntegrationTest.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
    final SimpleResultSyncPoint offlineTriggered = new SimpleResultSyncPoint();
    rosterOne.addPresenceEventListener(new AbstractPresenceEventListener() {

        @Override
        public void presenceUnavailable(FullJid jid, Presence presence) {
            if (!jid.equals(conTwo.getUser())) {
                return;
            }
            offlineTriggered.signal();
        }
    });
    // Disconnect conTwo, this should cause an 'unavilable' presence to be send from conTwo to
    // conOne.
    conTwo.disconnect();
    Boolean result = offlineTriggered.waitForResult(timeout);
    if (!result) {
        throw new Exception("presenceUnavailable() was not called");
    }
}
Also used : FullJid(org.jxmpp.jid.FullJid) Presence(org.jivesoftware.smack.packet.Presence) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) TimeoutException(java.util.concurrent.TimeoutException) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 2 with SimpleResultSyncPoint

use of org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint in project Smack by igniterealtime.

the class RosterIntegrationTest method ensureSubscribedTo.

private static void ensureSubscribedTo(final XMPPConnection conOne, final XMPPConnection conTwo, long timeout) throws TimeoutException, Exception {
    Roster rosterOne = Roster.getInstanceFor(conOne);
    Roster rosterTwo = Roster.getInstanceFor(conTwo);
    if (rosterOne.isSubscribedToMyPresence(conTwo.getUser())) {
        return;
    }
    final SubscribeListener subscribeListener = new SubscribeListener() {

        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(conTwo.getUser().asBareJid())) {
                return SubscribeAnswer.Approve;
            }
            return SubscribeAnswer.Deny;
        }
    };
    rosterOne.addSubscribeListener(subscribeListener);
    final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
    rosterTwo.addPresenceEventListener(new AbstractPresenceEventListener() {

        @Override
        public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
            if (!address.equals(conOne.getUser().asBareJid())) {
                return;
            }
            syncPoint.signal();
        }
    });
    rosterTwo.sendSubscriptionRequest(conOne.getUser().asBareJid());
    try {
        syncPoint.waitForResult(timeout);
    } finally {
        rosterOne.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)

Example 3 with SimpleResultSyncPoint

use of org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint in project Smack by igniterealtime.

the class IoTControlIntegrationTest method controlTest.

/**
     * Connection one provides a thing, which is controlled by connection two.
     *
     * @throws Exception 
     * @throws TimeoutException 
     */
@SmackIntegrationTest
public // @SmackSerialIntegrationTest
void controlTest() throws TimeoutException, Exception {
    final String key = StringUtils.randomString(12);
    final String sn = StringUtils.randomString(12);
    final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
    Thing controlThing = Thing.builder().setKey(key).setSerialNumber(sn).setControlRequestHandler(new ThingControlRequest() {

        @Override
        public void processRequest(Jid from, Collection<SetData> setData) throws XMPPErrorException {
            if (!from.equals(conTwo.getUser())) {
                return;
            }
            for (final SetData data : setData) {
                if (!data.getName().equals(testRunId))
                    continue;
                if (!(data instanceof SetBoolData))
                    continue;
                SetBoolData boolData = (SetBoolData) data;
                if (boolData.getBooleanValue()) {
                    syncPoint.signal();
                    break;
                }
            }
        }
    }).build();
    IoTControlManagerOne.installThing(controlThing);
    try {
        RosterIntegrationTest.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
        SetData data = new SetBoolData(testRunId, true);
        IoTSetResponse response = IoTControlManagerTwo.setUsingIq(conOne.getUser(), data);
        assertNotNull(response);
    } finally {
        IoTControlManagerOne.uninstallThing(controlThing);
        RosterIntegrationTest.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
    }
    syncPoint.waitForResult(timeout);
}
Also used : Jid(org.jxmpp.jid.Jid) ThingControlRequest(org.jivesoftware.smackx.iot.control.ThingControlRequest) Collection(java.util.Collection) SetBoolData(org.jivesoftware.smackx.iot.control.element.SetBoolData) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) SetData(org.jivesoftware.smackx.iot.control.element.SetData) IoTSetResponse(org.jivesoftware.smackx.iot.control.element.IoTSetResponse) AbstractSmackIntegrationTest(org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 4 with SimpleResultSyncPoint

use of org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint 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

SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)4 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)3 Presence (org.jivesoftware.smack.packet.Presence)3 Jid (org.jxmpp.jid.Jid)3 AbstractSmackIntegrationTest (org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest)2 BareJid (org.jxmpp.jid.BareJid)2 Collection (java.util.Collection)1 TimeoutException (java.util.concurrent.TimeoutException)1 ThingControlRequest (org.jivesoftware.smackx.iot.control.ThingControlRequest)1 IoTSetResponse (org.jivesoftware.smackx.iot.control.element.IoTSetResponse)1 SetBoolData (org.jivesoftware.smackx.iot.control.element.SetBoolData)1 SetData (org.jivesoftware.smackx.iot.control.element.SetData)1 FullJid (org.jxmpp.jid.FullJid)1