Skip to content

Commit

Permalink
Make the UUID mandatory (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleewho authored Jan 12, 2022
1 parent 193a42f commit 1a8a0ab
Show file tree
Hide file tree
Showing 52 changed files with 167 additions and 95 deletions.
17 changes: 11 additions & 6 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: java
version: v5.3.0
version: v6.0.0
schema: 1
scm: github.com/pubnub/java
files:
- build/libs/pubnub-gson-5.3.0-all.jar
- build/libs/pubnub-gson-6.0.0-all.jar
sdks:
-
type: library
Expand All @@ -23,8 +23,8 @@ sdks:
-
distribution-type: library
distribution-repository: GitHub
package-name: pubnub-gson-5.3.0
location: https://github.com/pubnub/java/releases/download/v5.3.0/pubnub-gson-5.3.0-all.jar
package-name: pubnub-gson-6.0.0
location: https://github.com/pubnub/java/releases/download/v6.0.0/pubnub-gson-6.0.0-all.jar
supported-platforms:
supported-operating-systems:
Android:
Expand Down Expand Up @@ -135,8 +135,8 @@ sdks:
-
distribution-type: library
distribution-repository: maven
package-name: pubnub-gson-5.3.0
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-gson/5.3.0/pubnub-gson-5.3.0.jar
package-name: pubnub-gson-6.0.0
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-gson/6.0.0/pubnub-gson-6.0.0.jar
supported-platforms:
supported-operating-systems:
Android:
Expand Down Expand Up @@ -234,6 +234,11 @@ sdks:
is-required: Required

changelog:
- date: 2022-01-12
version: v6.0.0
changes:
- type: improvement
text: "BREAKING CHANGES: uuid is required parameter in PNConfiguration constructor."
- date: 2021-12-16
version: v5.3.0
changes:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v6.0.0
January 12 2022

#### Modified
- BREAKING CHANGES: uuid is required parameter in PNConfiguration constructor.

## v5.3.0
December 16 2021

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your
<dependency>
<groupId>com.pubnub</groupId>
<artifactId>pubnub-gson</artifactId>
<version>5.3.0</version>
<version>6.0.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}
group = 'com.pubnub'

version = '5.3.0'
version = '6.0.0'

description = """"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ protected void createChannelGroup(final PubNub pnClient, final String channelGro
protected abstract PubNub privilegedClientPubNub();

private PubNub adminPubNub() {
final PNConfiguration pnConfiguration = new PNConfiguration();
final PNConfiguration pnConfiguration = new PNConfiguration(PubNub.generateUUID());
pnConfiguration.setSubscribeKey(itPamTestConfig.pamSubKey());
pnConfiguration.setPublishKey(itPamTestConfig.pamPubKey());
pnConfiguration.setSecretKey(itPamTestConfig.pamSecKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class ReconnectionProblemWithReconnectionPolicy extends AbstractReconnectionProblem {
@Override
protected PubNub privilegedClientPubNub() {
final PNConfiguration pnConfiguration = new PNConfiguration();
final PNConfiguration pnConfiguration = new PNConfiguration(PubNub.generateUUID());
pnConfiguration.setSubscribeKey(itPamTestConfig.pamSubKey());
pnConfiguration.setPublishKey(itPamTestConfig.pamPubKey());
pnConfiguration.setSubscribeTimeout(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class ReconnectionProblemWithoutReconnectionPolicy extends AbstractReconnectionProblem {
@Override
protected PubNub privilegedClientPubNub() {
final PNConfiguration pnConfiguration = new PNConfiguration();
final PNConfiguration pnConfiguration = new PNConfiguration(PubNub.generateUUID());
pnConfiguration.setSubscribeKey(itPamTestConfig.pamSubKey());
pnConfiguration.setPublishKey(itPamTestConfig.pamPubKey());
pnConfiguration.setSubscribeTimeout(SUBSCRIBE_TIMEOUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class ObjectsApiBaseIT {
protected final PubNub pubNubUnderTest = pubNub();

private PubNub pubNub() {
final PNConfiguration pnConfiguration = new PNConfiguration();
final PNConfiguration pnConfiguration = new PNConfiguration(PubNub.generateUUID());
pnConfiguration.setSubscribeKey(itTestConfig.subscribeKey());
pnConfiguration.setLogVerbosity(PNLogVerbosity.BODY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ protected void destroyClient(PubNub client) {
client.forceDestroy();
}

protected PNConfiguration getBasicPnConfiguration() {
final PNConfiguration pnConfiguration = new PNConfiguration();
protected PNConfiguration getBasicPnConfiguration() throws PubNubException {
final PNConfiguration pnConfiguration = new PNConfiguration(PubNub.generateUUID());
if (!needsServer()) {
pnConfiguration.setSubscribeKey(SUB_KEY);
pnConfiguration.setPublishKey(PUB_KEY);
Expand All @@ -144,8 +144,8 @@ protected PNConfiguration getBasicPnConfiguration() {
return pnConfiguration;
}

private PNConfiguration getServerPnConfiguration() {
final PNConfiguration pnConfiguration = new PNConfiguration();
private PNConfiguration getServerPnConfiguration() throws PubNubException {
final PNConfiguration pnConfiguration = new PNConfiguration(PubNub.generateUUID());
pnConfiguration.setSubscribeKey(PAM_SUB_KEY);
pnConfiguration.setPublishKey(PAM_PUB_KEY);
pnConfiguration.setSecretKey(PAM_SEC_KEY);
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/pubnub/api/PNConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import javax.net.ssl.X509ExtendedTrustManager;
import java.net.Proxy;
import java.net.ProxySelector;
import java.util.UUID;

import static com.pubnub.api.builder.PubNubErrorBuilder.PNERROBJ_UUID_NULL_OR_EMPTY;

@Getter
@Setter
Expand Down Expand Up @@ -95,6 +96,12 @@ public class PNConfiguration {
private String secretKey;
private String cipherKey;
private String authKey;

public void setUuid(@NotNull String uuid) throws PubNubException {
PubNubUtil.require(uuid != null && !uuid.trim().isEmpty(), PNERROBJ_UUID_NULL_OR_EMPTY);
this.uuid = uuid;
}

private String uuid;
/**
* If proxies are forcefully caching requests, set to true to allow the client to randomize the subdomain.
Expand Down Expand Up @@ -206,14 +213,17 @@ public class PNConfiguration {
@Deprecated
@Setter
private boolean managePresenceListManually;

/**
* Initialize the PNConfiguration with default values
*
* @param uuid
*/
public PNConfiguration() {
public PNConfiguration(@NotNull String uuid) throws PubNubException {
PubNubUtil.require(uuid != null && !uuid.isEmpty(), PNERROBJ_UUID_NULL_OR_EMPTY);
setPresenceTimeoutWithCustomInterval(PRESENCE_TIMEOUT, 0);

uuid = "pn-" + UUID.randomUUID().toString();

this.uuid = uuid;
nonSubscribeRequestTimeout = NON_SUBSCRIBE_REQUEST_TIMEOUT;
subscribeTimeout = SUBSCRIBE_TIMEOUT;
connectTimeout = CONNECT_TIMEOUT;
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/pubnub/api/PubNub.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class PubNub {
private static final int TIMESTAMP_DIVIDER = 1000;
private static final int MAX_SEQUENCE = 65535;

private static final String SDK_VERSION = "5.3.0";
private static final String SDK_VERSION = "6.0.0";
private final ListenerManager listenerManager;
private final StateManager stateManager;

Expand Down Expand Up @@ -136,6 +136,11 @@ public PubNub(@NotNull PNConfiguration initialConfig) {
instanceId = UUID.randomUUID().toString();
}

@NotNull
public static String generateUUID() {
return "pn-" + UUID.randomUUID();
}

@NotNull
public String getBaseUrl() {
return this.basePathManager.getBasePath();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/pubnub/api/PubNubUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,10 @@ public static <T> boolean isNullOrEmpty(final Collection<T> collection) {
return collection == null || collection.isEmpty();
}

public static void require(boolean value, PubNubError error) throws PubNubException {
if (!value) {
throw PubNubException.builder().pubnubError(error).build();
}
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/pubnub/api/builder/PubNubErrorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ public final class PubNubErrorBuilder {
*/
public static final int PNERR_TOKEN_MISSING = 168;

/**
* UUID can't be null nor empty
*/
public static final int PNERR_UUID_NULL_OR_EMPTY = 169;

// Error Objects
public static final PubNubError PNERROBJ_TIMEOUT = PubNubError.builder()
.errorCode(PNERR_TIMEOUT)
Expand Down Expand Up @@ -700,6 +705,11 @@ public final class PubNubErrorBuilder {
.message("Token missing.")
.build();

public static final PubNubError PNERROBJ_UUID_NULL_OR_EMPTY = PubNubError.builder()
.errorCode(PNERR_UUID_NULL_OR_EMPTY)
.message("Uuid can't be null nor empty.")
.build();

private PubNubErrorBuilder() {

}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/pubnub/api/PubNubExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PubNubExceptionTest extends TestHarness {
private Publish instance;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
PubNub pubnub = this.createPubNubInstance();
instance = pubnub.publish();
wireMockRule.start();
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/pubnub/api/PubNubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class PubNubTest {
private PNConfiguration pnConfiguration;

@Before
public void beforeEach() throws IOException {
pnConfiguration = new PNConfiguration();
public void beforeEach() throws IOException, PubNubException {
pnConfiguration = new PNConfiguration(PubNub.generateUUID());
pnConfiguration.setSubscribeKey("demo");
pnConfiguration.setPublishKey("demo");
pnConfiguration.setUseRandomInitializationVector(false);
Expand Down Expand Up @@ -99,7 +99,7 @@ public void getVersionAndTimeStamp() {
pubnub = new PubNub(pnConfiguration);
String version = pubnub.getVersion();
int timeStamp = pubnub.getTimestamp();
Assert.assertEquals("5.3.0", version);
Assert.assertEquals("6.0.0", version);
Assert.assertTrue(timeStamp > 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class DeleteMessagesEndpointTest extends TestHarness {
private PubNub pubnub;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
partialHistory = pubnub.deleteMessages();
wireMockRule.start();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/pubnub/api/endpoints/EndpointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class EndpointTest extends TestHarness {
private PubNub pubnub;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
pubnub.getConfiguration().setIncludeInstanceIdentifier(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class FetchMessagesEndpointTest extends TestHarness {
private PubNub pubnub;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
partialHistory = pubnub.fetchMessages();
wireMockRule.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class HeartbeatEndpointTest extends TestHarness {
private PubNub pubnub;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
RetrofitManager retrofitManager = new RetrofitManager(pubnub);
partialHeartbeat = new Heartbeat(pubnub, null, retrofitManager, new TokenManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public class HistoryBatchEndpointTest {
private final TelemetryManager telemetryManager = mock(TelemetryManager.class);
private final ArgumentCaptor<Map<String, String>> optionsCaptor = ArgumentCaptor.forClass(Map.class);

public HistoryBatchEndpointTest() throws PubNubException {
}

@Test
public void forSingleChannelFetchMessagesAlwaysPassMaxWhenItIsInBound() throws PubNubException {
//given
Expand Down Expand Up @@ -335,8 +338,8 @@ private RetrofitManager retrofitManagerMock(final HistoryService historyService)
return retrofitManager;
}

private PubNub pubNubMock() {
final PNConfiguration pnConfiguration = new PNConfiguration();
private PubNub pubNubMock() throws PubNubException {
final PNConfiguration pnConfiguration = new PNConfiguration(PubNub.generateUUID());
pnConfiguration.setSubscribeKey(SUBSCRIBE_KEY);

final PubNub pubnub = mock(PubNub.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class HistoryEndpointTest extends TestHarness {
private PubNub pubnub;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
partialHistory = pubnub.history();
wireMockRule.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MessageCountTest extends TestHarness {
private PubNub pubnub;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
wireMockRule.start();
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/com/pubnub/api/endpoints/TestHarness.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.pubnub.api.PNConfiguration;
import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubException;
import com.pubnub.api.enums.PNLogVerbosity;

public class TestHarness {
protected final static int PORT = 8080;

protected PubNub createPubNubInstance() {
PNConfiguration pnConfiguration = new PNConfiguration();
protected PubNub createPubNubInstance() throws PubNubException {
PNConfiguration pnConfiguration = new PNConfiguration(PubNub.generateUUID());
pnConfiguration.setOrigin("localhost" + ":" + PORT);
pnConfiguration.setSecure(false);
pnConfiguration.setSubscribeKey("mySubscribeKey");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TimeEndpointTest extends TestHarness {
private PubNub pubnub;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
partialTime = pubnub.time();
wireMockRule.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class GrantEndpointTest extends TestHarness {
private String uuid = "myUUID";

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
partialGrant = pubnub.grant();
pubnub.getConfiguration().setSecretKey("secretKey").setIncludeInstanceIdentifier(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class GrantTokenEndpointTest extends TestHarness {

private final PubNub pubnub = this.createPubNubInstance();

public GrantTokenEndpointTest() throws PubNubException {
}

@Before
public void beforeEach() throws IOException {
pubnub.getConfiguration().setSecretKey("secretKey").setIncludeInstanceIdentifier(true);
Expand Down Expand Up @@ -66,7 +69,7 @@ public void validate_SecretKeyMissing() {
@Test
public void validate_SubscribeKeyMissing() {
try {
new PubNub(new PNConfiguration().setSecretKey("secret")).grantToken()
new PubNub(new PNConfiguration(PubNub.generateUUID()).setSecretKey("secret")).grantToken()
.ttl(1)
.channelGroups(Collections.singletonList(ChannelGroupGrant.id("test").read()))
.sync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AddChannelChannelGroupEndpointTest extends TestHarness {
private PubNub pubnub;

@Before
public void beforeEach() throws IOException {
public void beforeEach() throws IOException, PubNubException {
pubnub = this.createPubNubInstance();
partialAddChannelChannelGroup = pubnub.addChannelsToChannelGroup();
wireMockRule.start();
Expand Down
Loading

0 comments on commit 1a8a0ab

Please sign in to comment.