Skip to content

Commit

Permalink
leave tests, history with auth key test
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxPresman committed Apr 22, 2016
1 parent 1467bb5 commit eb5d900
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/pubnub/api/endpoints/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ protected final Map<String, String> createBaseParams() {
// add the auth key for publish and subscribe.
if (this.pubnub.getConfiguration().getAuthKey() != null) {
if (getOperationType() == PNOperationType.PNPublishOperation
|| getOperationType() == PNOperationType.PNSubscribeOperation) {
|| getOperationType() == PNOperationType.PNSubscribeOperation
|| getOperationType() == PNOperationType.PNHistoryOperation) {
params.put("auth", pubnub.getConfiguration().getAuthKey());
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/pubnub/api/endpoints/presence/Leave.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import retrofit2.Call;
import retrofit2.Response;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

public Leave(Pubnub pubnub) {
super(pubnub);
channels = new ArrayList<>();
channelGroups = new ArrayList<>();
}

@Setter private List<String> channels;
Expand Down
51 changes: 47 additions & 4 deletions src/test/java/com.pubnub.api/endpoints/HistoryEndpointTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.pubnub.api.endpoints;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import com.pubnub.api.core.Pubnub;
import com.pubnub.api.core.PubnubException;
import com.pubnub.api.core.models.consumer_facing.PNHistoryResult;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertEquals;


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

@Test
public void testSyncAuthSuccess() throws PubnubException, JsonProcessingException {

pubnub.getConfiguration().setAuthKey("authKey");

List<Object> testArray = new ArrayList<Object>();
List<Object> historyItems = new ArrayList<Object>();
ObjectMapper mapper = new ObjectMapper();


Map<String, Object> historyEnvelope1 = new HashMap<String, Object>();
Map<String, Object> historyItem1 = new HashMap<String, Object>();
historyItem1.put("a", 11);
historyItem1.put("b", 22);
historyEnvelope1.put("timetoken", 1111);
historyEnvelope1.put("message", historyItem1);

Map<String, Object> historyEnvelope2 = new HashMap<String, Object>();
Map<String, Object> historyItem2 = new HashMap<String, Object>();
historyItem2.put("a", 33);
historyItem2.put("b", 44);
historyEnvelope2.put("timetoken", 2222);
historyEnvelope2.put("message", historyItem2);

historyItems.add(historyEnvelope1);
historyItems.add(historyEnvelope2);

testArray.add(historyItems);
testArray.add(1234);
testArray.add(4321);

stubFor(get(urlPathEqualTo("/v2/history/sub-key/mySubscribeKey/channel/niceChannel"))
.willReturn(aResponse().withBody(mapper.writeValueAsString(testArray))));


PNHistoryResult response = partialHistory.channel("niceChannel").includeTimetoken(true).sync();

List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
assertEquals("authKey", requests.get(0).queryParameter("auth").firstValue());
assertEquals(1, requests.size());
}


@org.junit.Test
public void testSyncEncryptedSuccess() throws IOException, PubnubException {
Expand Down
127 changes: 127 additions & 0 deletions src/test/java/com.pubnub.api/endpoints/presence/LeaveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.pubnub.api.endpoints.presence;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import com.jayway.awaitility.Awaitility;
import com.pubnub.api.callbacks.PNCallback;
import com.pubnub.api.core.Pubnub;
import com.pubnub.api.core.PubnubException;
import com.pubnub.api.core.models.consumer_facing.PNStatus;
import com.pubnub.api.endpoints.TestHarness;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertEquals;


public class LeaveTest extends TestHarness {

@Rule
public WireMockRule wireMockRule = new WireMockRule();

private Leave instance;
private Pubnub pubnub;

@Before
public void beforeEach() throws IOException {
pubnub = this.createPubNubInstance(8080);
instance = new Leave(pubnub);
}

@Test
public void subscribeChannelSync() throws PubnubException {

stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel/leave"))
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}")));

instance.channels(Arrays.asList("coolChannel")).sync();

List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
assertEquals(1, requests.size());

}

@Test
public void subscribeChannelsSync() throws PubnubException {

stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel,coolChannel2/leave"))
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}")));

instance.channels(Arrays.asList("coolChannel", "coolChannel2")).sync();

List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
assertEquals(1, requests.size());
}


@Test
public void subscribeChannelsWithGroupSync() throws PubnubException {

stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel,coolChannel2/leave"))
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}")));

instance.channels(Arrays.asList("coolChannel", "coolChannel2")).channelGroups(Arrays.asList("cg1")) .sync();

List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
assertEquals(1, requests.size());
assertEquals("cg1", requests.get(0).queryParameter("channel-group").firstValue());
}

@Test
public void subscribeChannelsWithGroupASync() throws PubnubException {

final AtomicBoolean statusArrived = new AtomicBoolean();

stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/coolChannel,coolChannel2/leave"))
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}")));

instance.channels(Arrays.asList("coolChannel", "coolChannel2")).channelGroups(Arrays.asList("cg1")) .async(new PNCallback<Boolean>() {
@Override
public void onResponse(Boolean result, PNStatus status) {
assertEquals(status.getAffectedChannels().get(0), "coolChannel");
assertEquals(status.getAffectedChannels().get(1), "coolChannel2");
assertEquals(status.getAffectedChannelGroups().get(0), "cg1");
statusArrived.set(true);
}
});


Awaitility.await().atMost(2, TimeUnit.SECONDS).untilAtomic(statusArrived, org.hamcrest.core.IsEqual.equalTo(true));
}

@Test
public void subscribeGroupsSync() throws PubnubException {

stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/,/leave"))
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}")));

instance.channelGroups(Arrays.asList("cg1", "cg2")).sync();

List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
assertEquals(1, requests.size());
assertEquals("cg1,cg2", requests.get(0).queryParameter("channel-group").firstValue());
}

@Test
public void subscribeGroupSync() throws PubnubException {

stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/,/leave"))
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\", \"action\": \"leave\"}")));

instance.channelGroups(Arrays.asList("cg1")).sync();

List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
assertEquals(1, requests.size());
assertEquals("cg1", requests.get(0).queryParameter("channel-group").firstValue());
}


}

0 comments on commit eb5d900

Please sign in to comment.