diff --git a/src/integrationTest/java/com/pubnub/api/integration/HistoryIntegrationTest.java b/src/integrationTest/java/com/pubnub/api/integration/HistoryIntegrationTest.java index 93c68510b..4a37d4379 100644 --- a/src/integrationTest/java/com/pubnub/api/integration/HistoryIntegrationTest.java +++ b/src/integrationTest/java/com/pubnub/api/integration/HistoryIntegrationTest.java @@ -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; @@ -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()); + } + } } diff --git a/src/main/java/com/pubnub/api/endpoints/FetchMessages.java b/src/main/java/com/pubnub/api/endpoints/FetchMessages.java index 178e1effe..bd7aa7a2f 100644 --- a/src/main/java/com/pubnub/api/endpoints/FetchMessages.java +++ b/src/main/java/com/pubnub/api/endpoints/FetchMessages.java @@ -188,6 +188,8 @@ protected PNFetchMessagesResult createResponse(Response i } else { messageItemBuilder.actions(null); } + messageItemBuilder.includeMessageType(includeMessageType); + items.add(messageItemBuilder.build()); } diff --git a/src/main/java/com/pubnub/api/models/consumer/history/HistoryMessageType.java b/src/main/java/com/pubnub/api/models/consumer/history/HistoryMessageType.java new file mode 100644 index 000000000..72389c6a6 --- /dev/null +++ b/src/main/java/com/pubnub/api/models/consumer/history/HistoryMessageType.java @@ -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); + } + } +} diff --git a/src/main/java/com/pubnub/api/models/consumer/history/PNFetchMessageItem.java b/src/main/java/com/pubnub/api/models/consumer/history/PNFetchMessageItem.java index 5c98fe6b2..21bf62b23 100644 --- a/src/main/java/com/pubnub/api/models/consumer/history/PNFetchMessageItem.java +++ b/src/main/java/com/pubnub/api/models/consumer/history/PNFetchMessageItem.java @@ -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; @@ -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; } } diff --git a/src/test/java/com/pubnub/api/endpoints/FetchMessagesEndpointTest.java b/src/test/java/com/pubnub/api/endpoints/FetchMessagesEndpointTest.java index 347e98500..94703e892 100644 --- a/src/test/java/com/pubnub/api/endpoints/FetchMessagesEndpointTest.java +++ b/src/test/java/com/pubnub/api/endpoints/FetchMessagesEndpointTest.java @@ -2,6 +2,7 @@ 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; @@ -9,14 +10,28 @@ 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; @@ -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) + ); + } }