|
| 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