Skip to content

Commit 78f0e1e

Browse files
committed
Initial tests for NodeThreadsGet.
1 parent f80757b commit 78f0e1e

5 files changed

Lines changed: 205 additions & 1 deletion

File tree

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/pubsub/get/NodeThreadsGet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
6262
}
6363
if (!parseRsmElement()) {
6464
outQueue.put(response);
65+
return;
6566
}
6667
getNodeThreads();
6768
addRsmElement();
@@ -158,7 +159,7 @@ private boolean userCanViewNode() throws NodeStoreException {
158159
}
159160

160161
private boolean parseRsmElement() throws NodeStoreException {
161-
if (null == resultSetManagement) {
162+
if (resultSetManagement == null) {
162163
return true;
163164
}
164165
Element maxEl = resultSetManagement.element("max");
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<iq type="get"
2+
from="juliet@shakespeare.lit/barracks"
3+
to="channels.shakespeare.lit"
4+
id="configure1">
5+
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
6+
<threads/>
7+
</pubsub>
8+
</iq>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<iq type="get"
2+
from="juliet@shakespeare.lit/barracks"
3+
to="channels.shakespeare.lit"
4+
id="configure1">
5+
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
6+
<threads node="/user/francisco@denmark.lit/posts" />
7+
</pubsub>
8+
</iq>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<iq type="get"
2+
from="juliet@shakespeare.lit/barracks"
3+
to="channels.shakespeare.lit"
4+
id="configure1">
5+
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
6+
<threads node="/user/francisco@denmark.lit/posts" />
7+
<set xmlns='http://jabber.org/protocol/rsm'>
8+
<max>10</max>
9+
<after>peterpan@neverland.lit</after>
10+
</set>
11+
</pubsub>
12+
</iq>

0 commit comments

Comments
 (0)