|
| 1 | +package com.pubnub.api.managers; |
| 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.SubscribeCallback; |
| 7 | +import com.pubnub.api.core.Pubnub; |
| 8 | +import com.pubnub.api.core.models.consumer_facing.PNMessageResult; |
| 9 | +import com.pubnub.api.core.models.consumer_facing.PNPresenceEventResult; |
| 10 | +import com.pubnub.api.core.models.consumer_facing.PNStatus; |
| 11 | +import com.pubnub.api.endpoints.EndpointTest; |
| 12 | +import org.junit.*; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
| 20 | + |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 22 | + |
| 23 | +public class SubscriptionManagerTest extends EndpointTest { |
| 24 | + |
| 25 | + private Pubnub pubnub; |
| 26 | + private SubscriptionManager instance; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void beforeEach() throws IOException { |
| 30 | + pubnub = this.createPubNubInstance(8080); |
| 31 | + instance = new SubscriptionManager(pubnub); |
| 32 | + } |
| 33 | + |
| 34 | + @After |
| 35 | + public void afterEach() { |
| 36 | + pubnub.stop(); |
| 37 | + } |
| 38 | + |
| 39 | + @Rule |
| 40 | + public WireMockRule wireMockRule = new WireMockRule(); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testSubscribeBuilder() { |
| 44 | + final AtomicInteger atomic = new AtomicInteger(0); |
| 45 | + stubFor(get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch1/0")) |
| 46 | + .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\"}]}"))); |
| 47 | + |
| 48 | + instance.addListener(new SubscribeCallback() { |
| 49 | + @Override |
| 50 | + public void status(Pubnub pubnub, PNStatus status) { |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void message(Pubnub pubnub, PNMessageResult message) { |
| 55 | + List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*"))); |
| 56 | + Assert.assertEquals(1, requests.size()); |
| 57 | + atomic.addAndGet(1); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void presence(Pubnub pubnub, PNPresenceEventResult presence) { |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + instance.adaptSubscribeBuilder(Arrays.asList("ch1", "ch2"), new ArrayList<String>(), false); |
| 66 | + |
| 67 | + Awaitility.await().atMost(5, TimeUnit.SECONDS) |
| 68 | + .untilAtomic(atomic, org.hamcrest.core.IsEqual.equalTo(1)); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testSubscribePresenceBuilder() { |
| 74 | + final AtomicInteger atomic = new AtomicInteger(0); |
| 75 | + stubFor(get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch2-pnpres,ch1,ch1-pnpres/0")) |
| 76 | + .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\"}]}"))); |
| 77 | + |
| 78 | + instance.addListener(new SubscribeCallback() { |
| 79 | + @Override |
| 80 | + public void status(Pubnub pubnub, PNStatus status) { |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void message(Pubnub pubnub, PNMessageResult message) { |
| 85 | + List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/v2/subscribe.*"))); |
| 86 | + Assert.assertEquals(1, requests.size()); |
| 87 | + Assert.assertEquals("{text=Enter Message Here}", message.getData().getMessage().toString()); |
| 88 | + atomic.addAndGet(1); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void presence(Pubnub pubnub, PNPresenceEventResult presence) { |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + instance.adaptSubscribeBuilder(Arrays.asList("ch1", "ch2"), new ArrayList<String>(), true); |
| 97 | + |
| 98 | + Awaitility.await().atMost(5, TimeUnit.SECONDS) |
| 99 | + .untilAtomic(atomic, org.hamcrest.core.IsEqual.equalTo(1)); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments