/* * Created on 2009-04-22 */ package org.jivesoftware.smackx.pubsub; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.XMPPError.Type; import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smackx.pubsub.listener.ItemDeleteListener; import org.jivesoftware.smackx.pubsub.listener.ItemEventListener; import org.jivesoftware.smackx.pubsub.listener.NodeConfigListener; public class TestEvents extends SmackTestCase { public TestEvents(String str) { super(str); } @Override protected int getMaxConnections() { return 2; } private String getService() { return "pubsub." + getServiceName(); } public void testCreateAndGetNode() throws Exception { String nodeId = "MyTestNode"; PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = null; try { creatorNode = (LeafNode)creatorMgr.getNode(nodeId); } catch (XMPPException e) { if (e.getXMPPError().getType() == Type.CANCEL && e.getXMPPError().getCondition().equals("item-not-found")) creatorNode = creatorMgr.createNode(nodeId); else throw e; } PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); assertNotNull(subNode); } public void testConfigureAndNotify() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, false, true); BlockingQueue queue = new ArrayBlockingQueue(3); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); NodeConfigListener sub1Handler = new NodeConfigCoordinator(queue, "sub1"); subNode.subscribe(getConnection(1).getUser()); subNode.addConfigurationListener(sub1Handler); ConfigureForm currentConfig = creatorNode.getNodeConfiguration(); ConfigureForm form = new ConfigureForm(currentConfig.createAnswerForm()); form.setPersistentItems(true); form.setDeliverPayloads(false); form.setNotifyConfig(true); creatorNode.sendConfigurationForm(form); ConfigurationEvent event = queue.poll(5, TimeUnit.SECONDS).event; assertEquals(nodeId, event.getNode()); assertNull(event.getConfiguration()); currentConfig = creatorNode.getNodeConfiguration(); form = new ConfigureForm(currentConfig.createAnswerForm()); form.setDeliverPayloads(true); creatorNode.sendConfigurationForm(form); event = queue.poll(5, TimeUnit.SECONDS).event; assertEquals(nodeId, event.getNode()); assertNotNull(event.getConfiguration()); assertTrue(event.getConfiguration().isPersistItems()); assertTrue(event.getConfiguration().isDeliverPayloads()); } public void testSendAndReceiveNoPayload() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false); BlockingQueue> queue = new ArrayBlockingQueue>(3); ItemEventCoordinator creatorHandler = new ItemEventCoordinator(queue, "creator"); creatorNode.addItemEventListener(creatorHandler); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); ItemEventCoordinator sub1Handler = new ItemEventCoordinator(queue, "sub1"); subNode.addItemEventListener(sub1Handler); Subscription sub1 = subNode.subscribe(getConnection(1).getUser()); // Send event String itemId = String.valueOf(System.currentTimeMillis()); creatorNode.send(new Item(itemId)); for(int i=0; i<2; i++) { ItemEventCoordinator coord = queue.take(); assertEquals(1, coord.events.getItems().size()); assertEquals(itemId, coord.events.getItems().iterator().next().getId()); } } public void testPublishAndReceiveNoPayload() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false); BlockingQueue> queue = new ArrayBlockingQueue>(3); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); ItemEventCoordinator sub1Handler = new ItemEventCoordinator(queue, "sub1"); subNode.addItemEventListener(sub1Handler); Subscription sub1 = subNode.subscribe(getConnection(1).getUser()); // Send event String itemId = String.valueOf(System.currentTimeMillis()); creatorNode.publish(new Item(itemId)); ItemEventCoordinator coord = queue.take(); assertEquals(1, coord.events.getItems().size()); assertEquals(itemId, coord.events.getItems().get(0).getId()); } public void testSendAndReceiveSimplePayload() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, true); BlockingQueue>> queue = new ArrayBlockingQueue>>(3); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); ItemEventCoordinator> sub1Handler = new ItemEventCoordinator>(queue, "sub1"); subNode.addItemEventListener(sub1Handler); Subscription sub1 = subNode.subscribe(getConnection(1).getUser()); // Send event String itemId = String.valueOf(System.currentTimeMillis()); String payloadString = "Sir Arthur Conan Doyle"; creatorNode.send(new PayloadItem(itemId, new SimplePayload("book", "pubsub:test:book", payloadString))); ItemEventCoordinator> coord = queue.take(); assertEquals(1, coord.events.getItems().size()); PayloadItem item = coord.events.getItems().get(0); assertEquals(itemId, item.getId()); assertTrue(item.getPayload() instanceof SimplePayload); assertEquals(payloadString, item.getPayload().toXML()); assertEquals("book", item.getPayload().getElementName()); } /* * For this test, the following extension needs to be added to the meta-inf/smack.providers file * * * car * pubsub:test:vehicle * org.jivesoftware.smackx.pubsub.CarExtensionProvider * */ /* public void testSendAndReceiveCarPayload() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, true); BlockingQueue>> queue = new ArrayBlockingQueue>>(3); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); Node subNode = subMgr.getNode(nodeId); ItemEventCoordinator> sub1Handler = new ItemEventCoordinator>(queue, "sub1"); subNode.addItemEventListener(sub1Handler); Subscription sub1 = subNode.subscribe(getConnection(1).getUser()); // Send event String itemId = String.valueOf(System.currentTimeMillis()); String payloadString = ""; creatorNode.send(new PayloadItem(itemId, new SimplePayload("car", "pubsub:test:vehicle", payloadString))); ItemEventCoordinator> coord = queue.take(); assertEquals(1, coord.events.getItems().size()); PayloadItem item = coord.events.getItems().get(0); assertEquals(itemId, item.getId()); assertTrue(item.getPayload() instanceof CarExtension); CarExtension car = (CarExtension)item.getPayload(); assertEquals("green", car.getColor()); assertEquals(4, car.getNumTires()); } */ public void testSendAndReceiveMultipleSubs() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false); BlockingQueue> queue = new ArrayBlockingQueue>(3); ItemEventCoordinator creatorHandler = new ItemEventCoordinator(queue, "creator"); creatorNode.addItemEventListener(creatorHandler); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); ItemEventCoordinator sub1Handler = new ItemEventCoordinator(queue, "sub1"); subNode.addItemEventListener(sub1Handler); Subscription sub1 = subNode.subscribe(getConnection(1).getUser()); ItemEventCoordinator sub2Handler = new ItemEventCoordinator(queue, "sub2"); subNode.addItemEventListener(sub2Handler); Subscription sub2 = subNode.subscribe(getConnection(1).getUser()); // Send event String itemId = String.valueOf(System.currentTimeMillis()); creatorNode.send(new Item(itemId)); for(int i=0; i<3; i++) { ItemEventCoordinator coord = queue.take(); assertEquals(1, coord.events.getItems().size()); assertEquals(itemId, coord.events.getItems().iterator().next().getId()); if (coord.id.equals("sub1") || coord.id.equals("sub2")) { assertEquals(2, coord.events.getSubscriptions().size()); } } } public void testSendAndReceiveMultipleItems() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, true); BlockingQueue>> queue = new ArrayBlockingQueue>>(3); ItemEventCoordinator> creatorHandler = new ItemEventCoordinator>(queue, "creator"); creatorNode.addItemEventListener(creatorHandler); creatorNode.subscribe(getConnection(0).getUser()); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); ItemEventCoordinator> sub1Handler = new ItemEventCoordinator>(queue, "sub1"); subNode.addItemEventListener(sub1Handler); Subscription sub1 = subNode.subscribe(getConnection(1).getUser()); ItemEventCoordinator> sub2Handler = new ItemEventCoordinator>(queue, "sub2"); subNode.addItemEventListener(sub2Handler); Subscription sub2 = subNode.subscribe(getConnection(1).getUser()); assertEquals(Subscription.State.subscribed, sub1.getState()); assertEquals(Subscription.State.subscribed, sub2.getState()); // Send event String itemId = String.valueOf(System.currentTimeMillis()); Collection> items = new ArrayList>(3); String ids[] = {"First-" + itemId, "Second-" + itemId, "Third-" + itemId}; items.add(new PayloadItem(ids[0], new SimplePayload("a", "pubsub:test", ""))); items.add(new PayloadItem(ids[1], new SimplePayload("a", "pubsub:test", ""))); items.add(new PayloadItem(ids[2], new SimplePayload("a", "pubsub:test", ""))); creatorNode.send(items); for(int i=0; i<3; i++) { ItemEventCoordinator> coord = queue.poll(5, TimeUnit.SECONDS); if (coord == creatorHandler) assertEquals(1, coord.events.getSubscriptions().size()); else assertEquals(2, coord.events.getSubscriptions().size()); List> itemResults = coord.events.getItems(); assertEquals(3, itemResults.size()); // assertEquals(ids[0], itemResults.get(0).getId()); // assertEquals("", itemResults.get(0).getPayload().toXML().replace('\"', '\'')); // assertEquals(ids[1], itemResults.get(1).getId()); // assertEquals("", itemResults.get(1).getPayload().toXML().replace('\"', '\'')); // assertEquals(ids[21], itemResults.get(2).getId()); // assertEquals("", itemResults.get(3).getPayload().toXML().replace('\"', '\'')); } } public void testSendAndReceiveDelayed() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false); // Send event String itemId = String.valueOf("DelayId-" + System.currentTimeMillis()); String payloadString = "Sir Arthur Conan Doyle"; creatorNode.send(new PayloadItem(itemId, new SimplePayload("book", "pubsub:test:book", payloadString))); Thread.sleep(1000); BlockingQueue>> queue = new ArrayBlockingQueue>>(3); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); ItemEventCoordinator> sub1Handler = new ItemEventCoordinator>(queue, "sub1"); subNode.addItemEventListener(sub1Handler); Subscription sub1 = subNode.subscribe(getConnection(1).getUser()); ItemEventCoordinator> coord = queue.take(); assertTrue(coord.events.isDelayed()); assertNotNull(coord.events.getPublishedDate()); } public void testDeleteItemAndNotify() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false); BlockingQueue queue = new ArrayBlockingQueue(3); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); ItemDeleteCoordinator sub1Handler = new ItemDeleteCoordinator(queue, "sub1"); subNode.addItemDeleteListener(sub1Handler); subNode.subscribe(getConnection(1).getUser()); // Send event String itemId = String.valueOf(System.currentTimeMillis()); Collection items = new ArrayList(3); String id1 = "First-" + itemId; String id2 = "Second-" + itemId; String id3 = "Third-" + itemId; items.add(new Item(id1)); items.add(new Item(id2)); items.add(new Item(id3)); creatorNode.send(items); creatorNode.deleteItem(id1); ItemDeleteCoordinator coord = queue.poll(5, TimeUnit.SECONDS); assertEquals(1, coord.event.getItemIds().size()); assertEquals(id1, coord.event.getItemIds().get(0)); creatorNode.deleteItem(Arrays.asList(id2, id3)); coord = queue.poll(5, TimeUnit.SECONDS); assertEquals(2, coord.event.getItemIds().size()); assertTrue(coord.event.getItemIds().contains(id2)); assertTrue(coord.event.getItemIds().contains(id3)); } public void testPurgeAndNotify() throws Exception { // Setup event source String nodeId = "TestNode" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); LeafNode creatorNode = getPubnode(creatorMgr, nodeId, true, false); BlockingQueue queue = new ArrayBlockingQueue(3); // Setup event receiver PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode = (LeafNode)subMgr.getNode(nodeId); ItemDeleteCoordinator sub1Handler = new ItemDeleteCoordinator(queue, "sub1"); subNode.addItemDeleteListener(sub1Handler); subNode.subscribe(getConnection(1).getUser()); // Send event String itemId = String.valueOf(System.currentTimeMillis()); Collection items = new ArrayList(3); String id1 = "First-" + itemId; String id2 = "Second-" + itemId; String id3 = "Third-" + itemId; items.add(new Item(id1)); items.add(new Item(id2)); items.add(new Item(id3)); creatorNode.send(items); creatorNode.deleteAllItems(); ItemDeleteCoordinator coord = queue.poll(5, TimeUnit.SECONDS); assertNull(nodeId, coord.event); } public void testListenerMultipleNodes() throws Exception { // Setup event source String nodeId1 = "Node-1-" + System.currentTimeMillis(); PubSubManager creatorMgr = new PubSubManager(getConnection(0), getService()); String nodeId2 = "Node-2-" + System.currentTimeMillis(); LeafNode creatorNode1 = getPubnode(creatorMgr, nodeId1, true, false); LeafNode creatorNode2 = getPubnode(creatorMgr, nodeId2, true, false); BlockingQueue> queue = new ArrayBlockingQueue>(3); PubSubManager subMgr = new PubSubManager(getConnection(1), getService()); LeafNode subNode1 = (LeafNode)subMgr.getNode(nodeId1); LeafNode subNode2 = (LeafNode)subMgr.getNode(nodeId2); subNode1.addItemEventListener(new ItemEventCoordinator(queue, "sub1")); subNode2.addItemEventListener(new ItemEventCoordinator(queue, "sub2")); subNode1.subscribe(getConnection(1).getUser()); subNode2.subscribe(getConnection(1).getUser()); creatorNode1.send(new Item("item1")); creatorNode2.send(new Item("item2")); boolean check1 = false; boolean check2 = false; for (int i=0; i<2; i++) { ItemEventCoordinator event = queue.take(); if (event.id.equals("sub1")) { assertEquals(event.events.getNodeId(), nodeId1); check1 = true; } else { assertEquals(event.events.getNodeId(), nodeId2); check2 = true; } } assertTrue(check1); assertTrue(check2); } class ItemEventCoordinator implements ItemEventListener { private BlockingQueue> theQueue; private ItemPublishEvent events; private String id; ItemEventCoordinator(BlockingQueue> queue, String id) { theQueue = queue; this.id = id; } @Override public void handlePublishedItems(ItemPublishEvent items) { events = items; theQueue.add(this); } @Override public String toString() { return "ItemEventCoordinator: " + id; } } class NodeConfigCoordinator implements NodeConfigListener { private BlockingQueue theQueue; private String id; private ConfigurationEvent event; NodeConfigCoordinator(BlockingQueue queue, String id) { theQueue = queue; this.id = id; } public void handleNodeConfiguration(ConfigurationEvent config) { event = config; theQueue.add(this); } @Override public String toString() { return "NodeConfigCoordinator: " + id; } } class ItemDeleteCoordinator implements ItemDeleteListener { private BlockingQueue theQueue; private String id; private ItemDeleteEvent event; ItemDeleteCoordinator(BlockingQueue queue, String id) { theQueue = queue; this.id = id; } public void handleDeletedItems(ItemDeleteEvent delEvent) { event = delEvent; theQueue.add(this); } public void handlePurge() { event = null; theQueue.add(this); } @Override public String toString() { return "ItemDeleteCoordinator: " + id; } } static private LeafNode getPubnode(PubSubManager manager, String id, boolean persistItems, boolean deliverPayload) throws XMPPException { ConfigureForm form = new ConfigureForm(FormType.submit); form.setPersistentItems(persistItems); form.setDeliverPayloads(deliverPayload); form.setAccessModel(AccessModel.open); return (LeafNode)manager.createNode(id, form); } }