2018-11-11 17:10:40 +01:00
|
|
|
/**
|
|
|
|
*
|
2019-03-16 21:12:05 +01:00
|
|
|
* Copyright 2015-2019 Florian Schmaus
|
2018-11-11 17:10:40 +01:00
|
|
|
*
|
|
|
|
* 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;
|
2019-03-16 21:12:05 +01:00
|
|
|
import org.jivesoftware.smack.roster.PresenceEventListener;
|
2018-11-11 17:10:40 +01:00
|
|
|
import org.jivesoftware.smack.roster.Roster;
|
|
|
|
import org.jivesoftware.smack.roster.RosterEntry;
|
|
|
|
import org.jivesoftware.smack.roster.SubscribeListener;
|
|
|
|
|
|
|
|
import org.jxmpp.jid.BareJid;
|
2019-03-16 21:12:05 +01:00
|
|
|
import org.jxmpp.jid.EntityFullJid;
|
2018-11-11 17:10:40 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-03-16 21:12:05 +01:00
|
|
|
public static void ensureSubscribedTo(final XMPPConnection presenceRequestReceiverConnection, final XMPPConnection presenceRequestingConnection, long timeout) throws TimeoutException, Exception {
|
|
|
|
final Roster presenceRequestReceiverRoster = Roster.getInstanceFor(presenceRequestReceiverConnection);
|
|
|
|
final Roster presenceRequestingRoster = Roster.getInstanceFor(presenceRequestingConnection);
|
2018-11-11 17:10:40 +01:00
|
|
|
|
2019-03-16 21:12:05 +01:00
|
|
|
final EntityFullJid presenceRequestReceiverAddress = presenceRequestReceiverConnection.getUser();
|
|
|
|
final EntityFullJid presenceRequestingAddress = presenceRequestingConnection.getUser();
|
|
|
|
|
|
|
|
if (presenceRequestReceiverRoster.isSubscribedToMyPresence(presenceRequestingAddress)) {
|
2018-11-11 17:10:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final SubscribeListener subscribeListener = new SubscribeListener() {
|
|
|
|
@Override
|
|
|
|
public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
|
2019-03-16 21:12:05 +01:00
|
|
|
if (from.equals(presenceRequestingConnection.getUser().asBareJid())) {
|
2018-11-11 17:10:40 +01:00
|
|
|
return SubscribeAnswer.Approve;
|
|
|
|
}
|
|
|
|
return SubscribeAnswer.Deny;
|
|
|
|
}
|
|
|
|
};
|
2019-03-16 21:12:05 +01:00
|
|
|
presenceRequestReceiverRoster.addSubscribeListener(subscribeListener);
|
2018-11-11 17:10:40 +01:00
|
|
|
|
|
|
|
final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
|
2019-03-16 21:12:05 +01:00
|
|
|
final PresenceEventListener presenceEventListener = new AbstractPresenceEventListener() {
|
2018-11-11 17:10:40 +01:00
|
|
|
@Override
|
|
|
|
public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
|
2019-03-16 21:12:05 +01:00
|
|
|
if (!address.equals(presenceRequestReceiverAddress.asBareJid())) {
|
2018-11-11 17:10:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
syncPoint.signal();
|
|
|
|
}
|
2019-03-16 21:12:05 +01:00
|
|
|
};
|
|
|
|
presenceRequestingRoster.addPresenceEventListener(presenceEventListener);
|
2018-11-11 17:10:40 +01:00
|
|
|
|
|
|
|
try {
|
2019-03-16 21:12:05 +01:00
|
|
|
presenceRequestingRoster.sendSubscriptionRequest(presenceRequestReceiverAddress.asBareJid());
|
|
|
|
|
2018-11-11 17:10:40 +01:00
|
|
|
syncPoint.waitForResult(timeout);
|
|
|
|
} finally {
|
2019-03-16 21:12:05 +01:00
|
|
|
presenceRequestReceiverRoster.removeSubscribeListener(subscribeListener);
|
|
|
|
presenceRequestingRoster.removePresenceEventListener(presenceEventListener);
|
2018-11-11 17:10:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|