|
| 1 | +package org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.get; |
| 2 | + |
| 3 | +import java.util.Date; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.concurrent.BlockingQueue; |
| 8 | +import java.util.concurrent.LinkedBlockingQueue; |
| 9 | + |
| 10 | +import junit.framework.Assert; |
| 11 | + |
| 12 | +import org.buddycloud.channelserver.channel.ChannelManager; |
| 13 | +import org.buddycloud.channelserver.channel.node.configuration.field.AccessModel; |
| 14 | +import org.buddycloud.channelserver.packetHandler.iq.IQTestHandler; |
| 15 | +import org.buddycloud.channelserver.pubsub.accessmodel.AccessModels; |
| 16 | +import org.buddycloud.channelserver.pubsub.affiliation.Affiliations; |
| 17 | +import org.buddycloud.channelserver.pubsub.model.NodeThread; |
| 18 | +import org.buddycloud.channelserver.pubsub.model.impl.NodeAffiliationImpl; |
| 19 | +import org.buddycloud.channelserver.pubsub.model.impl.NodeSubscriptionImpl; |
| 20 | +import org.buddycloud.channelserver.pubsub.subscription.Subscriptions; |
| 21 | +import org.dom4j.Element; |
| 22 | +import org.dom4j.tree.BaseElement; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.mockito.Mockito; |
| 26 | +import org.xmpp.packet.IQ; |
| 27 | +import org.xmpp.packet.Packet; |
| 28 | +import org.xmpp.packet.PacketError; |
| 29 | +import org.xmpp.resultsetmanagement.ResultSetImpl; |
| 30 | + |
| 31 | +public class NodeThreadsGetTest extends IQTestHandler { |
| 32 | + |
| 33 | + private BlockingQueue<Packet> queue; |
| 34 | + private ChannelManager channelManager; |
| 35 | + private NodeThreadsGet threadsGet; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUp() { |
| 39 | + this.queue = new LinkedBlockingQueue<Packet>(); |
| 40 | + this.channelManager = Mockito.mock(ChannelManager.class); |
| 41 | + this.threadsGet = new NodeThreadsGet(queue, channelManager); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testPassingThreadsAsElementName() { |
| 46 | + Element element = new BaseElement("threads"); |
| 47 | + Assert.assertTrue(threadsGet.accept(element)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testPassingNoThreadsAsElementName() { |
| 52 | + Element element = new BaseElement("non-threads"); |
| 53 | + Assert.assertFalse(threadsGet.accept(element)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testMissingNodeAttribute() throws Exception { |
| 58 | + IQ request = readStanzaAsIq("/iq/pubsub/threads/request-no-node.stanza"); |
| 59 | + Element threadsEl = request.getChildElement().element("threads"); |
| 60 | + threadsGet.process(threadsEl, request.getFrom(), request, null); |
| 61 | + Packet response = queue.poll(); |
| 62 | + |
| 63 | + PacketError error = response.getError(); |
| 64 | + Assert.assertNotNull(error); |
| 65 | + Assert.assertEquals(PacketError.Type.modify, error.getType()); |
| 66 | + Assert.assertEquals("nodeid-required", |
| 67 | + error.getApplicationConditionName()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testInexistentNode() throws Exception { |
| 72 | + IQ request = readStanzaAsIq("/iq/pubsub/threads/request-with-node.stanza"); |
| 73 | + Element threadsEl = request.getChildElement().element("threads"); |
| 74 | + threadsGet.process(threadsEl, request.getFrom(), request, null); |
| 75 | + Packet response = queue.poll(); |
| 76 | + |
| 77 | + PacketError error = response.getError(); |
| 78 | + Assert.assertNotNull(error); |
| 79 | + Assert.assertEquals(PacketError.Type.cancel, error.getType()); |
| 80 | + Assert.assertEquals(PacketError.Condition.item_not_found, error.getCondition()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testUserNotInAuthorizedChannel() throws Exception { |
| 85 | + IQ request = readStanzaAsIq("/iq/pubsub/threads/request-with-node.stanza"); |
| 86 | + Element threadsEl = request.getChildElement().element("threads"); |
| 87 | + |
| 88 | + String node = threadsEl.attributeValue("node"); |
| 89 | + Mockito.when(channelManager.nodeExists(node)).thenReturn(true); |
| 90 | + |
| 91 | + threadsGet.process(threadsEl, request.getFrom(), request, null); |
| 92 | + Packet response = queue.poll(); |
| 93 | + |
| 94 | + PacketError error = response.getError(); |
| 95 | + Assert.assertNotNull(error); |
| 96 | + Assert.assertEquals(PacketError.Type.auth, error.getType()); |
| 97 | + Assert.assertEquals(PacketError.Condition.forbidden, error.getCondition()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testUserOutcastInOpenChannel() throws Exception { |
| 102 | + IQ request = readStanzaAsIq("/iq/pubsub/threads/request-with-node.stanza"); |
| 103 | + Element threadsEl = request.getChildElement().element("threads"); |
| 104 | + |
| 105 | + String node = threadsEl.attributeValue("node"); |
| 106 | + Mockito.when(channelManager.nodeExists(node)).thenReturn(true); |
| 107 | + |
| 108 | + Map<String, String> conf = new HashMap<String, String>(); |
| 109 | + conf.put(AccessModel.FIELD_NAME, AccessModels.open.toString()); |
| 110 | + Mockito.when(channelManager.getNodeConf(node)).thenReturn(conf); |
| 111 | + |
| 112 | + NodeAffiliationImpl affiliation = new NodeAffiliationImpl(node, |
| 113 | + request.getFrom(), Affiliations.outcast, new Date()); |
| 114 | + Mockito.when(channelManager.getUserAffiliation(node, |
| 115 | + request.getFrom())).thenReturn(affiliation); |
| 116 | + |
| 117 | + NodeSubscriptionImpl subscription = new NodeSubscriptionImpl(node, |
| 118 | + request.getFrom(), Subscriptions.subscribed, new Date()); |
| 119 | + Mockito.when(channelManager.getUserSubscription(node, |
| 120 | + request.getFrom())).thenReturn(subscription); |
| 121 | + |
| 122 | + threadsGet.process(threadsEl, request.getFrom(), request, null); |
| 123 | + Packet response = queue.poll(); |
| 124 | + |
| 125 | + PacketError error = response.getError(); |
| 126 | + Assert.assertNotNull(error); |
| 127 | + Assert.assertEquals(PacketError.Type.auth, error.getType()); |
| 128 | + Assert.assertEquals(PacketError.Condition.forbidden, error.getCondition()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testWrongAfterItemRSM() throws Exception { |
| 133 | + IQ request = readStanzaAsIq("/iq/pubsub/threads/request-with-rsm.stanza"); |
| 134 | + Element threadsEl = request.getChildElement().element("threads"); |
| 135 | + |
| 136 | + String node = threadsEl.attributeValue("node"); |
| 137 | + Mockito.when(channelManager.nodeExists(node)).thenReturn(true); |
| 138 | + |
| 139 | + Map<String, String> conf = new HashMap<String, String>(); |
| 140 | + conf.put(AccessModel.FIELD_NAME, AccessModels.open.toString()); |
| 141 | + Mockito.when(channelManager.getNodeConf(node)).thenReturn(conf); |
| 142 | + |
| 143 | + Element rsmEl = request.getChildElement().element("set"); |
| 144 | + threadsGet.process(threadsEl, request.getFrom(), request, rsmEl); |
| 145 | + Packet response = queue.poll(); |
| 146 | + |
| 147 | + PacketError error = response.getError(); |
| 148 | + Assert.assertNotNull(error); |
| 149 | + Assert.assertEquals(PacketError.Type.cancel, error.getType()); |
| 150 | + Assert.assertEquals(PacketError.Condition.item_not_found, error.getCondition()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testSucessfulEmptyResponse() throws Exception { |
| 155 | + IQ request = readStanzaAsIq("/iq/pubsub/threads/request-with-node.stanza"); |
| 156 | + Element threadsEl = request.getChildElement().element("threads"); |
| 157 | + |
| 158 | + String node = threadsEl.attributeValue("node"); |
| 159 | + Mockito.when(channelManager.nodeExists(node)).thenReturn(true); |
| 160 | + |
| 161 | + Map<String, String> conf = new HashMap<String, String>(); |
| 162 | + conf.put(AccessModel.FIELD_NAME, AccessModels.open.toString()); |
| 163 | + Mockito.when(channelManager.getNodeConf(node)).thenReturn(conf); |
| 164 | + |
| 165 | + Mockito.when(channelManager.getNodeThreads(Mockito.eq(node), Mockito.anyString(), |
| 166 | + Mockito.anyInt())).thenReturn(new ResultSetImpl<NodeThread>( |
| 167 | + new LinkedList<NodeThread>())); |
| 168 | + |
| 169 | + threadsGet.process(threadsEl, request.getFrom(), request, null); |
| 170 | + Packet response = queue.poll(); |
| 171 | + |
| 172 | + Assert.assertNull(response.getError()); |
| 173 | + Assert.assertNull(response.getElement().element("pubsub").element("threads")); |
| 174 | + } |
| 175 | +} |
0 commit comments