use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class DirectoryRosterStoreTest method testStoreAddRemove.
/**
* Tests adding and removing entries.
*/
@Test
public void testStoreAddRemove() throws IOException {
File storeDir = tmpFolder.newFolder();
DirectoryRosterStore store = DirectoryRosterStore.init(storeDir);
assertEquals("Initial roster version", "", store.getRosterVersion());
BareJid userName = JidTestUtil.DUMMY_AT_EXAMPLE_ORG;
final RosterPacket.Item item1 = new Item(userName, null);
final String version1 = "1";
store.addEntry(item1, version1);
assertEquals("Adding entry sets version correctly", version1, store.getRosterVersion());
RosterPacket.Item storedItem = store.getEntry(userName);
assertNotNull("Added entry not found found", storedItem);
assertEquals("User of added entry", item1.getJid(), storedItem.getJid());
assertEquals("Name of added entry", item1.getName(), storedItem.getName());
assertEquals("Groups", item1.getGroupNames(), storedItem.getGroupNames());
assertEquals("ItemType of added entry", item1.getItemType(), storedItem.getItemType());
assertEquals("ItemStatus of added entry", item1.isSubscriptionPending(), storedItem.isSubscriptionPending());
assertEquals("Approved of added entry", item1.isApproved(), storedItem.isApproved());
final String version2 = "2";
final RosterPacket.Item item2 = new Item(userName, "Ursula Example");
item2.addGroupName("users");
item2.addGroupName("examples");
item2.setSubscriptionPending(true);
item2.setItemType(ItemType.none);
item2.setApproved(true);
store.addEntry(item2, version2);
assertEquals("Updating entry sets version correctly", version2, store.getRosterVersion());
storedItem = store.getEntry(userName);
assertNotNull("Added entry not found", storedItem);
assertEquals("User of added entry", item2.getJid(), storedItem.getJid());
assertEquals("Name of added entry", item2.getName(), storedItem.getName());
assertEquals("Groups", item2.getGroupNames(), storedItem.getGroupNames());
assertEquals("ItemType of added entry", item2.getItemType(), storedItem.getItemType());
assertEquals("ItemStatus of added entry", item2.isSubscriptionPending(), storedItem.isSubscriptionPending());
assertEquals("Approved of added entry", item2.isApproved(), storedItem.isApproved());
List<Item> entries = store.getEntries();
assertEquals("Number of entries", 1, entries.size());
final RosterPacket.Item item3 = new Item(JidTestUtil.BARE_JID_1, "Foo Bar");
item3.addGroupName("The Foo Fighters");
item3.addGroupName("Bar Friends");
item3.setSubscriptionPending(true);
item3.setItemType(ItemType.both);
final RosterPacket.Item item4 = new Item(JidTestUtil.BARE_JID_2, "Baba Baz");
item4.addGroupName("The Foo Fighters");
item4.addGroupName("Bar Friends");
item4.setSubscriptionPending(false);
item4.setItemType(ItemType.both);
item4.setApproved(true);
ArrayList<Item> items34 = new ArrayList<RosterPacket.Item>();
items34.add(item3);
items34.add(item4);
String version3 = "3";
store.resetEntries(items34, version3);
storedItem = store.getEntry(JidTestUtil.BARE_JID_1);
assertNotNull("Added entry not found", storedItem);
assertEquals("User of added entry", item3.getJid(), storedItem.getJid());
assertEquals("Name of added entry", item3.getName(), storedItem.getName());
assertEquals("Groups", item3.getGroupNames(), storedItem.getGroupNames());
assertEquals("ItemType of added entry", item3.getItemType(), storedItem.getItemType());
assertEquals("ItemStatus of added entry", item3.isSubscriptionPending(), storedItem.isSubscriptionPending());
assertEquals("Approved of added entry", item3.isApproved(), storedItem.isApproved());
storedItem = store.getEntry(JidTestUtil.BARE_JID_2);
assertNotNull("Added entry not found", storedItem);
assertEquals("User of added entry", item4.getJid(), storedItem.getJid());
assertEquals("Name of added entry", item4.getName(), storedItem.getName());
assertEquals("Groups", item4.getGroupNames(), storedItem.getGroupNames());
assertEquals("ItemType of added entry", item4.getItemType(), storedItem.getItemType());
assertEquals("ItemStatus of added entry", item4.isSubscriptionPending(), storedItem.isSubscriptionPending());
assertEquals("Approved of added entry", item4.isApproved(), storedItem.isApproved());
entries = store.getEntries();
assertEquals("Number of entries", 2, entries.size());
String version4 = "4";
store.removeEntry(JidTestUtil.BARE_JID_2, version4);
assertEquals("Removing entry sets version correctly", version4, store.getRosterVersion());
assertNull("Removed entry is gone", store.getEntry(userName));
entries = store.getEntries();
assertEquals("Number of entries", 1, entries.size());
}
use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class RosterIntegrationTest method ensureSubscribedTo.
private 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);
}
}
use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class AffiliationProvider method parse.
@Override
public Affiliation parse(XmlPullParser parser, int initialDepth) throws Exception {
String node = parser.getAttributeValue(null, "node");
BareJid jid = ParserUtils.getBareJidAttribute(parser);
String affiliationString = parser.getAttributeValue(null, "affiliation");
Affiliation.Type affiliationType = null;
if (affiliationString != null) {
affiliationType = Affiliation.Type.valueOf(affiliationString);
}
Affiliation affiliation;
if (node != null && jid == null) {
// affiliationType may be empty
affiliation = new Affiliation(node, affiliationType);
} else if (node == null && jid != null) {
// TODO
PubSubNamespace namespace = null;
affiliation = new Affiliation(jid, affiliationType, namespace);
} else {
throw new SmackException("Invalid affililation. Either one of 'node' or 'jid' must be set" + ". Node: " + node + ". Jid: " + jid + '.');
}
return affiliation;
}
use of org.jxmpp.jid.BareJid in project Smack by igniterealtime.
the class RosterTest method testEmptyGroupRosterPush.
/**
* Test processing a roster push with an empty group is equivalent with providing
* no group.
*
* @see <a href="http://www.igniterealtime.org/issues/browse/SMACK-294">SMACK-294</a>
*/
@Test
public void testEmptyGroupRosterPush() throws Throwable {
final BareJid contactJID = JidCreate.entityBareFrom("nurse@example.com");
assertNotNull("Can't get the roster from the provided connection!", roster);
final StringBuilder sb = new StringBuilder();
sb.append("<iq id=\"rostertest2\" type=\"set\" ").append("to=\"").append(connection.getUser()).append("\">").append("<query xmlns=\"jabber:iq:roster\">").append("<item jid=\"").append(contactJID).append("\">").append("<group></group>").append("</item>").append("</query>").append("</iq>");
final XmlPullParser parser = TestUtils.getIQParser(sb.toString());
final IQ rosterPush = PacketParserUtils.parseIQ(parser);
initRoster();
rosterListener.reset();
// Simulate receiving the roster push
connection.processStanza(rosterPush);
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 default subscription status!", ItemType.none, addedEntry.getType());
assertSame("The new contact shouldn't be member of any group!", 0, addedEntry.getGroups().size());
// 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 testIgnoreInvalidFrom.
/**
* Tests that roster pushes with invalid from are ignored.
* @throws XmppStringprepException
*
* @see <a href="http://xmpp.org/rfcs/rfc6121.html#roster-syntax-actions-push">RFC 6121, Section 2.1.6</a>
*/
@Test
public void testIgnoreInvalidFrom() throws XmppStringprepException {
final BareJid spammerJid = JidCreate.entityBareFrom("spam@example.com");
RosterPacket packet = new RosterPacket();
packet.setType(Type.set);
packet.setTo(connection.getUser());
packet.setFrom(JidCreate.entityBareFrom("mallory@example.com"));
packet.addRosterItem(new Item(spammerJid, "Cool products!"));
final String requestId = packet.getStanzaId();
// Simulate receiving the roster push
connection.processStanza(packet);
// Smack should reply with an error IQ
ErrorIQ errorIQ = (ErrorIQ) connection.getSentPacket();
assertEquals(requestId, errorIQ.getStanzaId());
assertEquals(Condition.service_unavailable, errorIQ.getError().getCondition());
assertNull("Contact was added to roster", Roster.getInstanceFor(connection).getEntry(spammerJid));
}
Aggregations