Skip to content

Commit

Permalink
Add reading message type in FetchMessages (#298)
Browse files Browse the repository at this point in the history
* Add reading message type in FetchMessages
  • Loading branch information
wkal-pubnub authored Dec 18, 2023
1 parent 9ff499f commit 94ae18e
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.pubnub.api.integration.util.BaseIntegrationTest;
import com.pubnub.api.integration.util.RandomGenerator;
import com.pubnub.api.models.consumer.PNPublishResult;
import com.pubnub.api.models.consumer.history.HistoryMessageType;
import com.pubnub.api.models.consumer.history.PNFetchMessageItem;
import com.pubnub.api.models.consumer.history.PNFetchMessagesResult;
import com.pubnub.api.models.consumer.history.PNHistoryItemResult;
Expand Down Expand Up @@ -660,4 +661,23 @@ public void testEmptyMeta() throws PubNubException {

// three responses from three different APIs will return a non-null meta field
}

@Test
public void testFetchSingleChannel_includeMessageTypeIsFalse() throws PubNubException {
final String expectedChannelName = random();

publishMixed(pubNub, 10, expectedChannelName);

final PNFetchMessagesResult fetchMessagesResult = pubNub.fetchMessages()
.channels(Collections.singletonList(expectedChannelName))
.includeMessageType(false)
.sync();

pause(3);

assert fetchMessagesResult != null;
for (PNFetchMessageItem messageItem : fetchMessagesResult.getChannels().get(expectedChannelName)) {
assertEquals(null ,messageItem.getMessageType());
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/pubnub/api/endpoints/FetchMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ protected PNFetchMessagesResult createResponse(Response<FetchMessagesEnvelope> i
} else {
messageItemBuilder.actions(null);
}
messageItemBuilder.includeMessageType(includeMessageType);

items.add(messageItemBuilder.build());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.pubnub.api.models.consumer.history;

import com.pubnub.api.PubNubException;

public enum HistoryMessageType {
MESSAGE,
FILE;

private static final int TYPE_MESSAGE = 0;
private static final int TYPE_FILE = 4;

public static HistoryMessageType of(Integer messageType) throws PubNubException {
if (messageType == null) {
return MESSAGE;
}
switch (messageType) {
case TYPE_MESSAGE: return MESSAGE;
case TYPE_FILE: return FILE;
default: throw new PubNubException("Unknown message type value $value", null, null, null, 0, null, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;
import com.pubnub.api.PubNubError;
import com.pubnub.api.PubNubException;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.List;
Expand All @@ -30,12 +32,20 @@ public class PNFetchMessageItem {

@SerializedName("message_type")
@Getter(AccessLevel.NONE)
private final String messageType;
private int getMessageType() {
if (messageType == null || messageType.isEmpty()) {
return 0;
} else {
return Integer.parseInt(messageType);
private final Integer messageType;

@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
private final boolean includeMessageType;

public HistoryMessageType getMessageType() {
if (!includeMessageType) {
return null;
}
try {
return HistoryMessageType.of(messageType);
} catch (PubNubException e) {
return null;
}
}

Expand Down
102 changes: 99 additions & 3 deletions src/test/java/com/pubnub/api/endpoints/FetchMessagesEndpointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import com.google.common.collect.Lists;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.pubnub.api.PubNub;
import com.pubnub.api.PubNubException;
import com.pubnub.api.builder.PubNubErrorBuilder;
import com.pubnub.api.crypto.CryptoModule;
import com.pubnub.api.models.consumer.history.HistoryMessageType;
import com.pubnub.api.models.consumer.history.PNFetchMessagesResult;
import org.junit.*;
import org.jetbrains.annotations.Nullable;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import java.util.stream.Collectors;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.findAll;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -153,5 +168,86 @@ public void testProcessMessageWithPnOtherEncryptedWithCrypto() throws PubNubExce

}

@Test
public void testMessageTypesAreProperlyDeserialized() throws PubNubException {
stubFor(
get(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/myChannel")).willReturn(
aResponse().withBody(
"{\n" +
" \"status\": 200,\n" +
" \"error\": false,\n" +
" \"error_message\": \"\",\n" +
" \"channels\": {\n" +
" \"myChannel\": [\n" +
" {\n" +
" \"message\": \"thisIsMessage1\",\n" +
" \"timetoken\": \"14797423056306675\",\n" +
" \"message_type\": 0\n" +
" },\n" +
" {\n" +
" \"message\": \"thisIsMessage1\",\n" +
" \"timetoken\": \"14797423056306675\",\n" +
" \"message_type\": null\n" +
" },\n" +
" {\n" +
" \"message\": \"thisIsMessage2\",\n" +
" \"timetoken\": \"14797423056306676\",\n" +
" \"message_type\": 4\n" +
" }\n" +
" ]\n" +
" }\n" +
" }"
)
)
);

@Nullable PNFetchMessagesResult response = pubnub.fetchMessages()
.channels(Collections.singletonList("myChannel"))
.includeMessageType(true)
.sync();
assertEquals(
response.getChannels().values().stream().flatMap(items -> items.stream().map(item -> item.getMessageType())).collect(Collectors.toList()),
Lists.newArrayList(HistoryMessageType.MESSAGE, HistoryMessageType.MESSAGE, HistoryMessageType.FILE)
);
}

@Test
public void testMessageTypesAreNull_includeMessageTypeIsFalse() throws PubNubException {
stubFor(
get(urlPathEqualTo("/v3/history/sub-key/mySubscribeKey/channel/myChannel")).willReturn(
aResponse().withBody(
"{\n" +
" \"status\": 200,\n" +
" \"error\": false,\n" +
" \"error_message\": \"\",\n" +
" \"channels\": {\n" +
" \"myChannel\": [\n" +
" {\n" +
" \"message\": \"thisIsMessage1\",\n" +
" \"timetoken\": \"14797423056306675\"\n" +
" },\n" +
" {\n" +
" \"message\": \"thisIsMessage1\",\n" +
" \"timetoken\": \"14797423056306675\"\n" +
" },\n" +
" {\n" +
" \"message\": \"thisIsMessage2\",\n" +
" \"timetoken\": \"14797423056306676\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }"
)
)
);

@Nullable PNFetchMessagesResult response = pubnub.fetchMessages()
.channels(Collections.singletonList("myChannel"))
.includeMessageType(false)
.sync();
assertEquals(
response.getChannels().values().stream().flatMap(items -> items.stream().map(item -> item.getMessageType())).collect(Collectors.toList()),
Lists.newArrayList(null, null, null)
);
}
}

0 comments on commit 94ae18e

Please sign in to comment.