Repository navigation
Expand file tree
/
Copy pathPubnubUnitTest.java
More file actions
381 lines (323 loc) · 12.2 KB
/
Copy pathPubnubUnitTest.java
File metadata and controls
381 lines (323 loc) · 12.2 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package com.pubnub.tests;
import static java.lang.System.*;
import static org.junit.Assert.*;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Ignore;
import org.junit.Test;
import com.pubnub.api.Callback;
import com.pubnub.api.Pubnub;
import com.pubnub.api.PubnubException;
class Receiver implements Callback {
public boolean successCallback(String channel, Object message) {
assertNull(message);
try {
if (message instanceof JSONObject) {
JSONObject obj = (JSONObject) message;
@SuppressWarnings("rawtypes")
Iterator keys = obj.keys();
while (keys.hasNext()) {
out.print(obj.get(keys.next().toString()) + " ");
}
out.println();
} else if (message instanceof String) {
String obj = (String) message;
out.print(obj + " ");
out.println();
} else if (message instanceof JSONArray) {
JSONArray obj = (JSONArray) message;
out.print(obj.toString() + " ");
out.println();
}
} catch (Exception e) {
fail();
}
// false, do not continue to subscribe
return false;
}
public void errorCallback(String channel, Object message) {
System.err.println("Channel:" + channel + "-" + message.toString());
}
public void connectCallback(String channel) {
out.println("Connected to channel :" + channel);
}
public void reconnectCallback(String channel) {
out.println("Reconnected to channel :" + channel);
}
public void disconnectCallback(String channel) {
out.println("Disconnected to channel :" + channel);
}
}
public class PubnubUnitTest {
static String publish_key = "demo";
static String subscribe_key = "demo";
static String secret_key = "demo";
static String cipher_key = "enigma"; // (Cipher key is optional)
static String channel = "hello_world";
static Pubnub pubnub = null;
@Test
public void testPublishHashMapOfStringObject() {
pubnub = new Pubnub(publish_key, subscribe_key, secret_key, cipher_key,
true);
try {
JSONArray response = pubnub.publish(channel, new JSONObject(
"{ 'text': 'Hello World!'}"));
assertTrue(response.get(0).toString().equals("1"));
} catch (JSONException e) {
fail("FAIL: TestPublish");
}
}
@Test
public void testSubscribeHashMapOfStringObject() {
pubnub = new Pubnub(publish_key, subscribe_key, secret_key, "", true);
// Callback Interface when a Message is Received
class SubscribeReceiver extends Receiver {
public void connectCallback(String channel) {
try {
pubnub.publish(channel, new JSONObject().put("text", "hi"));
} catch (JSONException e) {
fail("FAIL: TestSubscribe, publish");
}
}
public boolean successCallback(String channel, Object message) {
assertNotNull(message);
JSONObject obj = (JSONObject) message;
try {
assertTrue(obj.get("text").equals("hi"));
} catch (JSONException e) {
fail("FAIL: TestSubscribe, Success Callback");
}
// false, do not continue to subscribe
return false;
}
}
// Listen for Messages (Subscribe)
try {
pubnub.subscribe(channel, new SubscribeReceiver());
} catch (PubnubException e) {
fail("FAIL: TestSubscribe, PubnubException");
return;
}
return;
}
/**
*
*/
@Ignore
public void testPresenceHashMapOfStringObject() {
Pubnub pubnub_p = new Pubnub(publish_key, subscribe_key, secret_key,"", true);
final Pubnub pubnub_s = new Pubnub(publish_key,subscribe_key, secret_key, "", true);
class SubscribeReceiver extends Receiver {
@Override
public void connectCallback(String channel) {
}
@Override
public boolean successCallback(String channel, Object message) {
return false;
}
}
// Callback Interface when a Message is Received
class PresenceReceiver extends Receiver {
@Override
public void connectCallback(String channel) {
try {
pubnub_s.subscribe(PubnubUnitTest.channel, new SubscribeReceiver());
} catch (PubnubException e) {
fail("TestPresence : PubnubException");
return;
}
}
@Override
public boolean successCallback(String channel, Object message) {
assertNull(message);
JSONArray jsona = (JSONArray) message; JSONArray jsona0 = null;
try {
jsona0 = (JSONArray) jsona.get(0);
} catch (JSONException e1) {
fail("FAIL: TestPresence, publish"); return false;
}
if (jsona0.length() == 0 ) {
}
// false, do not continue to subscribe
try {
pubnub_s.publish(channel, new JSONObject("{'text': 'hi'}"));
} catch (JSONException e) {
fail("FAIL: TestPresence, publish");
}
return false;
}
}
// Listen for Messages (Presence)
try {
pubnub_p.presence(channel, new PresenceReceiver());
} catch (PubnubException e) {
fail("TestPresence : PubnubException");
return;
}
}
@Test
public void testHere_now() {
pubnub = new Pubnub(publish_key, subscribe_key, secret_key, "", true);
// Get Here Now
class SubscribeReceiver extends Receiver {
@Override
public void connectCallback(String channel) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
fail("InterruptedException");
}
JSONArray response = pubnub.here_now(channel);
try {
boolean uuidfound = false;
JSONArray uuidslist = (JSONArray) new JSONObject(response
.get(0).toString()).get("uuids");
for (int i = 0; i < uuidslist.length(); i++) {
if (uuidslist.getString(i).equals(pubnub.sessionUUID())) {
uuidfound = true;
break;
}
}
assertTrue(uuidfound);
} catch (JSONException e1) {
fail("JSONException");
}
assertNotNull(response);
try {
pubnub.publish(channel, new JSONObject().put("text", "hi"));
} catch (JSONException e) {
fail("JSONException");
}
}
@Override
public boolean successCallback(String channel, Object message) {
return false;
}
}
try {
pubnub.subscribe(channel, new SubscribeReceiver());
} catch (PubnubException e) {
fail("PubnubException");
}
}
private void testHistoryHashMapOfStringObject(Pubnub pubnub) {
// fresh channel required. Existing unencrypted messages published to
// channel cause errors in decryption
String channel = PubnubUnitTest.channel + '-' + pubnub.sessionUUID();
JSONObject message = new JSONObject();
try {
message.put("text", "Hello World!");
} catch (JSONException e) {
fail("FAIL: TestEncryptedHistory, JSON Exception");
}
JSONArray response = null;
for (int i = 0; i < 5; i++) {
response = pubnub.publish(channel, message);
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
fail("InterruptedException");
}
// Get History
response = pubnub.history(channel, 1);
assertTrue(response.length() == 1);
// Get History
response = pubnub.history(channel, 5);
assertTrue(response.length() == 5);
}
@Test
public void testUnencryptedHistoryHashMapOfStringObject() {
// Context setup
pubnub = new Pubnub(publish_key, subscribe_key, secret_key, "", true);
testHistoryHashMapOfStringObject(pubnub);
}
@Test
public void testEncryptedHistoryHashMapOfStringObject() {
// Context setup
pubnub = new Pubnub(publish_key, subscribe_key, secret_key, cipher_key,
true);
testHistoryHashMapOfStringObject(pubnub);
}
private void publishForDetailedHistory(String channel, int number,
int offset, String[] inputs) {
for (int i = 0 + offset; i < number + offset; i++) {
String msg = Integer.toString(i);
JSONObject json = new JSONObject();
try {
json.put("text", msg);
} catch (JSONException e) {
fail("FAIL: JSON Exception in publishForDetailedHistory");
}
pubnub.publish(channel, json);
if (inputs != null)
inputs[i] = msg;
}
}
public void testDetailedHistory(Pubnub pubnub) {
int total_msg = 6;
String[] inputs = new String[total_msg];
// fresh channel required. Existing unencrypted messages published to
// channel cause errors in decryption
String channel = PubnubUnitTest.channel + '-' + pubnub.sessionUUID();
long starttime = (long) pubnub.time();
publishForDetailedHistory(channel, total_msg / 2, 0, inputs);
long midtime = (long) pubnub.time();
publishForDetailedHistory(channel, total_msg / 2, total_msg / 2, inputs);
long endtime = (long) pubnub.time();
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
JSONArray response = null;
response = pubnub.detailedHistory(channel, starttime, endtime);
try {
assertTrue(response != null && ((JSONArray) response.get(0)).length() == total_msg);
} catch (JSONException e) {
fail("JSONException");
}
response = pubnub.detailedHistory(channel, starttime, midtime,
total_msg, true);
try {
assertTrue(response != null && ((JSONArray) response.get(0)).length() == total_msg/2);
} catch (JSONException e) {
fail("JSONException");
}
response = pubnub.detailedHistory(channel,(int)1, false);
try {
assertTrue(response != null &&
Integer.parseInt((String)((JSONObject)((JSONArray) response.get(0)).get(0)).get("text")) == total_msg -1);
} catch (JSONException e) {
fail("JSONException");
}
response = pubnub.detailedHistory(channel,(int)1, true);
try {
assertTrue(response != null &&
Integer.parseInt((String)((JSONObject)((JSONArray) response.get(0)).get(0)).get("text")) == 0);
} catch (JSONException e) {
fail("JSONException");
}
}
@Test
public void testUnencryptedDetailedHistory() {
pubnub = new Pubnub(publish_key, subscribe_key, secret_key, "", true);
testDetailedHistory(pubnub);
}
@Test
public void testEncryptedDetailedHistory() {
pubnub = new Pubnub(publish_key, subscribe_key, secret_key, cipher_key,
true);
testDetailedHistory(pubnub);
}
@Test
public void testTime() {
assertNotNull(pubnub.time());
}
@Test
public void testUuid() {
assertNotNull(Pubnub.uuid());
}
}