Skip to content

Commit 01ed9ec

Browse files
committed
Merge remote-tracking branch 'origin/codecov' into develop
2 parents 5d03cfb + 04af301 commit 01ed9ec

24 files changed

Lines changed: 2038 additions & 150 deletions

src/main/java/com/pubnub/api/endpoints/channel_groups/AddChannelChannelGroup.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ protected PNChannelGroupsAddChannelResult createResponse(Response<Envelope> inpu
5757
if (input.body() == null) {
5858
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).build();
5959
}
60+
6061
return PNChannelGroupsAddChannelResult.builder().build();
6162
}
6263

src/main/java/com/pubnub/api/endpoints/presence/SetState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected Call<Envelope<Map<String, Object>>> doWork(Map<String, String> params)
100100
@Override
101101
protected PNSetStateResult createResponse(Response<Envelope<Map<String, Object>>> input) throws PubNubException {
102102

103-
if (input.body() == null) {
103+
if (input.body() == null || input.body().getPayload() == null) {
104104
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).build();
105105
}
106106

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import com.fasterxml.jackson.databind.JsonNode;
2+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
3+
import com.pubnub.api.PubNub;
4+
import com.pubnub.api.PubNubError;
5+
import com.pubnub.api.PubNubException;
6+
import com.pubnub.api.endpoints.TestHarness;
7+
import com.pubnub.api.endpoints.pubsub.Publish;
8+
import org.junit.Before;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
12+
import java.io.IOException;
13+
14+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
15+
import static org.junit.Assert.assertEquals;
16+
17+
/**
18+
* Created by tukunare on 7/5/2016.
19+
*/
20+
public class PubNubExceptionTest extends TestHarness {
21+
@Rule
22+
public WireMockRule wireMockRule = new WireMockRule();
23+
24+
private PubNub pubnub;
25+
private Publish instance;
26+
27+
@Before
28+
public void beforeEach() throws IOException {
29+
pubnub = this.createPubNubInstance(8080);
30+
instance = pubnub.publish();
31+
}
32+
33+
@Test
34+
public void testPubnubError() throws PubNubException, InterruptedException {
35+
36+
stubFor(get(urlPathEqualTo("/publish/myPublishKey/mySubscribeKey/0/coolChannel/0/%22hi%22"))
37+
.willReturn(aResponse().withStatus(404).withBody("[1,\"Sent\",\"14598111595318003\"]")));
38+
39+
int statusCode = -1;
40+
PubNubError pubnubError = null;
41+
int pnErrorCode= - 1;
42+
int pnErroCodeExtended = -1;
43+
JsonNode pnErrorJNode = null;
44+
String pnErrorMessage = null;
45+
String pnErrorString = null;
46+
String response = null;
47+
String erroMsg = null;
48+
JsonNode jNode = null;
49+
50+
51+
try {
52+
instance.channel("coolChannel").message(new Object()).sync();
53+
}
54+
catch (PubNubException error) {
55+
statusCode = error.getStatusCode();
56+
pubnubError = error.getPubnubError();
57+
pnErrorCode = pubnubError.getErrorCode();
58+
pnErroCodeExtended = pubnubError.getErrorCodeExtended();
59+
pnErrorJNode = pubnubError.getErrorObject();
60+
pnErrorMessage = pubnubError.getMessage();
61+
pnErrorString = pubnubError.getErrorString();
62+
response = error.getResponse();
63+
erroMsg = error.getErrormsg();
64+
jNode = error.getJso();
65+
}
66+
67+
assertEquals(0, statusCode);
68+
}
69+
70+
71+
}

src/test/java/com/pubnub/api/PubNubTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.pubnub.api;
22

3+
import com.pubnub.api.callbacks.SubscribeCallback;
4+
import com.pubnub.api.enums.PNReconnectionPolicy;
5+
import com.pubnub.api.models.consumer.PNStatus;
6+
import com.pubnub.api.models.consumer.pubsub.PNMessageResult;
7+
import com.pubnub.api.models.consumer.pubsub.PNPresenceEventResult;
38
import org.junit.Assert;
49
import org.junit.Before;
510
import org.junit.Test;
611

712
import java.io.IOException;
13+
import java.util.Arrays;
814

915
public class PubNubTest {
1016
private PubNub pubnub;
@@ -56,4 +62,53 @@ public void testDecryptConfigurationKey() throws PubNubException {
5662

5763
}
5864

65+
@org.junit.Test
66+
public void testPNConfiguration() throws IOException, PubNubException {
67+
pnConfiguration.setSubscribeTimeout(3000);
68+
pnConfiguration.setConnectTimeout(4000);
69+
pnConfiguration.setNonSubscribeRequestTimeout(5000);
70+
pnConfiguration.setReconnectionPolicy(PNReconnectionPolicy.NONE);
71+
pubnub = new PubNub(pnConfiguration);
72+
73+
Assert.assertNotNull("pubnub object is null", pubnub);
74+
Assert.assertNotNull(pubnub.getConfiguration());
75+
Assert.assertEquals("https://pubsub.pubnub.com", pubnub.getBaseUrl());
76+
Assert.assertEquals(3000, pnConfiguration.getSubscribeTimeout());
77+
Assert.assertEquals(4000, pnConfiguration.getConnectTimeout());
78+
Assert.assertEquals(5000, pnConfiguration.getNonSubscribeRequestTimeout());
79+
}
80+
81+
@org.junit.Test(expected = PubNubException.class)
82+
public void testDecryptNull() throws PubNubException {
83+
pnConfiguration.setCipherKey("cipherKey");
84+
pubnub = new PubNub(pnConfiguration);
85+
Assert.assertEquals("test1", pubnub.decrypt(null).trim());
86+
}
87+
88+
@org.junit.Test(expected = PubNubException.class)
89+
public void testDecryptNull_B() throws PubNubException {
90+
pubnub = new PubNub(pnConfiguration);
91+
Assert.assertEquals("test1", pubnub.decrypt(null, "cipherKey").trim());
92+
}
93+
94+
@org.junit.Test
95+
public void GetVersionAndTimeStamp() throws PubNubException {
96+
pubnub = new PubNub(pnConfiguration);
97+
String version = pubnub.getVersion();
98+
int timeStamp = pubnub.getTimestamp();
99+
Assert.assertEquals("4.0.4", version);
100+
Assert.assertTrue(timeStamp > 0);
101+
}
102+
103+
@org.junit.Test(expected = PubNubException.class)
104+
public void testEcryptNull() throws PubNubException {
105+
pubnub = new PubNub(pnConfiguration);
106+
Assert.assertEquals("test1", pubnub.encrypt(null));
107+
}
108+
109+
@org.junit.Test(expected = PubNubException.class)
110+
public void testEcryptNull_B() throws PubNubException {
111+
pubnub = new PubNub(pnConfiguration);
112+
Assert.assertEquals("test1", pubnub.encrypt(null, "chiperKey"));
113+
}
59114
}

src/test/java/com/pubnub/api/endpoints/EndpointTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.pubnub.api.PubNub;
44
import com.pubnub.api.PubNubException;
5+
import com.pubnub.api.endpoints.channel_groups.RemoveChannelChannelGroup;
56
import com.pubnub.api.enums.PNOperationType;
7+
import com.pubnub.api.models.consumer.channel_group.PNChannelGroupsRemoveChannelResult;
68
import okhttp3.Request;
79
import org.junit.Assert;
810
import org.junit.Before;
@@ -12,8 +14,12 @@
1214
import retrofit2.Response;
1315

1416
import java.io.IOException;
17+
import java.util.Arrays;
1518
import java.util.Map;
1619

20+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
21+
import static org.junit.Assert.assertNotNull;
22+
1723
public class EndpointTest extends TestHarness {
1824

1925
PubNub pubnub;
@@ -97,6 +103,4 @@ public Request request() {
97103
endpoint.sync();
98104
}
99105

100-
101-
102106
}

src/test/java/com/pubnub/api/endpoints/HeartbeatEndpointTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,49 @@ public void testMissingChannelAndGroupSync() throws PubNubException, Interrupted
135135
partialHeartbeat.sync();
136136
}
137137

138+
@org.junit.Test
139+
public void testIsAuthRequiredSuccessSync() throws IOException, PubNubException, InterruptedException {
140+
141+
stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/heartbeat"))
142+
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\"}")));
143+
144+
pubnub.getConfiguration().setAuthKey("myKey");
145+
partialHeartbeat.channels(Arrays.asList("ch1")).sync();
146+
147+
List<LoggedRequest> requests = findAll(getRequestedFor(urlMatching("/.*")));
148+
assertEquals(1, requests.size());
149+
assertEquals("myKey", requests.get(0).queryParameter("auth").firstValue());
150+
}
151+
152+
@org.junit.Test(expected=PubNubException.class)
153+
public void testNullSubKeySync() throws PubNubException, InterruptedException {
154+
pubnub.getConfiguration().setPresenceTimeout(123);
155+
156+
stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/heartbeat"))
157+
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\"}")));
158+
159+
pubnub.getConfiguration().setSubscribeKey(null);
160+
partialHeartbeat.channels(Arrays.asList("ch1")).sync();
161+
}
162+
163+
@org.junit.Test(expected=PubNubException.class)
164+
public void testEmptySubKeySync() throws PubNubException, InterruptedException {
165+
pubnub.getConfiguration().setPresenceTimeout(123);
166+
167+
stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/heartbeat"))
168+
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\"}")));
169+
170+
pubnub.getConfiguration().setSubscribeKey("");
171+
partialHeartbeat.channels(Arrays.asList("ch1")).sync();
172+
}
173+
174+
@org.junit.Test(expected=PubNubException.class)
175+
public void testInvalidStateSync() throws PubNubException, InterruptedException {
176+
pubnub.getConfiguration().setPresenceTimeout(123);
177+
178+
stubFor(get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/heartbeat"))
179+
.willReturn(aResponse().withBody("{\"status\": 200, \"message\": \"OK\", \"service\": \"Presence\"}")));
180+
181+
partialHeartbeat.channels(Arrays.asList("ch1")).state(new Object()).sync();
182+
}
138183
}

0 commit comments

Comments
 (0)