Skip to content

Commit

Permalink
Add reading message type in Fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Dec 18, 2023
1 parent 36acd34 commit e53b5e3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
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,6 +3,7 @@
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;
Expand Down Expand Up @@ -30,12 +31,13 @@ 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;

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

Expand Down
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,46 @@ 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)
);
}
}

0 comments on commit e53b5e3

Please sign in to comment.