Search in sources :

Example 1 with PrivacyList

use of org.jivesoftware.smack.PrivacyList in project Smack by igniterealtime.

the class PrivacyTest method testCreateTwoLists.

/**
     * Check when a client set more than one list.
     */
public void testCreateTwoLists() {
    try {
        String listName1 = "1testCreateTwoLists";
        String listName2 = "2testCreateTwoLists";
        String groupName = "testCreateTwoListsGroup";
        PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
        PrivacyClient client = new PrivacyClient(privacyManager);
        privacyManager.addListener(client);
        // Add a list
        ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
        PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
        item.setValue(getConnection(0).getUser());
        items.add(item);
        privacyManager.createPrivacyList(listName1, items);
        Thread.sleep(500);
        // Add the another list
        ArrayList<PrivacyItem> itemsList2 = new ArrayList<PrivacyItem>();
        item = new PrivacyItem(PrivacyItem.Type.group.name(), false, 2);
        item.setValue(groupName);
        item.setFilterMessage(true);
        itemsList2.add(item);
        privacyManager.createPrivacyList(listName2, itemsList2);
        Thread.sleep(500);
        // Assert the list composition.
        PrivacyList[] privacyLists = privacyManager.getPrivacyLists();
        PrivacyList receivedList1 = null;
        PrivacyList receivedList2 = null;
        for (PrivacyList privacyList : privacyLists) {
            if (listName1.equals(privacyList.toString())) {
                receivedList1 = privacyList;
            }
            if (listName2.equals(privacyList.toString())) {
                receivedList2 = privacyList;
            }
        }
        PrivacyItem receivedItem;
        // Assert on the list 1
        assertNotNull(receivedList1);
        assertEquals(1, receivedList1.getItems().size());
        receivedItem = receivedList1.getItems().get(0);
        assertEquals(1, receivedItem.getOrder());
        assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
        assertEquals(true, receivedItem.isAllow());
        assertEquals(getConnection(0).getUser(), receivedItem.getValue());
        // Assert on the list 2
        assertNotNull(receivedList2);
        assertEquals(1, receivedList2.getItems().size());
        receivedItem = receivedList2.getItems().get(0);
        assertEquals(2, receivedItem.getOrder());
        assertEquals(PrivacyItem.Type.group, receivedItem.getType());
        assertEquals(groupName, receivedItem.getValue());
        assertEquals(false, receivedItem.isAllow());
        assertEquals(groupName, receivedItem.getValue());
        assertEquals(false, receivedItem.isFilterEverything());
        assertEquals(true, receivedItem.isFilterMessage());
        assertEquals(false, receivedItem.isFilterPresence_in());
        assertEquals(false, receivedItem.isFilterPresence_out());
        privacyManager.deletePrivacyList(listName1);
        privacyManager.deletePrivacyList(listName2);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : PrivacyList(org.jivesoftware.smack.PrivacyList) ArrayList(java.util.ArrayList) PrivacyListManager(org.jivesoftware.smack.PrivacyListManager) XMPPException(org.jivesoftware.smack.XMPPException)

Example 2 with PrivacyList

use of org.jivesoftware.smack.PrivacyList in project Smack by igniterealtime.

the class PrivacyTest method testCreateAndUpdateList.

/**
     * Check when a client set a new list and then update its content.
     */
public void testCreateAndUpdateList() {
    try {
        String listName = "testCreateAndUpdateList";
        String user = "tybalt@example.com";
        PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(getConnection(0));
        PrivacyClient client = new PrivacyClient(privacyManager);
        privacyManager.addListener(client);
        // Add the list that will be set as the active
        ArrayList<PrivacyItem> items = new ArrayList<PrivacyItem>();
        PrivacyItem item = new PrivacyItem(PrivacyItem.Type.jid.name(), true, 1);
        item.setValue(getConnection(0).getUser());
        items.add(item);
        privacyManager.createPrivacyList(listName, items);
        Thread.sleep(500);
        // Remove the existing item and add a new one.
        items.remove(item);
        item = new PrivacyItem(PrivacyItem.Type.jid.name(), false, 2);
        item.setValue(user);
        item.setFilterPresence_out(true);
        item.setFilterPresence_in(true);
        item.setFilterMessage(true);
        items.add(item);
        // Update the list on server
        privacyManager.updatePrivacyList(listName, items);
        Thread.sleep(500);
        // Assert the list composition.
        PrivacyList list = privacyManager.getPrivacyList(listName);
        assertEquals(1, list.getItems().size());
        // Assert the privacy item composition
        PrivacyItem receivedItem = list.getItems().get(0);
        assertEquals(2, receivedItem.getOrder());
        assertEquals(PrivacyItem.Type.jid, receivedItem.getType());
        assertEquals(false, receivedItem.isAllow());
        assertEquals(user, receivedItem.getValue());
        assertEquals(false, receivedItem.isFilterEverything());
        assertEquals(true, receivedItem.isFilterMessage());
        assertEquals(true, receivedItem.isFilterPresence_in());
        assertEquals(true, receivedItem.isFilterPresence_out());
        assertEquals(true, client.wasModified());
        privacyManager.deletePrivacyList(listName);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : PrivacyList(org.jivesoftware.smack.PrivacyList) ArrayList(java.util.ArrayList) PrivacyListManager(org.jivesoftware.smack.PrivacyListManager) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

ArrayList (java.util.ArrayList)2 PrivacyList (org.jivesoftware.smack.PrivacyList)2 PrivacyListManager (org.jivesoftware.smack.PrivacyListManager)2 XMPPException (org.jivesoftware.smack.XMPPException)2