summaryrefslogtreecommitdiffstats
blob: 2ec75bca641f6a354469faae51c0c6873adcd3a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (c) 2013 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.Before;
import com.isode.stroke.elements.BlockPayload;
import com.isode.stroke.elements.BlockListPayload;
import com.isode.stroke.elements.UnblockPayload;
import com.isode.stroke.client.StanzaChannel;
import com.isode.stroke.client.DummyStanzaChannel;
import com.isode.stroke.client.ClientBlockListManager;
import com.isode.stroke.queries.IQRouter;
import com.isode.stroke.elements.IQ;
import com.isode.stroke.queries.GenericRequest;
import com.isode.stroke.jid.JID;
import java.util.Vector;

public class ClientBlockListManagerTest {

	private JID ownJID_ = new JID();
	private IQRouter iqRouter_;
	private DummyStanzaChannel stanzaChannel_;
	private ClientBlockListManager clientBlockListManager_;

	private void helperInitialBlockListFetch(final Vector<JID> blockedJids) {
		BlockList blockList = clientBlockListManager_.requestBlockList();
		assertNotNull(blockList);

		// check for IQ request
		IQ request = stanzaChannel_.getStanzaAtIndex(new IQ(), 0);
		assertNotNull(request);
		BlockListPayload requestPayload = request.getPayload(new BlockListPayload());
		assertNotNull(requestPayload);

		assertEquals(BlockList.State.Requesting, blockList.getState());
		assertEquals(BlockList.State.Requesting, clientBlockListManager_.getBlockList().getState());

		// build IQ response
		BlockListPayload responsePayload = new BlockListPayload();
		for(final JID jid : blockedJids) {
			responsePayload.addItem(jid);
		}

		IQ response = IQ.createResult(ownJID_, new JID(), request.getID(), responsePayload);
		stanzaChannel_.sendIQ(response);
		stanzaChannel_.onIQReceived.emit(response);

		assertEquals(BlockList.State.Available, clientBlockListManager_.getBlockList().getState());
		assertEquals(responsePayload.getItems(), clientBlockListManager_.getBlockList().getItems());
	}

	@Before
	public void setUp() {
		ownJID_ = new JID("kev@wonderland.lit");
		stanzaChannel_ = new DummyStanzaChannel();
		iqRouter_ = new IQRouter(stanzaChannel_);
		iqRouter_.setJID(ownJID_);
		clientBlockListManager_ = new ClientBlockListManager(iqRouter_);
	}

	@Test
	public void testFetchBlockList() {
		Vector<JID> blockJids = new Vector<JID>();
		blockJids.add(new JID("romeo@montague.net"));
		blockJids.add(new JID("iago@shakespeare.lit"));
		helperInitialBlockListFetch(blockJids);

		assertEquals(2, clientBlockListManager_.getBlockList().getItems().size());
	}

	@Test
	public void testBlockCommand() {
		// start with an already fetched block list
		Vector<JID> vec = new Vector<JID>();
		vec.add(new JID("iago@shakespeare.lit"));
		helperInitialBlockListFetch(vec);

		assertEquals(1, clientBlockListManager_.getBlockList().getItems().size());
		assertEquals(BlockList.State.Available, clientBlockListManager_.getBlockList().getState());

		GenericRequest<BlockPayload> blockRequest = clientBlockListManager_.createBlockJIDRequest(new JID("romeo@montague.net"));
		blockRequest.send();
		IQ request = stanzaChannel_.getStanzaAtIndex(new IQ(), 2);
		assertNotNull(request);
		BlockPayload blockPayload = request.getPayload(new BlockPayload());
		assertNotNull(blockPayload);
		assertEquals(new JID("romeo@montague.net"), blockPayload.getItems().get(0));

		IQ blockRequestResponse = IQ.createResult(request.getFrom(), new JID(), request.getID());
		stanzaChannel_.sendIQ(blockRequestResponse);
		stanzaChannel_.onIQReceived.emit(blockRequestResponse);

		assertEquals((1), clientBlockListManager_.getBlockList().getItems().size());

		// send block push
		BlockPayload pushPayload = new BlockPayload();
		pushPayload.addItem(new JID("romeo@montague.net"));
		IQ blockPush = IQ.createRequest(IQ.Type.Set, ownJID_, "push1", pushPayload);
		stanzaChannel_.sendIQ(blockPush);
		stanzaChannel_.onIQReceived.emit(blockPush);

		Vector<JID> blockedJIDs = clientBlockListManager_.getBlockList().getItems();
		assertTrue(blockedJIDs.contains(new JID("romeo@montague.net")));
	}

	@Test
	public void testUnblockCommand() {
		// start with an already fetched block list
		Vector<JID> initialBlockList = new Vector<JID>();
		initialBlockList.add(new JID("iago@shakespeare.lit"));
		initialBlockList.add(new JID("romeo@montague.net"));
		helperInitialBlockListFetch(initialBlockList);

		assertEquals((2), clientBlockListManager_.getBlockList().getItems().size());
		assertEquals(BlockList.State.Available, clientBlockListManager_.getBlockList().getState());

		GenericRequest<UnblockPayload> unblockRequest = clientBlockListManager_.createUnblockJIDRequest(new JID("romeo@montague.net"));
		unblockRequest.send();
		IQ request = stanzaChannel_.getStanzaAtIndex(new IQ(), 2);
		assertNotNull(request);
		UnblockPayload unblockPayload = request.getPayload(new UnblockPayload());
		assertNotNull(unblockPayload);
		assertEquals(new JID("romeo@montague.net"), unblockPayload.getItems().get(0));

		IQ unblockRequestResponse = IQ.createResult(request.getFrom(), new JID(), request.getID());
		stanzaChannel_.sendIQ(unblockRequestResponse);
		stanzaChannel_.onIQReceived.emit(unblockRequestResponse);

		assertEquals((2), clientBlockListManager_.getBlockList().getItems().size());

		// send block push
		UnblockPayload pushPayload = new UnblockPayload();
		pushPayload.addItem(new JID("romeo@montague.net"));
		IQ unblockPush = IQ.createRequest(IQ.Type.Set, ownJID_, "push1", pushPayload);
		stanzaChannel_.sendIQ(unblockPush);
		stanzaChannel_.onIQReceived.emit(unblockPush);

		Vector<JID> blockedJIDs = clientBlockListManager_.getBlockList().getItems();
		assertFalse(blockedJIDs.contains(new JID("romeo@montague.net")));
	}

	@Test
	public void testUnblockAllCommand() {
		// start with an already fetched block list
		Vector<JID> initialBlockList = new Vector<JID>();
		initialBlockList.add(new JID("iago@shakespeare.lit"));
		initialBlockList.add(new JID("romeo@montague.net"));
		initialBlockList.add(new JID("benvolio@montague.net"));
		helperInitialBlockListFetch(initialBlockList);

		assertEquals((3), clientBlockListManager_.getBlockList().getItems().size());
		assertEquals(BlockList.State.Available, clientBlockListManager_.getBlockList().getState());

		GenericRequest<UnblockPayload> unblockRequest = clientBlockListManager_.createUnblockAllRequest();
		unblockRequest.send();
		IQ request = stanzaChannel_.getStanzaAtIndex(new IQ(), 2);
		assertNotNull(request);
		UnblockPayload unblockPayload = request.getPayload(new UnblockPayload());
		assertNotNull(unblockPayload);
		assertEquals(true, unblockPayload.getItems().isEmpty());

		IQ unblockRequestResponse = IQ.createResult(request.getFrom(), new JID(), request.getID());
		stanzaChannel_.sendIQ(unblockRequestResponse);
		stanzaChannel_.onIQReceived.emit(unblockRequestResponse);

		assertEquals((3), clientBlockListManager_.getBlockList().getItems().size());

		// send block push
		UnblockPayload pushPayload = new UnblockPayload();
		IQ unblockPush = IQ.createRequest(IQ.Type.Set, ownJID_, "push1", pushPayload);
		stanzaChannel_.sendIQ(unblockPush);
		stanzaChannel_.onIQReceived.emit(unblockPush);

		assertEquals(true, clientBlockListManager_.getBlockList().getItems().isEmpty());
	}
}