Skip to content

Commit 807668d

Browse files
committed
add get/remove namespaces
1 parent 4494970 commit 807668d

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

java/srcPubnubApi/com/pubnub/api/PubnubCore.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,61 @@ protected void invokeJSONArrayCallback(String response, Callback callback) {
10481048
}
10491049
}
10501050

1051+
/**
1052+
* Get all namespaces
1053+
*
1054+
* @param callback to invoke
1055+
*/
1056+
public void namespaces(Callback callback) {
1057+
final Callback cb = getWrappedCallback(callback);
1058+
1059+
String[] url = new String[]{getPubnubUrl(), "v1", "channel-registration", "sub-key",
1060+
this.SUBSCRIBE_KEY, "namespace"};
1061+
1062+
Hashtable parameters = PubnubUtil.hashtableClone(params);
1063+
1064+
HttpRequest hreq = new HttpRequest(url, parameters, new ResponseHandler() {
1065+
1066+
public void handleResponse(HttpRequest hreq, String response) {
1067+
invokeJSONArrayCallback(response, cb);
1068+
}
1069+
1070+
public void handleError(HttpRequest hreq, PubnubError error) {
1071+
cb.errorCallback(null, error);
1072+
}
1073+
});
1074+
1075+
_request(hreq, nonSubscribeManager);
1076+
}
1077+
1078+
/**
1079+
* Remove namespace
1080+
*
1081+
* @param namespace to remove
1082+
* @param callback to invoke
1083+
*/
1084+
public void removeNamespace(String namespace, Callback callback) {
1085+
final Callback cb = getWrappedCallback(callback);
1086+
1087+
String[] url = new String[]{getPubnubUrl(), "v1", "channel-registration", "sub-key",
1088+
this.SUBSCRIBE_KEY, "namespace", namespace, "remove"};
1089+
1090+
Hashtable parameters = PubnubUtil.hashtableClone(params);
1091+
1092+
HttpRequest hreq = new HttpRequest(url, parameters, new ResponseHandler() {
1093+
1094+
public void handleResponse(HttpRequest hreq, String response) {
1095+
cb.successCallback(null, PubnubUtil.parseJSON(response));
1096+
}
1097+
1098+
public void handleError(HttpRequest hreq, PubnubError error) {
1099+
cb.errorCallback(null, error);
1100+
}
1101+
});
1102+
1103+
_request(hreq, nonSubscribeManager);
1104+
}
1105+
10511106
public void group(Callback callback) {
10521107
group(null, callback);
10531108
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.pubnub.api;
2+
3+
import org.json.JSONArray;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.concurrent.CountDownLatch;
8+
import java.util.concurrent.TimeUnit;
9+
10+
import static com.pubnub.api.matchers.JSONAssert.assertJSONArrayHas;
11+
import static com.pubnub.api.matchers.JSONAssert.assertJSONArrayHasNo;
12+
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertFalse;
14+
15+
public class NamespaceTest {
16+
Pubnub pubnub = new Pubnub("demo", "demo");
17+
double random;
18+
19+
@Before
20+
public void setUp() {
21+
pubnub.setOrigin("dara24.devbuild");
22+
pubnub.setCacheBusting(false);
23+
24+
random = Math.random();
25+
}
26+
27+
@Test
28+
public void testGetAllNamespacesAndRemoveThem() throws InterruptedException {
29+
final CountDownLatch latch1 = new CountDownLatch(3);
30+
final CountDownLatch latch2 = new CountDownLatch(1);
31+
final CountDownLatch latch3 = new CountDownLatch(3);
32+
final CountDownLatch latch4 = new CountDownLatch(1);
33+
34+
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
35+
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
36+
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
37+
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4);
38+
39+
String[] groups = new String[]{"jtest1" + random, "jtest2" + random, "jtest3" + random};
40+
String[] namespaces = new String[]{"jspace1" + random, "jspace2" + random, "jspace13" + random};
41+
42+
// add
43+
for (int i = 0; i < groups.length; i++) {
44+
String group = groups[i];
45+
String namespace = namespaces[i];
46+
47+
pubnub.addChannelToGroup(namespace, group, "ch1", cb1);
48+
}
49+
50+
latch1.await(10, TimeUnit.SECONDS);
51+
52+
// get
53+
pubnub.namespaces(cb2);
54+
latch2.await(10, TimeUnit.SECONDS);
55+
56+
JSONArray result = (JSONArray) cb2.getResponse();
57+
58+
assertFalse("Error is thrown", cb1.responseIsError());
59+
assertEquals("OK", cb1.getResponse());
60+
61+
assertJSONArrayHas(namespaces[0], result);
62+
assertJSONArrayHas(namespaces[1], result);
63+
assertJSONArrayHas(namespaces[2], result);
64+
65+
// remove
66+
pubnub.removeNamespace(namespaces[0], cb3);
67+
pubnub.removeNamespace(namespaces[1], cb3);
68+
pubnub.removeNamespace(namespaces[2], cb3);
69+
70+
latch3.await(10, TimeUnit.SECONDS);
71+
72+
// get again
73+
pubnub.namespaces(cb4);
74+
latch4.await(10, TimeUnit.SECONDS);
75+
76+
result = (JSONArray) cb4.getResponse();
77+
78+
assertFalse("Error is thrown", cb3.responseIsError());
79+
assertEquals("OK", cb3.getResponse());
80+
81+
assertJSONArrayHasNo(namespaces[0], result);
82+
assertJSONArrayHasNo(namespaces[1], result);
83+
assertJSONArrayHasNo(namespaces[2], result);
84+
}
85+
}

0 commit comments

Comments
 (0)