|
5 | 5 | import com.jayway.awaitility.Awaitility; |
6 | 6 | import com.pubnub.api.callbacks.SubscribeCallback; |
7 | 7 | import com.pubnub.api.core.Pubnub; |
| 8 | +import com.pubnub.api.core.enums.PNOperationType; |
8 | 9 | import com.pubnub.api.core.models.consumer_facing.PNMessageResult; |
9 | 10 | import com.pubnub.api.core.models.consumer_facing.PNPresenceEventResult; |
10 | 11 | import com.pubnub.api.core.models.consumer_facing.PNStatus; |
|
15 | 16 | import java.util.Arrays; |
16 | 17 | import java.util.List; |
17 | 18 | import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.concurrent.atomic.AtomicBoolean; |
18 | 20 | import java.util.concurrent.atomic.AtomicInteger; |
19 | 21 |
|
20 | 22 | import static com.github.tomakehurst.wiremock.client.WireMock.*; |
@@ -205,6 +207,55 @@ public void presence(Pubnub pubnub, PNPresenceEventResult presence) { |
205 | 207 |
|
206 | 208 | } |
207 | 209 |
|
| 210 | + @Test |
| 211 | + public void testUnsubscribe() { |
| 212 | + |
| 213 | + final AtomicBoolean statusRecieved = new AtomicBoolean(); |
| 214 | + final AtomicBoolean messageRecieved = new AtomicBoolean(); |
| 215 | + |
| 216 | + stubFor(get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch2-pnpres,ch1,ch1-pnpres/0")) |
| 217 | + .willReturn(aResponse().withBody("{\"t\":{\"t\":\"14607577960932487\",\"r\":1},\"m\":[{\"a\":\"4\",\"f\":0,\"i\":\"Client-g5d4g\",\"p\":{\"t\":\"14607577960925503\",\"r\":1},\"k\":\"sub-c-4cec9f8e-01fa-11e6-8180-0619f8945a4f\",\"c\":\"coolChannel\",\"d\":{\"text\":\"Enter Message Here\"},\"b\":\"coolChan-bnel\"}]}"))); |
| 218 | + |
| 219 | + stubFor(get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch2-pnpres/0")) |
| 220 | + .willReturn(aResponse().withBody("{\"t\":{\"t\":\"14607577960932487\",\"r\":1},\"m\":[{\"a\":\"4\",\"f\":0,\"i\":\"Client-g5d4g\",\"p\":{\"t\":\"14607577960925503\",\"r\":1},\"k\":\"sub-c-4cec9f8e-01fa-11e6-8180-0619f8945a4f\",\"c\":\"coolChannel\",\"d\":{\"text\":\"Enter Message Here\"},\"b\":\"coolChan-bnel\"}]}"))); |
| 221 | + |
| 222 | + stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/leave")) |
| 223 | + .willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}"))); |
| 224 | + |
| 225 | + SubscribeCallback sub1 = new SubscribeCallback() { |
| 226 | + @Override |
| 227 | + public void status(Pubnub pubnub, PNStatus status) { |
| 228 | + if (status.getAffectedChannels().size() == 1 && status.getOperation() == PNOperationType.PNUnsubscribeOperation){ |
| 229 | + if (status.getAffectedChannels().get(0).equals("ch1")) { |
| 230 | + statusRecieved.set(true); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public void message(Pubnub pubnub, PNMessageResult message) { |
| 237 | + List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/v2/subscribe/mySubscribeKey/ch2,ch2-pnpres/0.*"))); |
| 238 | + |
| 239 | + if (!requests.isEmpty()) { |
| 240 | + messageRecieved.set(true); |
| 241 | + } |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public void presence(Pubnub pubnub, PNPresenceEventResult presence) { |
| 247 | + } |
| 248 | + }; |
| 249 | + |
| 250 | + pubnub.addListener(sub1); |
| 251 | + |
| 252 | + pubnub.subscribe().channels(Arrays.asList("ch1", "ch2")).withPresence().execute(); |
| 253 | + |
| 254 | + pubnub.unsubscribe().channels(Arrays.asList("ch1")).execute(); |
| 255 | + |
| 256 | + Awaitility.await().atMost(2, TimeUnit.SECONDS).untilAtomic(messageRecieved, org.hamcrest.core.IsEqual.equalTo(true)); |
| 257 | + Awaitility.await().atMost(2, TimeUnit.SECONDS).untilAtomic(statusRecieved, org.hamcrest.core.IsEqual.equalTo(true)); |
| 258 | + } |
208 | 259 |
|
209 | 260 |
|
210 | 261 | } |
0 commit comments