Skip to content

Commit ca59b46

Browse files
author
Max Presman
committed
adding some tests for endpoint base, moving UUID and auth key generation to base class, adding region configs
1 parent 2fbab85 commit ca59b46

23 files changed

Lines changed: 226 additions & 63 deletions

‎src/main/java/com/pubnub/api/core/Pubnub.java‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public final String decrypt(final String inputString, final String cipherKey) th
147147
* @param inputString String to be encrypted
148148
* @return String containing the encryption of inputString using cipherKey
149149
*/
150-
public final String encrypt(String inputString) throws PubnubException {
150+
public final String encrypt(final String inputString) throws PubnubException {
151151
if (inputString == null) {
152152
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_INVALID_ARGUMENTS).build();
153153
}
@@ -156,7 +156,7 @@ public final String encrypt(String inputString) throws PubnubException {
156156
}
157157

158158
/**
159-
* Perform Cryptographic encryption of an input string and the cipher key
159+
* Perform Cryptographic encryption of an input string and the cipher key.
160160
* @param inputString String to be encrypted
161161
* @param cipherKey cipher key to be used for encryption
162162
* @throws PubnubException throws exception in case of failed encryption
@@ -170,17 +170,24 @@ public final String encrypt(final String inputString, final String cipherKey) th
170170
return new Crypto(cipherKey).encrypt(inputString);
171171
}
172172

173-
public String getVersion() {
173+
/**
174+
* @return version of the SDK.
175+
*/
176+
public final String getVersion() {
174177
return sdkVersion;
175178
}
176179

177180
/**
178181
* Stop the SDK and terminate all listeners.
179182
*/
180-
public void stop() {
183+
public final void stop() {
181184
subscriptionManager.stop();
182185
}
183186

187+
/**
188+
* fetch the SDK version from the resource files.
189+
* @return Stringified representation of the SDK version.
190+
*/
184191
private String fetchSDKVersion() {
185192
byte[] encoded;
186193
try {

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,25 @@ private void writeFieldsToResponse(Response<Input> response, PNErrorStatus pnErr
169169

170170
}
171171

172-
protected String signSHA256(String key, String data) throws PubnubException {
173-
Mac sha256_HMAC;
172+
protected String signSHA256(final String key, final String data) throws PubnubException {
173+
Mac sha256HMAC;
174174
byte[] hmacData;
175175
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA256");
176176

177177
try {
178-
sha256_HMAC = Mac.getInstance("HmacSHA256");
178+
sha256HMAC = Mac.getInstance("HmacSHA256");
179179
} catch (NoSuchAlgorithmException e) {
180180
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
181181
}
182182

183183
try {
184-
sha256_HMAC.init(secretKey);
184+
sha256HMAC.init(secretKey);
185185
} catch (InvalidKeyException e) {
186186
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
187187
}
188188

189189
try {
190-
hmacData = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
190+
hmacData = sha256HMAC.doFinal(data.getBytes("UTF-8"));
191191
} catch (UnsupportedEncodingException e) {
192192
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_CRYPTO_ERROR).errormsg(e.getMessage()).build();
193193
}
@@ -218,6 +218,16 @@ protected final Map<String, String> createBaseParams() {
218218
Map<String, String> params = new HashMap<>();
219219

220220
params.put("pnsdk", "Java/" + this.pubnub.getVersion());
221+
params.put("uuid", this.pubnub.getConfiguration().getUuid());
222+
223+
// add the auth key for publish and subscribe.
224+
if (this.pubnub.getConfiguration().getAuthKey() != null) {
225+
if (getOperationType() == PNOperationType.PNPublishOperation
226+
|| getOperationType() == PNOperationType.PNSubscribeOperation) {
227+
params.put("auth", pubnub.getConfiguration().getAuthKey());
228+
}
229+
}
230+
221231

222232
return params;
223233
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ protected boolean validateParams() {
3838
protected Call<Envelope> doWork(Map<String, String> params) throws PubnubException {
3939
ObjectWriter ow = new ObjectMapper().writer();
4040

41-
params.put("uuid", pubnub.getConfiguration().getUuid());
4241
params.put("heartbeat", String.valueOf(pubnub.getConfiguration().getPresenceTimeout()));
4342

4443
if (channelGroups != null && channelGroups.size() > 0) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ protected Call<Envelope> doWork(Map<String, String> params) {
3434
String channelCSV;
3535
PresenceService service = this.createRetrofit().create(PresenceService.class);
3636

37-
params.put("uuid", pubnub.getConfiguration().getUuid());
38-
3937
if (channelGroups.size() > 0) {
4038
params.put("channel-group", PubnubUtil.joinString(channelGroups, ","));
4139
}

‎src/main/java/com/pubnub/api/endpoints/pubsub/Publish.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ protected final Call<List<Object>> doWork(Map<String, String> params) throws Pub
6262
}
6363
}
6464

65-
if (pubnub.getConfiguration().getAuthKey() != null) {
66-
params.put("auth", pubnub.getConfiguration().getAuthKey());
67-
}
68-
69-
params.put("uuid", pubnub.getConfiguration().getUuid());
70-
71-
if (shouldStore != null && !shouldStore) {
72-
params.put("store", "0");
65+
if (shouldStore != null) {
66+
if (shouldStore) {
67+
params.put("store", "1");
68+
} else {
69+
params.put("store", "0");
70+
}
7371
}
7472

7573
if (pubnub.getConfiguration().getCipherKey() != null) {

‎src/main/java/com/pubnub/api/endpoints/pubsub/Subscribe.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class Subscribe extends Endpoint<SubscribeEnvelope, SubscribeEnvelope> {
4949
@Setter private String region;
5050

5151
/**
52-
* Create a new Subscribe instance endpoint.
52+
* CreFte a new Subscribe instance endpoint.
5353
* @param pubnub supplied pubnub instance.
5454
*/
5555
public Subscribe(final Pubnub pubnub) {
@@ -100,7 +100,7 @@ protected final SubscribeEnvelope createResponse(final Response<SubscribeEnvelop
100100
throw PubnubException.builder().pubnubError(PubnubError.PNERROBJ_PARSING_ERROR).build();
101101
}
102102

103-
return input.body();
103+
return input.body();
104104
}
105105

106106
/**

‎src/main/java/com/pubnub/api/managers/SubscriptionManager.java‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ public class SubscriptionManager {
3434
private Pubnub pubnub;
3535
private Call<SubscribeEnvelope> subscribeCall;
3636
private Call<Envelope> heartbeatCall;
37+
/**
38+
* Store the latest timetoken to subscribe with, null by default to get the latest timetoken.
39+
*/
3740
private Long timetoken;
41+
42+
/**
43+
* Keep track of Region to support PSV2 specification.
44+
*/
45+
private String region;
46+
3847
Timer timer;
3948

4049
public SubscriptionManager(Pubnub pubnub) {
@@ -162,7 +171,8 @@ private void startSubscribeLoop() {
162171
}
163172

164173
subscribeCall = new Subscribe(pubnub)
165-
.channels(combinedChannels).channelGroups(combinedChannelGroups).timetoken(timetoken)
174+
.channels(combinedChannels).channelGroups(combinedChannelGroups)
175+
.timetoken(timetoken).region(region)
166176
.async(new PNCallback<SubscribeEnvelope>() {
167177
@Override
168178
public void onResponse(SubscribeEnvelope result, PNErrorStatus status) {
@@ -176,6 +186,7 @@ public void onResponse(SubscribeEnvelope result, PNErrorStatus status) {
176186
}
177187

178188
timetoken = result.getMetadata().getTimetoken();
189+
region = result.getMetadata().getRegion();
179190
startSubscribeLoop();
180191
}
181192
});
@@ -253,7 +264,7 @@ private void processIncomingMessages(List<SubscribeMessage> messages) {
253264

254265
announce(pnPresenceEventResult);
255266
} else {
256-
Object extractedMessage = null;
267+
Object extractedMessage;
257268

258269
try {
259270
extractedMessage = processMessage(message.getPayload());
@@ -264,6 +275,7 @@ private void processIncomingMessages(List<SubscribeMessage> messages) {
264275
pnStatus.setCategory(PNStatusCategory.PNDecryptionErrorCategory);
265276

266277
announce(pnStatus);
278+
return;
267279
}
268280

269281
PNMessageResult pnMessageResult = new PNMessageResult();
@@ -302,19 +314,23 @@ private Object processMessage(Object input) throws PubnubException {
302314
return outputObject;
303315
}
304316

305-
private void announce(PNStatus status) {
317+
/**
318+
* announce a PNStatus to listeners.
319+
* @param status PNStatus which will be broadcast to listeners.
320+
*/
321+
private void announce(final PNStatus status) {
306322
for (SubscribeCallback subscribeCallback: listeners) {
307323
subscribeCallback.status(this.pubnub, status);
308324
}
309325
}
310326

311-
private void announce(PNMessageResult message) {
327+
private void announce(final PNMessageResult message) {
312328
for (SubscribeCallback subscribeCallback: listeners) {
313329
subscribeCallback.message(this.pubnub, message);
314330
}
315331
}
316332

317-
private void announce(PNPresenceEventResult presence) {
333+
private void announce(final PNPresenceEventResult presence) {
318334
for (SubscribeCallback subscribeCallback: listeners) {
319335
subscribeCallback.presence(this.pubnub, presence);
320336
}
Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,107 @@
11
package com.pubnub.api.endpoints;
22

3-
import com.pubnub.api.core.PnConfiguration;
43
import com.pubnub.api.core.Pubnub;
4+
import com.pubnub.api.core.PubnubException;
5+
import com.pubnub.api.core.enums.PNOperationType;
6+
import okhttp3.Request;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import retrofit2.Call;
11+
import retrofit2.Callback;
12+
import retrofit2.Response;
513

6-
public class EndpointTest {
14+
import java.io.IOException;
15+
import java.util.Map;
716

8-
protected Pubnub createPubNubInstance(int port) {
9-
PnConfiguration pnConfiguration = new PnConfiguration();
10-
pnConfiguration.setOrigin("localhost" + ":" + port);
11-
pnConfiguration.setSecure(false);
12-
pnConfiguration.setSubscribeKey("mySubscribeKey");
13-
pnConfiguration.setPublishKey("myPublishKey");
14-
pnConfiguration.setUuid("myUUID");
17+
public class EndpointTest extends TestHarness {
1518

16-
return new Pubnub(pnConfiguration);
19+
Pubnub pubnub;
20+
21+
22+
@Before
23+
public void beforeEach() throws IOException {
24+
pubnub = this.createPubNubInstance(8080);
1725
}
26+
27+
@Test
28+
public void testUUID() throws PubnubException {
29+
Endpoint<Object, Object> endpoint = new Endpoint<Object, Object>(pubnub) {
30+
31+
@Override
32+
protected boolean validateParams() {
33+
return false;
34+
}
35+
36+
@Override
37+
protected Object createResponse(Response input) throws PubnubException {
38+
return null;
39+
}
40+
41+
@Override
42+
protected int getConnectTimeout() {
43+
return 0;
44+
}
45+
46+
@Override
47+
protected int getRequestTimeout() {
48+
return 0;
49+
}
50+
51+
@Override
52+
protected PNOperationType getOperationType() {
53+
return null;
54+
}
55+
56+
@Override
57+
protected Call doWork(Map baseParams) throws PubnubException {
58+
59+
Call<Object> fakeCall = new Call<Object>() {
60+
61+
@Override
62+
public Response<Object> execute() throws IOException {
63+
throw new IOException();
64+
}
65+
66+
@Override
67+
public void enqueue(Callback<Object> callback) {
68+
69+
}
70+
71+
@Override
72+
public boolean isExecuted() {
73+
return false;
74+
}
75+
76+
@Override
77+
public void cancel() {
78+
79+
}
80+
81+
@Override
82+
public boolean isCanceled() {
83+
return false;
84+
}
85+
86+
@Override
87+
public Call<Object> clone() {
88+
return null;
89+
}
90+
91+
@Override
92+
public Request request() {
93+
return null;
94+
}
95+
};
96+
97+
Assert.assertEquals("myUUID",baseParams.get("uuid"));
98+
return fakeCall;
99+
}
100+
};
101+
102+
endpoint.sync();
103+
}
104+
105+
106+
18107
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1919
import static org.junit.Assert.assertEquals;
2020

21-
public class HeartbeatEndpointTest extends EndpointTest {
21+
public class HeartbeatEndpointTest extends TestHarness {
2222

2323
private Heartbeat partialHeartbeat;
2424
private Pubnub pubnub;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.*;
2020

2121

22-
public class HistoryEndpointTest extends EndpointTest {
22+
public class HistoryEndpointTest extends TestHarness {
2323

2424
private History partialHistory;
2525
private Pubnub pubnub;

0 commit comments

Comments
 (0)