Skip to content

Commit

Permalink
Fix for NullPointerException in pubnub-gson when grant() is called …
Browse files Browse the repository at this point in the history
…without authKeys.
  • Loading branch information
wkal-pubnub committed Nov 5, 2024
1 parent cdda36f commit 141d376
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ public static PNAccessManagerGrantResult from(com.pubnub.api.models.consumer.acc
private static Map<String, Map<String, PNAccessManagerKeyData>> from(Map<String, Map<String, com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData>> data) {
Map<String, Map<String, PNAccessManagerKeyData>> newMap = new HashMap<>(data.size());
for (Map.Entry<String, Map<String, com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData>> stringMapEntry : data.entrySet()) {
if (stringMapEntry.getValue() == null) {
continue;
}
Map<String, PNAccessManagerKeyData> innerMap = new HashMap<>(stringMapEntry.getValue().size());
for (Map.Entry<String, com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData> innerEntry : stringMapEntry.getValue().entrySet()) {
if (innerEntry.getValue() == null) {
continue;
}
innerMap.put(innerEntry.getKey(), PNAccessManagerKeyData.from(innerEntry.getValue()));
}
newMap.put(stringMapEntry.getKey(), innerMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;
import org.jetbrains.annotations.NotNull;

@Data
@Builder(toBuilder = true)
Expand All @@ -29,7 +30,8 @@ public class PNAccessManagerKeyData {
@SerializedName("j")
private boolean joinEnabled;

static PNAccessManagerKeyData from(com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData data) {
@NotNull
static PNAccessManagerKeyData from(@NotNull com.pubnub.api.models.consumer.access_manager.PNAccessManagerKeyData data) {
return PNAccessManagerKeyData.builder()
.readEnabled(data.getReadEnabled())
.writeEnabled(data.getWriteEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
import com.pubnub.api.java.models.consumer.access_manager.PNAccessManagerKeyData;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.junit.Assert.assertEquals;

public class GrantIT extends BaseIntegrationTest {

@Test
public void grantWithoutAuthKeyDoesntCrash() throws PubNubException {
getServer().grant().channels(Arrays.asList("abc")).write(true).read(false).ttl(0).sync();
}

@Test
public void grantAllForUUID() throws PubNubException {
String uuid = "uuid123";
String authKey = "authKey123";
int ttl = 120;

PNAccessManagerGrantResult expectedResult = PNAccessManagerGrantResult.builder()
.channelGroups(Collections.emptyMap())
.channels(Collections.emptyMap())
Expand All @@ -35,7 +40,7 @@ public void grantAllForUUID() throws PubNubException {
.build())))
.build();

PNAccessManagerGrantResult result = getServer()
PNAccessManagerGrantResult result2 = getServer()
.grant()
.uuids(Collections.singletonList(uuid))
.authKeys(Collections.singletonList(authKey))
Expand All @@ -44,7 +49,6 @@ public void grantAllForUUID() throws PubNubException {
.update(true)
.delete(true)
.sync();
System.out.println(result);
assertEquals(expectedResult, result);
assertEquals(expectedResult, result2);
}
}

0 comments on commit 141d376

Please sign in to comment.