Skip to content

Commit

Permalink
[ECO-5193][TM*] Updated Message.java
Browse files Browse the repository at this point in the history
1. Changed metadata type from Map<String, String> to JsonObject instead,
updated relevant tests
  • Loading branch information
sacOO7 committed Jan 20, 2025
1 parent 52e0415 commit f51cde6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
20 changes: 8 additions & 12 deletions lib/src/main/java/io/ably/lib/types/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonArray;
Expand All @@ -21,6 +20,7 @@

import io.ably.lib.util.Log;


/**
* Contains an individual message that is sent to, or received from, Ably.
*/
Expand Down Expand Up @@ -95,7 +95,7 @@ public class Message extends BaseMessage {
public static class Operation {
public String clientId;
public String description;
public Map<String, String> metadata;
public JsonObject metadata;

void write(MessagePacker packer) throws IOException {
packer.packMapHeader(3);
Expand All @@ -110,9 +110,9 @@ void write(MessagePacker packer) throws IOException {
if(metadata != null) {
packer.packString("metadata");
packer.packMapHeader(metadata.size());
for(Map.Entry<String, String> entry : metadata.entrySet()) {
for(Map.Entry<String, JsonElement> entry : metadata.entrySet()) {
packer.packString(entry.getKey());
packer.packString(entry.getValue());
Serialisation.gsonToMsgpack(entry.getValue(), packer);
}
}
}
Expand All @@ -131,11 +131,11 @@ protected static Operation read(final MessageUnpacker unpacker) throws IOExcepti
break;
case "metadata":
int mapSize = unpacker.unpackMapHeader();
operation.metadata = new HashMap<>(mapSize);
operation.metadata = new JsonObject();
for (int j = 0; j < mapSize; j++) {
String key = unpacker.unpackString();
String value = unpacker.unpackString();
operation.metadata.put(key, value);
JsonElement value = Serialisation.msgpackToGson(unpacker.unpackValue());
operation.metadata.add(key, value);
}
break;
default:
Expand All @@ -155,11 +155,7 @@ protected static Operation read(final JsonObject jsonObject) throws MessageDecod
operation.description = jsonObject.get("description").getAsString();
}
if (jsonObject.has("metadata")) {
JsonObject metadataObject = jsonObject.getAsJsonObject("metadata");
operation.metadata = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : metadataObject.entrySet()) {
operation.metadata.put(entry.getKey(), entry.getValue().getAsString());
}
operation.metadata = jsonObject.getAsJsonObject("metadata");
}
return operation;
}
Expand Down
26 changes: 25 additions & 1 deletion lib/src/test/java/io/ably/lib/chat/ChatMessagesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChatMessagesTest extends ParameterizedTest {
/**
Expand Down Expand Up @@ -42,6 +44,15 @@ public void test_room_message_is_published() {
// send message to room
ChatRoom.SendMessageParams params = new ChatRoom.SendMessageParams();
params.text = "hello there";
params.metadata = new JsonObject();
JsonObject foo = new JsonObject();
foo.addProperty("bar", 1);
params.metadata.add("foo", foo);
Map<String, String> headers = new HashMap<>();
headers.put("header1", "value1");
headers.put("baz", "qux");
params.headers = headers;

JsonObject sendMessageResult = (JsonObject) room.sendMessage(params);
// check sendMessageResult has 2 fields and are not null
Assert.assertEquals(2, sendMessageResult.entrySet().size());
Expand All @@ -63,8 +74,21 @@ public void test_room_message_is_published() {
JsonObject data = (JsonObject) message.data;
// has two fields "text" and "metadata"
Assert.assertEquals(2, data.entrySet().size());
// Assert for received text
Assert.assertEquals("hello there", data.get("text").getAsString());
Assert.assertTrue(data.get("metadata").isJsonObject());
// Assert on received metadata
JsonObject metadata = data.getAsJsonObject("metadata");
Assert.assertTrue(metadata.has("foo"));
Assert.assertTrue(metadata.get("foo").isJsonObject());
Assert.assertEquals(1, metadata.getAsJsonObject("foo").get("bar").getAsInt());

// Assert sent headers as a part of message.extras.headers
JsonObject extrasJson = message.extras.asJsonObject();
Assert.assertTrue(extrasJson.has("headers"));
JsonObject headersJson = extrasJson.getAsJsonObject("headers");
Assert.assertEquals(2, headersJson.entrySet().size());
Assert.assertEquals("value1", headersJson.get("header1").getAsString());
Assert.assertEquals("qux", headersJson.get("baz").getAsString());

Assert.assertEquals(resultCreatedAt, String.valueOf(message.timestamp));

Expand Down
7 changes: 4 additions & 3 deletions lib/src/test/java/io/ably/lib/chat/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.ably.lib.http.HttpCore;
import io.ably.lib.http.HttpUtils;
import io.ably.lib.rest.AblyRest;
Expand Down Expand Up @@ -39,19 +40,19 @@ public JsonElement deleteMessage(String serial, DeleteMessageParams params) thro

public static class SendMessageParams {
public String text;
public Map<String, Object> metadata;
public JsonObject metadata;
public Map<String, String> headers;
}

public static class UpdateMessageParams {
public SendMessageParams message;
public String description;
public Map<String, Object> metadata;
public JsonObject metadata;
}

public static class DeleteMessageParams {
public String description;
public Map<String, Object> metadata;
public JsonObject metadata;
}

protected Optional<JsonElement> makeAuthorizedRequest(String url, String method, JsonElement body) throws AblyException {
Expand Down
21 changes: 10 additions & 11 deletions lib/src/test/java/io/ably/lib/types/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.msgpack.core.MessageUnpacker;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

public class MessageTest {

Expand Down Expand Up @@ -107,9 +106,9 @@ public void serialize_message_with_operation() {
Message.Operation operation = new Message.Operation();
operation.clientId = "operation-client-id";
operation.description = "operation-description";
operation.metadata = new HashMap<>();
operation.metadata.put("key1", "value1");
operation.metadata.put("key2", "value2");
operation.metadata = new JsonObject();
operation.metadata.addProperty("key1", "value1");
operation.metadata.addProperty("key2", "value2");
message.operation = operation;

// When
Expand Down Expand Up @@ -162,8 +161,8 @@ public void deserialize_message_with_operation() throws Exception {
assertEquals("test-key", message.connectionKey);
assertEquals("operation-client-id", message.operation.clientId);
assertEquals("operation-description", message.operation.description);
assertEquals("value1", message.operation.metadata.get("key1"));
assertEquals("value2", message.operation.metadata.get("key2"));
assertEquals("value1", message.operation.metadata.get("key1").getAsString());
assertEquals("value2", message.operation.metadata.get("key2").getAsString());
}

@Test
Expand Down Expand Up @@ -200,9 +199,9 @@ public void serialize_and_deserialize_with_msgpack() throws Exception {
Message.Operation operation = new Message.Operation();
operation.clientId = "operation-client-id";
operation.description = "operation-description";
operation.metadata = new HashMap<>();
operation.metadata.put("key1", "value1");
operation.metadata.put("key2", "value2");
operation.metadata = new JsonObject();
operation.metadata.addProperty("key1", "value1");
operation.metadata.addProperty("key2", "value2");
message.operation = operation;

// When Encode to MessagePack
Expand All @@ -227,7 +226,7 @@ public void serialize_and_deserialize_with_msgpack() throws Exception {
assertEquals("01826232498871-001@abcdefghij:001", unpacked.serial);
assertEquals("operation-client-id", unpacked.operation.clientId);
assertEquals("operation-description", unpacked.operation.description);
assertEquals("value1", unpacked.operation.metadata.get("key1"));
assertEquals("value2", unpacked.operation.metadata.get("key2"));
assertEquals("value1", unpacked.operation.metadata.get("key1").getAsString());
assertEquals("value2", unpacked.operation.metadata.get("key2").getAsString());
}
}

0 comments on commit f51cde6

Please sign in to comment.