Search in sources :

Example 1 with RosterStore

use of org.jivesoftware.smack.roster.rosterstore.RosterStore 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());
    }
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) DirectoryRosterStore(org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore) RosterStore(org.jivesoftware.smack.roster.rosterstore.RosterStore) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Collection(java.util.Collection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with RosterStore

use of org.jivesoftware.smack.roster.rosterstore.RosterStore in project Smack by igniterealtime.

the class RosterVersioningTest method testOtherVersionStored.

/**
     * Tests that a non-empty roster result empties the store.
     * @throws SmackException 
     * @throws XMPPException 
     * @throws XmppStringprepException 
     */
@Test(timeout = 5000)
public void testOtherVersionStored() throws XMPPException, SmackException, XmppStringprepException {
    Item vaglafItem = vaglafItem();
    // We expect that the roster request is the only packet sent. This is not part of the specification,
    // but a shortcut in the test implementation.
    Stanza sentPacket = connection.getSentPacket();
    if (sentPacket instanceof RosterPacket) {
        RosterPacket sentRP = (RosterPacket) sentPacket;
        RosterPacket answer = new RosterPacket();
        answer.setStanzaId(sentRP.getStanzaId());
        answer.setType(Type.result);
        answer.setTo(sentRP.getFrom());
        answer.setVersion("newVersion");
        answer.addRosterItem(vaglafItem);
        rosterListener.reset();
        connection.processStanza(answer);
        rosterListener.waitUntilInvocationOrTimeout();
    } else {
        assertTrue("Expected to get a RosterPacket ", false);
    }
    Roster roster = Roster.getInstanceFor(connection);
    assertEquals("Size of roster", 1, roster.getEntries().size());
    RosterEntry entry = roster.getEntry(vaglafItem.getJid());
    assertNotNull("Roster contains vaglaf entry", entry);
    assertEquals("vaglaf entry in roster equals the sent entry", vaglafItem, RosterEntry.toRosterItem(entry));
    RosterStore store = roster.getRosterStore();
    assertEquals("Size of store", 1, store.getEntries().size());
    Item item = store.getEntry(vaglafItem.getJid());
    assertNotNull("Store contains vaglaf entry");
    assertEquals("vaglaf entry in store equals the sent entry", vaglafItem, item);
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) DirectoryRosterStore(org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore) RosterStore(org.jivesoftware.smack.roster.rosterstore.RosterStore) RosterPacket(org.jivesoftware.smack.roster.packet.RosterPacket) Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 3 with RosterStore

use of org.jivesoftware.smack.roster.rosterstore.RosterStore in project Smack by igniterealtime.

the class RosterVersioningTest method testEqualVersionStored.

/**
     * Tests that receiving an empty roster result causes the roster to be populated
     * by all entries of the roster store.
     * @throws SmackException 
     * @throws XMPPException 
     */
@Test(timeout = 300000)
public void testEqualVersionStored() throws InterruptedException, IOException, XMPPException, SmackException {
    answerWithEmptyRosterResult();
    roster.waitUntilLoaded();
    Collection<RosterEntry> entries = roster.getEntries();
    assertSame("Size of the roster", 3, entries.size());
    HashSet<Item> items = new HashSet<Item>();
    for (RosterEntry entry : entries) {
        items.add(RosterEntry.toRosterItem(entry));
    }
    RosterStore store = DirectoryRosterStore.init(tmpFolder.newFolder());
    populateStore(store);
    assertEquals("Elements of the roster", new HashSet<Item>(store.getEntries()), items);
    for (RosterEntry entry : entries) {
        assertTrue("joe stevens".equals(entry.getName()) || "geoff hurley".equals(entry.getName()) || "higgins mcmann".equals(entry.getName()));
    }
    Collection<RosterGroup> groups = roster.getGroups();
    assertSame(3, groups.size());
    for (RosterGroup group : groups) {
        assertTrue("all".equals(group.getName()) || "friends".equals(group.getName()) || "partners".equals(group.getName()));
    }
}
Also used : Item(org.jivesoftware.smack.roster.packet.RosterPacket.Item) DirectoryRosterStore(org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore) RosterStore(org.jivesoftware.smack.roster.rosterstore.RosterStore) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Item (org.jivesoftware.smack.roster.packet.RosterPacket.Item)3 DirectoryRosterStore (org.jivesoftware.smack.roster.rosterstore.DirectoryRosterStore)3 RosterStore (org.jivesoftware.smack.roster.rosterstore.RosterStore)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)2 Collection (java.util.Collection)1 Stanza (org.jivesoftware.smack.packet.Stanza)1