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

import java.util.concurrent.TimeoutException;

import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException.NotLoggedInException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.AbstractPresenceEventListener;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.roster.SubscribeListener;

import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.Jid;

public class IntegrationTestRosterUtil {

    public static void ensureBothAccountsAreSubscribedToEachOther(XMPPConnection conOne, XMPPConnection conTwo, long timeout) throws TimeoutException, Exception {
        ensureSubscribedTo(conOne, conTwo, timeout);
        ensureSubscribedTo(conTwo, conOne, timeout);
    }

    public 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);
        }
    }

    public static void ensureBothAccountsAreNotInEachOthersRoster(XMPPConnection conOne, XMPPConnection conTwo)
            throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException,
            InterruptedException {
        notInRoster(conOne, conTwo);
        notInRoster(conTwo, conOne);
    }

    private static void notInRoster(XMPPConnection c1, XMPPConnection c2) throws NotLoggedInException,
            NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
        Roster roster = Roster.getInstanceFor(c1);
        RosterEntry c2Entry = roster.getEntry(c2.getUser().asBareJid());
        if (c2Entry == null) {
            return;
        }
        roster.removeEntry(c2Entry);
    }

}

@@ -25,9 +25,9 @@
 -201
-8
+9
  Flo
@@ -1118,24 +1118,84 @@
 ntListener;%0A
+import org.jivesoftware.smack.roster.PresenceEventListener;%0A
 import org.j
@@ -1364,16 +1364,52 @@
 areJid;%0A
+import org.jxmpp.jid.EntityFullJid;%0A
 import o
@@ -1808,22 +1808,49 @@
 nection 
-c
+presenceRequestReceiverC
 on
-O
 ne
+ction
 , final 
@@ -1856,38 +1856,60 @@
  XMPPConnection 
-conTwo
+presenceRequestingConnection
 , long timeout) 
@@ -1957,24 +1957,50 @@
     
+final 
 Roster 
-rosterOne
+presenceRequestReceiverRoster
  = R
@@ -2020,22 +2020,49 @@
 anceFor(
-c
+presenceRequestReceiverC
 on
-O
 ne
+ction
 );%0A     
@@ -2068,24 +2068,45 @@
     
+final 
 Roster 
-r
+presenceRequestingR
 oster
-Two
  = R
@@ -2130,39 +2130,284 @@
 For(
-conTwo);%0A%0A        if (r
+presenceRequestingConnection);%0A%0A        final EntityFullJid presenceRequestReceiverAddress = presenceRequestReceiverConnection.getUser();%0A        final EntityFullJid presenceRequestingAddress = presenceRequestingConnection.getUser();%0A%0A        if (presenceRequestReceiverR
 oster
-One
 .isS
@@ -2428,32 +2428,41 @@
 resence(
-conTwo.getUser()
+presenceRequestingAddress
 )) %7B%0A   
@@ -2712,22 +2712,44 @@
 .equals(
-conTwo
+presenceRequestingConnection
 .getUser
@@ -2915,25 +2915,45 @@
         
-rosterOne
+presenceRequestReceiverRoster
 .addSubs
@@ -3076,22 +3076,37 @@
     
-rosterTwo.addP
+final PresenceEventListener p
 rese
@@ -3121,17 +3121,19 @@
 Listener
-(
+ = 
 new Abst
@@ -3311,32 +3311,46 @@
 .equals(
-conOne.getUser()
+presenceRequestReceiverAddress
 .asBareJ
@@ -3463,17 +3463,16 @@
        %7D
-)
 ;%0A      
@@ -3477,58 +3477,188 @@
     
-rosterTwo.sendSubscriptionRequest(conOne.getUser()
+presenceRequestingRoster.addPresenceEventListener(presenceEventListener);%0A%0A        try %7B%0A            presenceRequestingRoster.sendSubscriptionRequest(presenceRequestReceiverAddress
 .asB
@@ -3665,38 +3665,24 @@
 areJid());%0A%0A
-        try %7B%0A
             
@@ -3751,17 +3751,37 @@
     
-r
+presenceRequestReceiverR
 oster
-One
 .rem
@@ -3821,16 +3821,105 @@
 tener);%0A
+            presenceRequestingRoster.removePresenceEventListener(presenceEventListener);%0A
         

Improve IntegrationTestRosterUtil.ensureSubscribedto()