2015-05-21 20:12:39 +02:00
|
|
|
/**
|
|
|
|
*
|
2019-02-04 08:59:39 +01:00
|
|
|
* Copyright 2015-2019 Florian Schmaus
|
2015-05-21 20:12:39 +02: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.jivesoftware.smack.roster;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
|
|
import org.jivesoftware.smack.roster.packet.RosterPacket.ItemType;
|
2016-12-23 23:41:31 +01:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
|
|
|
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
|
|
|
|
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
|
|
|
|
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
|
2018-11-11 17:10:40 +01:00
|
|
|
import org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
|
2016-06-30 17:01:46 +02:00
|
|
|
import org.jxmpp.jid.BareJid;
|
2015-05-21 20:12:39 +02:00
|
|
|
import org.jxmpp.jid.Jid;
|
|
|
|
|
|
|
|
public class RosterIntegrationTest extends AbstractSmackIntegrationTest {
|
|
|
|
|
|
|
|
private final Roster rosterOne;
|
|
|
|
private final Roster rosterTwo;
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
public RosterIntegrationTest(SmackIntegrationTestEnvironment<?> environment) {
|
2015-05-21 20:12:39 +02:00
|
|
|
super(environment);
|
|
|
|
rosterOne = Roster.getInstanceFor(conOne);
|
|
|
|
rosterTwo = Roster.getInstanceFor(conTwo);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SmackIntegrationTest
|
|
|
|
public void subscribeRequestListenerTest() throws TimeoutException, Exception {
|
2018-11-11 17:10:40 +01:00
|
|
|
IntegrationTestRosterUtil.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
|
2015-05-21 20:12:39 +02:00
|
|
|
|
2016-07-31 14:30:31 +02:00
|
|
|
final SubscribeListener subscribeListener = new SubscribeListener() {
|
2015-05-21 20:12:39 +02:00
|
|
|
@Override
|
|
|
|
public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
|
|
|
|
if (from.equals(conOne.getUser().asBareJid())) {
|
|
|
|
return SubscribeAnswer.Approve;
|
|
|
|
}
|
|
|
|
return SubscribeAnswer.Deny;
|
|
|
|
}
|
2016-07-31 14:30:31 +02:00
|
|
|
};
|
|
|
|
rosterTwo.addSubscribeListener(subscribeListener);
|
2015-05-21 20:12:39 +02:00
|
|
|
|
|
|
|
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) {
|
2015-06-06 09:00:19 +02:00
|
|
|
if (!jid.equals(conTwo.getUser().asBareJid())) {
|
2015-05-21 20:12:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-12-23 23:41:31 +01:00
|
|
|
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;
|
|
|
|
}
|
2015-05-21 20:12:39 +02:00
|
|
|
if (!rosterEntry.getName().equals(conTwosRosterName)) {
|
|
|
|
addedAndSubscribed.signalFailure("Roster name does not match");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!rosterEntry.getType().equals(ItemType.to)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addedAndSubscribed.signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-07-31 14:30:31 +02:00
|
|
|
try {
|
2019-03-16 21:13:00 +01:00
|
|
|
rosterOne.createItemAndRequestSubscription(conTwo.getUser().asBareJid(), conTwosRosterName, null);
|
2016-07-31 14:30:31 +02:00
|
|
|
|
2017-01-12 20:57:19 +01:00
|
|
|
assertTrue(addedAndSubscribed.waitForResult(2 * connection.getReplyTimeout()));
|
2016-07-31 14:30:31 +02:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
rosterTwo.removeSubscribeListener(subscribeListener);
|
|
|
|
}
|
2015-05-21 20:12:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|