IntegrationTestRosterUtil.java

  1. /**
  2.  *
  3.  * Copyright 2015-2019 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.igniterealtime.smack.inttest.util;

  18. import java.util.concurrent.TimeoutException;

  19. import org.jivesoftware.smack.SmackException.NoResponseException;
  20. import org.jivesoftware.smack.SmackException.NotConnectedException;
  21. import org.jivesoftware.smack.SmackException.NotLoggedInException;
  22. import org.jivesoftware.smack.XMPPConnection;
  23. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  24. import org.jivesoftware.smack.packet.Presence;
  25. import org.jivesoftware.smack.packet.StanzaError;
  26. import org.jivesoftware.smack.roster.AbstractPresenceEventListener;
  27. import org.jivesoftware.smack.roster.PresenceEventListener;
  28. import org.jivesoftware.smack.roster.Roster;
  29. import org.jivesoftware.smack.roster.RosterEntry;
  30. import org.jivesoftware.smack.roster.SubscribeListener;

  31. import org.jxmpp.jid.BareJid;
  32. import org.jxmpp.jid.EntityFullJid;
  33. import org.jxmpp.jid.Jid;

  34. public class IntegrationTestRosterUtil {

  35.     public static void ensureBothAccountsAreSubscribedToEachOther(XMPPConnection conOne, XMPPConnection conTwo, long timeout) throws TimeoutException, Exception {
  36.         ensureSubscribedTo(conOne, conTwo, timeout);
  37.         ensureSubscribedTo(conTwo, conOne, timeout);
  38.     }

  39.     public static void ensureSubscribedTo(final XMPPConnection presenceRequestReceiverConnection, final XMPPConnection presenceRequestingConnection, long timeout) throws TimeoutException, Exception {
  40.         final Roster presenceRequestReceiverRoster = Roster.getInstanceFor(presenceRequestReceiverConnection);
  41.         final Roster presenceRequestingRoster = Roster.getInstanceFor(presenceRequestingConnection);

  42.         final EntityFullJid presenceRequestReceiverAddress = presenceRequestReceiverConnection.getUser();
  43.         final EntityFullJid presenceRequestingAddress = presenceRequestingConnection.getUser();

  44.         if (presenceRequestReceiverRoster.isSubscribedToMyPresence(presenceRequestingAddress)) {
  45.             return;
  46.         }

  47.         final SubscribeListener subscribeListener = new SubscribeListener() {
  48.             @Override
  49.             public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
  50.                 if (from.equals(presenceRequestingConnection.getUser().asBareJid())) {
  51.                     return SubscribeAnswer.Approve;
  52.                 }
  53.                 return SubscribeAnswer.Deny;
  54.             }
  55.         };
  56.         presenceRequestReceiverRoster.addSubscribeListener(subscribeListener);

  57.         final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
  58.         final PresenceEventListener presenceEventListener = new AbstractPresenceEventListener() {
  59.             @Override
  60.             public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
  61.                 if (!address.equals(presenceRequestReceiverAddress.asBareJid())) {
  62.                     return;
  63.                 }
  64.                 syncPoint.signal();
  65.             }
  66.         };
  67.         presenceRequestingRoster.addPresenceEventListener(presenceEventListener);

  68.         try {
  69.             presenceRequestingRoster.sendSubscriptionRequest(presenceRequestReceiverAddress.asBareJid());

  70.             syncPoint.waitForResult(timeout, "Timeout while waiting for subscription request of '" + presenceRequestingAddress + "' to '" + presenceRequestReceiverAddress + "' to be answered.");
  71.         } finally {
  72.             presenceRequestReceiverRoster.removeSubscribeListener(subscribeListener);
  73.             presenceRequestingRoster.removePresenceEventListener(presenceEventListener);
  74.         }
  75.     }

  76.     public static void ensureBothAccountsAreNotInEachOthersRoster(XMPPConnection conOne, XMPPConnection conTwo)
  77.             throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException,
  78.             InterruptedException {
  79.         notInRoster(conOne, conTwo);
  80.         notInRoster(conTwo, conOne);
  81.     }

  82.     private static void notInRoster(XMPPConnection c1, XMPPConnection c2) throws NotLoggedInException,
  83.             NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  84.         Roster roster = Roster.getInstanceFor(c1);
  85.         RosterEntry c2Entry = roster.getEntry(c2.getUser().asBareJid());
  86.         if (c2Entry == null) {
  87.             return;
  88.         }
  89.         try {
  90.             roster.removeEntry(c2Entry);
  91.         } catch (XMPPErrorException e) {
  92.             // Account for race conditions: server-sided, the item might already have been removed.
  93.             if (e.getStanzaError().getCondition() == StanzaError.Condition.item_not_found) {
  94.                 // Trying to remove non-existing item. As it needs to be gone, this is fine.
  95.                 return;
  96.             }
  97.             throw e;
  98.         }
  99.     }

  100. }