use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.
the class RosterVersioningTest method vaglafItem.
private static Item vaglafItem() throws XmppStringprepException {
Item item = new Item(JidCreate.entityBareFrom("vaglaf@example.com"), "vaglaf the only");
item.setItemType(ItemType.both);
item.addGroupName("all");
item.addGroupName("friends");
item.addGroupName("partners");
return item;
}
use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.
the class RosterVersioningTest method testRosterVersioningWithCachedRosterAndPushes.
/**
* Test roster versioning with roster pushes.
*/
@Test(timeout = 5000)
public void testRosterVersioningWithCachedRosterAndPushes() throws Throwable {
answerWithEmptyRosterResult();
rosterListener.waitAndReset();
RosterStore store = roster.getRosterStore();
// Simulate a roster push adding vaglaf
{
RosterPacket rosterPush = new RosterPacket();
rosterPush.setTo(JidCreate.from("rostertest@example.com/home"));
rosterPush.setType(Type.set);
rosterPush.setVersion("v97");
Item pushedItem = vaglafItem();
rosterPush.addRosterItem(pushedItem);
rosterListener.reset();
connection.processStanza(rosterPush);
rosterListener.waitAndReset();
assertEquals("Expect store version after push", "v97", store.getRosterVersion());
Item storedItem = store.getEntry(JidCreate.from("vaglaf@example.com"));
assertNotNull("Expect vaglaf to be added", storedItem);
assertEquals("Expect vaglaf to be equal to pushed item", pushedItem, storedItem);
Collection<Item> rosterItems = new HashSet<Item>();
for (RosterEntry entry : roster.getEntries()) {
rosterItems.add(RosterEntry.toRosterItem(entry));
}
assertEquals(rosterItems, new HashSet<Item>(store.getEntries()));
}
// Simulate a roster push removing vaglaf
{
RosterPacket rosterPush = new RosterPacket();
rosterPush.setTo(JidCreate.from("rostertest@example.com/home"));
rosterPush.setType(Type.set);
rosterPush.setVersion("v98");
Item item = new Item(JidCreate.entityBareFrom("vaglaf@example.com"), "vaglaf the only");
item.setItemType(ItemType.remove);
rosterPush.addRosterItem(item);
rosterListener.reset();
connection.processStanza(rosterPush);
rosterListener.waitAndReset();
assertNull("Store doses not contain vaglaf", store.getEntry(JidCreate.entityBareFrom("vaglaf@example.com")));
assertEquals("Expect store version after push", "v98", store.getRosterVersion());
}
}
use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.
the class SubscriptionPreApprovalTest method testPreApproveAndCreate.
@Test
public void testPreApproveAndCreate() throws Throwable {
final BareJid contactJID = JidCreate.bareFrom("preapproval@example.com");
final String contactName = "PreApproval";
final String[] contactGroup = {};
connection.enableStreamFeature(SubscriptionPreApproval.INSTANCE);
final PreApproveAndCreateEntryResponder serverSimulator = new PreApproveAndCreateEntryResponder() {
@Override
void verifyRosterUpdateRequest(final RosterPacket updateRequest) {
final Item item = updateRequest.getRosterItems().iterator().next();
assertSame("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!", 0, item.getGroupNames().size());
}
@Override
void verifyPreApprovalRequest(Presence preApproval) {
assertSame("The provided name doesn't match the requested!", contactJID, preApproval.getTo());
assertSame("The provided presence type is incorrect!", Presence.Type.subscribed, preApproval.getType());
}
};
serverSimulator.start();
roster.preApproveAndCreateEntry(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!", 0, addedEntry.getGroups().size());
}
use of org.jivesoftware.smack.roster.packet.RosterPacket.Item in project Smack by igniterealtime.
the class Roster method removeEntry.
/**
* Removes a roster entry from the roster. The roster entry will also be removed from the
* unfiled entries or from any roster group where it could belong and will no longer be part
* of the roster. Note that this is a synchronous call -- Smack must wait for the server
* to send an updated subscription status.
*
* @param entry a roster entry.
* @throws XMPPErrorException if an XMPP error occurs.
* @throws NotLoggedInException if not logged in.
* @throws NoResponseException SmackException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
*/
public void removeEntry(RosterEntry entry) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
// The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet)
if (!entries.containsKey(entry.getJid())) {
return;
}
RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.set);
RosterPacket.Item item = RosterEntry.toRosterItem(entry);
// Set the item type as REMOVE so that the server will delete the entry
item.setItemType(RosterPacket.ItemType.remove);
packet.addRosterItem(item);
connection.createStanzaCollectorAndSend(packet).nextResultOrThrow();
}
use of org.jivesoftware.smack.roster.packet.RosterPacket.Item 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());
}
Aggregations