forked from ringcentral/pubnub-jtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHelper.java
More file actions
169 lines (136 loc) · 5.03 KB
/
TestHelper.java
File metadata and controls
169 lines (136 loc) · 5.03 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
package com.pubnub.api;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class TestHelper {
static class SimpleCallback extends Callback {
protected CountDownLatch latch;
Object response;
Boolean error;
public SimpleCallback(CountDownLatch latch) {
this.latch = latch;
}
public SimpleCallback() {}
public Object getResponse() {
return response;
}
public Boolean responseIsError() {
return error;
}
@Override
public void successCallback(String channel, Object message) {
this.response = message;
this.error = false;
if (this.latch != null) {
this.latch.countDown();
}
}
@Override
public void errorCallback(String channel, PubnubError error) {
this.error = true;
if (this.latch != null) {
this.latch.countDown();
}
}
}
static class SubscribeCallback extends SimpleCallback {
public SubscribeCallback(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void connectCallback(String channel, Object message) {
if (this.latch != null) {
this.latch.countDown();
}
}
}
static class PresenceCallback extends Callback {
private String uuid;
private String action;
private CountDownLatch latch;
public String getUUID() {
return uuid;
}
public String getAction() {
return action;
}
public PresenceCallback(CountDownLatch latch) {
this.latch = latch;
}
public void setLatch(CountDownLatch latch) {
this.latch = latch;
}
public PresenceCallback() {
}
@Override
public void successCallback(String channel, Object message) {
JSONObject resp = null;
try {
resp = new JSONObject(message.toString());
} catch (JSONException e1) {
e1.printStackTrace();
}
if (resp != null) {
try {
uuid = (String) resp.get("uuid");
action = (String) resp.get("action");
} catch (JSONException e) {
e.printStackTrace();
}
}
if (latch != null)
latch.countDown();
}
@Override
public void errorCallback(String channel, PubnubError error) {
if (latch != null)
latch.countDown();
}
}
public static void cleanup() throws InterruptedException, JSONException {
Pubnub pubnub = new Pubnub("demo", "demo");
pubnub.setCacheBusting(false);
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
pubnub.channelGroupListGroups(cb1);
latch1.await(10, TimeUnit.SECONDS);
JSONObject result = (JSONObject) cb1.getResponse();
JSONArray groups = result.getJSONArray("groups");
for (int i = 0; i < groups.length(); i++) {
final String group = groups.getString(i);
if (group.startsWith("jtest")) {
CountDownLatch latch = new CountDownLatch(1);
pubnub.channelGroupRemoveGroup(group, new TestHelper.SimpleCallback(latch) {
@Override
public void successCallback(String channel, Object message) {
System.out.println("Successfully removed group " + group);
super.successCallback(channel, message);
}
});
latch.await(10, TimeUnit.SECONDS);
}
}
pubnub.channelGroupListNamespaces(cb2);
latch2.await(10, TimeUnit.SECONDS);
result = (JSONObject) cb2.getResponse();
JSONArray namespaces = result.getJSONArray("namespaces");
for (int i = 0; i < namespaces.length(); i++) {
final String namespace = namespaces.getString(i);
if (namespace.startsWith("jtest")) {
CountDownLatch latch = new CountDownLatch(1);
pubnub.channelGroupRemoveNamespace(namespace, new TestHelper.SimpleCallback(latch) {
@Override
public void successCallback(String channel, Object message) {
System.out.println("Successfully removed namespace " + namespace);
super.successCallback(channel, message);
}
});
latch.await(10, TimeUnit.SECONDS);
}
}
}
}