Skip to content

Commit eb5d900

Browse files
committed
leave tests, history with auth key test
1 parent 1467bb5 commit eb5d900

4 files changed

Lines changed: 179 additions & 5 deletions

File tree

‎src/main/java/com/pubnub/api/endpoints/Endpoint.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ protected final Map<String, String> createBaseParams() {
200200
// add the auth key for publish and subscribe.
201201
if (this.pubnub.getConfiguration().getAuthKey() != null) {
202202
if (getOperationType() == PNOperationType.PNPublishOperation
203-
|| getOperationType() == PNOperationType.PNSubscribeOperation) {
203+
|| getOperationType() == PNOperationType.PNSubscribeOperation
204+
|| getOperationType() == PNOperationType.PNHistoryOperation) {
204205
params.put("auth", pubnub.getConfiguration().getAuthKey());
205206
}
206207
}

‎src/main/java/com/pubnub/api/endpoints/presence/Leave.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import retrofit2.Call;
1212
import retrofit2.Response;
1313

14+
import java.util.ArrayList;
1415
import java.util.List;
1516
import java.util.Map;
1617

@@ -19,6 +20,8 @@ public class Leave extends Endpoint<Envelope, Boolean> {
1920

2021
public Leave(Pubnub pubnub) {
2122
super(pubnub);
23+
channels = new ArrayList<>();
24+
channelGroups = new ArrayList<>();
2225
}
2326

2427
@Setter private List<String> channels;

‎src/test/java/com.pubnub.api/endpoints/HistoryEndpointTest.java‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package com.pubnub.api.endpoints;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.fasterxml.jackson.databind.JsonNode;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import com.github.tomakehurst.wiremock.junit.WireMockRule;
7+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
68
import com.pubnub.api.core.Pubnub;
79
import com.pubnub.api.core.PubnubException;
810
import com.pubnub.api.core.models.consumer_facing.PNHistoryResult;
911
import org.junit.Assert;
1012
import org.junit.Before;
1113
import org.junit.Rule;
14+
import org.junit.Test;
1215

1316
import java.io.IOException;
14-
import java.util.ArrayList;
15-
import java.util.HashMap;
16-
import java.util.List;
17-
import java.util.Map;
17+
import java.util.*;
1818

1919
import static com.github.tomakehurst.wiremock.client.WireMock.*;
20+
import static org.junit.Assert.assertEquals;
2021

2122

2223
public class HistoryEndpointTest extends TestHarness {
@@ -81,6 +82,48 @@ public void testSyncSuccess() throws IOException, PubnubException {
8182
Assert.assertEquals(((JsonNode) response.getMessages().get(1).getEntry()).get("b").asInt(), 44);
8283
}
8384

85+
@Test
86+
public void testSyncAuthSuccess() throws PubnubException, JsonProcessingException {
87+
88+
pubnub.getConfiguration().setAuthKey("authKey");
89+
90+
List<Object> testArray = new ArrayList<Object>();
91+
List<Object> historyItems = new ArrayList<Object>();
92+
ObjectMapper mapper = new ObjectMapper();
93+
94+
95+
Map<String, Object> historyEnvelope1 = new HashMap<String, Object>();
96+
Map<String, Object> historyItem1 = new HashMap<String, Object>();
97+
historyItem1.put("a", 11);
98+
historyItem1.put("b", 22);
99+
historyEnvelope1.put("timetoken", 1111);
100+
historyEnvelope1.put("message", historyItem1);
101+
102+
Map<String, Object> historyEnvelope2 = new HashMap<String, Object>();
103+
Map<String, Object> historyItem2 = new HashMap<String, Object>();
104+
historyItem2.put("a", 33);
105+
historyItem2.put("b", 44);
106+
historyEnvelope2.put("timetoken", 2222);
107+
historyEnvelope2.put("message", historyItem2);
108+
109+
historyItems.add(historyEnvelope1);
110+
historyItems.add(historyEnvelope2);
111+
112+
testArray.add(historyItems);
113+
testArray.add(1234);
114+
testArray.add(4321);
115+
116+
stubFor(get(urlPathEqualTo("/v2/history/sub-key/mySubscribeKey/channel/niceChannel"))
117+
.willReturn(aResponse().withBody(mapper.writeValueAsString(testArray))));
118+
119+
120+
PNHistoryResult response = partialHistory.channel("niceChannel").includeTimetoken(true).sync();
121+
122+
List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
123+
assertEquals("authKey", requests.get(0).queryParameter("auth").firstValue());
124+
assertEquals(1, requests.size());
125+
}
126+
84127

85128
@org.junit.Test
86129
public void testSyncEncryptedSuccess() throws IOException, PubnubException {
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

Comments
 (0)