use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class RosterTest method testAddRosterItem.
/**
* Test adding a roster item according to the example in
* <a href="http://xmpp.org/rfcs/rfc3921.html#roster-add"
* >RFC3921: Adding a Roster Item</a>.
*/
@Test
public void testAddRosterItem() throws Throwable {
// Constants for the new contact
final BareJid contactJID = JidCreate.entityBareFrom("nurse@example.com");
final String contactName = "Nurse";
final String[] contactGroup = { "Servants" };
// Setup
assertNotNull("Can't get the roster from the provided connection!", roster);
initRoster();
rosterListener.reset();
// Adding the new roster item
final RosterUpdateResponder serverSimulator = new RosterUpdateResponder() {
@Override
void verifyUpdateRequest(final RosterPacket updateRequest) {
final Item item = updateRequest.getRosterItems().iterator().next();
assertEquals("The provided JID doesn't match the requested!", contactJID, item.getJid());
assertSame("The provided name doesn't match the requested!", contactName, item.getName());
assertSame("The provided group number doesn't match the requested!", contactGroup.length, item.getGroupNames().size());
assertSame("The provided group doesn't match the requested!", contactGroup[0], item.getGroupNames().iterator().next());
}
};
serverSimulator.start();
roster.createEntry(contactJID, contactName, contactGroup);
serverSimulator.join();
// Check if an error occurred within the simulator
final Throwable exception = serverSimulator.getException();
if (exception != null) {
throw exception;
}
rosterListener.waitUntilInvocationOrTimeout();
// Verify the roster entry of the new contact
final RosterEntry addedEntry = roster.getEntry(contactJID);
assertNotNull("The new contact wasn't added to the roster!", addedEntry);
assertTrue("The roster listener wasn't invoked for the new contact!", rosterListener.getAddedAddresses().contains(contactJID));
assertSame("Setup wrong name for the new contact!", contactName, addedEntry.getName());
assertSame("Setup wrong default subscription status!", ItemType.none, addedEntry.getType());
assertSame("The new contact should be member of exactly one group!", 1, addedEntry.getGroups().size());
assertSame("Setup wrong group name for the added contact!", contactGroup[0], addedEntry.getGroups().iterator().next().getName());
// Verify the unchanged roster items
verifyRomeosEntry(roster.getEntry(JidCreate.entityBareFrom("romeo@example.net")));
verifyMercutiosEntry(roster.getEntry(JidCreate.entityBareFrom("mercutio@example.com")));
verifyBenvoliosEntry(roster.getEntry(JidCreate.entityBareFrom("benvolio@example.net")));
assertSame("Wrong number of roster entries.", 4, roster.getEntries().size());
}
use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class RosterTest method testUpdateRosterItem.
/**
* Test updating a roster item according to the example in
* <a href="http://xmpp.org/rfcs/rfc3921.html#roster-update"
* >RFC3921: Updating a Roster Item</a>.
*/
@Test
public void testUpdateRosterItem() throws Throwable {
// Constants for the updated contact
final BareJid contactJID = JidCreate.entityBareFrom("romeo@example.net");
final String contactName = "Romeo";
final String[] contactGroups = { "Friends", "Lovers" };
// Setup
assertNotNull("Can't get the roster from the provided connection!", roster);
initRoster();
rosterListener.reset();
// Updating the roster item
final RosterUpdateResponder serverSimulator = new RosterUpdateResponder() {
@Override
void verifyUpdateRequest(final RosterPacket updateRequest) {
final Item item = updateRequest.getRosterItems().iterator().next();
assertEquals("The provided JID doesn't match the requested!", contactJID, item.getJid());
assertSame("The provided name doesn't match the requested!", contactName, item.getName());
assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[0] + ")!", item.getGroupNames().contains(contactGroups[0]));
assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[1] + ")!", item.getGroupNames().contains(contactGroups[1]));
assertSame("The provided group number doesn't match the requested!", contactGroups.length, item.getGroupNames().size());
}
};
serverSimulator.start();
roster.createGroup(contactGroups[1]).addEntry(roster.getEntry(contactJID));
serverSimulator.join();
// Check if an error occurred within the simulator
final Throwable exception = serverSimulator.getException();
if (exception != null) {
throw exception;
}
rosterListener.waitUntilInvocationOrTimeout();
// Verify the roster entry of the updated contact
final RosterEntry addedEntry = roster.getEntry(contactJID);
assertNotNull("The contact was deleted from the roster!", addedEntry);
assertTrue("The roster listener wasn't invoked for the updated contact!", rosterListener.getUpdatedAddresses().contains(contactJID));
assertSame("Setup wrong name for the changed contact!", contactName, addedEntry.getName());
assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[0] + ")!", roster.getGroup(contactGroups[0]).contains(addedEntry));
assertTrue("The updated contact doesn't belong to the requested groups (" + contactGroups[1] + ")!", roster.getGroup(contactGroups[1]).contains(addedEntry));
assertSame("The updated contact should be member of two groups!", contactGroups.length, addedEntry.getGroups().size());
// Verify the unchanged roster items
verifyMercutiosEntry(roster.getEntry(JidCreate.entityBareFrom("mercutio@example.com")));
verifyBenvoliosEntry(roster.getEntry(JidCreate.entityBareFrom("benvolio@example.net")));
assertSame("Wrong number of roster entries (" + roster.getEntries() + ").", 3, roster.getEntries().size());
}
use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class IoTRemoveProvider method parse.
@Override
public IoTRemove parse(XmlPullParser parser, int initialDepth) throws Exception {
Jid jid = ParserUtils.getJidAttribute(parser);
if (jid.hasResource()) {
throw new SmackException("JID must be without resourcepart");
}
BareJid bareJid = jid.asBareJid();
NodeInfo nodeInfo = NodeInfoParser.parse(parser);
return new IoTRemove(bareJid, nodeInfo);
}
use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class IoTProvisioningManager method isFriend.
/**
* As the given provisioning server is the given JID is a friend.
*
* @param provisioningServer the provisioning server to ask.
* @param friendInQuestion the JID to ask about.
* @return <code>true</code> if the JID is a friend, <code>false</code> otherwise.
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
*/
public boolean isFriend(Jid provisioningServer, BareJid friendInQuestion) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
LruCache<BareJid, Void> cache = negativeFriendshipRequestCache.lookup(provisioningServer);
if (cache != null && cache.containsKey(friendInQuestion)) {
// We hit a cached negative isFriend response for this provisioning server.
return false;
}
IoTIsFriend iotIsFriend = new IoTIsFriend(friendInQuestion);
iotIsFriend.setTo(provisioningServer);
IoTIsFriendResponse response = connection().createStanzaCollectorAndSend(iotIsFriend).nextResultOrThrow();
assert (response.getJid().equals(friendInQuestion));
boolean isFriend = response.getIsFriendResult();
if (!isFriend) {
// Cache the negative is friend response.
if (cache == null) {
cache = new LruCache<>(1024);
negativeFriendshipRequestCache.put(provisioningServer, cache);
}
cache.put(friendInQuestion, null);
}
return isFriend;
}
use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class IoTIsFriendResponseProvider method parse.
@Override
public IoTIsFriendResponse parse(XmlPullParser parser, int initialDepth) throws Exception {
Jid jid = ParserUtils.getJidAttribute(parser);
BareJid bareJid = jid.asBareJid();
boolean result = ParserUtils.getBooleanAttribute(parser, "result");
return new IoTIsFriendResponse(bareJid, result);
}
Aggregations