|
| 1 | +package com.pubnub.api.endpoints.presence; |
| 2 | + |
| 3 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 4 | +import com.github.tomakehurst.wiremock.verification.LoggedRequest; |
| 5 | +import com.jayway.awaitility.Awaitility; |
| 6 | +import com.pubnub.api.callbacks.PNCallback; |
| 7 | +import com.pubnub.api.core.Pubnub; |
| 8 | +import com.pubnub.api.core.PubnubException; |
| 9 | +import com.pubnub.api.core.models.consumer_facing.PNStatus; |
| 10 | +import com.pubnub.api.endpoints.TestHarness; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Rule; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 20 | + |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | + |
| 24 | + |
| 25 | +public class LeaveTest extends TestHarness { |
| 26 | + |
| 27 | + @Rule |
| 28 | + public WireMockRule wireMockRule = new WireMockRule(); |
| 29 | + |
| 30 | + private Leave instance; |
| 31 | + private Pubnub pubnub; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void beforeEach() throws IOException { |
| 35 | + pubnub = this.createPubNubInstance(8080); |
| 36 | + instance = new Leave(pubnub); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void subscribeChannelSync() throws PubnubException { |
| 41 | + |
| 42 | + stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel/leave")) |
| 43 | + .willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}"))); |
| 44 | + |
| 45 | + instance.channels(Arrays.asList("coolChannel")).sync(); |
| 46 | + |
| 47 | + List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*"))); |
| 48 | + assertEquals(1, requests.size()); |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void subscribeChannelsSync() throws PubnubException { |
| 54 | + |
| 55 | + stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel,coolChannel2/leave")) |
| 56 | + .willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}"))); |
| 57 | + |
| 58 | + instance.channels(Arrays.asList("coolChannel", "coolChannel2")).sync(); |
| 59 | + |
| 60 | + List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*"))); |
| 61 | + assertEquals(1, requests.size()); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + @Test |
| 66 | + public void subscribeChannelsWithGroupSync() throws PubnubException { |
| 67 | + |
| 68 | + stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel,coolChannel2/leave")) |
| 69 | + .willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}"))); |
| 70 | + |
| 71 | + instance.channels(Arrays.asList("coolChannel", "coolChannel2")).channelGroups(Arrays.asList("cg1")) .sync(); |
| 72 | + |
| 73 | + List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*"))); |
| 74 | + assertEquals(1, requests.size()); |
| 75 | + assertEquals("cg1", requests.get(0).queryParameter("channel-group").firstValue()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void subscribeChannelsWithGroupASync() throws PubnubException { |
| 80 | + |
| 81 | + final AtomicBoolean statusArrived = new AtomicBoolean(); |
| 82 | + |
| 83 | + stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel,coolChannel2/leave")) |
| 84 | + .willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}"))); |
| 85 | + |
| 86 | + instance.channels(Arrays.asList("coolChannel", "coolChannel2")).channelGroups(Arrays.asList("cg1")) .async(new PNCallback<Boolean>() { |
| 87 | + @Override |
| 88 | + public void onResponse(Boolean result, PNStatus status) { |
| 89 | + assertEquals(status.getAffectedChannels().get(0), "coolChannel"); |
| 90 | + assertEquals(status.getAffectedChannels().get(1), "coolChannel2"); |
| 91 | + assertEquals(status.getAffectedChannelGroups().get(0), "cg1"); |
| 92 | + statusArrived.set(true); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + |
| 97 | + Awaitility.await().atMost(2, TimeUnit.SECONDS).untilAtomic(statusArrived, org.hamcrest.core.IsEqual.equalTo(true)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void subscribeGroupsSync() throws PubnubException { |
| 102 | + |
| 103 | + stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/,/leave")) |
| 104 | + .willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}"))); |
| 105 | + |
| 106 | + instance.channelGroups(Arrays.asList("cg1", "cg2")).sync(); |
| 107 | + |
| 108 | + List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*"))); |
| 109 | + assertEquals(1, requests.size()); |
| 110 | + assertEquals("cg1,cg2", requests.get(0).queryParameter("channel-group").firstValue()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void subscribeGroupSync() throws PubnubException { |
| 115 | + |
| 116 | + stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/,/leave")) |
| 117 | + .willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}"))); |
| 118 | + |
| 119 | + instance.channelGroups(Arrays.asList("cg1")).sync(); |
| 120 | + |
| 121 | + List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*"))); |
| 122 | + assertEquals(1, requests.size()); |
| 123 | + assertEquals("cg1", requests.get(0).queryParameter("channel-group").firstValue()); |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | +} |
0 commit comments